-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
68 lines (58 loc) · 1.94 KB
/
index.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
'use strict'
const path = require('path')
const { vendorVersion } = require('./package.json')
const { spawn } = require('child_process')
const argDict = require('./args.json')
const svgCleanerPath =
process.platform === 'darwin'
? path.join(__dirname, 'vendor', `svgcleaner-macos-${vendorVersion}`) :
process.platform === 'linux' && process.arch === 'x64'
? path.join(__dirname, 'vendor', `svgcleaner-linux-x86_64-${vendorVersion}`) :
process.platform === 'win32' && process.arch === 'x64'
? path.join(__dirname, 'vendor', `svgcleaner-win64-${vendorVersion}`) :
null
function convertArg(nodeArgKey, nodeArgValue) {
if (argDict[nodeArgKey] === undefined) {
console.warn(`Unrecognized option '${nodeArgKey}' passed.`)
}
const cliArgKey = argDict[nodeArgKey]
if (nodeArgValue === true) {
return `${cliArgKey}=true`
}
return `${cliArgKey}=${nodeArgValue}`
}
function convertArgs(argObject) {
const ret = []
for (const objKey in argObject) {
if (objKey === 'source' || objKey === 'target') {
ret.push(argObject[objKey])
continue
}
ret.push(convertArg(objKey, argObject[objKey]))
}
return ret
}
module.exports = function svgCleaner(cliArgs, wrapperArgs = {}) {
if (wrapperArgs.dev || wrapperArgs.showCliArgs) {
console.log(convertArgs(cliArgs))
}
if (!('quiet' in cliArgs)) {
cliArgs.quiet = true
}
if (wrapperArgs.outdir) {
require('mkdirp')(wrapperArgs.outdir, function (err) {
if (err) {
console.error(err)
}
})
}
const svgcleanerProc = spawn(svgCleanerPath, convertArgs(cliArgs))
if (wrapperArgs.dev || wrapperArgs.showCliOutput) {
svgcleanerProc.stdout.on('data', (data) => {
console.log(`stdout: ${data}`)
})
svgcleanerProc.stderr.on('data', (data) => {
console.warn(`ERR: ${data}`)
})
}
}