| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #!/usr/bin/env node
- /**
- * 一键重打包脚本:清理旧进程 + 重新打包 + 快速验证
- * 用法:node scripts/rebuild-and-verify.mjs [--target D:\qiwei-training]
- */
- import { spawnSync } from 'node:child_process';
- import path from 'node:path';
- import { fileURLToPath } from 'node:url';
- const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
- const DEFAULT_TARGET = 'D:\\qiwei-training';
- function parseArgs(argv = process.argv.slice(2)) {
- const targetIndex = argv.indexOf('--target');
- const target = targetIndex >= 0 && argv[targetIndex + 1]
- ? argv[targetIndex + 1]
- : DEFAULT_TARGET;
- return { target: path.resolve(target) };
- }
- function run(command, args, options = {}) {
- const base = { cwd: ROOT, encoding: 'utf8', stdio: 'inherit', ...options };
- return spawnSync(command, args, base);
- }
- function step(title, fn) {
- console.log(`\n[${title}]`);
- const start = Date.now();
- const result = fn();
- const elapsed = ((Date.now() - start) / 1000).toFixed(1);
- console.log(`✓ ${title} (${elapsed}s)`);
- return result;
- }
- async function main() {
- const options = parseArgs();
- console.log('\n=== 企微培训包一键重打包 ===');
- console.log(`目标目录: ${options.target}\n`);
- // 步骤 1:停止已有进程
- step('停止旧进程', () => {
- if (process.platform === 'win32') {
- // 停止 qiwei-workbench.exe
- run('taskkill', ['/F', '/IM', 'qiwei-workbench.exe'], { stdio: 'pipe' });
- // 等待端口释放
- const start = Date.now();
- while (Date.now() - start < 3000) {
- const check = run('netstat', ['-ano'], { stdio: 'pipe' });
- if (!check.stdout || !check.stdout.includes(':4320')) break;
- }
- } else {
- run('pkill', ['-f', 'qiwei-workbench']);
- }
- });
- // 步骤 2:执行打包
- step('执行打包', () => {
- const result = run('node', [
- 'scripts/build-training-package.mjs',
- '--outdir', options.target
- ]);
- if (result.status !== 0) {
- throw new Error('打包失败');
- }
- });
- // 步骤 3:快速验证
- if (process.platform === 'win32') {
- step('快速验证', () => {
- const result = run('powershell', [
- '-ExecutionPolicy', 'Bypass',
- '-File', 'scripts/quick-verify-training-package.ps1',
- options.target
- ], { stdio: 'pipe' });
- // 15 秒后自动停止
- setTimeout(() => {
- run('taskkill', ['/F', '/IM', 'qiwei-workbench.exe'], { stdio: 'pipe' });
- }, 15000);
- return result;
- });
- }
- console.log('\n=== 重打包完成 ===');
- console.log(`\n产物目录: ${options.target}`);
- console.log('下一步: 测试或打包为 ZIP 分发\n');
- }
- main().catch(error => {
- console.error('\n✗ 重打包失败:', error.message);
- process.exit(1);
- });
|