|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import os from 'node:os'; |
| 4 | + |
| 5 | +import { pkg, config, configPath } from '../src/static.js'; |
| 6 | +import { parse } from '../src/parse.js'; |
| 7 | +import { preview } from '../src/preview/index.js'; |
| 8 | +import { render } from '../src/render.js'; |
| 9 | +import { generateSVG, generateShape } from '../src/generate.js'; |
| 10 | + |
| 11 | +export async function handleRender(source, options) { |
| 12 | + let { content, error } = await read(source); |
| 13 | + if (error) { |
| 14 | + console.log(error.message); |
| 15 | + process.exit(1); |
| 16 | + } else { |
| 17 | + let title = 'image'; |
| 18 | + if (source) { |
| 19 | + let basename = path.basename(source); |
| 20 | + let extname = path.extname(basename); |
| 21 | + title = extname ? basename.split(extname)[0] : basename; |
| 22 | + } |
| 23 | + |
| 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}.`); |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export async function handleParse(source) { |
| 36 | + let { content, error } = await read(source); |
| 37 | + if (error) { |
| 38 | + console.log(error.message); |
| 39 | + process.exit(1); |
| 40 | + } else { |
| 41 | + try { |
| 42 | + console.log(JSON.stringify(parse(content), null, 2)); |
| 43 | + } catch (e) { |
| 44 | + console.log(e.message); |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export async function handlePreview(source, options) { |
| 51 | + let { content, error } = await read(source); |
| 52 | + if (error) { |
| 53 | + console.log(error.message); |
| 54 | + process.exit(1); |
| 55 | + } else { |
| 56 | + let title = path.basename(source); |
| 57 | + preview(source, title, options); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export async function handleGenerateSVG(source) { |
| 62 | + let { content, error } = await read(source); |
| 63 | + if (error) { |
| 64 | + console.log(error.message); |
| 65 | + process.exit(1); |
| 66 | + } else { |
| 67 | + content = content.trim(); |
| 68 | + if (/^svg\s*\{/i.test(content) || !content.length) { |
| 69 | + console.log(generateSVG(content)); |
| 70 | + } else { |
| 71 | + console.log('Not a valid SVG format'); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export async function handleGenerateShape(source) { |
| 77 | + let { content, error } = await read(source); |
| 78 | + if (error) { |
| 79 | + console.log(error.message); |
| 80 | + process.exit(1); |
| 81 | + } else { |
| 82 | + console.log(generateShape(content)); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export function handleSetConfig(field, value) { |
| 87 | + config[field] = value; |
| 88 | + if (value === '') { |
| 89 | + delete config[field]; |
| 90 | + } |
| 91 | + fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); |
| 92 | + console.log('OK') |
| 93 | +} |
| 94 | + |
| 95 | +export function handleDisplayConfig(field) { |
| 96 | + console.log(config[field]); |
| 97 | +} |
| 98 | + |
| 99 | +export function handleDisplayConfigList() { |
| 100 | + console.log(JSON.stringify(config, null, 2)); |
| 101 | +} |
| 102 | + |
| 103 | +export function getProgramName() { |
| 104 | + let name = path.basename(process.argv[1]); |
| 105 | + return pkg.bin[name] ? name : 'cssd'; |
| 106 | +} |
| 107 | + |
| 108 | +async function read(path) { |
| 109 | + let content = ''; |
| 110 | + let error = null; |
| 111 | + if (path === undefined) { |
| 112 | + console.log('No source file specified, reading from stdin...'); |
| 113 | + let key = os.platform() === 'win32' ? 'CTRL+Z' : 'CTRL+D'; |
| 114 | + console.log(`Press ${key} to finish input.\n`); |
| 115 | + try { |
| 116 | + content = await readFromStdin(); |
| 117 | + } catch (e) { |
| 118 | + error = e; |
| 119 | + } |
| 120 | + } else { |
| 121 | + try { |
| 122 | + content = fs.readFileSync(path, 'utf8'); |
| 123 | + } catch (e) { |
| 124 | + error = e; |
| 125 | + } |
| 126 | + } |
| 127 | + return { content, error }; |
| 128 | +} |
| 129 | + |
| 130 | +function readFromStdin() { |
| 131 | + return new Promise((resolve, reject) => { |
| 132 | + let content = ''; |
| 133 | + process.stdin.setEncoding('utf8'); |
| 134 | + process.stdin.on('readable', () => { |
| 135 | + let chunk; |
| 136 | + while ((chunk = process.stdin.read())) { |
| 137 | + content += chunk; |
| 138 | + } |
| 139 | + }); |
| 140 | + process.stdin.on('end', () => { |
| 141 | + console.log('\n'); |
| 142 | + resolve(content); |
| 143 | + }); |
| 144 | + process.stdin.on('error', reject); |
| 145 | + }); |
| 146 | +} |
| 147 | + |
0 commit comments