Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(devkit): improve project name and root format prompt #18591

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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