123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- "use strict";
- const fs = require("fs"),
- spawn = require("cross-spawn"),
- path = require("path"),
- log = require("../shared/logging");
- function findPackageJson(startDir) {
- let dir = path.resolve(startDir || process.cwd());
- do {
- const pkgFile = path.join(dir, "package.json");
- if (!fs.existsSync(pkgFile) || !fs.statSync(pkgFile).isFile()) {
- dir = path.join(dir, "..");
- continue;
- }
- return pkgFile;
- } while (dir !== path.resolve(dir, ".."));
- return null;
- }
- function installSyncSaveDev(packages) {
- const packageList = Array.isArray(packages) ? packages : [packages];
- const npmProcess = spawn.sync("npm", ["i", "--save-dev"].concat(packageList), { stdio: "inherit" });
- const error = npmProcess.error;
- if (error && error.code === "ENOENT") {
- const pluralS = packageList.length > 1 ? "s" : "";
- log.error(`Could not execute npm. Please install the following package${pluralS} with a package manager of your choice: ${packageList.join(", ")}`);
- }
- }
- function fetchPeerDependencies(packageName) {
- const npmProcess = spawn.sync(
- "npm",
- ["show", "--json", packageName, "peerDependencies"],
- { encoding: "utf8" }
- );
- const error = npmProcess.error;
- if (error && error.code === "ENOENT") {
- return null;
- }
- const fetchedText = npmProcess.stdout.trim();
- return JSON.parse(fetchedText || "{}");
- }
- function check(packages, opt) {
- const deps = new Set();
- const pkgJson = (opt) ? findPackageJson(opt.startDir) : findPackageJson();
- let fileJson;
- if (!pkgJson) {
- throw new Error("Could not find a package.json file. Run 'npm init' to create one.");
- }
- try {
- fileJson = JSON.parse(fs.readFileSync(pkgJson, "utf8"));
- } catch (e) {
- const error = new Error(e);
- error.messageTemplate = "failed-to-read-json";
- error.messageData = {
- path: pkgJson,
- message: e.message
- };
- throw error;
- }
- ["dependencies", "devDependencies"].forEach(key => {
- if (opt[key] && typeof fileJson[key] === "object") {
- Object.keys(fileJson[key]).forEach(dep => deps.add(dep));
- }
- });
- return packages.reduce((status, pkg) => {
- status[pkg] = deps.has(pkg);
- return status;
- }, {});
- }
- function checkDeps(packages, rootDir) {
- return check(packages, { dependencies: true, startDir: rootDir });
- }
- function checkDevDeps(packages) {
- return check(packages, { devDependencies: true });
- }
- function checkPackageJson(startDir) {
- return !!findPackageJson(startDir);
- }
- module.exports = {
- installSyncSaveDev,
- fetchPeerDependencies,
- findPackageJson,
- checkDeps,
- checkDevDeps,
- checkPackageJson
- };
|