forked from wordflowlab/writeflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteflow-cli.js
More file actions
executable file
·229 lines (191 loc) · 5.97 KB
/
writeflow-cli.js
File metadata and controls
executable file
·229 lines (191 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env node
/**
* WriteFlow 智能启动器 - 实现跨平台兼容性设计
*
* 解决 Windows 下的 ESM 模块加载问题:
* - 优先使用编译后的纯 Node.js 版本 (Windows 最佳)
* - 回退到 tsx 直接运行 (开发环境)
* - 提供详细的错误处理和用户指导
*
* 3层启动回退机制:
* 1. dist/cli/writeflow-cli.js (编译后,Windows 友好)
* 2. tsx + src/cli/writeflow-cli.ts (开发环境)
* 3. 错误处理和诊断信息
*/
import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
// 简单的日志实现 - 避免导入 TypeScript 文件
function debugLog(message, ...args) {
if (process.env.WRITEFLOW_DEBUG === 'true' || process.env.DEBUG === 'true') {
console.log(message, ...args);
}
}
function logError(message, error) {
console.error(`[ERROR] ${message}`, error || '');
}
function logWarn(message, data) {
console.warn(`[WARN] ${message}`, data || '');
}
function infoLog(message, ...args) {
console.log(message, ...args);
}
// ES 模块中的 __dirname 替代方案
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 获取启动参数
const args = process.argv.slice(2);
const srcEntry = path.join(__dirname, 'src', 'cli', 'writeflow-cli.ts');
const distEntry = path.join(__dirname, 'dist', 'cli', 'writeflow-cli.js');
// Windows 平台检测
const isWindows = process.platform === 'win32';
/**
* 方法1: 优先使用编译后的 dist 版本 (Windows 最佳)
*/
function tryDistVersion() {
if (fs.existsSync(distEntry)) {
const child = spawn(process.execPath, [distEntry, ...args], {
stdio: 'inherit',
env: {
...process.env,
NODE_ENV: process.env.NODE_ENV || 'production'
},
// Windows 专用设置
shell: false, // 编译版本不需要 shell
windowsHide: false
});
child.on('exit', code => {
process.exit(code || 0);
});
child.on('error', (error) => {
debugLog(`⚠️ 编译版本启动失败: ${error.message}`);
debugLog('🔄 尝试开发环境启动...');
tryTsxVersion();
});
return true;
}
return false;
}
/**
* 方法2: 使用 tsx 运行 TypeScript 源码 (开发环境)
*/
function tryTsxVersion() {
// 检查 tsx 是否可用
try {
execSync('tsx --version', { stdio: 'ignore' });
debugLog('✅ 使用开发环境 (tsx + TypeScript)');
const child = spawn('tsx', [srcEntry, ...args], {
stdio: 'inherit',
shell: isWindows, // Windows 需要 shell
env: {
...process.env,
NODE_ENV: process.env.NODE_ENV || 'development',
// Windows 专用 ESM 配置
...(isWindows ? {
TSX_TSCONFIG_PATH: path.join(__dirname, 'tsconfig.json'),
NODE_OPTIONS: '--enable-source-maps'
} : {})
}
});
child.on('exit', code => {
process.exit(code || 0);
});
child.on('error', (error) => {
console.error(`❌ tsx 启动失败: ${error.message}`);
showInstallationGuide();
});
return true;
} catch (error) {
console.warn('⚠️ tsx 不可用,正在安装...');
return tryInstallTsx();
}
}
/**
* 方法3: 尝试安装 tsx 并重试
*/
function tryInstallTsx() {
try {
console.log('📦 正在安装 tsx...');
// 尝试本地安装 tsx
execSync('npm install tsx --save-dev', {
stdio: 'inherit',
cwd: __dirname
});
console.log('✅ tsx 安装成功,重新启动...');
return tryTsxVersion();
} catch (error) {
console.error('❌ tsx 安装失败');
showInstallationGuide();
return false;
}
}
/**
* 显示安装指南和诊断信息
*/
function showInstallationGuide() {
console.log('\n' + '='.repeat(60));
console.log('🚨 WriteFlow 启动失败 - Windows ESM 兼容性问题');
console.log('='.repeat(60));
console.log('\n📋 诊断信息:');
console.log(` 操作系统: ${process.platform} ${process.arch}`);
console.log(` Node.js: ${process.version}`);
console.log(` 工作目录: ${process.cwd()}`);
console.log(` 编译版本: ${fs.existsSync(distEntry) ? '✅ 存在' : '❌ 缺失'}`);
console.log('\n🔧 解决方案:');
console.log('\n方案1: 构建编译版本 (推荐)');
console.log(' npm run build');
console.log(' node writeflow-cli.js');
console.log('\n方案2: 安装开发依赖');
console.log(' npm install tsx --save-dev');
console.log(' npx tsx src/cli.ts');
console.log('\n方案3: 全局安装 tsx');
console.log(' npm install -g tsx');
console.log(' tsx src/cli.ts');
console.log('\n📞 如需帮助:');
console.log(' GitHub: https://github.com/wordflowlab/writeflow');
console.log(' Issues: https://github.com/wordflowlab/writeflow/issues');
// Windows 专用提示
if (isWindows) {
console.log('\n🪟 Windows 专用提示:');
console.log(' 1. 确保使用 PowerShell 或 CMD (不推荐 Git Bash)');
console.log(' 2. 以管理员身份运行可能解决权限问题');
console.log(' 3. 检查 PATH 环境变量是否包含 Node.js');
}
console.log('\n' + '='.repeat(60));
process.exit(1);
}
/**
* 主启动逻辑
*/
function main() {
try {
// 优先级1: 尝试编译后的版本
if (tryDistVersion()) {
return;
}
// 优先级2: 尝试 tsx 开发版本
debugLog('📝 编译版本不存在,使用开发模式...');
if (tryTsxVersion()) {
return;
}
// 优先级3: 显示安装指南
showInstallationGuide();
} catch (error) {
console.error('💥 启动过程中出现意外错误:');
console.error(error);
showInstallationGuide();
}
}
// 处理意外退出
process.on('SIGINT', () => {
console.log('\n👋 WriteFlow 已退出');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n👋 WriteFlow 已终止');
process.exit(0);
});
// 启动应用
main();