txw/script/setup.js

101 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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 WEBS = ['txw-mhzc-web'];
const LOCAL_PKGS = [
'local-nodemodules/@cssyq/ggzc-web',
'local-nodemodules/@gt4/common-front',
'local-nodemodules/@gtff/tdesign-gt-vue',
];
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}]`);
for (const localPkg of LOCAL_PKGS) {
const pkgName = localPkg.split('/').pop();
const scope = localPkg.split('/')[1].slice(1); // remove '@'
const scopeDir = path.join(webPath, 'node_modules', `@${scope}`);
// 创建 symlink
const cssyqDir = path.join(webPath, 'node_modules/@cssyq');
const linkTarget = path.join(ROOT, '..', localPkg);
const linkPath = path.join(cssyqDir, 'ggzc-web');
fs.mkdirSync(scopeDir, { 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) {
// 忽略删除失败,继续创建
}
}
const isWin = process.platform === 'win32';
if (isWin) {
try {
fs.symlinkSync(linkTarget, linkPath, 'junction');
} catch (e) {
console.warn(` [警告] junction 失败,将复制替代链接`);
run(`xcopy /E /I /Y "${linkTarget}" "${linkPath}"`);
}
} else {
fs.symlinkSync(linkTarget, linkPath);
}
console.log(` 链接 @${scope}/${pkgName} -> ${localPkg}`);
}
// 修改 package.json 的 resolutions强制使用本地包
const pkgJsonPath = path.join(webPath, 'package.json');
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
pkgJson.resolutions = pkgJson.resolutions || {};
for (const localPkg of LOCAL_PKGS) {
const pkgName = localPkg.split('/').pop();
const localPath = path.join(ROOT, '..', localPkg);
pkgJson.resolutions[pkgName] = `link:${localPath}`;
}
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
console.log(` 已添加 resolutions`);
// 安装依赖
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`);
}