-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
54 lines (41 loc) · 1.62 KB
/
build.js
File metadata and controls
54 lines (41 loc) · 1.62 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
53
54
const path = require('path')
const fs = require('fs')
// Path to Stacks Icons, included by npm
const stacksIconPath = path.join(__dirname, 'node_modules/@stackoverflow/stacks-icons/build/lib')
// Empty object for list of icons
let iconsObj = {}
// Read the folder of icons
fs.readdir(stacksIconPath, (err, icons) => {
if (err) throw err
// We only want SVGs
icons = icons.filter(i => path.extname(i).toLowerCase() === '.svg')
// Loop over each file
icons.forEach(icon => {
// Name without extension
let name = path.basename(icon, '.svg')
// Grab the actual SVG code from the file
let code = fs.readFileSync(path.resolve(stacksIconPath, icon), 'utf8')
// Write to array
iconsObj[name] = code
})
// Number of icons
let iconsCount = Object.keys(iconsObj).length
// If we have an object of icons
if (iconsCount) {
// This is what we'll print in our front end JS
let jsDefinition = 'var stacksIcons = ' + JSON.stringify(iconsObj)
// Location of front-end JS
let jsFile = path.join(__dirname, 'index.js')
// Read/write
fs.readFile(jsFile, 'utf8', (err, data) => {
if (err) throw err
// Replace the object in the js file
var result = data.replace(/\/\/ Start icons(.|[\r\n])*\/\/ End icons/gm, "// Start icons\n" + jsDefinition + "\n// End icons")
// Write it back
fs.writeFile(jsFile, result, 'utf8', err => {
if (err) throw err
console.log('Successfuly built with ' + iconsCount + ' icons')
})
})
}
})