UNPKG

@tarojs/plugin-generator

Version:
122 lines 4.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updatePkgJson = exports.getCompilerType = exports.installDeps = exports.removeIncompatiblePkg = exports.mergePkgJson = exports.readPkgJson = exports.execCommand = void 0; /* eslint-disable no-console */ const node_child_process_1 = require("node:child_process"); const path = __importStar(require("node:path")); function execCommand(params) { const { command, cwd, args } = params; return new Promise((resolve, reject) => { const child = (0, node_child_process_1.spawn)(command, args, { cwd, stdio: 'pipe' }); let stdout = ''; let stderr = ''; child.stdout.on('data', (chunk) => { process.stdout.write(chunk); stdout += chunk.toString(); }); child.stderr.on('data', (chunk) => { process.stderr.write(chunk); stderr += chunk.toString(); }); child.on('close', (code) => { if (code === 0) { resolve(stdout); } else { const err = new Error(`Command failed with exit code ${code}\n${stderr}`); reject(err); } }); child.on('error', (err) => { reject(err); }); }); } exports.execCommand = execCommand; function readPkgJson(ctx) { const { appPath } = ctx.paths; const pkgPath = path.join(appPath, 'package.json'); const pkgJson = require(pkgPath); return pkgJson; } exports.readPkgJson = readPkgJson; function mergePkgJson(pkgJson, patch) { for (const propName in patch) { const origin = pkgJson[propName]; pkgJson[propName] = Object.assign(origin ?? {}, patch[propName]); } } exports.mergePkgJson = mergePkgJson; function removeIncompatiblePkg(pkgJson, pkgs) { for (const pkg of pkgs) { if (pkgJson.dependencies && pkg in pkgJson.dependencies) { Reflect.deleteProperty(pkgJson.dependencies, pkg); } if (pkgJson.devDependencies && pkg in pkgJson.devDependencies) { Reflect.deleteProperty(pkgJson.devDependencies, pkg); } if (pkgJson.peerDependencies && pkg in pkgJson.peerDependencies) { Reflect.deleteProperty(pkgJson.peerDependencies, pkg); } } } exports.removeIncompatiblePkg = removeIncompatiblePkg; async function installDeps(ctx) { const { appPath } = ctx.paths; const { fs } = ctx.helper; const pnpmLockPath = `${appPath}/pnpm-lock.yaml`; const yarnLockPath = `${appPath}/yarn.lock`; let npm = 'npm'; if (await fs.pathExists(pnpmLockPath)) { npm = 'pnpm'; } else if (await fs.pathExists(yarnLockPath)) { npm = 'yarn'; } return execCommand({ command: npm, args: ['install'], cwd: appPath }); } exports.installDeps = installDeps; function getCompilerType(compilerConfig) { return typeof compilerConfig === 'string' ? compilerConfig : compilerConfig?.type; } exports.getCompilerType = getCompilerType; async function updatePkgJson(ctx, patch) { const pkgJson = readPkgJson(ctx); mergePkgJson(pkgJson, patch); await ctx.helper.fs.outputJson(path.join(ctx.paths.appPath, 'package.json'), pkgJson, { encoding: 'utf-8', spaces: 2, }); try { await installDeps(ctx); console.log(ctx.helper.chalk.green('\n✅ 安装依赖完成')); } catch { console.log(ctx.helper.chalk.red('\n❌ 安装依赖失败,请手动安装')); } } exports.updatePkgJson = updatePkgJson; //# sourceMappingURL=index.js.map