22
33import fs from 'node:fs' ;
44import path from 'node:path' ;
5+ import os from 'node:os' ;
56
67import { pkg , config , configPath } from '../src/static.js' ;
78import { parse } from '../src/parse.js' ;
@@ -13,26 +14,58 @@ import { Command } from 'commander';
1314
1415const program = new Command ( ) ;
1516
16- function read ( path ) {
17+ async function read ( path ) {
1718 let content = '' ;
1819 let error = null ;
19- try {
20- content = fs . readFileSync ( path , 'utf8' ) ;
21- } catch ( e ) {
22- error = e ;
20+ if ( path === undefined ) {
21+ console . log ( 'No source file specified, reading from stdin...' ) ;
22+ let key = os . platform ( ) === 'win32' ? 'CTRL+Z' : 'CTRL+D' ;
23+ console . log ( `Press ${ key } to finish input.\n` ) ;
24+ try {
25+ content = await readFromStdin ( ) ;
26+ } catch ( e ) {
27+ error = e ;
28+ }
29+ } else {
30+ try {
31+ content = fs . readFileSync ( path , 'utf8' ) ;
32+ } catch ( e ) {
33+ error = e ;
34+ }
2335 }
2436 return { content, error } ;
2537}
2638
39+ function readFromStdin ( ) {
40+ return new Promise ( ( resolve , reject ) => {
41+ let content = '' ;
42+ process . stdin . setEncoding ( 'utf8' ) ;
43+ process . stdin . on ( 'readable' , ( ) => {
44+ let chunk ;
45+ while ( ( chunk = process . stdin . read ( ) ) ) {
46+ content += chunk ;
47+ }
48+ } ) ;
49+ process . stdin . on ( 'end' , ( ) => {
50+ resolve ( content ) ;
51+ } ) ;
52+ process . stdin . on ( 'error' , reject ) ;
53+ } ) ;
54+ }
55+
2756async function handleRender ( source , options ) {
28- let { content, error } = read ( source ) ;
57+ let { content, error } = await read ( source ) ;
2958 if ( error ) {
3059 console . log ( error . message ) ;
3160 process . exit ( 1 ) ;
3261 } else {
33- let basename = path . basename ( source ) ;
34- let extname = path . extname ( basename ) ;
35- let title = extname ? basename . split ( extname ) [ 0 ] : basename ;
62+ let title = 'image' ;
63+ if ( source ) {
64+ let basename = path . basename ( source ) ;
65+ let extname = path . extname ( basename ) ;
66+ title = extname ? basename . split ( extname ) [ 0 ] : basename ;
67+ }
68+
3669 let output = await render ( content , {
3770 title,
3871 output : options . output ,
@@ -44,8 +77,8 @@ async function handleRender(source, options) {
4477 }
4578}
4679
47- function handleParse ( source ) {
48- let { content, error } = read ( source ) ;
80+ async function handleParse ( source ) {
81+ let { content, error } = await read ( source ) ;
4982 if ( error ) {
5083 console . log ( error . message ) ;
5184 process . exit ( 1 ) ;
@@ -59,8 +92,8 @@ function handleParse(source) {
5992 }
6093}
6194
62- function handlePreview ( source , options ) {
63- let { content, error } = read ( source ) ;
95+ async function handlePreview ( source , options ) {
96+ let { content, error } = await read ( source ) ;
6497 if ( error ) {
6598 console . log ( error . message ) ;
6699 process . exit ( 1 ) ;
@@ -70,8 +103,8 @@ function handlePreview(source, options) {
70103 }
71104}
72105
73- function handleGenerateSVG ( source ) {
74- let { content, error } = read ( source ) ;
106+ async function handleGenerateSVG ( source ) {
107+ let { content, error } = await read ( source ) ;
75108 if ( error ) {
76109 console . log ( error . message ) ;
77110 process . exit ( 1 ) ;
@@ -85,8 +118,8 @@ function handleGenerateSVG(source) {
85118 }
86119}
87120
88- function handleGenerateShape ( source ) {
89- let { content, error } = read ( source ) ;
121+ async function handleGenerateShape ( source ) {
122+ let { content, error } = await read ( source ) ;
90123 if ( error ) {
91124 console . log ( error . message ) ;
92125 process . exit ( 1 ) ;
@@ -116,7 +149,7 @@ program
116149program
117150 . command ( 'render' )
118151 . description ( 'generate an image from the CSS Doodle source file' )
119- . argument ( '< source> ' , 'the CSS Doodle source file used to generate the image' )
152+ . argument ( '[ source] ' , 'the CSS Doodle source file used to generate the image' )
120153 . option ( '-o, --output <output>' , 'custom filename of the generated image' )
121154 . option ( '-x, --scale <scale>' , 'scale factor of the generated image, defaults to 1' )
122155 . action ( ( source , options ) => {
@@ -133,7 +166,7 @@ program.command('preview')
133166
134167program . command ( 'parse' )
135168 . description ( 'print the parsed tokens, helped to debug on development' )
136- . argument ( '< source> ' , 'source file to parse' )
169+ . argument ( '[ source] ' , 'source file to parse' )
137170 . action ( ( source ) => {
138171 handleParse ( source ) ;
139172 } ) ;
@@ -158,15 +191,15 @@ const commandGenerate = program.command('generate')
158191 cmd . help ( ) ;
159192 } ) ;
160193
161- commandGenerate . command ( 'svg < source> ' )
194+ commandGenerate . command ( 'svg [ source] ' )
162195 . description ( 'generate SVG code using svg() function' )
163196 . action ( ( source ) => {
164197 handleGenerateSVG ( source ) ;
165198 } )
166199
167- commandGenerate . command ( 'polygon < source> ' )
200+ commandGenerate . command ( 'polygon [ source] ' )
168201 . description ( 'generate CSS polygon() using shape() function' )
169- . action ( ( source ) => {
202+ . action ( source => {
170203 handleGenerateShape ( source ) ;
171204 } ) ;
172205
0 commit comments