1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.librarySchematic = exports.libraryGenerator = exports.addLint = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const devkit_1 = require("@nrwl/devkit");
|
6 | const path_1 = require("nx/src/utils/path");
|
7 | const path_2 = require("path");
|
8 | const run_tasks_in_serial_1 = require("../../utilities/run-tasks-in-serial");
|
9 | const ts_config_1 = require("../../utilities/ts-config");
|
10 | const versions_1 = require("../../utils/versions");
|
11 | function addProject(tree, options) {
|
12 | const projectConfiguration = {
|
13 | root: options.projectRoot,
|
14 | sourceRoot: (0, devkit_1.joinPathFragments)(options.projectRoot, 'src'),
|
15 | projectType: 'library',
|
16 | targets: {},
|
17 | tags: options.parsedTags,
|
18 | };
|
19 | if (options.buildable) {
|
20 | (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { '@nrwl/js': versions_1.nxVersion });
|
21 | projectConfiguration.targets.build = {
|
22 | executor: '@nrwl/js:tsc',
|
23 | outputs: ['{options.outputPath}'],
|
24 | options: {
|
25 | outputPath: options.libsDir != '.'
|
26 | ? `dist/${options.libsDir}/${options.projectDirectory}`
|
27 | : `dist/${options.projectDirectory}`,
|
28 | main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'),
|
29 | tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
|
30 | assets: [`${options.projectRoot}/*.md`],
|
31 | },
|
32 | };
|
33 | }
|
34 | (0, devkit_1.addProjectConfiguration)(tree, options.name, projectConfiguration);
|
35 | }
|
36 | function addLint(tree, options) {
|
37 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
38 | const { lintProjectGenerator } = (0, devkit_1.ensurePackage)('@nrwl/linter', versions_1.nxVersion);
|
39 | return lintProjectGenerator(tree, {
|
40 | project: options.name,
|
41 | linter: options.linter,
|
42 | skipFormat: true,
|
43 | tsConfigPaths: [
|
44 | (0, devkit_1.joinPathFragments)(options.projectRoot, 'tsconfig.lib.json'),
|
45 | ],
|
46 | unitTestRunner: options.unitTestRunner,
|
47 | eslintFilePatterns: [
|
48 | `${options.projectRoot}/**/*.${options.js ? 'js' : 'ts'}`,
|
49 | ],
|
50 | setParserOptionsProject: options.setParserOptionsProject,
|
51 | });
|
52 | });
|
53 | }
|
54 | exports.addLint = addLint;
|
55 | function updateTsConfig(tree, options) {
|
56 | (0, devkit_1.updateJson)(tree, (0, path_2.join)(options.projectRoot, 'tsconfig.json'), (json) => {
|
57 | if (options.strict) {
|
58 | json.compilerOptions = Object.assign(Object.assign({}, json.compilerOptions), { forceConsistentCasingInFileNames: true, strict: true, noImplicitReturns: true, noFallthroughCasesInSwitch: true });
|
59 | }
|
60 | return json;
|
61 | });
|
62 | }
|
63 | function updateRootTsConfig(host, options) {
|
64 | (0, devkit_1.updateJson)(host, (0, ts_config_1.getRootTsConfigPathInTree)(host), (json) => {
|
65 | const c = json.compilerOptions;
|
66 | c.paths = c.paths || {};
|
67 | delete c.paths[options.name];
|
68 | if (c.paths[options.importPath]) {
|
69 | throw new Error(`You already have a library using the import path "${options.importPath}". Make sure to specify a unique one.`);
|
70 | }
|
71 | c.paths[options.importPath] = [
|
72 | (0, devkit_1.joinPathFragments)(options.projectRoot, './src', 'index.' + (options.js ? 'js' : 'ts')),
|
73 | ];
|
74 | return json;
|
75 | });
|
76 | }
|
77 | function createFiles(tree, options) {
|
78 | const { className, name, propertyName } = (0, devkit_1.names)(options.name);
|
79 | const rootOffset = (0, devkit_1.offsetFromRoot)(options.projectRoot);
|
80 | (0, devkit_1.generateFiles)(tree, (0, path_2.join)(__dirname, './files/lib'), options.projectRoot, Object.assign(Object.assign({}, options), { dot: '.', className,
|
81 | name,
|
82 | propertyName, js: !!options.js, cliCommand: 'nx', strict: undefined, tmpl: '', offsetFromRoot: rootOffset, rootTsConfigPath: (0, ts_config_1.getRelativePathToRootTsConfig)(tree, options.projectRoot), hasUnitTestRunner: options.unitTestRunner !== 'none', hasLinter: options.linter !== 'none' }));
|
83 | if (options.unitTestRunner === 'none') {
|
84 | tree.delete((0, path_2.join)(options.projectRoot, 'src/lib', `${options.fileName}.spec.ts`));
|
85 | }
|
86 | if (options.skipBabelrc) {
|
87 | tree.delete((0, path_2.join)(options.projectRoot, '.babelrc'));
|
88 | }
|
89 | if (options.js) {
|
90 | (0, devkit_1.toJS)(tree);
|
91 | }
|
92 | if (!options.buildable) {
|
93 | tree.delete((0, path_2.join)(options.projectRoot, 'package.json'));
|
94 | }
|
95 | updateTsConfig(tree, options);
|
96 | }
|
97 | function addJest(tree, options) {
|
98 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
99 | const { jestProjectGenerator } = (0, devkit_1.ensurePackage)('@nrwl/jest', versions_1.nxVersion);
|
100 | return yield jestProjectGenerator(tree, Object.assign(Object.assign({}, options), { project: options.name, setupFile: 'none', supportTsx: true, babelJest: options.babelJest, skipSerializers: true, testEnvironment: options.testEnvironment, skipFormat: true }));
|
101 | });
|
102 | }
|
103 | function addTypescript(tree, options) {
|
104 |
|
105 | if (!options.skipTsConfig && !(0, ts_config_1.getRootTsConfigFileName)()) {
|
106 | (0, devkit_1.generateFiles)(tree, (0, devkit_1.joinPathFragments)(__dirname, './files/root'), '.', {});
|
107 | }
|
108 | return !options.js
|
109 | ? (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { typescript: versions_1.typescriptVersion })
|
110 | : () => { };
|
111 | }
|
112 | function libraryGenerator(tree, schema) {
|
113 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
114 | const options = normalizeOptions(tree, schema);
|
115 | const tasks = [];
|
116 | addTypescript(tree, options);
|
117 | createFiles(tree, options);
|
118 | addProject(tree, options);
|
119 | if (options.linter !== 'none') {
|
120 | const lintCallback = yield addLint(tree, options);
|
121 | tasks.push(lintCallback);
|
122 | }
|
123 | if (options.unitTestRunner === 'jest') {
|
124 | const jestCallback = yield addJest(tree, options);
|
125 | tasks.push(jestCallback);
|
126 | }
|
127 | if (!options.skipTsConfig) {
|
128 | updateRootTsConfig(tree, options);
|
129 | }
|
130 | if (!options.skipFormat) {
|
131 | yield (0, devkit_1.formatFiles)(tree);
|
132 | }
|
133 | return (0, run_tasks_in_serial_1.runTasksInSerial)(...tasks);
|
134 | });
|
135 | }
|
136 | exports.libraryGenerator = libraryGenerator;
|
137 | exports.default = libraryGenerator;
|
138 | exports.librarySchematic = (0, devkit_1.convertNxGenerator)(libraryGenerator);
|
139 | function normalizeOptions(tree, options) {
|
140 | const name = (0, devkit_1.names)(options.name).fileName;
|
141 | const { layoutDirectory, projectDirectory } = (0, devkit_1.extractLayoutDirectory)(options.directory ? `${(0, devkit_1.names)(options.directory).fileName}/${name}` : name);
|
142 | if (!options.unitTestRunner) {
|
143 | options.unitTestRunner = 'jest';
|
144 | }
|
145 | if (!options.linter) {
|
146 | const { Linter } = require('@nrwl/linter');
|
147 | options.linter = Linter.EsLint;
|
148 | }
|
149 | const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
|
150 | const fileName = getCaseAwareFileName({
|
151 | fileName: options.simpleModuleName ? name : projectName,
|
152 | pascalCaseFiles: options.pascalCaseFiles,
|
153 | });
|
154 | const { libsDir: defaultLibsDir, npmScope } = (0, devkit_1.getWorkspaceLayout)(tree);
|
155 | const libsDir = layoutDirectory !== null && layoutDirectory !== void 0 ? layoutDirectory : defaultLibsDir;
|
156 | const projectRoot = (0, devkit_1.joinPathFragments)(libsDir, projectDirectory);
|
157 | const parsedTags = options.tags
|
158 | ? options.tags.split(',').map((s) => s.trim())
|
159 | : [];
|
160 | const importPath = options.importPath || (0, path_1.getImportPath)(npmScope, projectDirectory);
|
161 | return Object.assign(Object.assign({}, options), { fileName, name: projectName, projectRoot,
|
162 | projectDirectory,
|
163 | parsedTags,
|
164 | importPath,
|
165 | libsDir });
|
166 | }
|
167 | function getCaseAwareFileName(options) {
|
168 | const normalized = (0, devkit_1.names)(options.fileName);
|
169 | return options.pascalCaseFiles ? normalized.className : normalized.fileName;
|
170 | }
|
171 |
|
\ | No newline at end of file |