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

This commit is contained in:
Leosvel Pérez Espinosa 2023-08-11 21:25:21 +01:00 committed by GitHub
parent df7dc03eda
commit ccd6e04d3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 85 deletions

View File

@ -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();
@ -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();

View File

@ -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(
@ -69,7 +63,7 @@ export async function determineProjectNameAndRootOptions(
const format =
options.projectNameAndRootFormat ?? (await determineFormat(formats));
return formats[format].options;
return formats[format];
}
function validateName(
@ -113,6 +107,15 @@ 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',
@ -120,16 +123,18 @@ async function determineFormat(
'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(
@ -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,
},
};
}
@ -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,
},
};
}