forked from jm59psut/A--PI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
115 lines (109 loc) · 3.28 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const fs = require("fs"),
babel = require("@babel/core"),
path = require('path')
var last_state = fs.existsSync("build_state.json") ? JSON.parse(fs.readFileSync("build_state.json")) : {},
cur_state = {}
function ChangeDir(file) {
var split = path.normalize(file).split(path.sep)
split[split.length - 3] = "src_compiled"
return split.join(path.sep)
}
/**
* @returns was file changed?
*/
function UodateState(file, stats) {
if(last_state[file] === undefined || last_state[file] !== stats.mtimeMs) {
cur_state[file] = stats.mtimeMs
return true
}
return false
}
function AbstractPath(file, depth = 2) {
if(depth === 1)
return path.basename(file)
return file.split(path.sep).slice(-depth).join(path.sep)
}
function Handle(file = "src", callback) {
fs.stat(file, (err, stats) => {
if(err) throw err
if(stats.isDirectory())
fs.readdir(file, (err, files) => {
if(err) throw err
files.forEach(subFile => Handle(`${file}/${subFile}`))
})
else {
if(!UodateState(file, stats))
return
var changed = ChangeDir(file)
if(!fs.existsSync(path.dirname(changed)))
fs.mkdirSync(path.dirname(changed))
if(/^.*\.(t|j)s$/.test(file)) {
changed = `${/^(.*)\.(t|j)s$/.exec(changed)[1]}.js`
console.log(`Recompiling ${path.basename(path.dirname(file))}/${path.basename(file)}`)
fs.writeFileSync(changed, babel.transformFileSync(file, {
filename: path.basename(path.dirname(file)),
retainLines: false,
comments: false,
compact: true,
presets: [
["@babel/preset-env", {
loose: true,
modules: false
}],
"@babel/preset-typescript",
"minify"
],
plugins: [
"@babel/plugin-proposal-class-properties"
]
}).code)
} else if(!/^.*\.(conf|xml|conf\.custom)$/.test(file)) {
console.log(`Copying ${AbstractPath(file)}`)
fs.copyFileSync(file, changed)
}
}
})
}
function DeleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(file => {
var curPath = `${path}/${file}`
if (fs.lstatSync(curPath).isDirectory())
DeleteFolderRecursive(curPath)
else
fs.unlinkSync(curPath)
})
fs.rmdirSync(path)
}
}
if(!fs.existsSync("build_state.json") && fs.existsSync("src_compiled"))
DeleteFolderRecursive("src_compiled")
if(!fs.existsSync("src_compiled")) {
if(fs.existsSync("build_state.json"))
fs.unlinkSync("build_state.json")
last_state = {}
fs.mkdirSync("src_compiled")
}
Handle()
process.on('exit', () => {
Object.entries(last_state).forEach(([file, time]) => {
let file_changed = ChangeDir(file)
if(/^.*\.(t|j)s$/.test(file))
file_changed = `${/^(.*)\.(t|j)s$/.exec(ChangeDir(file))[1]}.js`
if(!fs.existsSync(file)) {
console.log(`Deleting removed file [${file.substring(4)}] from src_compiled...`)
fs.unlinkSync(file_changed)
{ // remove empty dirs too
var base_dir = path.normalize(file).split(path.sep)
--base_dir.length
var old_dir = base_dir.join(path.sep)
base_dir[base_dir.length - 2] = "src_compiled"
var new_dir = base_dir.join(path.sep)
if(!fs.existsSync(old_dir) && fs.existsSync(new_dir) && fs.readdirSync(new_dir).length === 0) fs.rmdirSync(new_dir)
}
} else
if(cur_state[file] === undefined)
cur_state[file] = time
})
fs.writeFileSync("build_state.json", JSON.stringify(cur_state))
})