Skip to content

Commit 2be25eb

Browse files
authoredApr 27, 2023
feat(nest): adding simpleName option to library generator (#16024)
1 parent e3c50a9 commit 2be25eb

File tree

7 files changed

+66
-5
lines changed

7 files changed

+66
-5
lines changed
 

‎docs/generated/packages/nest/generators/library.json

+5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@
128128
"type": "boolean",
129129
"default": false,
130130
"x-priority": "internal"
131+
},
132+
"simpleName": {
133+
"description": "Don't include the directory in the name of the module of the library.",
134+
"type": "boolean",
135+
"default": false
131136
}
132137
},
133138
"additionalProperties": false,

‎packages/nest/src/generators/library/lib/add-exports-to-barrel.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Tree } from '@nx/devkit';
22
import { addGlobal, removeChange } from '@nx/js';
3-
import type { NormalizedOptions } from '../schema';
43
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
4+
import * as ts from 'typescript';
5+
import type { NormalizedOptions } from '../schema';
56

67
let tsModule: typeof import('typescript');
78

@@ -21,12 +22,17 @@ export function addExportsToBarrelFile(
2122
true
2223
);
2324

25+
// find the export in the source file
26+
const exportStatement = sourceFile.statements.find((statement) =>
27+
ts.isExportDeclaration(statement)
28+
);
29+
2430
sourceFile = removeChange(
2531
tree,
2632
sourceFile,
2733
indexPath,
2834
0,
29-
`export * from './lib/${options.fileName}';`
35+
exportStatement.getFullText()
3036
);
3137
sourceFile = addGlobal(
3238
tree,

‎packages/nest/src/generators/library/lib/create-files.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function createFiles(tree: Tree, options: NormalizedOptions): void {
1313
...names(options.projectName),
1414
tmpl: '',
1515
offsetFromRoot: offsetFromRoot(options.projectRoot),
16+
fileName: options.fileName,
1617
};
1718
generateFiles(
1819
tree,

‎packages/nest/src/generators/library/lib/normalize-options.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { extractLayoutDirectory, Tree } from '@nx/devkit';
2-
import { getWorkspaceLayout, joinPathFragments, names } from '@nx/devkit';
31
import type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nx/js/src/utils/schema';
42
import { Linter } from '@nx/linter';
3+
import {
4+
extractLayoutDirectory,
5+
getWorkspaceLayout,
6+
joinPathFragments,
7+
names,
8+
Tree,
9+
} from '@nx/devkit';
510
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';
611

712
export function normalizeOptions(
@@ -19,7 +24,7 @@ export function normalizeOptions(
1924
: name;
2025

2126
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
22-
const fileName = projectName;
27+
const fileName = options.simpleName ? name : projectName;
2328
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
2429

2530
const parsedTags = options.tags

‎packages/nest/src/generators/library/library.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,42 @@ describe('lib', () => {
348348
).toMatchSnapshot();
349349
});
350350
});
351+
352+
describe('--simpleName', () => {
353+
it('should generate a library with a simple name', async () => {
354+
await libraryGenerator(tree, {
355+
name: libName,
356+
simpleName: true,
357+
directory: 'api',
358+
service: true,
359+
controller: true,
360+
});
361+
362+
const indexFile = tree.read('libs/api/my-lib/src/index.ts', 'utf-8');
363+
364+
expect(indexFile).toContain(`export * from './lib/my-lib.module';`);
365+
expect(indexFile).toContain(`export * from './lib/my-lib.service';`);
366+
expect(indexFile).toContain(`export * from './lib/my-lib.controller';`);
367+
368+
expect(
369+
tree.exists('libs/api/my-lib/src/lib/my-lib.module.ts')
370+
).toBeTruthy();
371+
372+
expect(
373+
tree.exists('libs/api/my-lib/src/lib/my-lib.service.ts')
374+
).toBeTruthy();
375+
376+
expect(
377+
tree.exists('libs/api/my-lib/src/lib/my-lib.service.spec.ts')
378+
).toBeTruthy();
379+
380+
expect(
381+
tree.exists('libs/api/my-lib/src/lib/my-lib.controller.ts')
382+
).toBeTruthy();
383+
384+
expect(
385+
tree.exists('libs/api/my-lib/src/lib/my-lib.controller.spec.ts')
386+
).toBeTruthy();
387+
});
388+
});
351389
});

‎packages/nest/src/generators/library/schema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface LibraryGeneratorOptions {
3030
standaloneConfig?: boolean;
3131
setParserOptionsProject?: boolean;
3232
skipPackageJson?: boolean;
33+
simpleName?: boolean;
3334
}
3435

3536
export interface NormalizedOptions extends LibraryGeneratorOptions {

‎packages/nest/src/generators/library/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@
128128
"type": "boolean",
129129
"default": false,
130130
"x-priority": "internal"
131+
},
132+
"simpleName": {
133+
"description": "Don't include the directory in the name of the module of the library.",
134+
"type": "boolean",
135+
"default": false
131136
}
132137
},
133138
"additionalProperties": false,

1 commit comments

Comments
 (1)

vercel[bot] commented on Apr 27, 2023

@vercel[bot]

Successfully deployed to the following URLs:

nx-dev – ./

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

Please sign in to comment.