UNPKG

@webpack-cli/package-utils

Version:

A module to help managing packages and modules inside webpack CLI

96 lines 3.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const execa_1 = require("execa"); const cross_spawn_1 = __importDefault(require("cross-spawn")); const chalk = require("chalk"); const enquirer_1 = require("enquirer"); const processUtils_1 = require("./processUtils"); function getPackageManager() { const hasLocalYarn = fs_1.default.existsSync(path_1.default.resolve(process.cwd(), 'yarn.lock')); try { if (hasLocalYarn) { return 'yarn'; } else if (execa_1.sync('yarn', [' --version'], { stdio: 'ignore' }).stderr) { return 'yarn'; } else { return 'npm'; } } catch (e) { return 'npm'; } } exports.getPackageManager = getPackageManager; /** * * Returns the path to globally installed * npm packages, depending on the available * package manager determined by `getPackageManager` * * @returns {String} path - Path to global node_modules folder */ function getPathToGlobalPackages() { const manager = getPackageManager(); if (manager === 'yarn') { try { const yarnDir = cross_spawn_1.default .sync('yarn', ['global', 'dir']) .stdout.toString() .trim(); return path_1.default.join(yarnDir, 'node_modules'); } catch (e) { // Default to the global npm path below } } return require('global-modules'); } exports.getPathToGlobalPackages = getPathToGlobalPackages; function packageExists(packageName) { try { require(packageName); return true; } catch (err) { return false; } } exports.packageExists = packageExists; /** * * @param packageName * @param preMessage Message to show before the question */ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type async function promptInstallation(packageName, preMessage) { const packageManager = getPackageManager(); const options = [packageManager === 'yarn' ? 'add' : 'install', '-D', packageName]; const commandToBeRun = `${packageManager} ${options.join(' ')}`; if (preMessage) { preMessage(); } const question = `Would you like to install ${packageName}? (That will run ${chalk.green(commandToBeRun)})`; const { installConfirm } = await enquirer_1.prompt([ { type: 'confirm', name: 'installConfirm', message: question, initial: 'Y', }, ]); if (installConfirm) { await processUtils_1.runCommand(commandToBeRun); return packageExists(packageName); } // eslint-disable-next-line require-atomic-updates process.exitCode = -1; } exports.promptInstallation = promptInstallation; //# sourceMappingURL=packageUtils.js.map