tutanota/make.js

144 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-10-02 16:02:55 +02:00
const options = require('commander')
2017-08-15 13:54:22 +02:00
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/"))
2017-08-15 13:54:22 +02:00
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")
2017-08-15 13:54:22 +02:00
2018-10-02 16:02:55 +02:00
const packageJSON = require('./package.json')
const version = packageJSON.version
2017-08-15 13:54:22 +02:00
let start = new Date().getTime()
2018-10-02 16:02:55 +02:00
options
2018-12-21 13:29:35 +01:00
.usage('[options] [test|prod|local|host <url>], "local" is default')
.arguments('[stage] [host]')
2018-10-02 16:02:55 +02:00
.option('-c, --clean', 'Clean build directory')
2018-10-04 10:28:39 +02:00
.option('-w, --watch', 'Watch build dir and rebuild if necessary')
2018-10-02 16:02:55 +02:00
.option('-d, --desktop', 'assemble & start desktop client')
2018-12-21 13:29:35 +01:00
.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
})
2018-10-02 16:02:55 +02:00
.parse(process.argv)
2018-12-21 13:29:35 +01:00
2017-08-15 13:54:22 +02:00
let promise = Promise.resolve()
2018-10-02 16:02:55 +02:00
if (options.clean) {
2017-08-15 13:54:22 +02:00
promise = builder.clean()
}
2018-10-02 16:02:55 +02:00
let watch = !options.watch ? undefined : () => {}
2017-08-15 13:54:22 +02:00
promise
2018-10-04 10:28:39 +02:00
.then(prepareAssets)
2017-08-15 13:54:22 +02:00
.then(() => builder.build(["src"], watch))
2018-10-04 10:28:39 +02:00
.then(startDesktop)
.then(() => {
let now = new Date().getTime()
let time = Math.round((now - start) / 1000 * 100) / 100
2017-08-15 13:54:22 +02:00
console.log(`\n >>> Build completed in ${time}s\n`)
})
.then(() => {
2018-10-02 16:02:55 +02:00
if (options.watch) {
2017-08-15 13:54:22 +02:00
require('chokidar-socket-emitter')({port: 9082, path: 'build', relativeTo: 'build'})
}
})
2018-10-04 10:28:39 +02:00
function prepareAssets() {
2018-12-21 13:29:35 +01:00
let restUrl
2018-10-04 10:28:39 +02:00
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(() => {
2018-12-21 13:29:35 +01:00
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
2018-10-04 10:28:39 +02:00
}
return Promise.all([
createHtml(env.create(SystemConfig.devConfig(true), (options.stage
=== 'local') ? null : restUrl, version, "Browser")),
2018-12-21 13:29:35 +01:00
createHtml(env.create(SystemConfig.devConfig(true), restUrl, version, "App")),
createHtml(env.create(SystemConfig.devConfig(false), restUrl, version, "Desktop"))
2018-10-04 10:28:39 +02:00
])
})
}
function startDesktop() {
if (options.desktop) {
console.log("Trying to start desktop client...")
const version = require('./package.json').version
const packageJSON = require('./buildSrc/electron-package-json-template.js')(
"-debug",
version,
2018-12-21 13:29:35 +01:00
"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/')
)
})
2018-10-04 10:28:39 +02:00
.then(() => {
spawn("/bin/sh", ["-c", "npm start"], {
2018-10-09 11:56:40 +02:00
stdio: ['ignore', 'inherit', 'inherit'],
detached: false
})
2018-10-04 10:28:39 +02:00
})
}
}
2017-08-15 13:54:22 +02:00
function createHtml(env) {
let filenamePrefix
switch (env.mode) {
case "App":
filenamePrefix = "app"
break
case "Browser":
filenamePrefix = "index"
break
case "Desktop":
filenamePrefix = "desktop"
}
2017-08-15 13:54:22 +02:00
let imports = SystemConfig.baseDevDependencies.concat([`${filenamePrefix}.js`])
return Promise.all([
_writeFile(`./build/${filenamePrefix}.js`, [
2018-02-16 12:25:36 +01:00
`window.whitelabelCustomizations = null`,
2017-08-15 13:54:22 +02:00
`window.env = ${JSON.stringify(env, null, 2)}`,
`System.config(env.systemConfig)`,
`System.import("src/system-resolve.js").then(function() {
return System.import(${options.watch ? "'src/bootstrapHotReload.js'" : "'src/app.js'"})
})`
2017-08-15 13:54:22 +02:00
].join("\n")),
2019-08-16 16:25:01 +02:00
LaunchHtml.renderHtml(imports, env).then((content) => _writeFile(`./build/${filenamePrefix}.html`, content))
2017-08-15 13:54:22 +02:00
])
}
function _writeFile(targetFile, content) {
return fs.mkdirsAsync(path.dirname(targetFile)).then(() => fs.writeFileAsync(targetFile, content, 'utf-8'))
}