UNPKG

3.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.newGenerator = void 0;
4const tslib_1 = require("tslib");
5const devkit_1 = require("@nrwl/devkit");
6const path_1 = require("path");
7const presets_1 = require("../utils/presets");
8const generate_workspace_files_1 = require("./generate-workspace-files");
9const generate_preset_1 = require("./generate-preset");
10function newGenerator(host, options) {
11 return tslib_1.__awaiter(this, void 0, void 0, function* () {
12 options = normalizeOptions(options);
13 validateOptions(options, host);
14 yield (0, generate_workspace_files_1.generateWorkspaceFiles)(host, Object.assign(Object.assign({}, options), { nxCloud: undefined }));
15 (0, generate_preset_1.addPresetDependencies)(host, options);
16 const isCustomPreset = !Object.values(presets_1.Preset).includes(options.preset);
17 addCloudDependencies(host, options);
18 yield (0, devkit_1.formatFiles)(host);
19 return () => tslib_1.__awaiter(this, void 0, void 0, function* () {
20 (0, devkit_1.installPackagesTask)(host, false, options.directory, options.packageManager);
21 // TODO: move all of these into create-nx-workspace
22 if (options.preset !== presets_1.Preset.NPM &&
23 options.preset !== presets_1.Preset.Core &&
24 !isCustomPreset) {
25 yield (0, generate_preset_1.generatePreset)(host, options);
26 }
27 });
28 });
29}
30exports.newGenerator = newGenerator;
31exports.default = newGenerator;
32function validateOptions(options, host) {
33 if (options.skipInstall &&
34 options.preset !== presets_1.Preset.Apps &&
35 options.preset !== presets_1.Preset.Core &&
36 options.preset !== presets_1.Preset.TS &&
37 options.preset !== presets_1.Preset.Empty &&
38 options.preset !== presets_1.Preset.NPM) {
39 throw new Error(`Cannot select a preset when skipInstall is set to true.`);
40 }
41 if (options.skipInstall && options.nxCloud) {
42 throw new Error(`Cannot select nxCloud when skipInstall is set to true.`);
43 }
44 if (options.preset === presets_1.Preset.NodeServer && !options.framework) {
45 throw new Error(`Cannot generate ${options.preset} without selecting a framework`);
46 }
47 if (host.exists(options.name) &&
48 !host.isFile(options.name) &&
49 host.children(options.name).length > 0) {
50 throw new Error(`${(0, path_1.join)(host.root, options.name)} is not an empty directory.`);
51 }
52}
53function normalizeOptions(options) {
54 options.name = (0, devkit_1.names)(options.name).fileName;
55 if (!options.directory) {
56 options.directory = options.name;
57 }
58 // If the preset already contains a version in the name
59 // -- my-package@2.0.1
60 // -- @scope/package@version
61 const match = options.preset.match(/^(?<package>(@.+\/)?[^@]+)(@(?<version>\d+\.\d+\.\d+))?$/);
62 if (match) {
63 options.preset = match.groups.package;
64 options.presetVersion = match.groups.version;
65 }
66 return options;
67}
68function addCloudDependencies(host, options) {
69 if (options.nxCloud) {
70 return (0, devkit_1.addDependenciesToPackageJson)(host, {}, { '@nrwl/nx-cloud': 'latest' }, (0, path_1.join)(options.directory, 'package.json'));
71 }
72}
73//# sourceMappingURL=new.js.map
\No newline at end of file