-
Notifications
You must be signed in to change notification settings - Fork 583
/
postbuild.js
70 lines (57 loc) · 1.82 KB
/
postbuild.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
69
70
const fs = require("promise-fs")
const colors = require('colors-cli')
const files = []
const error = colors.red
const success = colors.green
const walker = async (files, path, nest) => {
const dirFiles = await fs.readdir(path)
for (let c = 0; c < dirFiles.length; c++) {
const file = dirFiles[c]
const stats = await fs.lstat(`${path}${file}`)
if (stats.isDirectory()) {
await walker(files, `${path}${file}/`, `${nest}/${file}`)
} else {
files.push(`${nest}/${file}`)
}
}
}
const deletor = async (path) => {
const dirFiles = await fs.readdir(path)
for (let c = 0; c < dirFiles.length; c++) {
const file = dirFiles[c]
const stats = await fs.lstat(`${path}${file}`)
if (stats.isDirectory()) {
await deletor(`${path}${file}/`)
} else {
await fs.unlink(`${path}${file}`)
}
}
}
deletor("./static/")
.then(() => {
console.log(success("Deleted all old files"))
return ""
})
.then(() => walker(files, "build/", "."))
.then(() => {
// console.log(files)
const fileWritePromises = files.map(name => {
return new Promise(async (resolve, reject) => {
try {
const data = await fs.readFile(`build/${name}`)
await fs.writeFile(name, data, {flag: "a+"})
resolve()
} catch (err) {
reject(err)
}
})
})
return Promise.all(fileWritePromises)
})
.then(() => {
console.log(success("All files have been written and ready to be deployed."))
})
.catch(err => {
console.log(error(err))
console.log(error("Please rebuild or checkout to the last stable release"))
})