Skip to content

Commit

Permalink
chore(solid): align package to nx 20
Browse files Browse the repository at this point in the history
Closes nxext#1134

BREAKING CHANGE: generator option 'name' is optional in favor of 'directory' one
  • Loading branch information
pawel-twardziak committed Oct 13, 2024
1 parent 3dbd5da commit 73e2bfc
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 97 deletions.
38 changes: 19 additions & 19 deletions docs/docs/solid/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ nx g application ... --dry-run

### Options

#### name (_**required**_)

Type: `string`

#### directory
#### directory (_**required**_)

Alias(es): d

Type: `string`

A directory where the lib is placed.
A directory where the project is placed.

#### e2eTestRunner

Expand All @@ -60,13 +56,13 @@ Possible values: `eslint`

The tool to use for running lint checks.

#### projectNameAndRootFormat
#### name

Type: `string`
Alias(es): n

Possible values: `as-provided`, `derived`
Type: `string`

Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).
A name of the project.

#### rootProject (**hidden**)

Expand Down Expand Up @@ -132,10 +128,14 @@ nx g library ... --dry-run

### Options

#### name (_**required**_)
#### directory (_**required**_)

Alias(es): d

Type: `string`

A directory where the project is placed.

#### buildable

Default: `false`
Expand All @@ -144,14 +144,6 @@ Type: `boolean`

Generate a buildable library.

#### directory

Alias(es): d

Type: `string`

A directory where the lib is placed.

#### e2eTestRunner

Default: `cypress`
Expand All @@ -178,6 +170,14 @@ Possible values: `eslint`

The tool to use for running lint checks.

#### name

Alias(es): n

Type: `string`

A name of the project.

#### publishable

Type: `boolean`
Expand Down
14 changes: 7 additions & 7 deletions packages/solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"tslib": "^2.3.0"
},
"peerDependencies": {
"@nx/devkit": "^19.0.0",
"@nx/vite": "^19.0.0",
"@nx/web": "^19.0.0",
"@nx/cypress": "^19.0.0",
"@nx/jest": "^19.0.0",
"@nx/eslint": "^19.0.0",
"@nx/js": "^19.0.0"
"@nx/devkit": "20.0.0",
"@nx/vite": "20.0.0",
"@nx/web": "20.0.0",
"@nx/cypress": "20.0.0",
"@nx/jest": "20.0.0",
"@nx/eslint": "20.0.0",
"@nx/js": "20.0.0"
},
"nx-migrate": {
"migrations": "./migrations.json"
Expand Down
10 changes: 7 additions & 3 deletions packages/solid/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import { Tree } from '@nx/devkit';
describe('Solid app generator', () => {
let tree: Tree;
const options: Schema = {
name: 'myApp',
directory: 'apps/my-app',
linter: Linter.EsLint,
unitTestRunner: 'vitest',
e2eTestRunner: 'cypress',
projectNameAndRootFormat: 'derived',
};

beforeEach(() => {
Expand All @@ -26,7 +25,12 @@ describe('Solid app generator', () => {
});

it('should add vite specific files as rootProject', async () => {
await applicationGenerator(tree, { ...options, rootProject: true });
await applicationGenerator(tree, {
...options,
directory: '.',
name: options.directory.replace('apps/', ''),
rootProject: true,
});
expect(tree.exists(`public/index.html`)).toBeFalsy();
expect(tree.exists(`index.html`)).toBeTruthy();
});
Expand Down
35 changes: 18 additions & 17 deletions packages/solid/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@ import { addJest } from './lib/add-jest';
import { updateJestConfig } from './lib/update-jest-config';
import { addVite } from './lib/add-vite';
import { createFiles } from './lib/create-project-files';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import {
determineProjectNameAndRootOptions,
ensureProjectName,
} from '@nx/devkit/src/generators/project-name-and-root-utils';

async function normalizeOptions<T extends Schema = Schema>(
host: Tree,
options: Schema,
callingGenerator = '@nxext/solid:application'
options: Schema
): Promise<NormalizedSchema<T>> {
await ensureProjectName(host, options, 'application');
const {
projectName: appProjectName,
projectRoot: appProjectRoot,
projectNameAndRootFormat,
projectName,
projectRoot,
names: projectNames,
} = await determineProjectNameAndRootOptions(host, {
name: options.name,
projectType: 'application',
directory: options.directory,
projectNameAndRootFormat: options.projectNameAndRootFormat,
rootProject: options.rootProject,
callingGenerator,
});
options.rootProject = appProjectRoot === '.';
options.projectNameAndRootFormat = projectNameAndRootFormat;
options.rootProject = projectRoot === '.';
const fileName =
/* options.simpleName
? projectNames.projectSimpleName
: */ projectNames.projectFileName;

const nxJson = readNxJson(host);

Expand All @@ -49,20 +53,18 @@ async function normalizeOptions<T extends Schema = Schema>(
e2ePort = nxJson.targetDefaults?.[e2eWebServerTarget].options?.port;
}

const e2eProjectName = options.rootProject ? 'e2e' : `${appProjectName}-e2e`;
const e2eProjectRoot = options.rootProject ? 'e2e' : `${appProjectRoot}-e2e`;
const e2eProjectName = options.rootProject ? 'e2e' : `${projectName}-e2e`;
const e2eProjectRoot = options.rootProject ? 'e2e' : `${projectRoot}-e2e`;
const e2eWebServerAddress = `http://localhost:${e2ePort}`;
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
//const fileName = options.pascalCaseFiles ? 'App' : 'app';
const fileName = 'App';

return {
...options,
name: names(options.name).fileName,
projectName: appProjectName,
appProjectRoot,
projectName,
appProjectRoot: projectRoot,
e2eProjectName,
e2eProjectRoot,
e2eWebServerAddress,
Expand All @@ -78,7 +80,6 @@ export async function applicationGenerator(
schema: Schema
): Promise<GeneratorCallback> {
return await applicationGeneratorInternal(host, {
projectNameAndRootFormat: 'derived',
...schema,
});
}
Expand Down
6 changes: 2 additions & 4 deletions packages/solid/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Linter } from '@nx/eslint';
import { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-and-root-utils';

export interface Schema {
name: string;
directory: string;
name?: string;
tags?: string;
linter: Linter;
unitTestRunner: 'jest' | 'vitest' | 'none';
e2eTestRunner: 'cypress' | 'none';
directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
rootProject?: boolean;
host?: string;
port?: number;
Expand Down
24 changes: 10 additions & 14 deletions packages/solid/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
"title": "",
"type": "object",
"properties": {
"name": {
"directory": {
"type": "string",
"description": "",
"description": "A directory where the project is placed.",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
"x-prompt": "What directory would you like to use? (full path; e.g. apps/<my-app-name>)",
"alias": "d"
},
"name": {
"type": "string",
"description": "A name of the project.",
"alias": "n"
},
"tags": {
"type": "string",
Expand All @@ -24,11 +30,6 @@
"enum": ["eslint"],
"default": "eslint"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
},
"unitTestRunner": {
"type": "string",
"enum": ["vitest", "jest", "none"],
Expand All @@ -41,11 +42,6 @@
"description": "Test runner to use for end to end (e2e) tests.",
"default": "cypress"
},
"directory": {
"type": "string",
"description": "A directory where the lib is placed.",
"alias": "d"
},
"rootProject": {
"description": "Create a application at the root of the workspace",
"type": "boolean",
Expand All @@ -58,5 +54,5 @@
"default": false
}
},
"required": ["name"]
"required": ["directory"]
}
14 changes: 7 additions & 7 deletions packages/solid/src/generators/component/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ import { names, Tree } from '@nx/devkit';
describe('component schematic', () => {
let tree: Tree;
const projectName = uniq('testprojekt');
const projectAppDirectory = `apps/${projectName}`;
const projectLibDirectory = `libs/${projectName}`;
const componentName = uniq('test');
const options: SolidComponentSchema = {
name: componentName,
project: projectName,
unitTestRunner: 'jest',
};

beforeEach(async () => {
tree = await createTestProject(projectName);
});

it('should run successfully', async () => {
tree = await createTestProject(projectAppDirectory);
await expect(componentGenerator(tree, options)).resolves.not.toThrowError();
});

it('should add file', async () => {
tree = await createTestProject(projectAppDirectory);
await componentGenerator(tree, options);
const name = names(componentName);
expect(
tree.exists(
`apps/${projectName}/src/components/${name.fileName}/${name.className}.ts`
`${projectAppDirectory}/src/components/${name.fileName}/${name.className}.ts`
)
);
});

it('should add file to barrel', async () => {
const tree = await createTestProject(projectName, 'library');
tree = await createTestProject(projectLibDirectory, 'library');
await componentGenerator(tree, options);
const name = names(componentName);

const indexFile = tree.read(`libs/${projectName}/src/index.ts`);
const indexFile = tree.read(`${projectLibDirectory}/src/index.ts`);
expect(indexFile.toString('utf-8')).toMatch(
`export { default as ${name.className} } from './components/${name.fileName}/${name.className}.ts';`
);
Expand Down
2 changes: 1 addition & 1 deletion packages/solid/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { libraryGenerator } from './library';
describe('solid library schematic', () => {
let tree: Tree;
const options: SolidLibrarySchema = {
name: 'myLib',
directory: 'libs/my-lib',
linter: Linter.EsLint,
unitTestRunner: 'jest',
e2eTestRunner: 'cypress',
Expand Down
17 changes: 8 additions & 9 deletions packages/solid/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import { updateJestConfig } from './lib/update-jest-config';
import { createFiles } from './lib/create-project-files';
import { addVite } from './lib/add-vite';
import { addVitest } from './lib/add-vitest';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import {
determineProjectNameAndRootOptions,
ensureProjectName,
} from '@nx/devkit/src/generators/project-name-and-root-utils';
import { createOrEditViteConfig } from '@nx/vite';

async function normalizeOptions(
host: Tree,
options: SolidLibrarySchema
): Promise<NormalizedSchema> {
await ensureProjectName(host, options, 'library');
const {
projectName,
names: projectNames,
Expand All @@ -32,16 +36,12 @@ async function normalizeOptions(
projectType: 'library',
directory: options.directory,
importPath: options.importPath,
projectNameAndRootFormat: options.projectNameAndRootFormat,
callingGenerator: '@nxext/solid:library',
rootProject: false,
});
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
: name;
const fileName = options.simpleName
? projectNames.projectSimpleName
: projectNames.projectFileName;

const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
Expand All @@ -53,7 +53,7 @@ async function normalizeOptions(
projectRoot,
parsedTags,
fileName,
projectDirectory,
projectDirectory: projectRoot,
importPath,
};
}
Expand All @@ -69,7 +69,6 @@ function updateLibPackageNpmScope(host: Tree, options: NormalizedSchema) {

export async function libraryGenerator(host: Tree, schema: SolidLibrarySchema) {
return await libraryGeneratorInternal(host, {
projectNameAndRootFormat: 'derived',
...schema,
});
}
Expand Down
Loading

0 comments on commit 73e2bfc

Please sign in to comment.