2018-09-24 11:56:41 +02:00
|
|
|
const Promise = require('bluebird')
|
2018-12-13 17:59:47 +01:00
|
|
|
const babel = Promise.promisifyAll(require("babel-core"))
|
2018-09-24 11:56:41 +02:00
|
|
|
const fs = Promise.promisifyAll(require("fs-extra"))
|
|
|
|
|
const path = require("path")
|
|
|
|
|
|
2020-03-26 17:10:42 +01:00
|
|
|
function build(dirname, version, targets, updateUrl, nameSuffix, notarize, outDir, unpacked) {
|
2018-10-02 16:19:03 +02:00
|
|
|
const targetString = Object.keys(targets)
|
|
|
|
|
.filter(k => typeof targets[k] !== "undefined")
|
|
|
|
|
.join(" ")
|
|
|
|
|
console.log("Building desktop client for v" + version + " (" + targetString + ")...")
|
2018-10-11 14:59:21 +02:00
|
|
|
const distDir = path.join(dirname, '/build/dist/')
|
2020-03-26 17:10:42 +01:00
|
|
|
outDir = path.join(outDir || path.join(distDir, ".."), 'desktop' + nameSuffix)
|
|
|
|
|
console.log("artifacts will be moved to", outDir)
|
2019-11-07 16:08:49 +01:00
|
|
|
const requiredEntities = fs.readdirSync(path.join(dirname, './src/api/entities/sys/'))
|
|
|
|
|
.map(fn => path.join(dirname, './src/api/entities/sys', fn))
|
|
|
|
|
const languageFiles = fs.readdirSync(path.join(dirname, './src/translations/'))
|
|
|
|
|
.map(fn => path.join(dirname, './src/translations', fn))
|
|
|
|
|
|
2018-12-13 17:59:47 +01:00
|
|
|
console.log("Updating electron-builder config...")
|
2018-10-11 14:59:21 +02:00
|
|
|
const content = require('./electron-package-json-template')(
|
2018-10-12 12:22:39 +02:00
|
|
|
nameSuffix,
|
2018-10-11 14:59:21 +02:00
|
|
|
version,
|
|
|
|
|
updateUrl,
|
2018-12-13 17:59:47 +01:00
|
|
|
path.join(dirname, "/resources/desktop-icons/logo-solo-red.png"),
|
2020-03-31 15:55:11 +02:00
|
|
|
nameSuffix !== '-snapshot' && updateUrl !== "", // don't sign if it's a test build or if we don't download updates
|
2020-03-26 17:10:42 +01:00
|
|
|
notarize,
|
|
|
|
|
unpacked
|
2018-10-11 14:59:21 +02:00
|
|
|
)
|
|
|
|
|
let writeConfig = fs.writeFileAsync("./build/dist/package.json", JSON.stringify(content), 'utf-8')
|
2018-10-02 14:46:53 +02:00
|
|
|
|
2018-09-24 15:29:24 +02:00
|
|
|
//prepare files
|
2018-10-11 14:59:21 +02:00
|
|
|
return writeConfig
|
2020-03-26 17:10:42 +01:00
|
|
|
.then(() => fs.removeAsync(outDir))
|
2018-10-11 14:59:21 +02:00
|
|
|
.then(() => {
|
2018-12-13 17:59:47 +01:00
|
|
|
console.log("Tracing dependencies...")
|
2019-10-01 10:21:08 +02:00
|
|
|
transpile(['./src/desktop/DesktopMain.js', './src/desktop/preload.js']
|
|
|
|
|
.concat(requiredEntities)
|
|
|
|
|
.concat(languageFiles), dirname, distDir)
|
2018-10-11 14:59:21 +02:00
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
console.log("Starting installer build...")
|
|
|
|
|
//package for linux, win, mac
|
2018-12-13 17:59:47 +01:00
|
|
|
const electronBuilder = require("electron-builder")
|
|
|
|
|
return electronBuilder.build({
|
2018-10-11 14:59:21 +02:00
|
|
|
_: ['build'],
|
|
|
|
|
win: targets.win,
|
|
|
|
|
mac: targets.mac,
|
|
|
|
|
linux: targets.linux,
|
|
|
|
|
p: 'always',
|
|
|
|
|
project: distDir
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
2020-03-26 17:10:42 +01:00
|
|
|
const installerDir = path.join(distDir, 'installers')
|
|
|
|
|
console.log("Move artifacts to", outDir)
|
|
|
|
|
const unpackedFilter = file => file.endsWith("-unpacked") || file === "mac"
|
|
|
|
|
const packedFilter = file => file.startsWith(content.name) || file.endsWith('.yml')
|
|
|
|
|
|
2018-10-11 14:59:21 +02:00
|
|
|
return Promise.all(
|
2020-03-26 17:10:42 +01:00
|
|
|
fs.readdirSync(installerDir)
|
|
|
|
|
.filter(unpacked ? unpackedFilter : packedFilter)
|
2018-10-11 14:59:21 +02:00
|
|
|
.map(file => fs.moveAsync(
|
2020-03-26 17:10:42 +01:00
|
|
|
path.join(installerDir, file),
|
|
|
|
|
path.join(outDir, file)
|
2018-10-11 14:59:21 +02:00
|
|
|
)
|
|
|
|
|
)
|
2020-03-26 17:10:42 +01:00
|
|
|
)
|
|
|
|
|
}).then(() => Promise.all([
|
|
|
|
|
fs.removeAsync(path.join(distDir, '/installers/')),
|
|
|
|
|
fs.removeAsync(path.join(distDir, '/node_modules/')),
|
|
|
|
|
fs.removeAsync(path.join(distDir, '/cache.json')),
|
|
|
|
|
fs.removeAsync(path.join(distDir, '/package.json')),
|
|
|
|
|
fs.removeAsync(path.join(distDir, '/package-lock.json')),
|
|
|
|
|
fs.removeAsync(path.join(distDir, '/src/')),
|
|
|
|
|
]))
|
2018-09-24 11:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-13 17:59:47 +01:00
|
|
|
/**
|
|
|
|
|
* takes files and transpiles them and their dependency tree from baseDir to distDir
|
|
|
|
|
* @param files array of relative paths to baseDir
|
|
|
|
|
* @param baseDir source Directory
|
|
|
|
|
* @param distDir target Directory
|
|
|
|
|
*/
|
|
|
|
|
function transpile(files, baseDir, distDir) {
|
|
|
|
|
let transpiledFiles = []
|
|
|
|
|
let nextFiles = files.map((file) => path.relative(baseDir, file))
|
|
|
|
|
while (nextFiles.length !== 0) {
|
|
|
|
|
let currentPath = nextFiles.pop()
|
|
|
|
|
let sourcePath = path.join(baseDir, currentPath)
|
2019-02-05 15:15:05 +01:00
|
|
|
if (!transpiledFiles.includes(sourcePath)) {
|
2018-12-13 17:59:47 +01:00
|
|
|
let {src, deps} = findDirectDepsAndTranspile(sourcePath)
|
|
|
|
|
fs.mkdirsSync(path.dirname(path.resolve(distDir, currentPath)))
|
|
|
|
|
fs.writeFileSync(path.join(distDir, currentPath), src, 'utf-8')
|
|
|
|
|
transpiledFiles.push(sourcePath)
|
|
|
|
|
let i;
|
|
|
|
|
for (i = 0; i < deps.length; i++) {
|
|
|
|
|
nextFiles.push(path.relative(baseDir, deps[i]))
|
|
|
|
|
}
|
|
|
|
|
//nextFiles.concat(deps)
|
|
|
|
|
nextFiles = nextFiles.filter((elem, i) => nextFiles.indexOf(elem === i))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log("transpiled files:")
|
|
|
|
|
console.log(transpiledFiles.map(p => path.relative(".", p)).join("\n"))
|
2019-02-05 15:15:05 +01:00
|
|
|
return Promise.resolve()
|
2018-12-13 17:59:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* transpiles the source and finds direct dependencies
|
|
|
|
|
* only finds files that are required by path (not by node module name)
|
|
|
|
|
* @param filePath absolute path to the file
|
|
|
|
|
* @returns {{src: *, deps: Array}} src: transpiled source, deps: array of absolute paths to dependencies
|
|
|
|
|
*/
|
|
|
|
|
function findDirectDepsAndTranspile(filePath) {
|
|
|
|
|
let deps = []
|
|
|
|
|
const regExpRequire = /require\(["'](\..*?)["']\)/g
|
|
|
|
|
let src = babelCompile(fs.readFileSync(filePath, 'utf-8')).code
|
|
|
|
|
|
|
|
|
|
let match = regExpRequire.exec(src)
|
|
|
|
|
while (match != null) {
|
|
|
|
|
if (match[1].indexOf(".js") === -1) {
|
|
|
|
|
match[1] = match[1] + ".js"
|
|
|
|
|
}
|
|
|
|
|
let foundPath = path.join(path.dirname(filePath), match[1])
|
|
|
|
|
deps.push(foundPath)
|
|
|
|
|
match = regExpRequire.exec(src)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {src, deps}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function babelCompile(src, srcFile) {
|
2019-02-05 15:15:05 +01:00
|
|
|
return babel.transform(src, {
|
2018-12-13 17:59:47 +01:00
|
|
|
"plugins": [
|
|
|
|
|
"transform-flow-strip-types",
|
|
|
|
|
"transform-class-properties",
|
|
|
|
|
],
|
|
|
|
|
"presets": [
|
|
|
|
|
"bluebird",
|
|
|
|
|
"es2015"
|
|
|
|
|
],
|
|
|
|
|
comments: false,
|
|
|
|
|
babelrc: false,
|
|
|
|
|
retainLines: true,
|
|
|
|
|
sourceMaps: srcFile != null ? "inline" : false,
|
|
|
|
|
filename: srcFile,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-24 11:56:41 +02:00
|
|
|
module.exports = {
|
2018-12-13 17:59:47 +01:00
|
|
|
build,
|
|
|
|
|
trace: transpile
|
2019-09-05 12:16:25 +02:00
|
|
|
}
|