gauge-based progress wrapper for spawn

This commit is contained in:
igorklopov 2016-08-16 17:41:33 +03:00
parent 2349363917
commit 3d4df1dfef
4 changed files with 39 additions and 35 deletions

View File

@ -1,9 +1,9 @@
import { mkdirp, remove } from 'fs-promise';
import { progress, spawn } from './spawn.js';
import { copyFile } from './copy-file.js';
import log from './log.js';
import patchesJson from '../patches/patches.json';
import path from 'path';
import { spawn } from './spawn.js';
import { tempPath } from './temp-path.js';
const windows = process.platform === 'win32';
@ -13,27 +13,17 @@ const nodePath = path.join(buildPath, 'node');
const patchesPath = path.resolve(__dirname, '../patches');
const nodeRepo = 'https://github.com/nodejs/node';
const gitCloneThresholds = {
' 0% ': 0, ' 1% ': 1, ' 5% ': 5, ' 10% ': 10, // eslint-disable-line key-spacing
' 20% ': 20, ' 40% ': 40, ' 60% ': 60, ' 80% ': 80
};
async function gitClone () {
log.info('Cloning Node.js from GitHub');
const args = [ 'clone', '--progress', nodeRepo, 'node' ];
const promise = spawn('git', args, { cwd: buildPath });
const { child } = promise;
const gauge = log.newItem('', 100);
gauge.enableProgress();
gauge.info('Cloning Node.js from GitHub');
try {
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
if (!(/^Receiving objects/.test(data))) return;
const d = data.indexOf('%');
const p = data.slice(d - 3, d) | 0;
gauge.name = p + '%';
gauge.completeWork(p - gauge.workDone);
});
await promise;
} finally {
gauge.disableProgress();
}
progress(promise.child, gitCloneThresholds);
await promise;
}
async function compileOnWindows (targetArch) {

View File

@ -2,7 +2,6 @@ import { createRelease, downloadAsset,
getRelease, uploadAsset } from './github.js';
import { mkdirp, remove } from 'fs-promise';
import assert from 'assert';
import log from './log.js';
import { moveFile } from './copy-file.js';
import path from 'path';
import { version } from '../package.json';
@ -43,19 +42,7 @@ export async function download (remote, local) {
const asset = assets[0];
const tempFile = local + '.downloading';
await mkdirp(path.dirname(tempFile));
const gauge = log.newItem('', 1);
gauge.enableProgress();
try {
await downloadAsset(asset, tempFile, (state) => {
const p = state.percentage;
gauge.name = (p * 100 | 0) + '%';
gauge.completeWork(p - gauge.workDone);
});
} finally {
gauge.disableProgress();
}
await downloadAsset(asset, tempFile);
await moveFile(tempFile, local);
await remove(tempFile);
return true;

View File

@ -2,6 +2,7 @@
import assert from 'assert';
import fs from 'fs';
import log from './log.js';
import progress from 'request-progress';
import request from 'request';
@ -74,16 +75,23 @@ export function uploadAsset (file, release, name) {
});
}
export function downloadAsset (asset, file, onProgress) {
export function downloadAsset (asset, file) {
const gauge = log.newItem('', 1);
gauge.enableProgress();
return new Promise((resolve, reject) => {
const headers = { Accept: 'application/octet-stream' };
const ws = fs.createWriteStream(file);
const url = asset.url;
const req = progress(request2.get(url, { headers }, (error, response) => {
gauge.disableProgress();
if (error) return reject(error);
resolve(response);
}));
req.on('progress', onProgress);
req.on('progress', (state) => {
const p = state.percentage;
gauge.name = (p * 100 | 0) + '%';
gauge.completeWork(p - gauge.workDone);
});
req.pipe(ws);
});
}

View File

@ -1,4 +1,5 @@
import chipo from 'child_process';
import log from './log.js';
export function spawn (cmd, args, opts) {
const child = chipo.spawn(cmd, args, opts);
@ -12,3 +13,21 @@ export function spawn (cmd, args, opts) {
promise.child = child;
return promise;
}
export function progress (child, thresholds) {
const gauge = log.newItem('', 100);
gauge.enableProgress();
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
for (const key in thresholds) {
if (data.indexOf(key) >= 0) {
const p = thresholds[key];
gauge.name = p + '%';
gauge.completeWork(p - gauge.workDone);
}
}
});
child.on('close', () => {
gauge.disableProgress();
});
}