Skip to content

Commit ceb1dca

Browse files
Merge branch 'feat/list-extended-schematics' of https://github.com/xdissent/nest-cli into xdissent-feat/list-extended-schematics
2 parents 1e69046 + a063bbd commit ceb1dca

File tree

8 files changed

+164
-15
lines changed

8 files changed

+164
-15
lines changed

lib/schematics/custom.collection.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { readFileSync } from 'fs';
2-
import { dirname, join } from 'path';
1+
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
32
import { AbstractCollection } from './abstract.collection';
43
import { Schematic } from './nest.collection';
54

@@ -11,18 +10,30 @@ export interface CollectionSchematic {
1110

1211
export class CustomCollection extends AbstractCollection {
1312
public getSchematics(): Schematic[] {
14-
const collectionPackagePath = dirname(require.resolve(this.collection));
15-
const collectionPath = join(collectionPackagePath, 'collection.json');
16-
const collection = JSON.parse(readFileSync(collectionPath, 'utf8'));
17-
const schematics = Object.entries(collection.schematics).map(
18-
([name, value]) => {
19-
const schematic = value as CollectionSchematic;
20-
const description = schematic.description;
21-
const alias = schematic?.aliases?.length ? schematic.aliases[0] : '';
22-
return { name, description, alias };
23-
},
13+
const workflow = new NodeWorkflow(process.cwd(), {});
14+
const collection = workflow.engine.createCollection(this.collection);
15+
const collectionDescs = [
16+
collection.description,
17+
...(collection.baseDescriptions ?? []),
18+
];
19+
const usedNames = new Set<string>();
20+
const schematics: Schematic[] = [];
21+
for (const collectionDesc of collectionDescs) {
22+
const schematicsDescs = Object.entries(collectionDesc.schematics);
23+
for (const [name, { description, aliases = [] }] of schematicsDescs) {
24+
if (usedNames.has(name)) {
25+
continue;
26+
}
27+
usedNames.add(name);
28+
const alias = aliases.find((a) => !usedNames.has(a)) ?? name;
29+
for (const alias of aliases) {
30+
usedNames.add(alias);
31+
}
32+
schematics.push({ name, alias, description });
33+
}
34+
}
35+
return schematics.sort((a, b) =>
36+
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
2437
);
25-
26-
return schematics;
2738
}
2839
}

test/jest-config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"transform": {
66
"^.+\\.(t|j)s$": "ts-jest"
77
},
8-
"coverageDirectory": "../coverage"
8+
"coverageDirectory": "../coverage",
9+
"modulePaths": ["<rootDir>/test/lib/schematics/fixtures"]
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { resolve } from 'path';
2+
import { AbstractRunner } from '../../../lib/runners';
3+
import { CustomCollection } from '../../../lib/schematics/custom.collection';
4+
5+
describe('Custom Collection', () => {
6+
it(`should list schematics from simple collection`, async () => {
7+
const mock = jest.fn();
8+
mock.mockImplementation(() => {
9+
return {
10+
logger: {},
11+
run: jest.fn().mockImplementation(() => Promise.resolve()),
12+
};
13+
});
14+
const mockedRunner = mock();
15+
const collection = new CustomCollection(
16+
require.resolve('./fixtures/simple/collection.json'),
17+
mockedRunner as AbstractRunner,
18+
);
19+
const schematics = collection.getSchematics();
20+
expect(schematics).toEqual([
21+
{ name: 'simple1', alias: 's1', description: 'Simple schematic 1' },
22+
{ name: 'simple2', alias: 's2', description: 'Simple schematic 2' },
23+
{ name: 'simple3', alias: 's3', description: 'Simple schematic 3' },
24+
]);
25+
});
26+
27+
it(`should list schematics from extended collection`, async () => {
28+
const mock = jest.fn();
29+
mock.mockImplementation(() => {
30+
return {
31+
logger: {},
32+
run: jest.fn().mockImplementation(() => Promise.resolve()),
33+
};
34+
});
35+
const mockedRunner = mock();
36+
const collection = new CustomCollection(
37+
require.resolve('./fixtures/extended/collection.json'),
38+
mockedRunner as AbstractRunner,
39+
);
40+
const schematics = collection.getSchematics();
41+
expect(schematics).toEqual([
42+
{ name: 'extend1', alias: 'x1', description: 'Extended schematic 1' },
43+
{ name: 'extend2', alias: 'x2', description: 'Extended schematic 2' },
44+
{ name: 'simple1', alias: 's1', description: 'Override schematic 1' },
45+
{
46+
name: 'simple2',
47+
alias: 'os2',
48+
description: 'Override schematic 2',
49+
},
50+
{
51+
name: 'simple3',
52+
alias: 'simple3',
53+
description: 'Simple schematic 3',
54+
},
55+
]);
56+
});
57+
58+
it(`should list schematics from package with collection.json path in package.json`, async () => {
59+
const mock = jest.fn();
60+
mock.mockImplementation(() => {
61+
return {
62+
logger: {},
63+
run: jest.fn().mockImplementation(() => Promise.resolve()),
64+
};
65+
});
66+
const mockedRunner = mock();
67+
const collection = new CustomCollection(
68+
'package',
69+
mockedRunner as AbstractRunner,
70+
);
71+
const schematics = collection.getSchematics();
72+
expect(schematics).toEqual([
73+
{ name: 'package1', alias: 'pkg1', description: 'Package schematic 1' },
74+
]);
75+
});
76+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"$schema": "../../../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"extends": ["../simple/collection.json"],
4+
"schematics": {
5+
"simple1": {
6+
"factory": "factory",
7+
"description": "Override schematic 1",
8+
"aliases": ["s1", "simp1"]
9+
},
10+
"simple2": {
11+
"factory": "factory",
12+
"description": "Override schematic 2",
13+
"aliases": ["os2", "s2", "simp2"]
14+
},
15+
"extend1": {
16+
"factory": "factory",
17+
"description": "Extended schematic 1",
18+
"aliases": ["x1", "ext1"]
19+
},
20+
"extend2": {
21+
"factory": "factory",
22+
"description": "Extended schematic 2",
23+
"aliases": ["x2", "ext2", "s3", "simp3"]
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "../../../../../../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"package1": {
5+
"factory": "factory",
6+
"description": "Package schematic 1",
7+
"aliases": ["pkg1"]
8+
}
9+
}
10+
}

test/lib/schematics/fixtures/package/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "package",
3+
"version": "0.0.0",
4+
"schematics": "./a/b/c/collection.json"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "../../../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"simple1": {
5+
"factory": "factory",
6+
"description": "Simple schematic 1",
7+
"aliases": ["s1", "simp1"]
8+
},
9+
"simple2": {
10+
"factory": "factory",
11+
"description": "Simple schematic 2",
12+
"aliases": ["s2", "simp2"]
13+
},
14+
"simple3": {
15+
"factory": "factory",
16+
"description": "Simple schematic 3",
17+
"aliases": ["s3", "simp3"]
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)