styimat 8.2.0 → 8.4.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 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,86 @@ 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 代码:');
139
- console.log('='.repeat(50));
140
-
141
- try {
142
- // 读取 stdin
143
- const inputData = fs.readFileSync(0, 'utf8').trim();
136
+ console.log('styimat 交互模式');
137
+ console.log('请输入 CSS 代码,按 Ctrl+D (Unix) 或 Ctrl+Z (Windows) 提交转换');
138
+ console.log('按 Ctrl+C 退出交互模式');
139
+
140
+ let sessionCount = 0;
141
+
142
+ while (true) {
143
+ sessionCount++;
144
+ process.stderr.write('styimat>');
144
145
 
145
- if (!inputData) {
146
- console.log('没有输入内容');
147
- return;
146
+ try {
147
+ // 使用 fs.createReadStream 读取输入
148
+ const input = await readFromStdin();
149
+
150
+ if (input === null) {
151
+ break;
152
+ }
153
+
154
+ if (input.trim() === '') {
155
+ continue;
156
+ }
157
+
158
+ const output = await convertStyimat(input);
159
+
160
+ console.log('\n转换结果:');
161
+ console.log(output);
162
+
163
+ } catch (error) {
164
+ console.error('转换错误:', error.message);
165
+ console.log('请重试...');
148
166
  }
167
+ }
168
+
169
+ console.log('\n再见!');
170
+ }
149
171
 
150
- const output = await convertStyimat(inputData);
151
- console.log('\n转换结果:');
152
- console.log(output);
172
+ // 使用 fs.createReadStream 读取 stdin
173
+ function readFromStdin() {
174
+ return new Promise((resolve, reject) => {
175
+ const chunks = [];
176
+ let totalBytes = 0;
153
177
 
154
- } catch (error) {
155
- console.error('错误:', error.message);
156
- }
178
+ // 创建 stdin
179
+ const stream = fs.createReadStream("/dev/stdin", {
180
+ encoding: 'utf8',
181
+ highWaterMark: 1024 // 每次读取 1KB
182
+ });
183
+
184
+ stream.on('data', (chunk) => {
185
+ chunks.push(chunk);
186
+ totalBytes += chunk.length;
187
+ process.stderr.write('........'); // 显示正在读取的指示
188
+ });
189
+
190
+ stream.on('end', () => {
191
+ process.stderr.write('\n'); // 换行
192
+
193
+ if (chunks.length === 0) {
194
+ resolve(null); // 没有输入
195
+ } else {
196
+ const input = chunks.join('');
197
+ resolve(input);
198
+ }
199
+ });
200
+
201
+ stream.on('error', (err) => {
202
+ reject(err);
203
+ });
204
+
205
+ // 设置超时(30秒无输入)
206
+ const timeout = setTimeout(() => {
207
+ stream.destroy();
208
+ console.log('\n读取超时');
209
+ resolve(null);
210
+ }, 30000);
211
+
212
+ stream.on('end', () => {
213
+ clearTimeout(timeout);
214
+ });
215
+ });
157
216
  }
158
217
 
159
218
  async function convertAndOutput(inputContent, outputFile) {
package/bin/cli.min.js ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+ const fs=require("fs"),path=require("path"),chokidar=require("chokidar");async function main(){const s=process.argv.slice(2);let e="",t=null,i=!1,o=null;for(let r=0;r<s.length;r++){const n=s[r];if(n==="--watch"||n==="-w")i=!0;else if(n==="--interactive"||n==="-i"){interactiveMode();return}else n==="--help"||n==="-h"?(c(),process.exit(0)):n.startsWith("-")?(console.error(`\u9519\u8BEF: \u672A\u77E5\u9009\u9879 ${n}`),c(),process.exit(1)):o?t?(console.error("\u9519\u8BEF: \u53C2\u6570\u8FC7\u591A"),c(),process.exit(1)):t=n:o=n}function c(){console.log(`
3
+ \u7528\u6CD5: styimat [\u9009\u9879] [\u8F93\u5165\u6587\u4EF6] [\u8F93\u51FA\u6587\u4EF6]
4
+
5
+ \u9009\u9879:
6
+ -w, --watch \u76D1\u63A7\u8F93\u5165\u6587\u4EF6\u53D8\u5316\u5E76\u81EA\u52A8\u8F6C\u6362
7
+ -h, --help \u663E\u793A\u5E2E\u52A9\u4FE1\u606F
8
+ -i, --interactive \u8FDB\u5165\u4EA4\u4E92\u6A21\u5F0F
9
+
10
+ \u793A\u4F8B:
11
+ styimat # \u4ECE stdin \u8BFB\u53D6\uFF0C\u8F93\u51FA\u5230 stdout
12
+ styimat input.css # \u4ECE\u6587\u4EF6\u8BFB\u53D6\uFF0C\u8F93\u51FA\u5230 stdout
13
+ styimat input.css output.css # \u4ECE\u6587\u4EF6\u8BFB\u53D6\uFF0C\u8F93\u51FA\u5230\u6587\u4EF6
14
+ styimat -w input.css output.css # \u76D1\u63A7\u5E76\u81EA\u52A8\u8F6C\u6362
15
+ styimat -i # \u8FDB\u5165\u4EA4\u4E92\u6A21\u5F0F
16
+ `)}if(!o){i&&(console.error("\u9519\u8BEF: \u76D1\u63A7\u6A21\u5F0F\u9700\u8981\u6307\u5B9A\u8F93\u5165\u6587\u4EF6"),process.exit(1)),e=fs.readFileSync(0,"utf8"),await convertAndOutput(e,null);return}if(fs.existsSync(o)||(console.error(`\u9519\u8BEF: \u8F93\u5165\u6587\u4EF6 "${o}" \u4E0D\u5B58\u5728`),process.exit(1)),i){t||(console.error("\u9519\u8BEF: \u76D1\u63A7\u6A21\u5F0F\u9700\u8981\u6307\u5B9A\u8F93\u51FA\u6587\u4EF6"),process.exit(1)),console.log(`\u76D1\u63A7\u6A21\u5F0F\u5DF2\u542F\u52A8: ${o} -> ${t}`),console.log("\u6309 Ctrl+C \u9000\u51FA"),await convertFile(o,t);const r=chokidar.watch(o,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:200,pollInterval:100}});r.on("change",async n=>{console.log(`${new Date().toLocaleTimeString()} - \u68C0\u6D4B\u5230\u53D8\u5316: ${n}`),await convertFile(n,t)}).on("error",n=>{console.error(`\u76D1\u63A7\u9519\u8BEF: ${n.message}`)}),process.on("SIGINT",()=>{console.log("\u505C\u6B62\u76D1\u63A7..."),r.close(),process.exit(0)})}else e=fs.readFileSync(o,"utf8"),await convertAndOutput(e,t)}async function convertFile(s,e){try{const t=fs.readFileSync(s,"utf8"),i=await convertStyimat(t);fs.writeFileSync(e,i,"utf8"),console.log(`${new Date().toLocaleTimeString()} - \u8F6C\u6362\u5B8C\u6210: ${s} -> ${e}`)}catch(t){console.error(`${new Date().toLocaleTimeString()} - \u8F6C\u6362\u9519\u8BEF: ${t.message}`)}}async function interactiveMode(){console.log("styimat \u4EA4\u4E92\u6A21\u5F0F"),console.log("\u8BF7\u8F93\u5165 CSS \u4EE3\u7801\uFF0C\u6309 Ctrl+D (Unix) \u6216 Ctrl+Z (Windows) \u63D0\u4EA4\u8F6C\u6362"),console.log("\u6309 Ctrl+C \u9000\u51FA\u4EA4\u4E92\u6A21\u5F0F");let s=0;for(;;){s++,process.stderr.write("styimat>");try{const e=await readFromStdin();if(e===null)break;if(e.trim()==="")continue;const t=await convertStyimat(e);console.log(`
17
+ \u8F6C\u6362\u7ED3\u679C:`),console.log(t)}catch(e){console.error("\u8F6C\u6362\u9519\u8BEF:",e.message),console.log("\u8BF7\u91CD\u8BD5...")}}console.log(`
18
+ \u518D\u89C1\uFF01`)}function readFromStdin(){return new Promise((s,e)=>{const t=[];let i=0;const o=fs.createReadStream("/dev/stdin",{encoding:"utf8",highWaterMark:1024});o.on("data",r=>{t.push(r),i+=r.length,process.stderr.write("........")}),o.on("end",()=>{if(process.stderr.write(`
19
+ `),t.length===0)s(null);else{const r=t.join("");s(r)}}),o.on("error",r=>{e(r)});const c=setTimeout(()=>{o.destroy(),console.log(`
20
+ \u8BFB\u53D6\u8D85\u65F6`),s(null)},3e4);o.on("end",()=>{clearTimeout(c)})})}async function convertAndOutput(s,e){try{const t=await convertStyimat(s);e?fs.writeFileSync(e,t,"utf8"):process.stdout.write(t)}catch(t){console.error("\u8F6C\u6362\u9519\u8BEF:",t.message),process.exit(1)}}async function convertStyimat(s){const e=[path.join(path.dirname(__dirname),"dist","styimat.js"),path.join(process.cwd(),"dist","styimat.js"),path.join(process.cwd(),"node_modules","styimat","dist","styimat.js")];let t=null;for(const o of e)try{if(fs.existsSync(o)){t=require(o);break}}catch{}if(!t)throw new Error("\u627E\u4E0D\u5230 styimat.js \u6A21\u5757");const i=t.styimat||t;if(typeof i.convert!="function")throw new Error("styimat.convert \u4E0D\u662F\u51FD\u6570");return await i.convert(s)}main().catch(s=>{console.error("\u7A0B\u5E8F\u9519\u8BEF:",s.message),process.exit(1)});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "styimat",
3
- "version": "8.2.0",
3
+ "version": "8.4.0",
4
4
  "description": "一个高效的CSS变量预处理库,支持Lab/LCH颜色空间自动转换、嵌套选择器和Display P3广色域,让现代CSS开发更简洁强大。",
5
5
  "keywords": [
6
6
  "css",
@@ -43,10 +43,11 @@
43
43
  "dist/styimat.min.mjs",
44
44
  "LICENSE",
45
45
  "README.md",
46
- "bin/cli.js"
46
+ "bin/cli.js",
47
+ "bin/cli.min.js"
47
48
  ],
48
49
  "bin": {
49
- "styimat": "bin/cli.js"
50
+ "styimat": "bin/cli.min.js"
50
51
  },
51
52
  "repository": {
52
53
  "type": "git",
@@ -58,7 +59,7 @@
58
59
  "homepage": "https://gitee.com/wxy6987/styimat#readme",
59
60
  "scripts": {
60
61
  "test": "echo \"No tests yet\" && exit 0",
61
- "build:min": "npx esbuild dist/styimat.js --minify --outfile=dist/styimat.min.js && node build.js && npx esbuild dist/styimat.mjs --minify --outfile=dist/styimat.min.mjs",
62
+ "build:min": "npx esbuild dist/styimat.js --minify --outfile=dist/styimat.min.js && node build.js && npx esbuild dist/styimat.mjs --minify --outfile=dist/styimat.min.mjs && npx esbuild bin/cli.js --minify --outfile=bin/cli.min.js",
62
63
  "prepare": "npm run build:min",
63
64
  "prepublishOnly": "npm run build:min",
64
65
  "postpublish": "npm run git:auto-commit && npm run git:auto-tag",