UNPKG

@webpack-cli/package-utils

Version:

A module to help managing packages and modules inside webpack CLI

62 lines 1.96 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const execa_1 = __importStar(require("execa")); const packageUtils_1 = require("./packageUtils"); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); /** * * Spawns a new process using the respective package manager * * @param {String} pkg - The dependency to be installed * @param {Boolean} isNew - indicates if it needs to be updated or installed * @returns {Function} spawn - Installs the package */ function spawnWithArg(pkg, isNew) { const packageManager = packageUtils_1.getPackageManager(); let options = []; if (packageManager === 'npm') { options = [isNew ? 'install' : 'update', '-g', pkg]; } else { options = ['global', isNew ? 'add' : 'upgrade', pkg]; } return execa_1.sync(packageManager, options, { stdio: 'inherit', }); } /** * * Spawns a new process * */ function spawnChild(pkg) { const rootPath = packageUtils_1.getPathToGlobalPackages(); const pkgPath = path_1.default.resolve(rootPath, pkg); const isNew = !fs_1.default.existsSync(pkgPath); return spawnWithArg(pkg, isNew); } exports.spawnChild = spawnChild; async function runCommand(command, args = []) { try { await execa_1.default(command, args, { stdio: 'inherit', shell: true, }); } catch (e) { throw new Error(e); } } exports.runCommand = runCommand; //# sourceMappingURL=processUtils.js.map