-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·52 lines (44 loc) · 1.88 KB
/
cli.js
File metadata and controls
executable file
·52 lines (44 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env node
'use strict'
import { program } from 'commander';
import fs from "fs";
import path from "path";
import isSvg from 'is-svg';
import { parse } from 'svgson';
import { pathThatSvg } from 'path-that-svg';
// with the import statement we already have a svg root element, we dont have to create a new one by svg()
import svg from 'svg-builder';
const getPaths = (node) => {
let paths = []
if (node.name == 'path') {
let d = node.attributes.d.replaceAll('\n', ' ');
// split geometry by 'M', filter out empty (the first one is always empty) and put an 'M' before every splitted string
// and finally push the splitted string to paths array
paths.push(...d.split('M').filter((dSplit) => dSplit.length > 0).map(dSplit => 'M' + dSplit));
} else if (node.children && Array.isArray(node.children)) {
// process the paths children recursively
paths.push(...node.children.map(getPaths));
}
return paths;
}
program
.description('Convert an entire svg to path elements, split combined absolute paths into seperate paths, and save them to a new svg file')
.argument('<input file>', 'path to input file')
.requiredOption('-o, --output <path>', 'path to output file')
.action(async (inputFile, options) => {
const file = fs.readFileSync(inputFile);
const fileData = file.toString();
if (!isSvg(fileData)) {
throw 'Invalid SVG'
}
// convert all elements to paths
const pathedSVG = await parse(await pathThatSvg(fileData));
// get an array of paths
const SVGPaths = getPaths(pathedSVG).flat(Infinity);
svg.width(pathedSVG.attributes.width).height(pathedSVG.attributes.height);
for (const d of SVGPaths) {
svg.path({d: d});
}
fs.writeFileSync(options.output, svg.render());
});
program.parse();