txw/setup.js
2026-04-03 14:32:29 +08:00

83 lines
2.2 KiB
JavaScript

#!/usr/bin/env node
/**
* 碳信网 Web 项目初始化脚本
* 用法: node setup.js
* 支持 Windows 和 Mac
*/
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const ROOT = __dirname;
const WEBS = ['txw-gxzx-web', 'txw-kxtfwzx-web', 'txw-mhzc-web', 'txw-tzzx-web', 'txw-ytzx-web', 'txw-yygl-web'];
const LOCAL_PKG = 'local-nodemodules/@cssyq/ggzc-web';
function run(cmd, opts = {}) {
console.log(` > ${cmd}`);
try {
execSync(cmd, { stdio: 'inherit', shell: true, ...opts });
} catch (e) {
console.error(` [失败] ${cmd}`);
process.exit(1);
}
}
console.log('=== 碳信网 Web 项目初始化 ===\n');
for (const web of WEBS) {
const webPath = path.join(ROOT, web);
if (!fs.existsSync(webPath)) {
console.warn(`[跳过] ${web} 目录不存在`);
continue;
}
console.log(`\n[${web}]`);
// 创建 symlink
const cssyqDir = path.join(webPath, 'node_modules/@cssyq');
const linkTarget = path.join(ROOT, LOCAL_PKG);
const linkPath = path.join(cssyqDir, 'ggzc-web');
fs.mkdirSync(cssyqDir, { recursive: true });
// 删除旧链接(可能已存在)
// 删除旧链接
if (fs.existsSync(linkPath)) {
try {
const stat = fs.lstatSync(linkPath);
if (stat.isSymbolicLink() || stat.isDirectory()) {
fs.rmSync(linkPath, { recursive: true, force: true });
} else {
fs.unlinkSync(linkPath);
}
} catch (e) {
// 忽略删除失败,继续创建
}
}
// 创建 symlink
const isWin = process.platform === 'win32';
if (isWin) {
try {
fs.symlinkSync(linkTarget, linkPath, 'junction');
} catch (e) {
// junction 失败则用目录拷贝替代
console.warn(` [警告] junction 失败,将复制替代链接`);
run(`xcopy /E /I /Y "${linkTarget}" "${linkPath}"`);
}
} else {
fs.symlinkSync(linkTarget, linkPath);
}
console.log(` 链接 @cssyq/ggzc-web -> ${LOCAL_PKG}`);
// 安装依赖
console.log(' 安装依赖...');
run(`cd "${webPath}" && yarn install --prefer-offline --ignore-engines`);
}
console.log('\n=== 初始化完成 ===');
console.log('\n启动开发服务示例:');
for (const web of WEBS) {
console.log(` cd ${web} && yarn dev`);
}