@nx/react:library - Add simpleName option to remove dir name from generated file names (#17226)

This commit is contained in:
Zachary Morgan 2023-05-31 10:28:23 -04:00 committed by GitHub
parent 825190674c
commit b0732e1554
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 2 deletions

View File

@ -183,6 +183,11 @@
"description": "Create a React library with a minimal setup, no separate test files.",
"type": "boolean",
"default": false
},
"simpleName": {
"description": "Don't include the directory in the name of the module of the library.",
"type": "boolean",
"default": false
}
},
"required": ["name"],

View File

@ -21,6 +21,7 @@ export function createFiles(host: Tree, options: NormalizedSchema) {
...names(options.name),
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot),
fileName: options.fileName,
};
generateFiles(

View File

@ -26,7 +26,7 @@ export function normalizeOptions(
: name;
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName;
const fileName = options.simpleName ? name : projectName;
const { libsDir: defaultLibsDir } = getWorkspaceLayout(host);
const libsDir = layoutDirectory ?? defaultLibsDir;
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);

View File

@ -29,6 +29,7 @@ describe('lib', () => {
style: 'css',
component: true,
strict: true,
simpleName: false,
};
beforeEach(() => {
@ -736,6 +737,30 @@ describe('lib', () => {
});
});
describe('--simpleName', () => {
it('should generate a library with a simple name', async () => {
await libraryGenerator(tree, {
...defaultSchema,
simpleName: true,
directory: 'myDir',
});
const indexFile = tree.read('libs/my-dir/my-lib/src/index.ts', 'utf-8');
expect(indexFile).toContain(`export * from './lib/my-lib';`);
expect(
tree.exists('libs/my-dir/my-lib/src/lib/my-lib.module.css')
).toBeTruthy();
expect(
tree.exists('libs/my-dir/my-lib/src/lib/my-lib.spec.tsx')
).toBeTruthy();
expect(tree.exists('libs/my-dir/my-lib/src/lib/my-lib.tsx')).toBeTruthy();
});
});
it.each`
style
${'styled-components'}

View File

@ -129,7 +129,7 @@ export async function libraryGenerator(host: Tree, schema: Schema) {
if (options.component) {
const componentTask = await componentGenerator(host, {
name: options.name,
name: options.fileName,
project: options.name,
flat: true,
style: options.style,

View File

@ -26,6 +26,7 @@ export interface Schema {
tags?: string;
unitTestRunner?: 'jest' | 'vitest' | 'none';
minimal?: boolean;
simpleName?: boolean;
}
export interface NormalizedSchema extends Schema {

View File

@ -186,6 +186,11 @@
"description": "Create a React library with a minimal setup, no separate test files.",
"type": "boolean",
"default": false
},
"simpleName": {
"description": "Don't include the directory in the name of the module of the library.",
"type": "boolean",
"default": false
}
},
"required": ["name"]