Add capture() to spawn, add rsync itemize parser
This commit is contained in:
33
lib/spawn.js
33
lib/spawn.js
@@ -4,17 +4,14 @@
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
/**
|
||||
* Spawn a process and stream its output.
|
||||
* Spawn a process and stream its output to stdout/stderr.
|
||||
* @param {string} cmd
|
||||
* @param {string[]} args
|
||||
* @param {{ dryRun?: boolean, label?: string }} opts
|
||||
* @param {{ dryRun?: boolean }} opts
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function run(cmd, args, { dryRun = false, label } = {}) {
|
||||
const display = [cmd, ...args].join(' ');
|
||||
if (label) console.log(`[${label}] ${display}`);
|
||||
else console.log(`$ ${display}`);
|
||||
|
||||
export async function run(cmd, args, { dryRun = false } = {}) {
|
||||
console.log(`$ ${[cmd, ...args].join(' ')}`);
|
||||
if (dryRun) return;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -26,3 +23,25 @@ export async function run(cmd, args, { dryRun = false, label } = {}) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn a process and capture stdout as a string.
|
||||
* stderr is inherited (shown to user). Never used in dry-run context.
|
||||
* @param {string} cmd
|
||||
* @param {string[]} args
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export async function capture(cmd, args) {
|
||||
console.log(`$ ${[cmd, ...args].join(' ')}`);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(cmd, args, { stdio: ['inherit', 'pipe', 'inherit'] });
|
||||
const chunks = [];
|
||||
child.stdout.on('data', chunk => chunks.push(chunk));
|
||||
child.on('error', reject);
|
||||
child.on('close', code => {
|
||||
if (code === 0) resolve(Buffer.concat(chunks).toString('utf8'));
|
||||
else reject(new Error(`${cmd} exited with code ${code}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user