Skip to content

Commit 7302603

Browse files
authored
feat(js): provide a new way of generating project name and root directory for libraries (#18420)
1 parent 9cd0cf8 commit 7302603

File tree

10 files changed

+1000
-71
lines changed

10 files changed

+1000
-71
lines changed

docs/generated/packages/js/generators/library.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "library",
3-
"factory": "./src/generators/library/library#libraryGenerator",
3+
"factory": "./src/generators/library/library#libraryGeneratorInternal",
44
"schema": {
55
"$schema": "http://json-schema.org/schema",
66
"$id": "NxTypescriptLibrary",
@@ -14,13 +14,18 @@
1414
"description": "Library name.",
1515
"$default": { "$source": "argv", "index": 0 },
1616
"x-prompt": "What name would you like to use for the library?",
17-
"pattern": "^[a-zA-Z].*$"
17+
"pattern": "(?:^@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*\\/[a-zA-Z0-9-~][a-zA-Z0-9-._~]*|^[a-zA-Z][^:]*)$"
1818
},
1919
"directory": {
2020
"type": "string",
2121
"description": "A directory where the lib is placed.",
2222
"x-priority": "important"
2323
},
24+
"projectNameAndRootFormat": {
25+
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
26+
"type": "string",
27+
"enum": ["as-provided", "derived"]
28+
},
2429
"linter": {
2530
"description": "The tool to use for running lint checks.",
2631
"type": "string",
@@ -142,7 +147,7 @@
142147
"aliases": ["lib"],
143148
"x-type": "library",
144149
"description": "Create a library",
145-
"implementation": "/packages/js/src/generators/library/library#libraryGenerator.ts",
150+
"implementation": "/packages/js/src/generators/library/library#libraryGeneratorInternal.ts",
146151
"hidden": false,
147152
"path": "/packages/js/src/generators/library/schema.json",
148153
"type": "generator"

e2e/js/src/js.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,55 @@ export function ${lib}Wildcard() {
230230
// restore nx.json
231231
updateFile('nx.json', () => originalNxJson);
232232
});
233+
234+
it('should generate project with name and directory as provided when --project-name-and-root-format=as-provided', async () => {
235+
const lib1 = uniq('lib1');
236+
runCLI(
237+
`generate @nx/js:lib ${lib1} --directory=shared --bundler=tsc --project-name-and-root-format=as-provided`
238+
);
239+
240+
// check files are generated without the layout directory ("libs/") and
241+
// in the directory provided (no project name appended)
242+
checkFilesExist('shared/src/index.ts');
243+
// check project name is as provided (no prefixed directory name)
244+
expect(runCLI(`build ${lib1}`)).toContain(
245+
'Done compiling TypeScript files'
246+
);
247+
// check tests pass
248+
const testResult = await runCLIAsync(`test ${lib1}`);
249+
expect(testResult.combinedOutput).toContain(
250+
'Test Suites: 1 passed, 1 total'
251+
);
252+
}, 500_000);
253+
254+
it('should support generating with a scoped project name when --project-name-and-root-format=as-provided', async () => {
255+
const scopedLib = uniq('@my-org/lib1');
256+
257+
// assert scoped project names are not supported when --project-name-and-root-format=derived
258+
expect(() =>
259+
runCLI(
260+
`generate @nx/js:lib ${scopedLib} --bundler=tsc --project-name-and-root-format=derived`
261+
)
262+
).toThrow();
263+
264+
runCLI(
265+
`generate @nx/js:lib ${scopedLib} --bundler=tsc --project-name-and-root-format=as-provided`
266+
);
267+
268+
// check files are generated without the layout directory ("libs/") and
269+
// using the project name as the directory when no directory is provided
270+
checkFilesExist(
271+
`${scopedLib}/src/index.ts`,
272+
`${scopedLib}/src/lib/${scopedLib.split('/')[1]}.ts`
273+
);
274+
// check build works
275+
expect(runCLI(`build ${scopedLib}`)).toContain(
276+
'Done compiling TypeScript files'
277+
);
278+
// check tests pass
279+
const testResult = await runCLIAsync(`test ${scopedLib}`);
280+
expect(testResult.combinedOutput).toContain(
281+
'Test Suites: 1 passed, 1 total'
282+
);
283+
}, 500_000);
233284
});

packages/devkit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"homepage": "https://nx.dev",
3030
"dependencies": {
3131
"ejs": "^3.1.7",
32+
"enquirer": "~2.3.6",
3233
"ignore": "^5.0.4",
3334
"tmp": "~0.2.1",
3435
"tslib": "^2.3.0",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy