UNPKG

@ts-dev-tools/core

Version:
231 lines (230 loc) 11.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PackageManagerService = exports.PackageManagerType = void 0; var child_process_1 = require("child_process"); var fs_1 = require("fs"); var path_1 = require("path"); var PackageManagerType; (function (PackageManagerType) { PackageManagerType["yarn"] = "yarn"; PackageManagerType["npm"] = "npm"; })(PackageManagerType || (exports.PackageManagerType = PackageManagerType = {})); var PackageManagerService = /** @class */ (function () { function PackageManagerService() { } PackageManagerService.detectPackageManager = function (dirPath) { if ((0, fs_1.existsSync)((0, path_1.join)(dirPath, "yarn.lock"))) { return PackageManagerType.yarn; } return PackageManagerType.npm; }; PackageManagerService.addDevPackage = function (packageName, dirPath) { return __awaiter(this, void 0, void 0, function () { var packageManager, isMonorepo, args; return __generator(this, function (_a) { switch (_a.label) { case 0: packageManager = PackageManagerService.detectPackageManager(dirPath); return [4 /*yield*/, PackageManagerService.isMonorepo(dirPath)]; case 1: isMonorepo = _a.sent(); args = [packageManager]; switch (packageManager) { case PackageManagerType.yarn: args.push("add", "--dev"); if (isMonorepo) { args.push("--ignore-workspace-root-check"); } break; case PackageManagerType.npm: args.push("install", "--save-dev"); if (isMonorepo) { args.push("--no-workspaces"); } break; } args.push(packageName); return [4 /*yield*/, PackageManagerService.execCommand(args, dirPath, true)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }; PackageManagerService.isMonorepo = function (dirPath) { return __awaiter(this, void 0, void 0, function () { var packageManager, args, output; return __generator(this, function (_a) { switch (_a.label) { case 0: packageManager = PackageManagerService.detectPackageManager(dirPath); args = [packageManager]; switch (packageManager) { case PackageManagerType.yarn: args.push("workspaces", "info"); break; case PackageManagerType.npm: args.push("--workspaces", "list"); break; } args.push("> /dev/null 2>&1 && echo true || echo false;"); return [4 /*yield*/, PackageManagerService.execCommand(args, dirPath, true)]; case 1: output = _a.sent(); return [2 /*return*/, output.trim() === "true"]; } }); }); }; PackageManagerService.isPackageInstalled = function (packageName, dirPath) { return __awaiter(this, void 0, void 0, function () { var packageManager, args, output, installedPackages; var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: packageManager = PackageManagerService.detectPackageManager(dirPath); args = [ packageManager, "list", "--depth=1", "--json", "--no-progress", "--pattern=\"".concat(packageName, "\""), "--non-interactive", ]; return [4 /*yield*/, PackageManagerService.execCommand(args, dirPath, true)]; case 1: output = _c.sent(); installedPackages = JSON.parse(output); switch (packageManager) { case PackageManagerType.yarn: return [2 /*return*/, (_b = (_a = installedPackages === null || installedPackages === void 0 ? void 0 : installedPackages.data) === null || _a === void 0 ? void 0 : _a.trees) === null || _b === void 0 ? void 0 : _b.some(function (tree) { return tree.name.startsWith(packageName + "@"); })]; case PackageManagerType.npm: return [2 /*return*/, installedPackages.dependencies ? Object.prototype.hasOwnProperty.call(installedPackages.dependencies, packageName) : false]; } return [2 /*return*/]; } }); }); }; PackageManagerService.getNodeModulesPath = function (dirPath) { return __awaiter(this, void 0, void 0, function () { var packageManager, nodeModulesPath, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: packageManager = PackageManagerService.detectPackageManager(dirPath); _a = packageManager; switch (_a) { case PackageManagerType.yarn: return [3 /*break*/, 1]; case PackageManagerType.npm: return [3 /*break*/, 2]; } return [3 /*break*/, 4]; case 1: nodeModulesPath = (0, path_1.join)(dirPath, "node_modules"); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, PackageManagerService.execCommand([packageManager, "root", "--no-progress", "--non-interactive"], dirPath, true)]; case 3: nodeModulesPath = (_b.sent()).trim(); return [3 /*break*/, 4]; case 4: if (nodeModulesPath) { return [2 /*return*/, nodeModulesPath]; } throw new Error("Node modules path not found for package manager ".concat(packageManager)); } }); }); }; PackageManagerService.execCommand = function (args_1, cwd_1) { return __awaiter(this, arguments, void 0, function (args, cwd, silent) { var cmd; if (silent === void 0) { silent = false; } return __generator(this, function (_a) { if (!args.length) { throw new Error("Command args must not be empty"); } if (cwd && !(0, fs_1.existsSync)(cwd)) { throw new Error("Directory \"".concat(cwd, "\" does not exist")); } if (Array.isArray(args)) { cmd = args.shift() || ""; } else { cmd = args; args = []; } return [2 /*return*/, new Promise(function (resolve, reject) { var child = (0, child_process_1.spawn)(cmd, args, { stdio: silent ? "pipe" : "inherit", shell: true, windowsVerbatimArguments: true, cwd: cwd, }); var output = ""; var error = ""; child.on("exit", function (code) { if (code) { return reject(error); } resolve(output); }); if (child.stdout) { child.stdout.on("data", function (data) { output += "\n".concat(data); }); } if (child.stderr) { child.stderr.on("data", function (data) { error += "\n".concat(data); }); } })]; }); }); }; return PackageManagerService; }()); exports.PackageManagerService = PackageManagerService;