UNPKG

@luban-cli/cli-lib-service

Version:
87 lines 3.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.injectRequire = exports.replaceLib = exports.getProjectTsConfigJson = exports.getProjectPackageJson = exports.getProjectPath = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const cwd = process.cwd(); function getProjectPath(...filePath) { return path_1.default.join(cwd, ...filePath); } exports.getProjectPath = getProjectPath; function getProjectPackageJson() { const filePath = getProjectPath("package.json"); if (filePath) { try { return JSON.parse(fs_1.default.readFileSync(filePath, "utf-8")); } catch (err) { console.error(err); } } return { name: "", version: "" }; } exports.getProjectPackageJson = getProjectPackageJson; function getProjectTsConfigJson() { const filePath = getProjectPath("tsconfig.json"); if (filePath) { try { return JSON.parse(fs_1.default.readFileSync(filePath, "utf-8")); } catch (err) { console.error(err); } } return { compilerOptions: {} }; } exports.getProjectTsConfigJson = getProjectTsConfigJson; function replaceLib() { return { visitor: { ImportDeclaration: (_path) => { if (_path.node.source && /\/lib\//.test(_path.node.source.value)) { const esModule = _path.node.source.value.replace("/lib/", "/es/"); const esPath = path_1.default.dirname(getProjectPath("node_modules", esModule)); if (fs_1.default.existsSync(esPath)) { _path.node.source.value = esModule; } } }, ExportNamedDeclaration: (_path) => { if (_path.node.source && /\/lib\//.test(_path.node.source.value)) { const esModule = _path.node.source.value.replace("/lib/", "/es/"); const esPath = path_1.default.dirname(getProjectPath("node_modules", esModule)); if (fs_1.default.existsSync(esPath)) { _path.node.source.value = esModule; } } }, }, }; } exports.replaceLib = replaceLib; let injected = false; function injectRequire() { if (injected) return; const Module = require("module"); const oriRequire = Module.prototype.require; Module.prototype.require = function (...args) { const moduleName = args[0]; try { return oriRequire.apply(this, args); } catch (err) { const newArgs = [...args]; if (Array.isArray(moduleName) && moduleName[0] !== "/") { newArgs[0] = getProjectPath("node_modules", moduleName[0]); } return oriRequire.apply(this, newArgs); } }; injected = true; } exports.injectRequire = injectRequire; //# sourceMappingURL=share.js.map