git-spawned-promise v0.1.1
git-spawned-promise
Promisify a git child process, settling once the process exit and its stdio streams and transformers close.
Usage
Operations can ignore stdout (run(...cmd) method), can return it (get(...cmd)method) or can split it while transforming each element (array(...cmd, {sep, map})).
In any case, they capture stderr and reject errors when the exit codes are not zero, which will including sdterr in their messages and as a property:
const gitSpawnedPromise = require('../');
async function init(path = '.') {
const git = gitPromise();
// `run()`` ignores stdout.
await git.run('init', path);
await git.run('commit', '--allow-empty', '--message', 'chore: first commit');
await git.run('commit', '--allow-empty', '--message', 'feat: first feature');
await git.run('commit', '--allow-empty', '--message', 'fix: first fix');
await git.run('commit', '--allow-empty', '--message', 'feat: second feature');
const log = ['log', '--format="%s"', '--no-merges', '--grep', 'feat', 'HEAD'];
const map = msg => msg.slice(5).trim();
const [hash, feats] = await Promise.all([
// `get()` resolves with stdout
git.get('parse-rev', 'HEAD'),
// `array` splits stdout stream and pipe transform stream.
git.array(...log, {map, sep: '\n'})
]);
console.log(`Status up to ${hash.slice(0, 7)}...`);
console.log(feats.map(title => ` ${title}`).join('\n'));
}
// stderr available (when the error is a git error).
init(repo)
.catch(err => console.log(`${err.stderr} (${err.code})`));API
gitSpawnedPromise:function({gitDir: ?string}): GitClientBound a
GitClientto a local repository.GitClient:function(cmd: string[], ?options: ClientOption): Promise<any,Error>Run the git command and settle once the git child process exit and the stdout/stderr pipe closes, or when either fail.
ClientOption:{ignore: ?boolean, sep: ?string, map: mapper[]}ignore: ignore stdout when set totrue(falseby default).sep: separator to split the stream (default to\nif a mapper is provided).map: when splitting a stream, apply each element and replace the returned value. If the value is a promise it will resolve it.
if
sepormapare set, thegitCLientPromisewill resolve to anArray.Mapper: function(item: any): anyA handler for a
stream.Transformerforstdout. Unlike regular handler async values are passed via aPromiseinstead of a callback function, and returningnull(orPromise<null>)does not close the stream; the Transformer would just skip the value.If the mapper throws or reject, the
GitClientreturnedPromisewill reject with that error.GitClient.run:function(...cmd: string[]): Promise<void,GitError>gitClient.run(...cmd)is equivalent togitClient(cmd, {capture: true}).GitClient.get:function(...cmd: string[]): Promise<string,GitError>gitClient.get(...cmd)is equivalent togitClient(cmd).GitClient.array:function(...cmd: string[], ?options: ClientOption): Promise<any[],Error>gitClient.array(...cmd)is equivalent togitClient(cmd, {sep: '\n', map: someMapperOrMappperArray}).
Installation
using npm:
npm install git-spawned-promiseLicense
MIT License
Copyright (c) 2017 Damien Lebrun