Skip to content

Commit

Permalink
fix(rspack): make directory required
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Sep 30, 2024
1 parent dacf80f commit c0e600e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 78 deletions.
19 changes: 9 additions & 10 deletions docs/generated/packages/rspack/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
"description": "React + Rspack application generator.",
"examples": [
{
"command": "nx g app myapp --directory=myorg",
"command": "nx g app myorg/myapp",
"description": "Generate `apps/myorg/myapp` and `apps/myorg/myapp-e2e`"
}
],
"properties": {
"directory": {
"type": "string",
"description": "The directory to nest the app under.",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What directory would you like to use for the application?"
},
"name": {
"description": "The name of the application.",
"type": "string",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the application?",
"pattern": "^[a-zA-Z].*$",
"x-priority": "important"
"pattern": "^[a-zA-Z].*$"
},
"framework": {
"type": "string",
Expand Down Expand Up @@ -69,10 +72,6 @@
"enum": ["none", "cypress"],
"default": "cypress"
},
"directory": {
"type": "string",
"description": "The directory to nest the app under."
},
"tags": {
"type": "string",
"description": "Add tags to the application (used for linting).",
Expand All @@ -85,7 +84,7 @@
},
"rootProject": { "type": "boolean", "x-priority": "internal" }
},
"required": ["name"],
"required": ["directory"],
"presets": []
},
"aliases": ["app"],
Expand Down
2 changes: 1 addition & 1 deletion e2e/rspack/tests/rspack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('rspack e2e', () => {
it('should create rspack root project and additional apps', async () => {
const project = uniq('myapp');
runCLI(
`generate @nx/rspack:preset ${project} --framework=react --unitTestRunner=jest --e2eTestRunner=cypress`
`generate @nx/rspack:preset ${project} --framework=react --unitTestRunner=jest --e2eTestRunner=cypress --verbose`
);

// Added this so that the nx-ecosystem-ci tests don't throw jest error
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function (
});
tasks.push(initTask);

const options = normalizeOptions(tree, _options);
const options = await normalizeOptions(tree, _options);

options.style ??= 'css';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,51 @@ describe('normalizeOptions', () => {
tree = createTreeWithEmptyWorkspace();
});

it('should set { rootProject: true } when --rootProject=true is passed', () => {
it('should set { rootProject: true } when --rootProject=true is passed', async () => {
expect(
normalizeOptions(tree, {
name: 'demo',
style: 'css',
rootProject: true,
}).rootProject
(
await normalizeOptions(tree, {
directory: 'demo',
style: 'css',
rootProject: true,
})
).rootProject
).toBeTruthy();
});

it('should set { rootProject: false } when --rootProject=undefined is passed', () => {
it('should set { rootProject: false } when --rootProject=undefined is passed', async () => {
expect(
normalizeOptions(tree, {
name: 'demo',
style: 'css',
}).rootProject
(
await normalizeOptions(tree, {
directory: 'demo',
style: 'css',
})
).rootProject
).toBeFalsy();
});

it('should set { rootProject: false } when --rootProject=false is passed', () => {
it('should set { rootProject: false } when --rootProject=false is passed', async () => {
expect(
normalizeOptions(tree, {
name: 'demo',
style: 'css',
rootProject: false,
}).rootProject
(
await normalizeOptions(tree, {
directory: 'demo',
style: 'css',
rootProject: false,
})
).rootProject
).toBeFalsy();
});

it('should set { rootProject: false } when --monorepo=true and --rootProject=true is passed', () => {
it('should set { rootProject: false } when --monorepo=true and --rootProject=true is passed', async () => {
expect(
normalizeOptions(tree, {
name: 'demo',
style: 'css',
monorepo: true,
rootProject: true,
}).rootProject
(
await normalizeOptions(tree, {
directory: 'demo',
style: 'css',
monorepo: true,
rootProject: true,
})
).rootProject
).toBeFalsy();
});
});
43 changes: 14 additions & 29 deletions packages/rspack/src/generators/application/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
import {
extractLayoutDirectory,
getWorkspaceLayout,
names,
normalizePath,
Tree,
} from '@nx/devkit';
import { names, Tree } from '@nx/devkit';
import { ApplicationGeneratorSchema, NormalizedSchema } from '../schema';
import {
determineProjectNameAndRootOptions,
ensureProjectName,
} from '@nx/devkit/src/generators/project-name-and-root-utils';

export function normalizeDirectory(options: ApplicationGeneratorSchema) {
const { projectDirectory } = extractLayoutDirectory(options.directory);
return projectDirectory
? `${names(projectDirectory).fileName}/${names(options.name).fileName}`
: names(options.name).fileName;
}

export function normalizeProjectName(options: ApplicationGeneratorSchema) {
return normalizeDirectory(options).replace(new RegExp('/', 'g'), '-');
}

export function normalizeOptions(
export async function normalizeOptions(
host: Tree,
options: ApplicationGeneratorSchema
): NormalizedSchema {
): Promise<NormalizedSchema> {
await ensureProjectName(host, options, 'application');
const { projectName: appProjectName, projectRoot: appProjectRoot } =
await determineProjectNameAndRootOptions(host, {
...options,
projectType: 'application',
});
// --monorepo takes precedence over --rootProject
// This won't be needed once we add --bundler=rspack to the @nx/react:app preset
const rootProject = !options.monorepo && options.rootProject;
const appDirectory = normalizeDirectory(options);
const appProjectName = normalizeProjectName(options);
const e2eProjectName = options.rootProject
? 'e2e'
: `${names(options.name).fileName}-e2e`;

const { layoutDirectory } = extractLayoutDirectory(options.directory);
const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;
const appProjectRoot = rootProject
? '.'
: normalizePath(`${appsDir}/${appDirectory}`);
: `${names(appProjectName).fileName}-e2e`;

const normalized = {
...options,
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export interface ApplicationGeneratorSchema {
name: string;
directory: string;
name?: string;
framework?: Framework;
style: 'css' | 'scss' | 'less' | 'styl';
unitTestRunner?: 'none' | 'jest';
e2eTestRunner?: 'none' | 'cypress';
directory?: string;
tags?: string;
rootProject?: boolean;
monorepo?: boolean;
Expand Down
21 changes: 10 additions & 11 deletions packages/rspack/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
"description": "React + Rspack application generator.",
"examples": [
{
"command": "nx g app myapp --directory=myorg",
"command": "nx g app myorg/myapp",
"description": "Generate `apps/myorg/myapp` and `apps/myorg/myapp-e2e`"
}
],
"properties": {
"name": {
"description": "The name of the application.",
"directory": {
"type": "string",
"description": "The directory to nest the app under.",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use for the application?",
"pattern": "^[a-zA-Z].*$",
"x-priority": "important"
"x-prompt": "What directory would you like to use for the application?"
},
"name": {
"description": "The name of the application.",
"type": "string",
"pattern": "^[a-zA-Z].*$"
},
"framework": {
"type": "string",
Expand Down Expand Up @@ -75,10 +78,6 @@
"enum": ["none", "cypress"],
"default": "cypress"
},
"directory": {
"type": "string",
"description": "The directory to nest the app under."
},
"tags": {
"type": "string",
"description": "Add tags to the application (used for linting).",
Expand All @@ -94,5 +93,5 @@
"x-priority": "internal"
}
},
"required": ["name"]
"required": ["directory"]
}
1 change: 1 addition & 0 deletions packages/rspack/src/generators/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PresetGeneratorSchema } from './schema';
export default async function (tree: Tree, options: PresetGeneratorSchema) {
const appTask = applicationGenerator(tree, {
...options,
directory: '.',
// Since `--style` is not passed down to custom preset, we're using individual flags for now.
style: options.sass
? 'scss'
Expand Down

0 comments on commit c0e600e

Please sign in to comment.