-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·74 lines (65 loc) · 1.58 KB
/
cli.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
'use strict'
const { extname, resolve } = require('path')
const fs = require('fs')
const { promisify } = require('util')
const meow = require('meow')
const outlinStroke = require('svg-outline-stroke')
const mkdirp = require('mkdirp')
const readFile = promisify(fs.readFile)
const readdir = promisify(fs.readdir)
const writeFile = promisify(fs.writeFile)
const stat = promisify(fs.lstat)
const mkdir = promisify(mkdirp)
const cli = meow(
`
Usage
$ outline-stroke <input>
Options
--output, -o Specify output file/folder
Examples
$ outline-stroke icon.svg
<svg>…</svg
`,
{
flags: {
output: {
type: 'string',
alias: 'o',
},
},
}
)
const isSvg = filename => extname(filename) === '.svg'
const outlineSvg = async input => {
const svg = await readFile(input)
return await outlinStroke(svg)
}
const doOutlineStroke = async (input, { output }) => {
const s = await stat(input)
if (s.isDirectory()) {
const files = await readdir(input)
const svgs = files.filter(isSvg)
if (output) {
await mkdir(output)
}
for (const svg of svgs) {
const outlined = await outlineSvg(resolve(input, svg))
if (output) {
await writeFile(resolve(output, svg), outlined)
} else {
console.log(outlined)
}
}
return
}
if (s.isFile() && isSvg(input)) {
const outlined = await outlineSvg(input)
if (output) {
await writeFile(`${output.replace('.svg', '')}.svg`, outlined)
} else {
console.log(outlined)
}
}
}
doOutlineStroke(cli.input[0], cli.flags)