UNPKG

@hypermod/cli

Version:

To download and run codemods, we provide a CLI tool called @hypermod/cli.

66 lines (65 loc) 3.02 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const install_pkg_1 = require("@antfu/install-pkg"); const ModuleLoader = (config) => { const getInfo = (packageName) => { const entryPath = require.resolve(packageName); const location = entryPath.split(packageName)[0] + packageName; const pkgJsonRaw = fs_extra_1.default.readFileSync(path_1.default.join(location, 'package.json'), 'utf8'); const pkgJson = JSON.parse(pkgJsonRaw); return { location, entryPath, pkgJson, }; }; const install = (packageName) => __awaiter(void 0, void 0, void 0, function* () { var _a; yield (0, install_pkg_1.installPackage)(packageName, { cwd: __dirname, packageManager: 'npm', additionalArgs: [ '--force', // --registry=https://your-custom-registry-url/ --//your-custom-registry-url/:_authToken=YOUR_AUTH_TOKEN config.npmRegistryUrl ? `--registry=${config.npmRegistryUrl}` : '', config.authToken ? `--${config.npmRegistryUrl}/:_authToken=${config.authToken}` : '', ], }); const { pkgJson } = getInfo(packageName); // Install whitelisted devDependencies if ((_a = pkgJson === null || pkgJson === void 0 ? void 0 : pkgJson.hypermod) === null || _a === void 0 ? void 0 : _a.dependencies) { yield Promise.all(pkgJson.hypermod.dependencies.map((dep) => { const version = pkgJson.devDependencies[dep]; if (!version) return; return (0, install_pkg_1.installPackage)(`${dep}@${version}`, { cwd: __dirname, packageManager: 'npm', additionalArgs: ['--force'], }); })); } }); return { install, getInfo, require: (packageName) => require(packageName), }; }; exports.default = ModuleLoader;