-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
executable file
·65 lines (49 loc) · 1.63 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env node
import Process from "process"
// config
const SET_DIRECTORY = process.argv[2]
const targetModule = pascalize(SET_DIRECTORY)
import elmxParser from 'elmx'
import fs from 'fs';
function pascalize(text) {
text = text.replace(/[-_\s.]+(.)?/g, (match, c) => c ? c.toUpperCase() : '');
return text.substr(0, 1).toUpperCase() + text.substr(1);
}
async function run () {
const body = convert().join('\n\n')
const content = skeleton + body
const target = `${targetModule}.elm`
fs.writeFile(target, content, () => log(target, 'written'))
}
function log (a) {
console.log(Array.from(arguments).join(' '))
return a
}
const skeleton = `module ${targetModule} exposing (..)
{-| Generated with elm-svg-icons -}
import Svg
import Svg.Attributes
import Html.Attributes
`
const convert = () =>
fs.readdirSync(SET_DIRECTORY)
.map(file => {
// read file as string and convert to elm
const sourceFile = `${SET_DIRECTORY}/${file}`
try {
const source = (fs.readFileSync(sourceFile)).toString()
const generated = elmxParser(source)
.replace(/viewbox/g, 'viewBox')
.replace(/Html/g, 'Svg')
.replace(/Svg\.Attributes/g, 'Html.Attributes')
.replace(/"svg" \[/g, '"svg" [Svg.Attributes.fill fill,')
const targetName = pascalize(file.split('.')[0])
const elmSource = `${targetName[0].toLowerCase() + targetName.slice(1)} fill = ${generated}`
return elmSource
} catch (e) {
log(sourceFile, 'failed')
console.error(e)
return ''
}
})
run()