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.
Files changed (2) hide show
  1. package/bin/cli.js +80 -19
  2. 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('停止监控...\n');
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('提示: 在这个模式下,您需要一次性输入所有 CSS 代码');
136
- console.log('输入完成后按 Ctrl+D (Unix) 或 Ctrl+Z (Windows)\n');
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
- try {
142
- // 读取 stdin
143
- const inputData = fs.readFileSync(0, 'utf8').trim();
144
-
145
- if (!inputData) {
146
- console.log('没有输入内容');
147
- return;
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
- const output = await convertStyimat(inputData);
151
- console.log('\n转换结果:');
152
- console.log(output);
173
+ // 使用 fs.createReadStream 读取 stdin
174
+ function readFromStdin() {
175
+ return new Promise((resolve, reject) => {
176
+ const chunks = [];
177
+ let totalBytes = 0;
153
178
 
154
- } catch (error) {
155
- console.error('错误:', error.message);
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "styimat",
3
- "version": "8.2.0",
3
+ "version": "8.3.0",
4
4
  "description": "一个高效的CSS变量预处理库,支持Lab/LCH颜色空间自动转换、嵌套选择器和Display P3广色域,让现代CSS开发更简洁强大。",
5
5
  "keywords": [
6
6
  "css",