-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
68 lines (61 loc) · 2.25 KB
/
build.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
const fs = require('fs')
const path = require('path')
// Gets array of all directories within a specified path
const getDirectories = path => fs
.readdirSync(path)
.filter(file => fs
.statSync(`${path}/${file}`)
.isDirectory()
)
const listing = getDirectories(path.resolve(__dirname, './api/clean'))
let modjs = fs.createWriteStream('./module.js')
modjs.on('open', () => {
modjs.write(`// THIS IS AN AUTO-GENERATED FILE, DO NOT MANUALLY MODIFY\n\n`)
modjs.write(`module.exports = {`)
listing.forEach((apiType, i) => {
modjs.write(`${i === 0 ? '\n' : ',\n'} '${apiType}': {`)
fs.readdirSync(path.resolve(__dirname, `./api/clean/${apiType}`), 'utf8')
.filter(f => f.indexOf('.js') === f.length - 3) // only files with .js extension
.map(f => f.slice(0, f.length - 3))
.forEach((f, k) => {
modjs.write(`${k === 0 ? '\n' : ',\n'} '${f}': require('./api/clean/${apiType}/${f}.js')`)
})
modjs.write('\n }')
})
modjs.write(`,\n 'utils': {`)
fs.readdirSync(path.resolve(__dirname, `./api/util/`), 'utf8')
.filter(f => f.indexOf('.js') === f.length - 3) // only files with .js extension
.map(f => f.slice(0, f.length - 3))
.forEach((f, k) => {
const mn = f
.split('-')
.map((a, i) => i > 0 // exclude first (for camelCase)
? a[0].toUpperCase() + a.substring(1) // up the 1st char & combine
: a
)
.join('')
modjs.write(`${k === 0 ? '\n' : ',\n'} '${mn}': require('./api/util/${f}.js')`)
})
modjs.write('\n }')
modjs.write('\n}\n')
})
fs.readFile(`./package.json`, 'utf8', (err, data) => {
if (err) return console.log(err)
const newContents = data
.split('\n')
.map(line => {
if (line.indexOf(`"version"`) === -1) return line
const sp1 = line.split(`:`) // [' "version"', ' "2.0.0",']
const sp2 = sp1[1].split(`"`) // [' ', '2.0.0', ',']
const vers = sp2[1].split(`.`).map(a => Number(a)) // [2, 0, 0]
vers[vers.length - 1] += 1 // updates last version number
sp2[1] = vers.join(`.`)
sp1[1] = sp2.join(`"`)
line = sp1.join(`:`)
return line
})
.join('\n')
fs.writeFile(`./package.json`, newContents, 'utf8', (err) => {
if (err) return console.log(err)
})
})