/
generator.ts
114 lines (97 loc) · 2.82 KB
/
generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import type { Tree } from '@nrwl/devkit';
import {
convertNxGenerator,
generateFiles,
getWorkspaceLayout,
names,
readJson,
readProjectConfiguration,
updateJson,
} from '@nrwl/devkit';
import * as path from 'path';
import type { Schema } from './schema';
interface NormalizedSchema extends Schema {
fileName: string;
className: string;
projectRoot: string;
projectSourceRoot: string;
npmScope: string;
npmPackageName: string;
}
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const { npmScope } = getWorkspaceLayout(host);
const { fileName, className } = names(options.name);
const { root: projectRoot, sourceRoot: projectSourceRoot } =
readProjectConfiguration(host, options.project);
const npmPackageName = readJson<{ name: string }>(
host,
path.join(projectRoot, 'package.json')
).name;
let description: string;
if (options.description) {
description = options.description;
} else {
description = `${options.name} generator`;
}
return {
...options,
fileName,
className,
description,
projectRoot,
projectSourceRoot,
npmScope,
npmPackageName,
};
}
function addFiles(host: Tree, options: NormalizedSchema) {
const indexPath = `${options.projectSourceRoot}/generators/${options.fileName}/files/src/index.ts__template__`;
if (!host.exists(indexPath)) {
host.write(indexPath, 'const variable = "<%= projectName %>";');
}
generateFiles(
host,
path.join(__dirname, './files/generator'),
`${options.projectSourceRoot}/generators`,
{
...options,
tmpl: '',
}
);
if (options.unitTestRunner === 'none') {
host.delete(
path.join(
options.projectSourceRoot,
'generators',
options.fileName,
`generator.spec.ts`
)
);
}
}
function updateGeneratorJson(host: Tree, options: NormalizedSchema) {
let generatorPath: string;
if (host.exists(path.join(options.projectRoot, 'generators.json'))) {
generatorPath = path.join(options.projectRoot, 'generators.json');
} else {
generatorPath = path.join(options.projectRoot, 'collection.json');
}
return updateJson(host, generatorPath, (json) => {
let generators = json.generators ?? json.schematics;
generators = generators || {};
generators[options.name] = {
factory: `./src/generators/${options.name}/generator`,
schema: `./src/generators/${options.name}/schema.json`,
description: options.description,
};
json.generators = generators;
return json;
});
}
export async function generatorGenerator(host: Tree, schema: Schema) {
const options = normalizeOptions(host, schema);
addFiles(host, options);
updateGeneratorJson(host, options);
}
export default generatorGenerator;
export const generatorSchematic = convertNxGenerator(generatorGenerator);