-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
159 lines (136 loc) · 4.65 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const BUILD_DIR = `${__dirname}/build`
const SRC_DIR = `${__dirname}/src`
const DATA_DIR = `${SRC_DIR}/data`
const CHILD_PROCESS = require('child_process')
const FSE = require('fs-extra')
const GLOB = require('glob')
const PUG = require('pug')
const CONCAT = require('concat')
const CRYPTO = require('crypto')
const PATH = require('path')
const PACKAGE = require('./package.json')
const RELEASE = require(`${DATA_DIR}/release.json`)
const LINUX_PKG = `${DATA_DIR}/Giada-${RELEASE.version}-x86_64.AppImage`
const WINDOWS_PKG = `${DATA_DIR}/giada-${RELEASE.version}-x86_64-windows.zip`
const MACOS_PKG = `${DATA_DIR}/giada-${RELEASE.version}-x86_64-macos.zip`
const SOURCE_PKG = `${DATA_DIR}/giada-${RELEASE.version}-src.tar.gz`
/* -------------------------------------------------------------------------- */
function tryExecSync(cmd) {
try {
console.log(CHILD_PROCESS.execSync(cmd).toString())
} catch (error) {
console.error(error.stdout.toString())
process.exit(1)
}
}
function md5(file) {
return CRYPTO.createHash('md5')
.update(FSE.readFileSync(file), 'utf8')
.digest('hex')
}
function getFileSizeMb(file) {
const bytes = FSE.statSync(file).size
const megaBytes = bytes / (1024 * 1024)
const round = Math.round(megaBytes * 10 + Number.EPSILON) / 10
return round
}
function setup() {
console.log('Create build dir')
FSE.removeSync(BUILD_DIR)
FSE.mkdirsSync(BUILD_DIR)
}
function checkFormat() {
console.log('Check formatting')
tryExecSync('npm run check-format')
}
function compileHTML() {
let constants = PACKAGE.constants
constants.version = PACKAGE.version
let release = RELEASE
release.linux = {
md5: md5(LINUX_PKG),
path: LINUX_PKG.replace(DATA_DIR, 'data'),
size: getFileSizeMb(LINUX_PKG),
}
release.windows = {
md5: md5(WINDOWS_PKG),
path: WINDOWS_PKG.replace(DATA_DIR, 'data'),
size: getFileSizeMb(WINDOWS_PKG),
}
release.macos = {
md5: md5(MACOS_PKG),
path: MACOS_PKG.replace(DATA_DIR, 'data'),
size: getFileSizeMb(MACOS_PKG),
}
release.source = {
md5: md5(SOURCE_PKG),
path: SOURCE_PKG.replace(DATA_DIR, 'data'),
size: getFileSizeMb(SOURCE_PKG),
}
const opt = {
NODE_ENV: process.env.NODE_ENV,
CONSTANTS: constants,
RELEASE: release,
}
GLOB.sync(`${SRC_DIR}/html/*.pug`).forEach(function (file) {
console.log(`Compile HTML: ${file}`)
const fnc = PUG.compileFile(file)
const out = `${BUILD_DIR}/${PATH.basename(file, '.pug')}.html`
FSE.writeFileSync(out, fnc(opt))
})
GLOB.sync(`${SRC_DIR}/html/documentation/*.pug`).forEach(function (file) {
console.log(`Compile HTML: ${file}`)
const fnc = PUG.compileFile(file)
const out = `${BUILD_DIR}/documentation-${PATH.basename(
file,
'.pug'
)}.html`
FSE.writeFileSync(out, fnc(opt))
})
GLOB.sync(`${SRC_DIR}/html/tutorials/*.pug`).forEach(function (file) {
console.log(`Compile HTML: ${file}`)
const fnc = PUG.compileFile(file)
const out = `${BUILD_DIR}/tutorial-${PATH.basename(file, '.pug')}.html`
FSE.writeFileSync(out, fnc(opt))
})
}
function compileJs() {
console.log(`Compile JavaScript`)
tryExecSync(
`npm run compile-js -- -outfile ${BUILD_DIR}/js/main-${PACKAGE.version}.js`
)
}
function copyStatic() {
console.log(`Copy images`)
FSE.mkdirsSync(`${BUILD_DIR}/images`)
FSE.copySync(`${SRC_DIR}/images`, `${BUILD_DIR}/images`)
console.log(`Copy packages`)
FSE.mkdirsSync(`${BUILD_DIR}/data`)
FSE.copySync(LINUX_PKG, `${BUILD_DIR}/data/${PATH.basename(LINUX_PKG)}`)
FSE.copySync(WINDOWS_PKG, `${BUILD_DIR}/data/${PATH.basename(WINDOWS_PKG)}`)
FSE.copySync(MACOS_PKG, `${BUILD_DIR}/data/${PATH.basename(MACOS_PKG)}`)
FSE.copySync(SOURCE_PKG, `${BUILD_DIR}/data/${PATH.basename(SOURCE_PKG)}`)
}
function compileCSS() {
console.log(`Compile CSS`)
FSE.mkdirsSync(`${BUILD_DIR}/css`)
if (process.env.NODE_ENV === 'prod') {
const files = GLOB.sync(`${SRC_DIR}/css/*.css`)
CONCAT(files, `${BUILD_DIR}/css/main-${PACKAGE.version}.css`)
} else {
FSE.copySync(`${SRC_DIR}/css/`, `${BUILD_DIR}/css/`)
}
}
/* -------------------------------------------------------------------------- */
if (process.argv[2] != 'dev' && process.argv[2] != 'prod') {
console.log('Usage: nodejs build.js <dev|prod>')
process.exit(1)
}
process.env.NODE_ENV = process.argv[2]
checkFormat()
setup()
compileHTML()
compileJs()
copyStatic()
compileCSS()
//if (process.env.NODE_ENV === 'prod') {}