insomnia/packages/insomnia-app/scripts/build.js

248 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-04-26 20:33:39 +00:00
const { appConfig } = require('../config');
Update Electron and node-libcurl (#1215) * A bunch of things * Publish - insomnia-app@1.0.41 - insomnia-cookies@0.0.8 - insomnia-httpsnippet@1.16.13 - insomnia-importers@2.0.7 - insomnia-libcurl@0.0.17 - insomnia-url@0.1.4 - insomnia-xpath@1.0.5 - insomnia-plugin-cookie-jar@1.0.3 - insomnia-plugin-jsonpath@1.0.7 - insomnia-plugin-now@1.0.7 - insomnia-plugin-os@1.0.9 - insomnia-plugin-prompt@1.1.7 - insomnia-plugin-request@1.0.13 - insomnia-plugin-response@1.0.11 - insomnia-plugin-uuid@1.0.6 * Package deps * Remove fsevents again * Appveyor node verison * Remove unused dep * Try something * Try another thing * NPM audit * Yet again * Fix test * Fix test * Remove npm dep * Fix webview * Fix React sortable and bump version * Comment out broken test for now * Publish - insomnia-app@1.0.42 - insomnia-cookies@0.0.9 - insomnia-httpsnippet@1.16.14 - insomnia-importers@2.0.8 - insomnia-libcurl@0.0.18 - insomnia-xpath@1.0.6 - insomnia-plugin-cookie-jar@1.0.4 - insomnia-plugin-file@1.0.5 - insomnia-plugin-jsonpath@1.0.8 - insomnia-plugin-now@1.0.8 - insomnia-plugin-os@1.0.10 - insomnia-plugin-request@1.0.14 - insomnia-plugin-response@1.0.12 - insomnia-plugin-uuid@1.0.7 * Remove deprecated Electron Builder config options * Try some more * Publish - insomnia-app@1.0.43 - insomnia-libcurl@0.0.19 * Bump * Publish - insomnia-app@1.0.44 - insomnia-libcurl@0.0.20 * Hopefully fix windows builds * DEBUG on travis * Hopefully fix dependency issues * Undo package-lock copy * Debug * More debugging * Another check * Fix debug check * Try just build * Extra logging * Escape windows paths differently (Fixes #1217) * Try packing more deps * Publish - insomnia-app@1.0.45 - insomnia-cookies@0.0.10 - insomnia-httpsnippet@1.16.15 - insomnia-importers@2.0.9 - insomnia-libcurl@0.0.21 - insomnia-xpath@1.0.7 - insomnia-plugin-cookie-jar@1.0.5 - insomnia-plugin-jsonpath@1.0.9 - insomnia-plugin-now@1.0.9 - insomnia-plugin-os@1.0.11 - insomnia-plugin-request@1.0.15 - insomnia-plugin-response@1.0.13 - insomnia-plugin-uuid@1.0.8 * Adjust some deps * Zip build dir into release files * More debug info * Undo travis tar debug * Downgrade electron-builder to 20.19 * Downgrade electron-builder to last working version * Try electron-builder 20.20.4 * Change bundled deps * Add electron-builder-lib back
2018-10-20 02:11:58 +00:00
const childProcess = require('child_process');
const webpack = require('webpack');
const licenseChecker = require('license-checker');
const rimraf = require('rimraf');
const ncp = require('ncp').ncp;
const path = require('path');
const mkdirp = require('mkdirp');
const fs = require('fs');
const { getBuildContext } = require('./getBuildContext');
// Start build if ran from CLI
if (require.main === module) {
process.nextTick(async () => {
await module.exports.start(false);
});
}
module.exports.start = async function(forceFromGitRef) {
const buildContext = getBuildContext(forceFromGitRef);
if (!buildContext.smokeTest && !buildContext.version) {
2020-04-26 20:33:39 +00:00
console.log(`[build] Skipping build for ref "${buildContext.gitRef}"`);
process.exit(0);
}
if (!buildContext.smokeTest && appConfig().version !== buildContext.version) {
console.log(
`[build] App version mismatch with Git tag ${appConfig().version} != ${buildContext.version}`,
);
2020-04-26 20:33:39 +00:00
process.exit(1);
}
// These must be required after APP_ID environment variable is set above
const configRenderer = require('../webpack/webpack.config.production.babel');
const configMain = require('../webpack/webpack.config.electron.babel');
const buildFolder = path.join('../build');
2020-04-26 20:33:39 +00:00
if (buildContext.smokeTest) {
console.log('[build] Starting build to smoke test');
} else {
console.log(`[build] Starting build for ref "${buildContext.gitRef}"`);
}
2020-04-26 20:33:39 +00:00
console.log(`[build] npm: ${childProcess.spawnSync('npm', ['--version']).stdout}`.trim());
console.log(`[build] node: ${childProcess.spawnSync('node', ['--version']).stdout}`.trim());
Update Electron and node-libcurl (#1215) * A bunch of things * Publish - insomnia-app@1.0.41 - insomnia-cookies@0.0.8 - insomnia-httpsnippet@1.16.13 - insomnia-importers@2.0.7 - insomnia-libcurl@0.0.17 - insomnia-url@0.1.4 - insomnia-xpath@1.0.5 - insomnia-plugin-cookie-jar@1.0.3 - insomnia-plugin-jsonpath@1.0.7 - insomnia-plugin-now@1.0.7 - insomnia-plugin-os@1.0.9 - insomnia-plugin-prompt@1.1.7 - insomnia-plugin-request@1.0.13 - insomnia-plugin-response@1.0.11 - insomnia-plugin-uuid@1.0.6 * Package deps * Remove fsevents again * Appveyor node verison * Remove unused dep * Try something * Try another thing * NPM audit * Yet again * Fix test * Fix test * Remove npm dep * Fix webview * Fix React sortable and bump version * Comment out broken test for now * Publish - insomnia-app@1.0.42 - insomnia-cookies@0.0.9 - insomnia-httpsnippet@1.16.14 - insomnia-importers@2.0.8 - insomnia-libcurl@0.0.18 - insomnia-xpath@1.0.6 - insomnia-plugin-cookie-jar@1.0.4 - insomnia-plugin-file@1.0.5 - insomnia-plugin-jsonpath@1.0.8 - insomnia-plugin-now@1.0.8 - insomnia-plugin-os@1.0.10 - insomnia-plugin-request@1.0.14 - insomnia-plugin-response@1.0.12 - insomnia-plugin-uuid@1.0.7 * Remove deprecated Electron Builder config options * Try some more * Publish - insomnia-app@1.0.43 - insomnia-libcurl@0.0.19 * Bump * Publish - insomnia-app@1.0.44 - insomnia-libcurl@0.0.20 * Hopefully fix windows builds * DEBUG on travis * Hopefully fix dependency issues * Undo package-lock copy * Debug * More debugging * Another check * Fix debug check * Try just build * Extra logging * Escape windows paths differently (Fixes #1217) * Try packing more deps * Publish - insomnia-app@1.0.45 - insomnia-cookies@0.0.10 - insomnia-httpsnippet@1.16.15 - insomnia-importers@2.0.9 - insomnia-libcurl@0.0.21 - insomnia-xpath@1.0.7 - insomnia-plugin-cookie-jar@1.0.5 - insomnia-plugin-jsonpath@1.0.9 - insomnia-plugin-now@1.0.9 - insomnia-plugin-os@1.0.11 - insomnia-plugin-request@1.0.15 - insomnia-plugin-response@1.0.13 - insomnia-plugin-uuid@1.0.8 * Adjust some deps * Zip build dir into release files * More debug info * Undo travis tar debug * Downgrade electron-builder to 20.19 * Downgrade electron-builder to last working version * Try electron-builder 20.20.4 * Change bundled deps * Add electron-builder-lib back
2018-10-20 02:11:58 +00:00
if (process.version.indexOf('v12.') !== 0) {
console.log('[build] Node v12.x.x is required to build');
2019-10-30 14:24:32 +00:00
process.exit(1);
}
// Remove folders first
console.log('[build] Removing existing directories');
await emptyDir(buildFolder);
// Build the things
console.log('[build] Building license list');
await buildLicenseList('../', path.join(buildFolder, 'opensource-licenses.txt'));
2019-04-19 22:56:50 +00:00
console.log('[build] Building Webpack renderer');
await buildWebpack(configRenderer);
2019-04-19 22:56:50 +00:00
console.log('[build] Building Webpack main');
await buildWebpack(configMain);
// Copy necessary files
console.log('[build] Copying files');
await copyFiles('../bin', buildFolder);
await copyFiles('../app/static', path.join(buildFolder, 'static'));
await copyFiles('../app/icons', buildFolder);
// Generate necessary files needed by `electron-builder`
await generatePackageJson('../package.json', path.join(buildFolder, 'package.json'));
// Install Node modules
console.log('[build] Installing dependencies');
await install(buildFolder);
console.log('[build] Complete!');
2020-04-26 20:33:39 +00:00
return buildContext;
};
2018-06-25 17:42:50 +00:00
async function buildWebpack(config) {
return new Promise((resolve, reject) => {
2019-09-18 00:02:42 +00:00
const compiler = webpack(config);
compiler.run((err, stats) => {
2019-04-19 22:56:50 +00:00
if (err) {
reject(err);
2019-04-19 22:56:50 +00:00
} else if (stats.hasErrors()) {
reject(new Error('Failed to build webpack'));
2019-09-18 00:02:42 +00:00
console.log(stats.toJson().errors);
} else {
resolve();
}
});
});
}
2018-06-25 17:42:50 +00:00
async function emptyDir(relPath) {
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) {
return new Promise((resolve, reject) => {
const source = path.resolve(__dirname, relSource);
const dest = path.resolve(__dirname, relDest);
2020-04-26 20:33:39 +00:00
console.log(`[build] copy "${relSource}" to "${relDest}"`);
ncp(source, dest, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async function buildLicenseList(relSource, relDest) {
return new Promise((resolve, reject) => {
const source = path.resolve(__dirname, relSource);
const dest = path.resolve(__dirname, relDest);
mkdirp.sync(path.dirname(dest));
licenseChecker.init(
{
start: source,
production: true,
},
(err, packages) => {
if (err) {
return reject(err);
}
const out = [];
for (const pkgName of Object.keys(packages)) {
const { licenses, repository, publisher, email, licenseFile: lf } = packages[pkgName];
const licenseFile = (lf || '').includes('README') ? null : lf;
const txt = licenseFile ? fs.readFileSync(licenseFile) : '[no license file]';
const body = [
'-------------------------------------------------------------------------',
'',
`PACKAGE: ${pkgName}`,
licenses ? `LICENSES: ${licenses}` : null,
repository ? `REPOSITORY: ${repository}` : null,
publisher ? `PUBLISHER: ${publisher}` : null,
email ? `EMAIL: ${email}` : null,
'\n' + txt,
]
.filter(v => v !== null)
.join('\n');
out.push(`${body}\n\n`);
}
const header = [
'This application bundles the following third-party packages in ',
'accordance with the following licenses:',
'-------------------------------------------------------------------------',
'',
'',
].join('\n');
fs.writeFileSync(dest, header + out.join('\n\n'));
resolve();
},
);
});
}
2018-06-25 17:42:50 +00:00
async function install(relDir) {
2019-09-18 00:02:42 +00:00
return new Promise(resolve => {
const prefix = path.resolve(__dirname, relDir);
Update Electron and node-libcurl (#1215) * A bunch of things * Publish - insomnia-app@1.0.41 - insomnia-cookies@0.0.8 - insomnia-httpsnippet@1.16.13 - insomnia-importers@2.0.7 - insomnia-libcurl@0.0.17 - insomnia-url@0.1.4 - insomnia-xpath@1.0.5 - insomnia-plugin-cookie-jar@1.0.3 - insomnia-plugin-jsonpath@1.0.7 - insomnia-plugin-now@1.0.7 - insomnia-plugin-os@1.0.9 - insomnia-plugin-prompt@1.1.7 - insomnia-plugin-request@1.0.13 - insomnia-plugin-response@1.0.11 - insomnia-plugin-uuid@1.0.6 * Package deps * Remove fsevents again * Appveyor node verison * Remove unused dep * Try something * Try another thing * NPM audit * Yet again * Fix test * Fix test * Remove npm dep * Fix webview * Fix React sortable and bump version * Comment out broken test for now * Publish - insomnia-app@1.0.42 - insomnia-cookies@0.0.9 - insomnia-httpsnippet@1.16.14 - insomnia-importers@2.0.8 - insomnia-libcurl@0.0.18 - insomnia-xpath@1.0.6 - insomnia-plugin-cookie-jar@1.0.4 - insomnia-plugin-file@1.0.5 - insomnia-plugin-jsonpath@1.0.8 - insomnia-plugin-now@1.0.8 - insomnia-plugin-os@1.0.10 - insomnia-plugin-request@1.0.14 - insomnia-plugin-response@1.0.12 - insomnia-plugin-uuid@1.0.7 * Remove deprecated Electron Builder config options * Try some more * Publish - insomnia-app@1.0.43 - insomnia-libcurl@0.0.19 * Bump * Publish - insomnia-app@1.0.44 - insomnia-libcurl@0.0.20 * Hopefully fix windows builds * DEBUG on travis * Hopefully fix dependency issues * Undo package-lock copy * Debug * More debugging * Another check * Fix debug check * Try just build * Extra logging * Escape windows paths differently (Fixes #1217) * Try packing more deps * Publish - insomnia-app@1.0.45 - insomnia-cookies@0.0.10 - insomnia-httpsnippet@1.16.15 - insomnia-importers@2.0.9 - insomnia-libcurl@0.0.21 - insomnia-xpath@1.0.7 - insomnia-plugin-cookie-jar@1.0.5 - insomnia-plugin-jsonpath@1.0.9 - insomnia-plugin-now@1.0.9 - insomnia-plugin-os@1.0.11 - insomnia-plugin-request@1.0.15 - insomnia-plugin-response@1.0.13 - insomnia-plugin-uuid@1.0.8 * Adjust some deps * Zip build dir into release files * More debug info * Undo travis tar debug * Downgrade electron-builder to 20.19 * Downgrade electron-builder to last working version * Try electron-builder 20.20.4 * Change bundled deps * Add electron-builder-lib back
2018-10-20 02:11:58 +00:00
const p = childProcess.spawn('npm', ['install', '--production', '--no-optional'], {
cwd: prefix,
shell: true,
Update Electron and node-libcurl (#1215) * A bunch of things * Publish - insomnia-app@1.0.41 - insomnia-cookies@0.0.8 - insomnia-httpsnippet@1.16.13 - insomnia-importers@2.0.7 - insomnia-libcurl@0.0.17 - insomnia-url@0.1.4 - insomnia-xpath@1.0.5 - insomnia-plugin-cookie-jar@1.0.3 - insomnia-plugin-jsonpath@1.0.7 - insomnia-plugin-now@1.0.7 - insomnia-plugin-os@1.0.9 - insomnia-plugin-prompt@1.1.7 - insomnia-plugin-request@1.0.13 - insomnia-plugin-response@1.0.11 - insomnia-plugin-uuid@1.0.6 * Package deps * Remove fsevents again * Appveyor node verison * Remove unused dep * Try something * Try another thing * NPM audit * Yet again * Fix test * Fix test * Remove npm dep * Fix webview * Fix React sortable and bump version * Comment out broken test for now * Publish - insomnia-app@1.0.42 - insomnia-cookies@0.0.9 - insomnia-httpsnippet@1.16.14 - insomnia-importers@2.0.8 - insomnia-libcurl@0.0.18 - insomnia-xpath@1.0.6 - insomnia-plugin-cookie-jar@1.0.4 - insomnia-plugin-file@1.0.5 - insomnia-plugin-jsonpath@1.0.8 - insomnia-plugin-now@1.0.8 - insomnia-plugin-os@1.0.10 - insomnia-plugin-request@1.0.14 - insomnia-plugin-response@1.0.12 - insomnia-plugin-uuid@1.0.7 * Remove deprecated Electron Builder config options * Try some more * Publish - insomnia-app@1.0.43 - insomnia-libcurl@0.0.19 * Bump * Publish - insomnia-app@1.0.44 - insomnia-libcurl@0.0.20 * Hopefully fix windows builds * DEBUG on travis * Hopefully fix dependency issues * Undo package-lock copy * Debug * More debugging * Another check * Fix debug check * Try just build * Extra logging * Escape windows paths differently (Fixes #1217) * Try packing more deps * Publish - insomnia-app@1.0.45 - insomnia-cookies@0.0.10 - insomnia-httpsnippet@1.16.15 - insomnia-importers@2.0.9 - insomnia-libcurl@0.0.21 - insomnia-xpath@1.0.7 - insomnia-plugin-cookie-jar@1.0.5 - insomnia-plugin-jsonpath@1.0.9 - insomnia-plugin-now@1.0.9 - insomnia-plugin-os@1.0.11 - insomnia-plugin-request@1.0.15 - insomnia-plugin-response@1.0.13 - insomnia-plugin-uuid@1.0.8 * Adjust some deps * Zip build dir into release files * More debug info * Undo travis tar debug * Downgrade electron-builder to 20.19 * Downgrade electron-builder to last working version * Try electron-builder 20.20.4 * Change bundled deps * Add electron-builder-lib back
2018-10-20 02:11:58 +00:00
});
p.stdout.on('data', data => {
console.log(data.toString());
});
p.stderr.on('data', data => {
console.log(data.toString());
});
p.on('exit', code => {
console.log('child process exited with code ' + code.toString());
resolve();
});
});
}
function generatePackageJson(relBasePkg, relOutPkg) {
// Read package.json's
const basePath = path.resolve(__dirname, relBasePkg);
const outPath = path.resolve(__dirname, relOutPkg);
const basePkg = JSON.parse(fs.readFileSync(basePath));
2020-04-26 20:33:39 +00:00
const app = appConfig();
const appPkg = {
2020-04-26 20:33:39 +00:00
name: app.name,
version: app.version,
2020-04-26 20:33:39 +00:00
productName: app.productName,
longName: app.longName,
description: basePkg.description,
2017-11-26 23:05:44 +00:00
license: basePkg.license,
homepage: basePkg.homepage,
author: basePkg.author,
2020-04-26 20:33:39 +00:00
copyright: `Copyright © ${new Date().getFullYear()} ${basePkg.author}`,
main: 'main.min.js',
dependencies: {},
};
2020-04-26 20:33:39 +00:00
console.log(`[build] Generated build config for ${appPkg.name} ${appPkg.version}`);
for (const key of Object.keys(appPkg)) {
if (key === undefined) {
throw new Error(`[build] missing "app.${key}" from package.json`);
}
}
// Figure out which dependencies to pack
const allDependencies = Object.keys(basePkg.dependencies);
const packedDependencies = basePkg.packedDependencies;
2018-10-17 16:42:33 +00:00
const unpackedDependencies = allDependencies.filter(name => !packedDependencies.includes(name));
// Add dependencies
2020-04-26 20:33:39 +00:00
console.log(`[build] Adding ${unpackedDependencies.length} node 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;
}
fs.writeFileSync(outPath, JSON.stringify(appPkg, null, 2));
}