Skip to content

Commit 56060d0

Browse files
committed
Add generate svg command
1 parent 2baecc1 commit 56060d0

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

bin/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { pkg, config, configPath } from '../src/static.js';
77
import { parse } from '../src/parse.js';
88
import { preview } from '../src/preview/index.js';
99
import { render } from '../src/render.js';
10+
import { generateSVG } from '../src/generate-svg.js';
1011

1112
import { Command } from 'commander';
1213

@@ -23,7 +24,7 @@ function read(path) {
2324
return { content, error };
2425
}
2526

26-
async function handleGenerate(source, options) {
27+
async function handleRender(source, options) {
2728
let { content, error } = read(source);
2829
if (error) {
2930
console.log(error.message);
@@ -69,6 +70,21 @@ function handlePreview(source, options) {
6970
}
7071
}
7172

73+
function handleGenerate(source) {
74+
let { content, error } = read(source);
75+
if (error) {
76+
console.log(error.message);
77+
process.exit(1);
78+
} else {
79+
content = content.trim();
80+
if (/^svg\s*\{/i.test(content) || !content.length) {
81+
console.log(generateSVG(content));
82+
} else {
83+
console.log('Not a valid SVG format');
84+
}
85+
}
86+
}
87+
7288
function handleSetBrowser(executablePath) {
7389
config.browserPath = executablePath;
7490
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
@@ -94,7 +110,7 @@ program
94110
.option('-o, --output <output>', 'custom filename of the generated image')
95111
.option('-x, --scale <scale>', 'scale factor of the generated image, defaults to 1')
96112
.action((source, options) => {
97-
handleGenerate(source, options);
113+
handleRender(source, options);
98114
});
99115

100116
program.command('preview')
@@ -105,13 +121,24 @@ program.command('preview')
105121
handlePreview(source, options);
106122
});
107123

124+
program.command('generate')
125+
.description('generate code using CSS Doodle generators')
126+
.action((_, cmd) => {
127+
cmd.help();
128+
})
129+
.command('svg <source>')
130+
.action((source, options, command) => {
131+
handleGenerate(source);
132+
});
133+
108134
program.command('parse')
109135
.description('print the parsed tokens, helped to debug on development')
110136
.argument('<source>', 'source file to parse')
111137
.action((source) => {
112138
handleParse(source);
113139
});
114140

141+
115142
program.command('config')
116143
.description('display/set the configuration')
117144
.action(() => {

src/generate-svg.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { svg } from 'css-doodle/generator';
2+
3+
export function generateSVG(code) {
4+
return svg(code);
5+
}

src/preview/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export function preview(sourceFile, title, options = {}) {
5757
serverProcess.kill();
5858
process.exit(1);
5959
});
60-
}
6160
}
6261

6362
if (message.exit) {

0 commit comments

Comments
 (0)