forked from nrwl/nx
/
library.ts
165 lines (148 loc) · 4.56 KB
/
library.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
addDependenciesToPackageJson,
formatFiles,
installPackagesTask,
moveFilesToNewDirectory,
Tree,
} from '@nrwl/devkit';
import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter';
import { jestProjectGenerator } from '@nrwl/jest';
import { Linter } from '@nrwl/linter';
import { convertToNxProjectGenerator } from '@nrwl/workspace';
import init from '../../generators/init/init';
import { postcssVersion } from '../../utils/versions';
import addLintingGenerator from '../add-linting/add-linting';
import karmaProjectGenerator from '../karma-project/karma-project';
import setupTailwindGenerator from '../setup-tailwind/setup-tailwind';
import { addModule } from './lib/add-module';
import {
enableStrictTypeChecking,
setLibraryStrictDefault,
} from './lib/enable-strict-type-checking';
import { normalizeOptions } from './lib/normalize-options';
import { NormalizedSchema } from './lib/normalized-schema';
import { updateLibPackageNpmScope } from './lib/update-lib-package-npm-scope';
import { updateProject } from './lib/update-project';
import { updateTsConfig } from './lib/update-tsconfig';
import { Schema } from './schema';
export async function libraryGenerator(host: Tree, schema: Partial<Schema>) {
// Do some validation checks
if (!schema.routing && schema.lazy) {
throw new Error(`To use "--lazy" option, "--routing" must also be set.`);
}
if (schema.publishable === true && !schema.importPath) {
throw new Error(
`For publishable libs you have to provide a proper "--importPath" which needs to be a valid npm package name (e.g. my-awesome-lib or @myorg/my-lib)`
);
}
if (schema.addTailwind && !schema.buildable && !schema.publishable) {
throw new Error(
`To use "--addTailwind" option, you have to set either "--buildable" or "--publishable".`
);
}
const options = normalizeOptions(host, schema);
await init(host, {
...options,
skipFormat: true,
});
const runAngularLibrarySchematic = wrapAngularDevkitSchematic(
'@schematics/angular',
'library'
);
await runAngularLibrarySchematic(host, {
name: options.name,
prefix: options.prefix,
entryFile: 'index',
skipPackageJson: !(options.publishable || options.buildable),
skipTsConfig: true,
});
if (options.ngCliSchematicLibRoot !== options.projectRoot) {
moveFilesToNewDirectory(
host,
options.ngCliSchematicLibRoot,
options.projectRoot
);
}
await updateProject(host, options);
updateTsConfig(host, options);
await addUnitTestRunner(host, options);
updateNpmScopeIfBuildableOrPublishable(host, options);
addModule(host, options);
setStrictMode(host, options);
await addLinting(host, options);
if (options.addTailwind) {
await setupTailwindGenerator(host, {
project: options.name,
skipFormat: true,
});
}
if (options.buildable || options.publishable) {
addDependenciesToPackageJson(
host,
{},
{
postcss: postcssVersion,
'postcss-import': '^14.0.2',
'postcss-preset-env': '^6.7.0',
'postcss-url': '^10.1.1',
}
);
}
if (options.standaloneConfig) {
await convertToNxProjectGenerator(host, {
project: options.name,
all: false,
skipFormat: true,
});
}
if (!options.skipFormat) {
await formatFiles(host);
}
return () => {
installPackagesTask(host);
};
}
async function addUnitTestRunner(host: Tree, options: NormalizedSchema) {
if (options.unitTestRunner === 'jest') {
await jestProjectGenerator(host, {
project: options.name,
setupFile: 'angular',
supportTsx: false,
skipSerializers: false,
skipFormat: true,
});
} else if (options.unitTestRunner === 'karma') {
await karmaProjectGenerator(host, {
project: options.name,
skipFormat: true,
});
}
}
function updateNpmScopeIfBuildableOrPublishable(
host: Tree,
options: NormalizedSchema
) {
if (options.buildable || options.publishable) {
updateLibPackageNpmScope(host, options);
}
}
function setStrictMode(host: Tree, options: NormalizedSchema) {
if (options.strict) {
enableStrictTypeChecking(host, options);
} else {
setLibraryStrictDefault(host, options.strict);
}
}
async function addLinting(host: Tree, options: NormalizedSchema) {
if (options.linter === Linter.None) {
return;
}
await addLintingGenerator(host, {
projectName: options.name,
projectRoot: options.projectRoot,
prefix: options.prefix,
setParserOptionsProject: options.setParserOptionsProject,
skipFormat: true,
});
}
export default libraryGenerator;