@shower/cli
Version:
Command line interface for Shower
28 lines (23 loc) • 911 B
JavaScript
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
/**
* Install node modules and save to dependencies in package.json
* @param {string} cwd cwd dir
* @param {string|string[]} packages Node module or modules to install
* @param {string} mode Type package installing
* @returns {void}
*/
export async function installDependencies (cwd, packages, mode = 'save') {
packages = Array.isArray(packages) ? packages : [packages];
try {
return await execFileAsync('npm', ['i', '--package-lock', `--${mode}`].concat(packages), { cwd });
} catch (error) {
if (error.code === 'ENOENT') {
const pluralS = packages.length > 1 ? 's' : '';
console.error(`Could not execute npm. Please install the following package${pluralS} with a package manager of your choice: ${packages.join(', ')}`);
} else {
throw error;
}
}
}