cdbrelease/fromgit.js
Aaron Suen 1f0ae91cf3 New layered config system.
This allows --fromgit to override built-in defaults (e.g. setting
min/max MT version) but not explicit cmdline settings.
2019-03-31 09:47:16 -04:00

79 lines
2.0 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const util = require('util');
const child = require('child_process');
const tmp = require('tmp-promise');
const fsx = require('fs-extra');
const config = require('./config');
const readFile = util.promisify(fs.readFile);
function spawn(path, params, ...args) {
console.log(`> ${path} ${params.join(' ')}`);
const proc = child.spawn(path, params, ...args);
proc.promise = new Promise((res, rejraw) => {
function rej(e) { rejraw(`${path} ${params.join(' ')}: ${e}`); }
proc.on('error', rej);
proc.on('exit', (code, sig) => {
if(sig) rej('signal: ' + sig);
if(code !== 0) rej('code: ' + code);
res();
});
});
return proc;
}
module.exports = async () => {
const luahook = (await readFile('luahook.lua')).toString();
const tmpdir = await tmp.dir({mode: 0700, prefix: 'cdbrelease-'});
try {
const git = config.execgit || 'git';
const tar = config.exectar || 'tar';
const lua = config.execlua || 'lua5.1';
const repodir = path.join(tmpdir.path, 'repo');
await spawn(git, [
'clone',
'--bare',
'--depth=1',
'-b', config.branch,
config.fromgit,
repodir
], {stdio: 'inherit'}).promise;
const outdir = path.join(tmpdir.path, 'export');
await fsx.emptyDir(outdir);
const garch = spawn(git, [
'--git-dir=' + repodir,
'archive',
'--format=tar',
config.branch
], {stdio: ['inherit', 'pipe', 'inherit']});
const untar = spawn(tar, [
'-C', outdir,
'-xf', '-'
], {stdio: ['pipe', 'inherit', 'inherit']});
garch.stdout.pipe(untar.stdin);
await garch.promise;
await untar.promise;
let buff = '';
const hook = spawn(lua, ['-'], {
stdio: ['pipe', 'pipe', 'inherit'],
cwd: outdir
});
hook.stdout.on('data', x => buff += x.toString());
hook.stdin.write(luahook);
hook.stdin.end();
await hook.promise;
const spec = JSON.parse(buff);
config.set('fromgit', spec);
}
finally {
await fsx.emptyDir(tmpdir.path);
await tmpdir.cleanup();
}
};