install-peerdeps
Version:
CLI to automatically install peerDeps
182 lines (165 loc) • 8.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("@babel/polyfill");
var _commander = require("commander");
var _picocolors = _interopRequireDefault(require("picocolors"));
var _promptly = require("promptly");
var _package = _interopRequireDefault(require("../package.json"));
var C = _interopRequireWildcard(require("./constants"));
var _hasYarn = _interopRequireDefault(require("./has-yarn"));
var _helpers = require("./helpers");
var _installPeerdeps = _interopRequireDefault(require("./install-peerdeps"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
// Create program object
const program = new _commander.Command("install-peerdeps");
// Get relevant package information
const {
name,
version
} = _package.default;
/**
* Error message that is printed when the program can't
* parse the package string.
*/
function printPackageFormatError() {
console.error(`${C.errorText} Please specify the package to install with peerDeps in the form of \`package\` or \`package@n.n.n\``);
console.error(`${C.errorText} At this time you must provide the full semver version of the package.`);
console.error(`${C.errorText} Alternatively, omit it to automatically install the latest version of the package.`);
}
// Create program
program.version(version).description("Installs the specified package along with correct peerDeps.").option("-D", "Install the package as a devDependency (alias for `-d`)").option("-d, --dev", "Install the package as a devDependency").option("-g, --global", "Install the package globally").option("-o, --only-peers", "Install only peerDependencies of the package").option("-S, --silent", "If using npm, don't save in package.json").option("-Y, --yarn", "Install with Yarn").option("-P, --pnpm", "Install with pnpm").option("-n, --no-registry", "Use local node_modules instead of a remote registry to get the list of peerDependencies").option("--dry-run", "Do not install packages, but show the install command that will be run").option('-x, --extra-args "<extra_args>"', "Extra arguments to pass through to the underlying package manager").usage("<package>[@<version>], default version is 'latest'").parse(process.argv);
// Print program name and version (like what Yarn does)
console.log(_picocolors.default.bold(`${name} v${version}`));
// Make sure we're installing at least one package
if (program.args.length === 0) {
console.error(`${C.errorText} Please specify a package to install with peerDeps.`);
// An exit code of "9" indicates an invalid argument
// See https://nodejs.org/api/process.html#process_exit_codes
process.exitCode = 9;
program.help();
}
// Make sure we're installing no more than one package
if (program.args.length > 1) {
console.error(`${C.errorText} Too many arguments. Please specify ONE package at a time to install with peerDeps. Alternatively, pass extra arguments with --extra-args "<extra_args>".`);
// An exit code of "9" indicates an invalid argument
// See https://nodejs.org/api/process.html#process_exit_codes
process.exit(9);
}
// The first argument after the options is the name of the package
const packageString = program.args[0];
const {
packageName,
packageVersion
} = (0, _helpers.parsePackageString)(packageString);
// If we can't get a package name out,
// print the format error
if (!packageName) {
printPackageFormatError();
process.exit(9);
}
/** @type {C.npm | C.yarn | C.pnpm} */
let packageManager = C.npm; // Default package manager is npm
if (program.yarn && program.pnpm) {
console.error(`${C.errorText} Option --yarn and --pnpm cannot be used concurrently.`);
process.exit(9);
}
if (program.yarn) {
packageManager = C.yarn;
}
if (program.pnpm) {
packageManager = C.pnpm;
}
// Yarn does not allow silent install of dependencies
if (program.yarn && program.silent) {
console.error(`${C.errorText} Option --silent cannot be used with --yarn.`);
process.exit(9);
}
const devMode = program.dev || program.D;
// Dev option can't be used with silent,
// since --dev means it should be saved
// as a devDependency
if (devMode && program.silent) {
console.error(`${C.errorText} Option --silent cannot be used with --dev.`);
process.exit(9);
}
// Dev option can't be used with global,
// since --dev means it should be saved
// as a devDependency (locally)
if (devMode && program.silent) {
console.error(`${C.errorText} Option --dev cannot be used with --global.`);
process.exit(9);
}
// Define options object to pass to
// the installPeerDeps function
const options = {
packageName,
// If packageVersion is undefined, default to "latest"
version: packageVersion || "latest",
noRegistry: !program.registry,
dev: devMode,
global: program.global,
onlyPeers: program.onlyPeers,
silent: program.silent,
packageManager,
dryRun: program.dryRun,
auth: program.auth,
// Args after -- will be passed through
extraArgs: program.extraArgs || ""
};
// Disabled this rule so we can hoist the callback
/* eslint-disable no-use-before-define */
// Check if the user has Yarn but didn't specify the Yarn option
// However, don't show prompt if user wants to install silently
if ((0, _hasYarn.default)() && packageManager !== C.yarn && !program.silent) {
// If they do, ask if they want to use Yarn
(0, _promptly.confirm)("It seems as if you are using Yarn. Would you like to use Yarn for the installation? (y/n)").then(value => {
// Value is true or false; if true, they want to use Yarn
if (value) {
packageManager = C.yarn;
}
// Now install, but with the new packageManager
(0, _installPeerdeps.default)(_objectSpread(_objectSpread({}, options), {}, {
packageManager
}), installCb);
}).catch(err => {
if (err) {
console.error(`${C.errorText} ${err.message}`);
process.exit(1);
}
});
} else {
// If they don't have Yarn or they've already
// opted to use Yarn, go ahead and install
(0, _installPeerdeps.default)(options, installCb);
}
/**
* Callback which is called after the installation
* process finishes
* @callback
* @param {Error} [err] - the error, if any, that occurred during installation
*/
function installCb(err) {
if (err) {
console.error(`${C.errorText} ${err.message}`);
process.exit(1);
}
let successMessage = `${C.successText} ${packageName} and its peerDeps were installed successfully.`;
if (program.onlyPeers) {
successMessage = `${C.successText} The peerDeps of ${packageName} were installed successfully.`;
}
console.log(successMessage);
process.exit(0);
}
/* eslint-enable */
var _default = exports.default = program;
;