-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.js
executable file
·65 lines (58 loc) · 2.55 KB
/
build.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
#!/usr/bin/env babel-node
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
export const HTERM_REPO = 'https://chromium.googlesource.com/apps/libapps';
export const HTERM_BRANCH = 'master';
export const OUTFILE = 'dist/index.js';
export const TMPDIR = path.resolve(__dirname, 'tmp');
export function buildHterm(repo, branch, outfile, tmpdir = TMPDIR) {
const gitargs = `--work-tree="${tmpdir}" --git-dir="${tmpdir}/.git"`;
execSync(`mkdir -p ${tmpdir}`);
execSync(`git clone ${repo} ${tmpdir}`);
execSync(`git ${gitargs} checkout ${branch}`);
execSync(`${tmpdir}/hterm/bin/mkdist.sh`);
// modified version of https://github.com/umdjs/umd/blob/95563fd6b46f06bda0af143ff67292e7f6ede6b7/templates/returnExportsGlobal.js
const htermEncoding = 'utf8';
fs.writeFileSync(path.join(__dirname, outfile), `
(function (root, factory) {
var GLOBAL_NAME = '_htermExports';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports'], function (exports) {
root[GLOBAL_NAME] = factory(exports);
});
} else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
// CommonJS
factory(exports);
} else {
// Browser globals
root[GLOBAL_NAME] = factory({});
}
}(this, function (exports) {
${/* libdot */fs.readFileSync(`${tmpdir}/hterm/dist/js/hterm_all.js`, htermEncoding).replace('lib.ensureRuntimeDependencies_();', '')}
exports.lib = lib;
${/* hterm */fs.readFileSync(`${tmpdir}/hterm/dist/js/hterm.js`, htermEncoding)}
exports.hterm = hterm;
}));
`.replace(/^\s+/, '').replace(/\s+$/, '\n'));
const [, htermVersion] = fs.readFileSync(`${tmpdir}/hterm/doc/ChangeLog.md`).toString().match(/([\d.]+)/) || [];
const htermRev = execSync(`git ${gitargs} rev-parse HEAD`).toString().trim();
execSync(`rm -rf ${tmpdir}`);
return {
version: htermVersion,
rev: htermRev,
};
}
export function updateVersion(packageJSONPath, htermVersion, htermRev) {
const pkg = JSON.parse(fs.readFileSync(packageJSONPath));
pkg.version = `${pkg.version.replace(/\+.*$/, '')}+${htermVersion ? `${htermVersion}.sha.` : ''}${htermRev.slice(0, 7)}`;
fs.writeFileSync(packageJSONPath, `${JSON.stringify(pkg, null, ' ')}\n`);
return pkg.version;
}
if (require.main === module) {
const hterm = buildHterm(HTERM_REPO, HTERM_BRANCH, OUTFILE);
console.log(`built ${OUTFILE}`); // eslint-disable-line no-console
const version = updateVersion('package.json', hterm.version, hterm.rev);
console.log(version); // eslint-disable-line no-console
}