Skip to content

Commit

Permalink
feat(misc): introduce a way to set the project name/root format for a…
Browse files Browse the repository at this point in the history
…ll generators
  • Loading branch information
FrozenPandaz committed Sep 1, 2023
1 parent ad2d1e8 commit deb4e37
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 34 deletions.
9 changes: 5 additions & 4 deletions docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ Where new apps + libs should be placed

#### Type declaration

| Name | Type |
| :-------- | :------- |
| `appsDir` | `string` |
| `libsDir` | `string` |
| Name | Type |
| :-------------------------- | :----------------------------- |
| `appsDir?` | `string` |
| `libsDir?` | `string` |
| `projectNameAndRootFormat?` | `"as-provided"` \| `"derived"` |
9 changes: 5 additions & 4 deletions docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ Where new apps + libs should be placed

#### Type declaration

| Name | Type |
| :-------- | :------- |
| `appsDir` | `string` |
| `libsDir` | `string` |
| Name | Type |
| :-------------------------- | :----------------------------- |
| `appsDir?` | `string` |
| `libsDir?` | `string` |
| `projectNameAndRootFormat?` | `"as-provided"` \| `"derived"` |

#### Inherited from

Expand Down
10 changes: 9 additions & 1 deletion e2e/utils/create-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
RunCmdOpts,
runCommand,
} from './command-utils';
import { output } from '@nx/devkit';
import { NxJsonConfiguration, output } from '@nx/devkit';
import { readFileSync } from 'fs';
import { join } from 'path';

Expand All @@ -41,6 +41,7 @@ let projName: string;
export function newProject({
name = uniq('proj'),
packageManager = getSelectedPackageManager(),
unsetProjectNameAndRootFormat = true,
} = {}): string {
try {
const projScope = 'proj';
Expand All @@ -51,6 +52,13 @@ export function newProject({
packageManager,
});

if (unsetProjectNameAndRootFormat) {
updateJson<NxJsonConfiguration>('nx.json', (nxJson) => {
delete nxJson.workspaceLayout;
return nxJson;
});
}

// Temporary hack to prevent installing with `--frozen-lockfile`
if (isCI && packageManager === 'pnpm') {
updateFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,21 @@ describe('determineProjectNameAndRootOptions', () => {

expect(promptSpy).toHaveBeenCalledTimes(2);

expect(readNxJson(tree).generators['@nx/some-plugin:app']).toEqual({
expect(readNxJson(tree).workspaceLayout).toEqual({
projectNameAndRootFormat: 'as-provided',
});

promptSpy.mockReset();

await determineProjectNameAndRootOptions(tree, {
name: 'libName',
projectType: 'library',
directory: 'shared',
callingGenerator: '@nx/some-plugin:app',
});

expect(promptSpy).not.toHaveBeenCalled();

// restore original interactive mode
restoreOriginalInteractiveMode();
});
Expand Down
22 changes: 16 additions & 6 deletions packages/devkit/src/generators/project-name-and-root-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export async function determineProjectNameAndRootOptions(
const formats = getProjectNameAndRootFormats(tree, options);
const format =
options.projectNameAndRootFormat ??
(await determineFormat(tree, formats, options.callingGenerator));
(getDefaultProjectNameAndRootFormat(tree) === 'as-provided'
? 'as-provided'
: await determineFormat(tree, formats, options.callingGenerator));

return {
...formats[format],
Expand Down Expand Up @@ -167,11 +169,7 @@ async function determineFormat(
initial: true,
});
if (saveDefault) {
const nxJson = readNxJson(tree);
nxJson.generators ??= {};
nxJson.generators[callingGenerator] ??= {};
nxJson.generators[callingGenerator].projectNameAndRootFormat = result;
updateNxJson(tree, nxJson);
setProjectNameAndRootFormatDefault(tree);
} else {
logger.warn(deprecationWarning);
}
Expand All @@ -183,6 +181,18 @@ async function determineFormat(
return result;
}

function setProjectNameAndRootFormatDefault(tree: Tree) {
const nxJson = readNxJson(tree);
nxJson.workspaceLayout ??= {};
nxJson.workspaceLayout.projectNameAndRootFormat = 'as-provided';
updateNxJson(tree, nxJson);
}

function getDefaultProjectNameAndRootFormat(tree: Tree) {
const nxJson = readNxJson(tree);
return nxJson.workspaceLayout?.projectNameAndRootFormat ?? 'derived';
}

function getProjectNameAndRootFormats(
tree: Tree,
options: ProjectGenerationOptions
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
"appsDir": {
"type": "string",
"description": "Default folder name for apps."
},
"projectNameAndRootFormat": {
"type": "string",
"description": "Default method of handling arguments for generating projects",
"enum": ["as-provided", "derived"]
}
},
"additionalProperties": false
Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
* Where new apps + libs should be placed
*/
workspaceLayout?: {
libsDir: string;
appsDir: string;
libsDir?: string;
appsDir?: string;
projectNameAndRootFormat?: 'as-provided' | 'derived';
};
/**
* Available Task Runners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@ exports[`new should generate an empty nx.json 1`] = `
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": {
"projectNameAndRootFormat": "as-provided",
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": {
"projectNameAndRootFormat": "as-provided",
},
}
`);
const validateNxJson = ajv.compile(nxSchema);
Expand Down Expand Up @@ -174,6 +177,9 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": {
"projectNameAndRootFormat": "as-provided",
},
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ function createNxJson(
},
},
},
workspaceLayout: {
projectNameAndRootFormat: 'as-provided',
},
};

nxJson.targetDefaults = {
Expand Down
17 changes: 9 additions & 8 deletions packages/workspace/src/generators/new/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,28 @@ export interface NormalizedSchema extends Schema {
isCustomPreset: boolean;
}

export async function newGenerator(host: Tree, opts: Schema) {
export async function newGenerator(tree: Tree, opts: Schema) {
const options = normalizeOptions(opts);
validateOptions(options, host);
validateOptions(options, tree);

await generateWorkspaceFiles(host, { ...options, nxCloud: undefined } as any);
await generateWorkspaceFiles(tree, { ...options, nxCloud: undefined } as any);

addPresetDependencies(host, options);
addCloudDependencies(host, options);
addPresetDependencies(tree, options);

addCloudDependencies(tree, options);

return async () => {
const pmc = getPackageManagerCommand(options.packageManager);
if (pmc.preInstall) {
execSync(pmc.preInstall, {
cwd: joinPathFragments(host.root, options.directory),
cwd: joinPathFragments(tree.root, options.directory),
stdio: process.env.NX_GENERATE_QUIET === 'true' ? 'ignore' : 'inherit',
});
}
installPackagesTask(host, false, options.directory, options.packageManager);
installPackagesTask(tree, false, options.directory, options.packageManager);
// TODO: move all of these into create-nx-workspace
if (options.preset !== Preset.NPM && !options.isCustomPreset) {
await generatePreset(host, options);
await generatePreset(tree, options);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,35 @@ exports[`preset should create files (preset = react-standalone bundler = webpack
},
}
`;

exports[`preset should create files (preset = web-components) 1`] = `
[
".babelrc",
"index.html",
"public",
"src",
"tsconfig.app.json",
"tsconfig.json",
"project.json",
"vite.config.ts",
"tsconfig.spec.json",
".eslintrc.json",
]
`;

exports[`preset should create files (preset = web-components) 2`] = `
[
"app",
"assets",
"main.ts",
"styles.css",
]
`;

exports[`preset should create files (preset = web-components) 3`] = `
[
"app.element.css",
"app.element.spec.ts",
"app.element.ts",
]
`;
Loading

0 comments on commit deb4e37

Please sign in to comment.