11import fs from 'node:fs' ;
22import path from 'node:path' ;
3- import os from 'node:os' ;
4- import { stdin } from 'node:process' ;
53import { execSync } from 'node:child_process' ;
64
75import { config , configPath , configDownloadPath } from './static.js'
6+ import { generateSVG , generateShape } from './generate.js' ;
87import { parse } from './parse.js' ;
98import { preview } from './preview/index.js' ;
109import { render } from './render.js' ;
11- import { generateSVG , generateShape } from './generate .js' ;
10+ import { read } from './read .js' ;
1211
1312export async function handleRender ( source , options ) {
14- let { content, error } = await read ( source ) ;
13+ const { content, error, type } = await read ( source ) ;
1514 if ( error ) {
1615 return console . error ( error . message ) ;
1716 }
1817 let title = 'image' ;
1918 if ( source ) {
20- let basename = path . basename ( source ) ;
21- let extname = path . extname ( basename ) ;
19+ const basename = path . basename ( source ) ;
20+ const extname = path . extname ( basename ) ;
2221 title = extname ? basename . split ( extname ) [ 0 ] : basename ;
2322 }
24- let output = await render ( content , {
25- title,
26- output : options . output ,
27- scale : options . scale
28- } ) ;
29- if ( output ) {
30- console . log ( `Saved to ${ output } .` ) ;
23+ options . title = title ;
24+ options . type = type ;
25+
26+ try {
27+ const start = Date . now ( ) ;
28+ const output = await render ( content , options ) ;
29+ const time = ( Date . now ( ) - start ) / 1000 ;
30+
31+ if ( output ) {
32+ console . log ( `Saved to ${ output } . (${ time } s)` ) ;
33+ }
34+ } catch ( e ) {
35+ console . error ( e . message ) ;
36+ process . exit ( 1 ) ;
3137 }
3238}
3339
@@ -45,10 +51,16 @@ export async function handleParse(source) {
4551}
4652
4753export async function handlePreview ( source , options ) {
48- let { content, error } = await read ( source ) ;
54+ let { content, error, type } = await read ( source ) ;
4955 if ( error ) {
5056 return console . error ( error . message ) ;
5157 }
58+ if ( type === 'codepen' ) {
59+ return console . error ( 'error: CodePen url is not supported for preview' ) ;
60+ }
61+ if ( type === 'html' ) {
62+ return console . error ( 'error: HTML is not supported for preview' ) ;
63+ }
5264 if ( source === undefined ) {
5365 options . fromStdin = true ;
5466 preview ( content , 'css-doodle' , options ) ;
@@ -129,48 +141,6 @@ export function handleUpgrade() {
129141 execSync ( 'npm update -g @css-doodle/cli' , { stdio : 'inherit' } ) ;
130142}
131143
132- async function read ( path ) {
133- let content = '' ;
134- let error = null ;
135- if ( path === undefined ) {
136- console . log ( 'No source file specified, reading from stdin:' ) ;
137- let key = os . platform ( ) === 'win32' ? 'CTRL+Z' : 'CTRL+D' ;
138- console . log ( `(Press ${ key } to finish input.)\n` ) ;
139- try {
140- content = await readFromStdin ( ) ;
141- console . log ( '\n' ) ;
142- } catch ( e ) {
143- error = e ;
144- }
145- } else {
146- try {
147- content = fs . readFileSync ( path , 'utf8' ) ;
148- } catch ( e ) {
149- error = e ;
150- }
151- }
152-
153- if ( content ) {
154- content = content . trim ( ) ;
155- }
156-
157- if ( ! error && ! content ) {
158- error = new Error ( 'error: empty input' ) ;
159- }
160-
161- return { content, error } ;
162- }
163-
164- function readFromStdin ( ) {
165- return new Promise ( ( resolve , reject ) => {
166- let content = '' ;
167- stdin . setEncoding ( 'utf8' ) ;
168- stdin . on ( 'data' , c => content += c ) ;
169- stdin . on ( 'end' , ( ) => resolve ( content ) ) ;
170- stdin . on ( 'error' , reject ) ;
171- } ) ;
172- }
173-
174144async function fetchCssDoodleSource ( version ) {
175145 let result = '' , error ;
176146
0 commit comments