-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.js
125 lines (95 loc) · 2.37 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
116
117
118
119
120
121
122
123
124
125
var fs = require('fs');
var path = require('path');
var child = require('child_process')
var dir = path.join(process.cwd(), 'node_modules/hue-name')
var isWin = process.platform === 'win32'
var onlyFront = false
var argv = process.argv || []
argv.forEach(arg => {
if (arg == '--only-front')
onlyFront = true
})
function runBackBuild() {
console.log(' 3/3 - Packaging Application')
var newEnv = JSON.parse(JSON.stringify(process.env))
var buildProc = child.spawn('npm' + (isWin ? '.cmd' : ''), ['run', 'build-back'],
{
cwd: process.cwd(),
env: newEnv
})
buildProc.stdout.on('data', (data) => {
if (data) {
var sData = String(data)
console.log(sData)
}
})
buildProc.stderr.on('data', (data) => {
var err = new Error(String(data))
// throw err
})
buildProc.on('exit', (code) => {
process.exit()
})
}
function cleanBackBuild() {
console.log(' 2/3 - Cleaning Package Folder')
var newEnv = JSON.parse(JSON.stringify(process.env))
var buildProc = child.spawn('npm' + (isWin ? '.cmd' : ''), ['run', 'prepackage'],
{
cwd: process.cwd(),
env: newEnv
})
buildProc.stdout.on('data', (data) => {
if (data) {
var sData = String(data)
console.log(sData)
}
})
buildProc.stderr.on('data', (data) => {
var err = new Error(String(data))
throw err
})
buildProc.on('exit', (code) => {
runBackBuild()
})
}
function runFrontBuild() {
console.log(' 1/' + (onlyFront ? '1' : '3') + ' - Building Front-end')
var newEnv = JSON.parse(JSON.stringify(process.env))
var buildProc = child.spawn('npm' + (isWin ? '.cmd' : ''), ['run', 'build-react'],
{
cwd: process.cwd(),
env: newEnv
})
buildProc.stdout.on('data', (data) => {
if (data) {
var sData = String(data)
console.log(sData)
}
})
buildProc.stderr.on('data', (data) => {
var err = new Error(String(data))
// throw err
})
buildProc.on('exit', (code) => {
if (onlyFront)
process.exit()
else
cleanBackBuild()
})
}
if (fs.existsSync(dir)) {
// compile es6 module, otherwise building front-end fails
var file = path.join(dir, 'index.js')
if (fs.existsSync(file)) {
var babel = require('babel-core')
fs.readFile(file, function(err, fileData) {
if (err) {
throw err
}
var result = babel.transform(fileData, { presets: ['env'] })
fs.writeFileSync(file, result.code)
runFrontBuild()
})
} else runFrontBuild()
} else runFrontBuild()