2017-11-20 16:07:36 +00:00
|
|
|
const npm = require('npm');
|
|
|
|
const webpack = require('webpack');
|
|
|
|
const rimraf = require('rimraf');
|
|
|
|
const ncp = require('ncp').ncp;
|
|
|
|
const path = require('path');
|
|
|
|
const mkdirp = require('mkdirp');
|
2017-11-26 20:45:40 +00:00
|
|
|
const fs = require('fs');
|
2017-11-20 16:07:36 +00:00
|
|
|
const configRenderer = require('../webpack/webpack.config.production.babel');
|
|
|
|
const configMain = require('../webpack/webpack.config.electron.babel');
|
|
|
|
|
2017-11-26 20:45:40 +00:00
|
|
|
// Start build if ran from CLI
|
2017-11-26 23:04:47 +00:00
|
|
|
if (require.main === module) {
|
2017-11-26 20:45:40 +00:00
|
|
|
process.nextTick(async () => {
|
|
|
|
await module.exports.start();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
module.exports.start = async function() {
|
2017-11-20 16:07:36 +00:00
|
|
|
// Remove folders first
|
|
|
|
console.log('[build] Removing existing directories');
|
|
|
|
await emptyDir('../build');
|
|
|
|
|
|
|
|
// Build the things
|
|
|
|
console.log('[build] Building Webpack');
|
|
|
|
await buildWebpack(configRenderer);
|
|
|
|
await buildWebpack(configMain);
|
|
|
|
|
|
|
|
// Copy necessary files
|
|
|
|
console.log('[build] Copying files');
|
|
|
|
await copyFiles('../bin', '../build/');
|
|
|
|
await copyFiles('../app/static', '../build/static');
|
|
|
|
await copyFiles('../app/icons/', '../build/');
|
|
|
|
|
2017-11-26 20:45:40 +00:00
|
|
|
// Generate package.json
|
2017-11-26 23:04:47 +00:00
|
|
|
await generatePackageJson('../package.json', '../build/package.json');
|
2017-11-26 20:45:40 +00:00
|
|
|
|
2017-11-20 16:07:36 +00:00
|
|
|
// Install Node modules
|
|
|
|
console.log('[build] Installing dependencies');
|
|
|
|
await install('../build/');
|
|
|
|
|
|
|
|
console.log('[build] Complete!');
|
2017-11-26 20:45:40 +00:00
|
|
|
};
|
2017-11-20 16:07:36 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function buildWebpack(config) {
|
2017-11-20 16:07:36 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
webpack(config, (err, stats) => {
|
|
|
|
if (err || stats.hasErrors()) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function emptyDir(relPath) {
|
2017-11-20 16:07:36 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const dir = path.resolve(__dirname, relPath);
|
|
|
|
rimraf(dir, err => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
mkdirp.sync(dir);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function copyFiles(relSource, relDest) {
|
2017-11-20 16:07:36 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const source = path.resolve(__dirname, relSource);
|
|
|
|
const dest = path.resolve(__dirname, relDest);
|
|
|
|
ncp(source, dest, err => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function install(relDir) {
|
2017-11-20 16:07:36 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const prefix = path.resolve(__dirname, relDir);
|
2018-06-25 17:42:50 +00:00
|
|
|
npm.load(
|
2018-07-12 21:24:48 +00:00
|
|
|
{
|
|
|
|
prefix,
|
|
|
|
buildFromSource: true,
|
|
|
|
production: true,
|
|
|
|
optional: false,
|
|
|
|
'package-lock': false
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
err => {
|
2017-11-20 16:07:36 +00:00
|
|
|
if (err) {
|
2018-06-25 17:42:50 +00:00
|
|
|
return reject(err);
|
2017-11-20 16:07:36 +00:00
|
|
|
}
|
2018-06-25 17:42:50 +00:00
|
|
|
|
|
|
|
npm.commands.install([prefix], err => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2017-11-20 16:07:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function generatePackageJson(relBasePkg, relOutPkg) {
|
2017-11-26 20:45:40 +00:00
|
|
|
// Read package.json's
|
|
|
|
const basePath = path.resolve(__dirname, relBasePkg);
|
|
|
|
const outPath = path.resolve(__dirname, relOutPkg);
|
|
|
|
|
|
|
|
const basePkg = JSON.parse(fs.readFileSync(basePath));
|
|
|
|
|
2017-11-26 23:04:47 +00:00
|
|
|
const appPkg = {
|
|
|
|
name: 'insomnia',
|
|
|
|
version: basePkg.app.version,
|
|
|
|
productName: basePkg.app.productName,
|
|
|
|
longName: basePkg.app.longName,
|
|
|
|
description: basePkg.description,
|
2017-11-26 23:05:44 +00:00
|
|
|
license: basePkg.license,
|
2017-11-26 23:04:47 +00:00
|
|
|
homepage: basePkg.homepage,
|
|
|
|
author: basePkg.author,
|
|
|
|
main: 'main.min.js',
|
|
|
|
dependencies: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const key of Object.keys(appPkg)) {
|
|
|
|
if (key === undefined) {
|
|
|
|
throw new Error(`[build] missing "app.${key}" from package.json`);
|
|
|
|
}
|
|
|
|
}
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
// Figure out which dependencies to pack
|
|
|
|
const allDependencies = Object.keys(basePkg.dependencies);
|
|
|
|
const packedDependencies = basePkg.packedDependencies;
|
|
|
|
const unpackedDependencies = allDependencies.filter(
|
|
|
|
name => !packedDependencies.includes(name)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add dependencies
|
|
|
|
for (const name of unpackedDependencies) {
|
|
|
|
const version = basePkg.dependencies[name];
|
|
|
|
if (!version) {
|
|
|
|
throw new Error(`Failed to find packed dep "${name}" in dependencies`);
|
|
|
|
}
|
|
|
|
appPkg.dependencies[name] = version;
|
2017-11-30 13:55:29 +00:00
|
|
|
console.log(`[build] Adding native Node dep ${name}`);
|
2017-11-26 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(outPath, JSON.stringify(appPkg, null, 2));
|
|
|
|
}
|