UNPKG

7.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.generateWorkspaceFiles = exports.DEFAULT_NRWL_PRETTIER_CONFIG = void 0;
4const tslib_1 = require("tslib");
5const devkit_1 = require("@nrwl/devkit");
6const versions_1 = require("../../utils/versions");
7const path_1 = require("path");
8const presets_1 = require("../utils/presets");
9const default_base_1 = require("../../utilities/default-base");
10exports.DEFAULT_NRWL_PRETTIER_CONFIG = {
11 singleQuote: true,
12};
13function generateWorkspaceFiles(tree, options) {
14 return tslib_1.__awaiter(this, void 0, void 0, function* () {
15 if (!options.name) {
16 throw new Error(`Invalid options, "name" is required.`);
17 }
18 options = normalizeOptions(options);
19 createReadme(tree, options);
20 createFiles(tree, options);
21 createNxJson(tree, options);
22 createPrettierrc(tree, options);
23 const [packageMajor] = (0, devkit_1.getPackageManagerVersion)(options.packageManager).split('.');
24 if (options.packageManager === 'pnpm' && +packageMajor >= 7) {
25 createNpmrc(tree, options);
26 }
27 else if (options.packageManager === 'yarn' && +packageMajor >= 2) {
28 createYarnrcYml(tree, options);
29 }
30 setPresetProperty(tree, options);
31 addNpmScripts(tree, options);
32 createAppsAndLibsFolders(tree, options);
33 setUpWorkspacesInPackageJson(tree, options);
34 yield (0, devkit_1.formatFiles)(tree);
35 });
36}
37exports.generateWorkspaceFiles = generateWorkspaceFiles;
38function setPresetProperty(tree, options) {
39 (0, devkit_1.updateJson)(tree, (0, path_1.join)(options.directory, 'nx.json'), (json) => {
40 if (options.preset === presets_1.Preset.Core || options.preset === presets_1.Preset.NPM) {
41 addPropertyWithStableKeys(json, 'extends', 'nx/presets/npm.json');
42 delete json.implicitDependencies;
43 delete json.targetDefaults;
44 delete json.targetDependencies;
45 delete json.workspaceLayout;
46 delete json.npmScope;
47 }
48 return json;
49 });
50}
51function createAppsAndLibsFolders(tree, options) {
52 if (options.preset === presets_1.Preset.Core ||
53 options.preset === presets_1.Preset.TS ||
54 options.preset === presets_1.Preset.NPM) {
55 tree.write((0, path_1.join)(options.directory, 'packages/.gitkeep'), '');
56 }
57 else if (options.preset === presets_1.Preset.AngularStandalone ||
58 options.preset === presets_1.Preset.ReactStandalone ||
59 options.preset === presets_1.Preset.NodeServer) {
60 // don't generate any folders
61 }
62 else {
63 tree.write((0, path_1.join)(options.directory, 'apps/.gitkeep'), '');
64 tree.write((0, path_1.join)(options.directory, 'libs/.gitkeep'), '');
65 }
66}
67function createNxJson(tree, { directory, npmScope, packageManager, defaultBase, preset }) {
68 const nxJson = {
69 $schema: './node_modules/nx/schemas/nx-schema.json',
70 npmScope: npmScope,
71 affected: {
72 defaultBase,
73 },
74 tasksRunnerOptions: {
75 default: {
76 runner: 'nx/tasks-runners/default',
77 options: {
78 cacheableOperations: ['build', 'lint', 'test', 'e2e'],
79 },
80 },
81 },
82 };
83 nxJson.targetDefaults = {
84 build: {
85 dependsOn: ['^build'],
86 },
87 };
88 if (defaultBase === 'main') {
89 delete nxJson.affected;
90 }
91 if (preset !== presets_1.Preset.Core &&
92 preset !== presets_1.Preset.NPM &&
93 preset !== presets_1.Preset.Empty) {
94 nxJson.namedInputs = {
95 default: ['{projectRoot}/**/*', 'sharedGlobals'],
96 production: ['default'],
97 sharedGlobals: [],
98 };
99 nxJson.targetDefaults.build.inputs = ['production', '^production'];
100 }
101 (0, devkit_1.writeJson)(tree, (0, path_1.join)(directory, 'nx.json'), nxJson);
102}
103function createFiles(tree, options) {
104 const formattedNames = (0, devkit_1.names)(options.name);
105 const filesDirName = options.preset === presets_1.Preset.AngularStandalone ||
106 options.preset === presets_1.Preset.ReactStandalone ||
107 options.preset === presets_1.Preset.NodeServer
108 ? './files-root-app'
109 : options.preset === presets_1.Preset.NPM || options.preset === presets_1.Preset.Core
110 ? './files-package-based-repo'
111 : './files-integrated-repo';
112 (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, filesDirName), options.directory, Object.assign(Object.assign({ formattedNames, dot: '.', tmpl: '', cliCommand: 'nx', nxCli: false, prettierVersion: versions_1.prettierVersion }, options), { nxVersion: versions_1.nxVersion, packageManager: options.packageManager }));
113}
114function createReadme(tree, { name, appName, directory }) {
115 const formattedNames = (0, devkit_1.names)(name);
116 (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, './files-readme'), directory, {
117 formattedNames,
118 appName,
119 name,
120 });
121}
122function createPrettierrc(tree, options) {
123 (0, devkit_1.writeJson)(tree, (0, path_1.join)(options.directory, '.prettierrc'), exports.DEFAULT_NRWL_PRETTIER_CONFIG);
124}
125// ensure that pnpm install add all the missing peer deps
126function createNpmrc(tree, options) {
127 tree.write((0, path_1.join)(options.directory, '.npmrc'), 'strict-peer-dependencies=false\nauto-install-peers=true\n');
128}
129// ensure that yarn (berry) install uses classic node linker
130function createYarnrcYml(tree, options) {
131 tree.write((0, path_1.join)(options.directory, '.yarnrc.yml'), 'nodeLinker: node-modules\n');
132}
133function addNpmScripts(tree, options) {
134 if (options.preset === presets_1.Preset.AngularStandalone ||
135 options.preset === presets_1.Preset.ReactStandalone ||
136 options.preset === presets_1.Preset.NodeServer) {
137 (0, devkit_1.updateJson)(tree, (0, path_1.join)(options.directory, 'package.json'), (json) => {
138 Object.assign(json.scripts, {
139 start: 'nx serve',
140 build: 'nx build',
141 test: 'nx test',
142 });
143 return json;
144 });
145 }
146}
147function addPropertyWithStableKeys(obj, key, value) {
148 const copy = Object.assign({}, obj);
149 Object.keys(obj).forEach((k) => {
150 delete obj[k];
151 });
152 obj[key] = value;
153 Object.keys(copy).forEach((k) => {
154 obj[k] = copy[k];
155 });
156}
157function normalizeOptions(options) {
158 let defaultBase = options.defaultBase || (0, default_base_1.deduceDefaultBase)();
159 return Object.assign(Object.assign({ npmScope: options.name }, options), { defaultBase });
160}
161function setUpWorkspacesInPackageJson(tree, options) {
162 if (options.preset === presets_1.Preset.NPM || options.preset === presets_1.Preset.Core) {
163 if (options.packageManager === 'pnpm') {
164 tree.write((0, path_1.join)(options.directory, 'pnpm-workspace.yaml'), `packages:
165 - 'packages/*'
166`);
167 }
168 else {
169 (0, devkit_1.updateJson)(tree, (0, path_1.join)(options.directory, 'package.json'), (json) => {
170 json.workspaces = ['packages/*'];
171 return json;
172 });
173 }
174 }
175}
176//# sourceMappingURL=generate-workspace-files.js.map
\No newline at end of file