forked from tutao/tutanota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
140 lines (128 loc) · 4.81 KB
/
make.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
const options = require('commander')
const Promise = require('bluebird')
const path = require("path")
const Builder = require('./buildSrc/Builder.js').Builder
const builder = new Builder(path.join(__dirname, '.'), path.join(__dirname, "build/"))
const fs = Promise.Promise.promisifyAll(require("fs-extra"))
const env = require('./buildSrc/env.js')
const LaunchHtml = require('./buildSrc/LaunchHtml.js')
const SystemConfig = require('./buildSrc/SystemConfig.js')
const os = require("os")
const spawn = require('child_process').spawn
const desktopBuilder = require("./buildSrc/DesktopBuilder")
const packageJSON = require('./package.json')
const version = packageJSON.version
let start = new Date().getTime()
options
.usage('[options] [test|prod|local|host <url>], "local" is default')
.arguments('[stage] [host]')
.option('-c, --clean', 'Clean build directory')
.option('-w, --watch', 'Watch build dir and rebuild if necessary')
.option('-d, --desktop', 'assemble & start desktop client')
.action(function (stage, host) {
if (!["test", "prod", "local", "host", undefined].includes(stage)
|| (stage !== "host" && host)
|| (stage === "host" && !host)) {
options.outputHelp()
process.exit(1)
}
options.stage = stage || "local"
options.host = host
})
.parse(process.argv)
let promise = Promise.resolve()
if (options.clean) {
promise = builder.clean()
}
let watch = !options.watch ? undefined : () => {}
promise
.then(prepareAssets)
.then(() => builder.build(["src"], watch))
.then(startDesktop)
.then(() => {
let now = new Date().getTime()
let time = Math.round((now - start) / 1000 * 100) / 100
console.log(`\n >>> Build completed in ${time}s\n`)
})
.then(() => {
if (options.watch) {
require('chokidar-socket-emitter')({port: 9082, path: 'build', relativeTo: 'build'})
}
})
function prepareAssets() {
let restUrl
return Promise.resolve()
.then(() => fs.copyAsync(path.join(__dirname, '/resources/favicon'), path.join(__dirname, '/build/images')))
.then(() => fs.copyAsync(path.join(__dirname, '/resources/images/'), path.join(__dirname, '/build/images')))
.then(() => fs.copyAsync(path.join(__dirname, '/libs'), path.join(__dirname, '/build/libs')))
.then(() => {
if (options.stage === 'test') {
restUrl = 'https://test.tutanota.com'
} else if (options.stage === 'prod') {
restUrl = 'https://mail.tutanota.com'
} else if (options.stage === 'local') {
restUrl = "http://" + os.hostname().split(".")[0] + ":9000"
} else { // host
restUrl = options.host
}
return Promise.all([
createHtml(env.create(SystemConfig.devConfig(true), (options.stage === 'local') ? null : restUrl, version, "Browser")),
createHtml(env.create(SystemConfig.devConfig(true), restUrl, version, "App")),
createHtml(env.create(SystemConfig.devConfig(false), restUrl, version, "Desktop"))
])
})
}
function startDesktop() {
if (options.desktop) {
console.log("Trying to start desktop client...")
const packageJSON = require('./buildSrc/electron-package-json-template.js')(
"",
"0.0.1",
"http://localhost:9000",
path.join(__dirname, "/resources/desktop-icons/logo-solo-red.png"),
false
)
const content = JSON.stringify(packageJSON)
return fs.writeFileAsync("./build/package.json", content, 'utf-8')
.then(() => {
return desktopBuilder.trace(
['./src/desktop/DesktopMain.js', './src/desktop/preload.js'],
__dirname,
path.join(__dirname, '/build/')
)
})
.then(() => {
spawn("/bin/sh", ["-c", "npm start"], {
stdio: ['ignore', 'inherit', 'inherit'],
detached: false
})
})
}
}
function createHtml(env) {
let filenamePrefix
switch (env.mode) {
case "App":
filenamePrefix = "app"
break
case "Browser":
filenamePrefix = "index"
break
case "Desktop":
filenamePrefix = "desktop"
}
let imports = SystemConfig.baseDevDependencies.concat([`${filenamePrefix}.js`])
return Promise.all([
_writeFile(`./build/${filenamePrefix}.js`, [
`window.whitelabelCustomizations = null`,
`window.env = ${JSON.stringify(env, null, 2)}`,
`System.config(env.systemConfig)`,
`System.import("src/system-resolve.js")`
+ (options.watch ? `.then(function() { System.import('src/bootstrapHotReload.js') })` : `;System.import('src/app.js')`)
].join("\n")),
_writeFile(`./build/${filenamePrefix}.html`, LaunchHtml.renderHtml(imports, env))
])
}
function _writeFile(targetFile, content) {
return fs.mkdirsAsync(path.dirname(targetFile)).then(() => fs.writeFileAsync(targetFile, content, 'utf-8'))
}