Skip to content

Commit

Permalink
feat(misc): save directory and name format to nx json defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Aug 17, 2023
1 parent 54496e8 commit 50e460f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ async function normalizeOptions(
projectNameAndRootFormat: isRootProject
? 'as-provided'
: options.projectNameAndRootFormat,
callingGenerator: '@nx/cypress:cypress-project',
}
);

Expand Down
29 changes: 25 additions & 4 deletions packages/devkit/src/generators/project-name-and-root-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
} from '../utils/get-workspace-layout';
import { names } from '../utils/names';

const { joinPathFragments, readJson, readNxJson } = requireNx();
const { joinPathFragments, readJson, readNxJson, updateNxJson } = requireNx();

export type ProjectNameAndRootFormat = 'as-provided' | 'derived';
export type ProjectGenerationOptions = {
name: string;
projectType: ProjectType;
callingGenerator?: string;
directory?: string;
importPath?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
Expand Down Expand Up @@ -61,7 +62,8 @@ export async function determineProjectNameAndRootOptions(
validateName(options.name, options.projectNameAndRootFormat);
const formats = getProjectNameAndRootFormats(tree, options);
const format =
options.projectNameAndRootFormat ?? (await determineFormat(formats));
options.projectNameAndRootFormat ??
(await determineFormat(tree, formats, options.callingGenerator));

return formats[format];
}
Expand Down Expand Up @@ -97,7 +99,9 @@ function validateName(
}

async function determineFormat(
formats: ProjectNameAndRootFormats
tree: Tree,
formats: ProjectNameAndRootFormats,
callingGenerator?: string
): Promise<ProjectNameAndRootFormat> {
if (!formats.derived) {
return 'as-provided';
Expand All @@ -116,7 +120,7 @@ async function determineFormat(
Root: ${formats['derived'].projectRoot}`;
const derivedSelectedValue = `${formats['derived'].projectName} @ ${formats['derived'].projectRoot} (This was derived from the folder structure. Please provide the exact name and directory in the future)`;

return await prompt<{ format: ProjectNameAndRootFormat }>({
const result = await prompt<{ format: ProjectNameAndRootFormat }>({
type: 'select',
name: 'format',
message:
Expand All @@ -135,6 +139,23 @@ async function determineFormat(
}).then(({ format }) =>
format === asProvidedSelectedValue ? 'as-provided' : 'derived'
);

if (result === 'as-provided' && callingGenerator) {
const { saveDefault } = await prompt<{ saveDefault: boolean }>({
type: 'confirm',
message: 'Would you like to save this layout as a default?',
name: 'saveDefault',
});
if (saveDefault) {
const nxJson = readNxJson(tree);
nxJson.generators ??= {};
nxJson.generators[callingGenerator] ??= {};
nxJson.generators[callingGenerator].projectNameAndRootFormat = result;
updateNxJson(tree, nxJson);
}
}

return result;
}

function getProjectNameAndRootFormats(
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ async function normalizeOptions(
importPath: options.importPath,
projectNameAndRootFormat: options.projectNameAndRootFormat,
rootProject: options.rootProject,
callingGenerator: '@nx/js:library',
});
options.rootProject = projectRoot === '.';
const fileName = getCaseAwareFileName({
Expand Down

0 comments on commit 50e460f

Please sign in to comment.