feat(nest): adding simpleName option to library generator (#16024)

This commit is contained in:
Ashley Hunter 2023-04-27 15:38:31 +01:00 committed by GitHub
parent e3c50a9a48
commit 2be25eb45c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 5 deletions

View File

@ -128,6 +128,11 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"simpleName": {
"description": "Don't include the directory in the name of the module of the library.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,

View File

@ -1,7 +1,8 @@
import type { Tree } from '@nx/devkit';
import { addGlobal, removeChange } from '@nx/js';
import type { NormalizedOptions } from '../schema';
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
import * as ts from 'typescript';
import type { NormalizedOptions } from '../schema';
let tsModule: typeof import('typescript');
@ -21,12 +22,17 @@ export function addExportsToBarrelFile(
true
);
// find the export in the source file
const exportStatement = sourceFile.statements.find((statement) =>
ts.isExportDeclaration(statement)
);
sourceFile = removeChange(
tree,
sourceFile,
indexPath,
0,
`export * from './lib/${options.fileName}';`
exportStatement.getFullText()
);
sourceFile = addGlobal(
tree,

View File

@ -13,6 +13,7 @@ export function createFiles(tree: Tree, options: NormalizedOptions): void {
...names(options.projectName),
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot),
fileName: options.fileName,
};
generateFiles(
tree,

View File

@ -1,7 +1,12 @@
import { extractLayoutDirectory, Tree } from '@nx/devkit';
import { getWorkspaceLayout, joinPathFragments, names } from '@nx/devkit';
import type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nx/js/src/utils/schema';
import { Linter } from '@nx/linter';
import {
extractLayoutDirectory,
getWorkspaceLayout,
joinPathFragments,
names,
Tree,
} from '@nx/devkit';
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';
export function normalizeOptions(
@ -19,7 +24,7 @@ export function normalizeOptions(
: name;
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName;
const fileName = options.simpleName ? name : projectName;
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
const parsedTags = options.tags

View File

@ -348,4 +348,42 @@ describe('lib', () => {
).toMatchSnapshot();
});
});
describe('--simpleName', () => {
it('should generate a library with a simple name', async () => {
await libraryGenerator(tree, {
name: libName,
simpleName: true,
directory: 'api',
service: true,
controller: true,
});
const indexFile = tree.read('libs/api/my-lib/src/index.ts', 'utf-8');
expect(indexFile).toContain(`export * from './lib/my-lib.module';`);
expect(indexFile).toContain(`export * from './lib/my-lib.service';`);
expect(indexFile).toContain(`export * from './lib/my-lib.controller';`);
expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.module.ts')
).toBeTruthy();
expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.service.ts')
).toBeTruthy();
expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.service.spec.ts')
).toBeTruthy();
expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.controller.ts')
).toBeTruthy();
expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.controller.spec.ts')
).toBeTruthy();
});
});
});

View File

@ -30,6 +30,7 @@ export interface LibraryGeneratorOptions {
standaloneConfig?: boolean;
setParserOptionsProject?: boolean;
skipPackageJson?: boolean;
simpleName?: boolean;
}
export interface NormalizedOptions extends LibraryGeneratorOptions {

View File

@ -128,6 +128,11 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"simpleName": {
"description": "Don't include the directory in the name of the module of the library.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,