styimat 8.2.0 → 8.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +80 -19
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -20,6 +20,7 @@ async function main() {
|
|
|
20
20
|
watchMode = true;
|
|
21
21
|
} else if (arg === '--interactive' || arg === '-i') {
|
|
22
22
|
interactiveMode();
|
|
23
|
+
return; // 交互模式会自己处理退出
|
|
23
24
|
} else if (arg === '--help' || arg === '-h') {
|
|
24
25
|
showHelp();
|
|
25
26
|
process.exit(0);
|
|
@@ -108,7 +109,7 @@ async function main() {
|
|
|
108
109
|
|
|
109
110
|
// 处理进程终止
|
|
110
111
|
process.on('SIGINT', () => {
|
|
111
|
-
console.log('
|
|
112
|
+
console.log('停止监控...');
|
|
112
113
|
watcher.close();
|
|
113
114
|
process.exit(0);
|
|
114
115
|
});
|
|
@@ -132,28 +133,88 @@ async function convertFile(inputFilePath, outputFilePath) {
|
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
async function interactiveMode() {
|
|
135
|
-
console.log('
|
|
136
|
-
console.log('
|
|
137
|
-
|
|
138
|
-
console.log('请输入 CSS 代码:');
|
|
136
|
+
console.log('styimat 交互模式');
|
|
137
|
+
console.log('请输入 CSS 代码,按 Ctrl+D (Unix) 或 Ctrl+Z (Windows) 提交转换');
|
|
138
|
+
console.log('按 Ctrl+C 退出交互模式');
|
|
139
139
|
console.log('='.repeat(50));
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
140
|
+
|
|
141
|
+
let sessionCount = 0;
|
|
142
|
+
|
|
143
|
+
while (true) {
|
|
144
|
+
sessionCount++;
|
|
145
|
+
try {
|
|
146
|
+
// 使用 fs.createReadStream 读取输入
|
|
147
|
+
const input = await readFromStdin();
|
|
148
|
+
|
|
149
|
+
if (input === null) {
|
|
150
|
+
console.log('检测到退出信号');
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (input.trim() === '') {
|
|
155
|
+
console.log('输入为空,跳过');
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const output = await convertStyimat(input);
|
|
160
|
+
|
|
161
|
+
console.log('\n转换结果:');
|
|
162
|
+
console.log(output);
|
|
163
|
+
|
|
164
|
+
} catch (error) {
|
|
165
|
+
console.error('转换错误:', error.message);
|
|
166
|
+
console.log('请重试...');
|
|
148
167
|
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
console.log('\n再见!');
|
|
171
|
+
}
|
|
149
172
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
173
|
+
// 使用 fs.createReadStream 读取 stdin
|
|
174
|
+
function readFromStdin() {
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
const chunks = [];
|
|
177
|
+
let totalBytes = 0;
|
|
153
178
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
179
|
+
// 创建 stdin 流
|
|
180
|
+
const stream = fs.createReadStream("/dev/stdin", {
|
|
181
|
+
encoding: 'utf8',
|
|
182
|
+
highWaterMark: 1024 // 每次读取 1KB
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
stream.on('data', (chunk) => {
|
|
187
|
+
chunks.push(chunk);
|
|
188
|
+
totalBytes += chunk.length;
|
|
189
|
+
process.stderr.write('.'); // 显示正在读取的指示
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
stream.on('end', () => {
|
|
193
|
+
process.stderr.write('\n'); // 换行
|
|
194
|
+
|
|
195
|
+
if (chunks.length === 0) {
|
|
196
|
+
resolve(null); // 没有输入
|
|
197
|
+
} else {
|
|
198
|
+
const input = chunks.join('');
|
|
199
|
+
resolve(input);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
stream.on('error', (err) => {
|
|
204
|
+
reject(err);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// 设置超时(30秒无输入)
|
|
208
|
+
const timeout = setTimeout(() => {
|
|
209
|
+
stream.destroy();
|
|
210
|
+
console.log('\n读取超时');
|
|
211
|
+
resolve(null);
|
|
212
|
+
}, 30000);
|
|
213
|
+
|
|
214
|
+
stream.on('end', () => {
|
|
215
|
+
clearTimeout(timeout);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
157
218
|
}
|
|
158
219
|
|
|
159
220
|
async function convertAndOutput(inputContent, outputFile) {
|