1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
---|
29 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
---|
30 | };
|
---|
31 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
32 | exports.UnknownPackageManagerException = void 0;
|
---|
33 | const core_1 = require("@angular-devkit/core");
|
---|
34 | const child_process_1 = require("child_process");
|
---|
35 | const ora_1 = __importDefault(require("ora"));
|
---|
36 | const path = __importStar(require("path"));
|
---|
37 | const rxjs_1 = require("rxjs");
|
---|
38 | const src_1 = require("../../src");
|
---|
39 | const packageManagers = {
|
---|
40 | 'npm': {
|
---|
41 | quietArgument: '--quiet',
|
---|
42 | commands: {
|
---|
43 | installAll: 'install',
|
---|
44 | installPackage: 'install',
|
---|
45 | },
|
---|
46 | },
|
---|
47 | 'cnpm': {
|
---|
48 | commands: {
|
---|
49 | installAll: 'install',
|
---|
50 | installPackage: 'install',
|
---|
51 | },
|
---|
52 | },
|
---|
53 | 'yarn': {
|
---|
54 | quietArgument: '--silent',
|
---|
55 | commands: {
|
---|
56 | installPackage: 'add',
|
---|
57 | },
|
---|
58 | },
|
---|
59 | 'pnpm': {
|
---|
60 | quietArgument: '--silent',
|
---|
61 | commands: {
|
---|
62 | installAll: 'install',
|
---|
63 | installPackage: 'install',
|
---|
64 | },
|
---|
65 | },
|
---|
66 | };
|
---|
67 | class UnknownPackageManagerException extends core_1.BaseException {
|
---|
68 | constructor(name) {
|
---|
69 | super(`Unknown package manager "${name}".`);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | exports.UnknownPackageManagerException = UnknownPackageManagerException;
|
---|
73 | function default_1(factoryOptions = {}) {
|
---|
74 | const packageManagerName = factoryOptions.packageManager || 'npm';
|
---|
75 | const packageManagerProfile = packageManagers[packageManagerName];
|
---|
76 | if (!packageManagerProfile) {
|
---|
77 | throw new UnknownPackageManagerException(packageManagerName);
|
---|
78 | }
|
---|
79 | const rootDirectory = factoryOptions.rootDirectory || process.cwd();
|
---|
80 | return (options = { command: 'install' }) => {
|
---|
81 | let taskPackageManagerProfile = packageManagerProfile;
|
---|
82 | let taskPackageManagerName = packageManagerName;
|
---|
83 | if (factoryOptions.allowPackageManagerOverride && options.packageManager) {
|
---|
84 | taskPackageManagerProfile = packageManagers[options.packageManager];
|
---|
85 | if (!taskPackageManagerProfile) {
|
---|
86 | throw new UnknownPackageManagerException(options.packageManager);
|
---|
87 | }
|
---|
88 | taskPackageManagerName = options.packageManager;
|
---|
89 | }
|
---|
90 | const bufferedOutput = [];
|
---|
91 | const spawnOptions = {
|
---|
92 | stdio: options.hideOutput ? 'pipe' : 'inherit',
|
---|
93 | shell: true,
|
---|
94 | cwd: path.join(rootDirectory, options.workingDirectory || ''),
|
---|
95 | };
|
---|
96 | const args = [];
|
---|
97 | if (options.packageName) {
|
---|
98 | if (options.command === 'install') {
|
---|
99 | args.push(taskPackageManagerProfile.commands.installPackage);
|
---|
100 | }
|
---|
101 | args.push(options.packageName);
|
---|
102 | }
|
---|
103 | else if (options.command === 'install' && taskPackageManagerProfile.commands.installAll) {
|
---|
104 | args.push(taskPackageManagerProfile.commands.installAll);
|
---|
105 | }
|
---|
106 | if (options.quiet && taskPackageManagerProfile.quietArgument) {
|
---|
107 | args.push(taskPackageManagerProfile.quietArgument);
|
---|
108 | }
|
---|
109 | if (factoryOptions.registry) {
|
---|
110 | args.push(`--registry="${factoryOptions.registry}"`);
|
---|
111 | }
|
---|
112 | if (factoryOptions.force) {
|
---|
113 | args.push('--force');
|
---|
114 | }
|
---|
115 | return new rxjs_1.Observable((obs) => {
|
---|
116 | var _a, _b;
|
---|
117 | const spinner = ora_1.default({
|
---|
118 | text: `Installing packages (${taskPackageManagerName})...`,
|
---|
119 | // Workaround for https://github.com/sindresorhus/ora/issues/136.
|
---|
120 | discardStdin: process.platform != 'win32',
|
---|
121 | }).start();
|
---|
122 | const childProcess = child_process_1.spawn(taskPackageManagerName, args, spawnOptions).on('close', (code) => {
|
---|
123 | if (code === 0) {
|
---|
124 | spinner.succeed('Packages installed successfully.');
|
---|
125 | spinner.stop();
|
---|
126 | obs.next();
|
---|
127 | obs.complete();
|
---|
128 | }
|
---|
129 | else {
|
---|
130 | if (options.hideOutput) {
|
---|
131 | bufferedOutput.forEach(({ stream, data }) => stream.write(data));
|
---|
132 | }
|
---|
133 | spinner.fail('Package install failed, see above.');
|
---|
134 | obs.error(new src_1.UnsuccessfulWorkflowExecution());
|
---|
135 | }
|
---|
136 | });
|
---|
137 | if (options.hideOutput) {
|
---|
138 | (_a = childProcess.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (data) => bufferedOutput.push({ stream: process.stdout, data: data }));
|
---|
139 | (_b = childProcess.stderr) === null || _b === void 0 ? void 0 : _b.on('data', (data) => bufferedOutput.push({ stream: process.stderr, data: data }));
|
---|
140 | }
|
---|
141 | });
|
---|
142 | };
|
---|
143 | }
|
---|
144 | exports.default = default_1;
|
---|