1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | 'use strict';
|
24 |
|
25 | const chalk = require('chalk');
|
26 | const { Command } = require('commander');
|
27 | const { EOL } = require('os');
|
28 | const fs = require('fs');
|
29 | const getStdin = require('get-stdin').buffer;
|
30 | const glob = require('glob');
|
31 | const path = require('path');
|
32 | const util = require('util');
|
33 |
|
34 | const Converter = require('./Converter');
|
35 |
|
36 | const findFiles = util.promisify(glob);
|
37 | const writeFile = util.promisify(fs.writeFile);
|
38 |
|
39 | const _applyOptions = Symbol('applyOptions');
|
40 | const _baseDir = Symbol('baseDir');
|
41 | const _command = Symbol('command');
|
42 | const _convertFiles = Symbol('convertFiles');
|
43 | const _convertInput = Symbol('convertInput');
|
44 | const _errorStream = Symbol('errorStream');
|
45 | const _outputStream = Symbol('outputStream');
|
46 | const _parseOptions = Symbol('parseOptions');
|
47 | const _provider = Symbol('provider');
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | class CLI {
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | constructor(provider, options = {}) {
|
70 | this[_provider] = provider;
|
71 | this[_baseDir] = options.baseDir || process.cwd();
|
72 | this[_errorStream] = options.errorStream || process.stderr;
|
73 | this[_outputStream] = options.outputStream || process.stdout;
|
74 | this[_command] = new Command()
|
75 | .version(provider.getVersion())
|
76 | .usage('[options] [files...]');
|
77 |
|
78 | const format = provider.getFormat();
|
79 |
|
80 | this[_applyOptions]([
|
81 | {
|
82 | flags: '--no-color',
|
83 | description: 'disables color output'
|
84 | },
|
85 | {
|
86 | flags: '--background <color>',
|
87 | description: 'specify background color for transparent regions in SVG'
|
88 | },
|
89 | {
|
90 | flags: '--base-url <url>',
|
91 | description: 'specify base URL to use for all relative URLs in SVG'
|
92 | },
|
93 | {
|
94 | flags: '--filename <filename>',
|
95 | description: `specify filename for the ${format} output when processing STDIN`
|
96 | },
|
97 | {
|
98 | flags: '--height <value>',
|
99 | description: `specify height for ${format}`
|
100 | },
|
101 | {
|
102 | flags: '--scale <value>',
|
103 | description: 'specify scale to apply to dimensions [1]',
|
104 | transformer: parseInt
|
105 | },
|
106 | {
|
107 | flags: '--width <value>',
|
108 | description: `specify width for ${format}`
|
109 | }
|
110 | ]);
|
111 | this[_applyOptions](provider.getCLIOptions());
|
112 | }
|
113 |
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | error(message) {
|
122 | this[_errorStream].write(`${message}${EOL}`);
|
123 | }
|
124 |
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | output(message) {
|
133 | this[_outputStream].write(`${message}${EOL}`);
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | async parse(args = []) {
|
146 | const command = this[_command].parse(args);
|
147 | const converter = new Converter(this[_provider]);
|
148 | const options = this[_parseOptions]();
|
149 |
|
150 | try {
|
151 | if (command.args.length) {
|
152 | const filePaths = [];
|
153 |
|
154 | for (const arg of command.args) {
|
155 | const files = await findFiles(arg, {
|
156 | absolute: true,
|
157 | cwd: this[_baseDir],
|
158 | nodir: true
|
159 | });
|
160 |
|
161 | filePaths.push(...files);
|
162 | }
|
163 |
|
164 | await this[_convertFiles](converter, filePaths, options);
|
165 | } else {
|
166 | const input = await getStdin();
|
167 |
|
168 | await this[_convertInput](converter, input, options,
|
169 | command.filename ? path.resolve(this[_baseDir], command.filename) : null);
|
170 | }
|
171 | } finally {
|
172 | await converter.destroy();
|
173 | }
|
174 | }
|
175 |
|
176 | [_applyOptions](options) {
|
177 | if (!options) {
|
178 | return;
|
179 | }
|
180 |
|
181 | for (const option of options) {
|
182 | this[_command].option(option.flags, option.description, option.transformer);
|
183 | }
|
184 | }
|
185 |
|
186 | async [_convertFiles](converter, filePaths, options) {
|
187 | const format = this[_provider].getFormat();
|
188 |
|
189 | for (const inputFilePath of filePaths) {
|
190 | const outputFilePath = await converter.convertFile(inputFilePath, options);
|
191 |
|
192 | this.output(`Converted SVG file to ${format} file: ` +
|
193 | `${chalk.blue(inputFilePath)} -> ${chalk.blue(outputFilePath)}`);
|
194 | }
|
195 |
|
196 | this.output(chalk.green('Done!'));
|
197 | }
|
198 |
|
199 | async [_convertInput](converter, input, options, filePath) {
|
200 | if (!options.baseUrl) {
|
201 | options.baseFile = this[_baseDir];
|
202 | }
|
203 |
|
204 | const output = await converter.convert(input, options);
|
205 |
|
206 | if (filePath) {
|
207 | await writeFile(filePath, output);
|
208 |
|
209 | this.output(`Converted SVG input to ${this[_provider].getFormat()} file: ${chalk.blue(filePath)}`);
|
210 | this.output(chalk.green('Done!'));
|
211 | } else {
|
212 | this[_outputStream].write(output);
|
213 | }
|
214 | }
|
215 |
|
216 | [_parseOptions]() {
|
217 | const command = this[_command];
|
218 | const options = {
|
219 | background: command.background,
|
220 | baseUrl: command.baseUrl,
|
221 | height: command.height,
|
222 | scale: command.scale,
|
223 | width: command.width
|
224 | };
|
225 |
|
226 | this[_provider].parseCLIOptions(options, command);
|
227 |
|
228 | return options;
|
229 | }
|
230 |
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | get provider() {
|
238 | return this[_provider];
|
239 | }
|
240 |
|
241 | }
|
242 |
|
243 | module.exports = CLI;
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|