123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- "use strict";
- /**
- * @license
- * Copyright Google LLC All Rights Reserved.
- *
- * Use of this source code is governed by an MIT-style license that can be
- * found in the LICENSE file at https://angular.dev/license
- */
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- var desc = Object.getOwnPropertyDescriptor(m, k);
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
- desc = { enumerable: true, get: function() { return m[k]; } };
- }
- Object.defineProperty(o, k2, desc);
- }) : (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- o[k2] = m[k];
- }));
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
- Object.defineProperty(o, "default", { enumerable: true, value: v });
- }) : function(o, v) {
- o["default"] = v;
- });
- var __importStar = (this && this.__importStar) || function (mod) {
- if (mod && mod.__esModule) return mod;
- var result = {};
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
- __setModuleDefault(result, mod);
- return result;
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.UnknownPackageManagerException = void 0;
- exports.default = default_1;
- const core_1 = require("@angular-devkit/core");
- const child_process_1 = require("child_process");
- const ora_1 = __importDefault(require("ora"));
- const path = __importStar(require("path"));
- const rxjs_1 = require("rxjs");
- const src_1 = require("../../src");
- const packageManagers = {
- 'npm': {
- commands: {
- installAll: 'install',
- installPackage: 'install',
- },
- },
- 'cnpm': {
- commands: {
- installAll: 'install',
- installPackage: 'install',
- },
- },
- 'yarn': {
- commands: {
- installAll: 'install',
- installPackage: 'add',
- },
- },
- 'bun': {
- commands: {
- installAll: 'install',
- installPackage: 'add',
- },
- },
- 'pnpm': {
- commands: {
- installAll: 'install',
- installPackage: 'install',
- },
- },
- };
- class UnknownPackageManagerException extends core_1.BaseException {
- constructor(name) {
- super(`Unknown package manager "${name}".`);
- }
- }
- exports.UnknownPackageManagerException = UnknownPackageManagerException;
- function default_1(factoryOptions = {}) {
- const packageManagerName = factoryOptions.packageManager || 'npm';
- const packageManagerProfile = packageManagers[packageManagerName];
- if (!packageManagerProfile) {
- throw new UnknownPackageManagerException(packageManagerName);
- }
- const rootDirectory = factoryOptions.rootDirectory || process.cwd();
- return (options = { command: 'install' }) => {
- let taskPackageManagerProfile = packageManagerProfile;
- let taskPackageManagerName = packageManagerName;
- if (factoryOptions.allowPackageManagerOverride && options.packageManager) {
- taskPackageManagerProfile = packageManagers[options.packageManager];
- if (!taskPackageManagerProfile) {
- throw new UnknownPackageManagerException(options.packageManager);
- }
- taskPackageManagerName = options.packageManager;
- }
- const bufferedOutput = [];
- const spawnOptions = {
- shell: true,
- cwd: path.join(rootDirectory, options.workingDirectory || ''),
- };
- if (options.hideOutput) {
- spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'pipe'] : 'pipe';
- }
- else {
- spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'inherit'] : 'inherit';
- }
- const args = [];
- if (options.packageName) {
- if (options.command === 'install') {
- args.push(taskPackageManagerProfile.commands.installPackage);
- }
- args.push(options.packageName);
- }
- else if (options.command === 'install' && taskPackageManagerProfile.commands.installAll) {
- args.push(taskPackageManagerProfile.commands.installAll);
- }
- if (!options.allowScripts) {
- // Yarn requires special handling since Yarn 2+ no longer has the `--ignore-scripts` flag
- if (taskPackageManagerName === 'yarn') {
- spawnOptions.env = {
- ...process.env,
- // Supported with yarn 1
- 'npm_config_ignore_scripts': 'true',
- // Supported with yarn 2+
- 'YARN_ENABLE_SCRIPTS': 'false',
- };
- }
- else {
- args.push('--ignore-scripts');
- }
- }
- if (factoryOptions.registry) {
- args.push(`--registry="${factoryOptions.registry}"`);
- }
- if (factoryOptions.force) {
- args.push('--force');
- }
- return new rxjs_1.Observable((obs) => {
- const spinner = (0, ora_1.default)({
- text: `Installing packages (${taskPackageManagerName})...`,
- // Workaround for https://github.com/sindresorhus/ora/issues/136.
- discardStdin: process.platform != 'win32',
- }).start();
- const childProcess = (0, child_process_1.spawn)(taskPackageManagerName, args, spawnOptions).on('close', (code) => {
- if (code === 0) {
- spinner.succeed('Packages installed successfully.');
- spinner.stop();
- obs.next();
- obs.complete();
- }
- else {
- if (options.hideOutput) {
- bufferedOutput.forEach(({ stream, data }) => stream.write(data));
- }
- spinner.fail('Package install failed, see above.');
- obs.error(new src_1.UnsuccessfulWorkflowExecution());
- }
- });
- if (options.hideOutput) {
- childProcess.stdout?.on('data', (data) => bufferedOutput.push({ stream: process.stdout, data: data }));
- childProcess.stderr?.on('data', (data) => bufferedOutput.push({ stream: process.stderr, data: data }));
- }
- });
- };
- }
|