cdbrelease/fromgit.js
2020-03-27 18:00:32 -04:00

93 lines
2.1 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);
await require('./loadmeta')(config.path ?
path.join(outdir, config.path) : outdir,
outdir);
} finally {
await fsx.emptyDir(tmpdir.path);
await tmpdir.cleanup();
}
};