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 typescript_1 = require("../../utilities/typescript");
|
10 | const versions_1 = require("../../utils/versions");
|
11 |
|
12 | const { jestProjectGenerator } = require('@nrwl/jest');
|
13 |
|
14 | const { lintProjectGenerator, Linter } = require('@nrwl/linter');
|
15 | function addProject(tree, options) {
|
16 | const projectConfiguration = {
|
17 | root: options.projectRoot,
|
18 | sourceRoot: (0, devkit_1.joinPathFragments)(options.projectRoot, 'src'),
|
19 | projectType: 'library',
|
20 | targets: {},
|
21 | tags: options.parsedTags,
|
22 | };
|
23 | if (options.buildable) {
|
24 | const { libsDir } = (0, devkit_1.getWorkspaceLayout)(tree);
|
25 | (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { '@nrwl/js': versions_1.nxVersion });
|
26 | projectConfiguration.targets.build = {
|
27 | executor: '@nrwl/js:tsc',
|
28 | outputs: ['{options.outputPath}'],
|
29 | options: {
|
30 | outputPath: `dist/${libsDir}/${options.projectDirectory}`,
|
31 | main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'),
|
32 | tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
|
33 | assets: [`${options.projectRoot}/*.md`],
|
34 | },
|
35 | };
|
36 | }
|
37 | (0, devkit_1.addProjectConfiguration)(tree, options.name, projectConfiguration, options.standaloneConfig);
|
38 | }
|
39 | function addLint(tree, options) {
|
40 | return lintProjectGenerator(tree, {
|
41 | project: options.name,
|
42 | linter: options.linter,
|
43 | skipFormat: true,
|
44 | tsConfigPaths: [
|
45 | (0, devkit_1.joinPathFragments)(options.projectRoot, 'tsconfig.lib.json'),
|
46 | ],
|
47 | unitTestRunner: options.unitTestRunner,
|
48 | eslintFilePatterns: [
|
49 | `${options.projectRoot}/**/*.${options.js ? 'js' : 'ts'}`,
|
50 | ],
|
51 | setParserOptionsProject: options.setParserOptionsProject,
|
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, typescript_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, typescript_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 | 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 }));
|
100 | });
|
101 | }
|
102 | function libraryGenerator(tree, schema) {
|
103 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
104 | const options = normalizeOptions(tree, schema);
|
105 | createFiles(tree, options);
|
106 | if (!options.skipTsConfig) {
|
107 | updateRootTsConfig(tree, options);
|
108 | }
|
109 | addProject(tree, options);
|
110 | const tasks = [];
|
111 | if (options.linter !== 'none') {
|
112 | const lintCallback = yield addLint(tree, options);
|
113 | tasks.push(lintCallback);
|
114 | }
|
115 | if (options.unitTestRunner === 'jest') {
|
116 | const jestCallback = yield addJest(tree, options);
|
117 | tasks.push(jestCallback);
|
118 | }
|
119 | if (!options.skipFormat) {
|
120 | yield (0, devkit_1.formatFiles)(tree);
|
121 | }
|
122 | return (0, run_tasks_in_serial_1.runTasksInSerial)(...tasks);
|
123 | });
|
124 | }
|
125 | exports.libraryGenerator = libraryGenerator;
|
126 | exports.default = libraryGenerator;
|
127 | exports.librarySchematic = (0, devkit_1.convertNxGenerator)(libraryGenerator);
|
128 | function normalizeOptions(tree, options) {
|
129 | const name = (0, devkit_1.names)(options.name).fileName;
|
130 | const projectDirectory = options.directory
|
131 | ? `${(0, devkit_1.names)(options.directory).fileName}/${name}`
|
132 | : name;
|
133 | if (!options.unitTestRunner) {
|
134 | options.unitTestRunner = 'jest';
|
135 | }
|
136 | if (!options.linter) {
|
137 | options.linter = Linter.EsLint;
|
138 | }
|
139 | const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
|
140 | const fileName = getCaseAwareFileName({
|
141 | fileName: options.simpleModuleName ? name : projectName,
|
142 | pascalCaseFiles: options.pascalCaseFiles,
|
143 | });
|
144 | const { libsDir, npmScope } = (0, devkit_1.getWorkspaceLayout)(tree);
|
145 | const projectRoot = (0, devkit_1.joinPathFragments)(libsDir, projectDirectory);
|
146 | const parsedTags = options.tags
|
147 | ? options.tags.split(',').map((s) => s.trim())
|
148 | : [];
|
149 | const importPath = options.importPath || (0, path_1.getImportPath)(npmScope, projectDirectory);
|
150 | return Object.assign(Object.assign({}, options), { fileName, name: projectName, projectRoot,
|
151 | projectDirectory,
|
152 | parsedTags,
|
153 | importPath });
|
154 | }
|
155 | function getCaseAwareFileName(options) {
|
156 | const normalized = (0, devkit_1.names)(options.fileName);
|
157 | return options.pascalCaseFiles ? normalized.className : normalized.fileName;
|
158 | }
|
159 |
|
\ | No newline at end of file |