-
Notifications
You must be signed in to change notification settings - Fork 1
/
svg2ts.mjs
51 lines (37 loc) · 1.25 KB
/
svg2ts.mjs
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
import fs from "fs"
import path from "path"
import camelCase from 'camelcase';
const input = process.argv[2] || '.'
const output = process.argv[3]
const prefix = ""
console.log('convert', input, output)
function scan(input) {
console.log("scan", input)
let files = fs.readdirSync(input)
files.forEach(file => {
const filename = path.join(input, file)
const stat = fs.statSync(filename)
const ext = path.extname(file)
if (stat.isFile() && ext === ".svg") {
convert(filename)
}
if (stat.isDirectory()) {
scan(filename)
}
})
}
scan(input)
function convert(filename) {
console.log("convert", filename)
const ext = path.extname(filename)
const base = path.basename(filename, ext)
const out = path.join(output || path.dirname(filename), base + '_svg.ts')
fs.writeFileSync(out, "// !!! Generated by svg2ts.mjs\n")
fs.appendFileSync(out, `// From ${filename} \n`)
let variable = prefix + base + "_svg"
variable = camelCase(variable, {pascalCase: true})
fs.appendFileSync(out, `export const ${variable} : string = \``)
const content = fs.readFileSync(filename)
fs.appendFileSync(out, content)
fs.appendFileSync(out, "`\n")
}