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