Skip to content

Commit

Permalink
feat(devkit): improve project name and root format prompt (#18591)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez authored Aug 11, 2023
1 parent df7dc03 commit ccd6e04
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 85 deletions.
62 changes: 24 additions & 38 deletions packages/devkit/src/generators/project-name-and-root-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,29 +247,22 @@ describe('determineProjectNameAndRootOptions', () => {
directory: 'shared',
});

expect(promptSpy).toHaveBeenCalledWith(
expect.objectContaining({
type: 'select',
name: 'format',
message:
'What should be the project name and where should it be generated?',
choices: [
{
message: `Recommended:
expect(promptSpy).toHaveBeenCalled();
const promptCallOptions = promptSpy.mock.calls[0][0] as any;
expect(promptCallOptions.choices).toStrictEqual([
{
message: `As provided:
Name: lib-name
Root: shared`,
name: 'as-provided',
},
{
message: `Legacy:
name: 'lib-name @ shared',
},
{
message: `Derived:
Name: shared-lib-name
Root: shared/lib-name`,
name: 'derived',
},
],
initial: 'as-provided',
})
);
name: 'shared-lib-name @ shared/lib-name (This was derived from the folder structure. Please provide the exact name and directory in the future)',
},
]);

// restore original interactive mode
restoreOriginalInteractiveMode();
Expand Down Expand Up @@ -523,29 +516,22 @@ describe('determineProjectNameAndRootOptions', () => {
directory: 'shared',
});

expect(promptSpy).toHaveBeenCalledWith(
expect.objectContaining({
type: 'select',
name: 'format',
message:
'What should be the project name and where should it be generated?',
choices: [
{
message: `Recommended:
expect(promptSpy).toHaveBeenCalled();
const promptCallOptions = promptSpy.mock.calls[0][0] as any;
expect(promptCallOptions.choices).toStrictEqual([
{
message: `As provided:
Name: lib-name
Root: shared`,
name: 'as-provided',
},
{
message: `Legacy:
name: 'lib-name @ shared',
},
{
message: `Derived:
Name: shared-lib-name
Root: libs/shared/lib-name`,
name: 'derived',
},
],
initial: 'as-provided',
})
);
name: 'shared-lib-name @ libs/shared/lib-name (This was derived from the folder structure. Please provide the exact name and directory in the future)',
},
]);

// restore original interactive mode
restoreOriginalInteractiveMode();
Expand Down
84 changes: 37 additions & 47 deletions packages/devkit/src/generators/project-name-and-root-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,8 @@ export type ProjectNameAndRootOptions = {
};

type ProjectNameAndRootFormats = {
'as-provided': {
description: string;
options: ProjectNameAndRootOptions;
};
derived?: {
description: string;
options: ProjectNameAndRootOptions;
};
'as-provided': ProjectNameAndRootOptions;
derived?: ProjectNameAndRootOptions;
};

export async function determineProjectNameAndRootOptions(
Expand All @@ -69,7 +63,7 @@ export async function determineProjectNameAndRootOptions(
const format =
options.projectNameAndRootFormat ?? (await determineFormat(formats));

return formats[format].options;
return formats[format];
}

function validateName(
Expand Down Expand Up @@ -113,23 +107,34 @@ async function determineFormat(
return 'derived';
}

const asProvidedDescription = `As provided:
Name: ${formats['as-provided'].projectName}
Root: ${formats['as-provided'].projectRoot}`;
const asProvidedSelectedValue = `${formats['as-provided'].projectName} @ ${formats['as-provided'].projectRoot}`;
const derivedDescription = `Derived:
Name: ${formats['derived'].projectName}
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 }>({
type: 'select',
name: 'format',
message:
'What should be the project name and where should it be generated?',
choices: [
{
message: formats['as-provided'].description,
name: 'as-provided',
message: asProvidedDescription,
name: asProvidedSelectedValue,
},
{
message: formats['derived'].description,
name: 'derived',
message: derivedDescription,
name: derivedSelectedValue,
},
],
initial: 'as-provided' as any,
}).then(({ format }) => format);
}).then(({ format }) =>
format === asProvidedSelectedValue ? 'as-provided' : 'derived'
);
}

function getProjectNameAndRootFormats(
Expand All @@ -150,18 +155,13 @@ function getProjectNameAndRootFormats(
const nameWithoutScope = asProvidedProjectName.split('/')[1];
return {
'as-provided': {
description: `Recommended:
Name: ${asProvidedProjectName}
Root: ${asProvidedProjectDirectory}`,
options: {
projectName: asProvidedProjectName,
names: {
projectSimpleName: nameWithoutScope,
projectFileName: nameWithoutScope,
},
importPath: options.importPath ?? asProvidedProjectName,
projectRoot: asProvidedProjectDirectory,
projectName: asProvidedProjectName,
names: {
projectSimpleName: nameWithoutScope,
projectFileName: nameWithoutScope,
},
importPath: options.importPath ?? asProvidedProjectName,
projectRoot: asProvidedProjectDirectory,
},
};
}
Expand Down Expand Up @@ -219,32 +219,22 @@ function getProjectNameAndRootFormats(

return {
'as-provided': {
description: `Recommended:
Name: ${asProvidedProjectName}
Root: ${asProvidedProjectDirectory}`,
options: {
projectName: asProvidedProjectName,
names: {
projectSimpleName: asProvidedProjectName,
projectFileName: asProvidedProjectName,
},
importPath: asProvidedImportPath,
projectRoot: asProvidedProjectDirectory,
projectName: asProvidedProjectName,
names: {
projectSimpleName: asProvidedProjectName,
projectFileName: asProvidedProjectName,
},
importPath: asProvidedImportPath,
projectRoot: asProvidedProjectDirectory,
},
derived: {
description: `Legacy:
Name: ${derivedProjectName}
Root: ${derivedProjectDirectory}`,
options: {
projectName: derivedProjectName,
names: {
projectSimpleName: derivedSimpleProjectName,
projectFileName: derivedProjectName,
},
importPath: derivedImportPath,
projectRoot: derivedProjectDirectory,
projectName: derivedProjectName,
names: {
projectSimpleName: derivedSimpleProjectName,
projectFileName: derivedProjectName,
},
importPath: derivedImportPath,
projectRoot: derivedProjectDirectory,
},
};
}
Expand Down

1 comment on commit ccd6e04

@vercel
Copy link

@vercel vercel bot commented on ccd6e04 Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.