Skip to content

Commit

Permalink
Merge pull request #88 from tschaub/updates
Browse files Browse the repository at this point in the history
Assorted updates
  • Loading branch information
tschaub authored Apr 16, 2020
2 parents 82f6c75 + 44db3ad commit ac63890
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 1,658 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
name: Tests
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- master

env:
CI: true

Expand All @@ -11,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [6, 8, 10, 12]
node: [8, 10, 12]

steps:
- name: Clone repository
Expand All @@ -26,7 +33,7 @@ jobs:
- run: npm --version

- name: Install npm dependencies
run: npm install # switch to `ci` when Node.js 6.x is dropped
run: npm ci

- name: Run tests
run: npm test
59 changes: 29 additions & 30 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const cp = require('child_process');
const path = require('path');
const util = require('util');

const Q = require('q');
const fs = require('q-io/fs');
const fse = require('fs-extra');

let git = 'git';

Expand All @@ -26,9 +24,8 @@ util.inherits(ProcessError, Error);
* Execute a git command.
* @param {Array.<string>} args Arguments (e.g. ['remote', 'update']).
* @param {string} cwd Repository directory.
* @return {Promise} A promise. The promise will be resolved with the exit code
* or rejected with an error. To get stdout, use a progress listener (e.g.
* `promise.progress(function(chunk) {console.log(String(chunk);}))`).
* @return {Promise} A promise. The promise will be resolved with stdout as a string
* or rejected with an error.
*/
exports = module.exports = function(args, cwd) {
return spawn(git, args, cwd);
Expand All @@ -50,24 +47,26 @@ exports.exe = function(exe) {
* @return {Promise} A promise.
*/
function spawn(exe, args, cwd) {
const deferred = Q.defer();
const child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
const buffer = [];
child.stderr.on('data', chunk => {
buffer.push(chunk.toString());
});
child.stdout.on('data', chunk => {
deferred.notify(chunk);
});
child.on('close', code => {
if (code) {
const msg = buffer.join('') || `Process failed: ${code}`;
deferred.reject(new ProcessError(code, msg));
} else {
deferred.resolve(code);
}
const promise = new Promise((resolve, reject) => {
const child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
const stderrBuffer = [];
const stdoutBuffer = [];
child.stderr.on('data', chunk => {
stderrBuffer.push(chunk.toString());
});
child.stdout.on('data', chunk => {
stdoutBuffer.push(chunk.toString());
});
child.on('close', code => {
if (code) {
const msg = stderrBuffer.join('') || `Process failed: ${code}`;
reject(new ProcessError(code, msg));
} else {
resolve(stdoutBuffer.join(''));
}
});
});
return deferred.promise;
return promise;
}

/**
Expand All @@ -88,16 +87,16 @@ exports.init = function init(cwd) {
* @return {Promise} A promise.
*/
exports.clone = function clone(repo, dir, branch, options) {
return fs.exists(dir).then(exists => {
return fse.pathExists(dir).then(exists => {
if (exists) {
return Q.resolve();
return Promise.resolve();
}
return fs.makeTree(path.dirname(path.resolve(dir))).then(() => {
return fse.ensureDir(path.dirname(path.resolve(dir))).then(() => {
const args = ['clone', repo, dir, '--branch', branch, '--single-branch'];
if (options.depth) {
args.push('--depth', options.depth);
}
return spawn(git, args).fail(err => {
return spawn(git, args).catch(err => {
// try again without branch options
return spawn(git, ['clone', repo, dir]);
});
Expand Down Expand Up @@ -161,7 +160,7 @@ exports.checkout = function checkout(remote, branch, cwd) {
return spawn(git, ['checkout', '--orphan', branch], cwd);
}
// unhandled error
return Q.reject(error);
return Promise.reject(error);
}
);
};
Expand Down Expand Up @@ -196,9 +195,9 @@ exports.commit = function commit(message, cwd) {
return spawn(git, ['diff-index', '--quiet', 'HEAD', '.'], cwd)
.then(() => {
// nothing to commit
return Q.resolve();
return Promise.resolve();
})
.fail(() => {
.catch(() => {
return spawn(git, ['commit', '-m', message], cwd);
});
};
Expand Down
43 changes: 21 additions & 22 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path');
const async = require('async');
const fs = require('graceful-fs');
const Q = require('q');

/**
* Generate a list of unique directory paths given a list of file paths.
Expand Down Expand Up @@ -126,32 +125,32 @@ function makeDir(path, callback) {
* @return {Promise} A promise.
*/
exports.copy = function(files, base, dest) {
const deferred = Q.defer();

const pairs = [];
const destFiles = [];
files.forEach(file => {
const src = path.resolve(base, file);
const relative = path.relative(base, src);
const target = path.join(dest, relative);
pairs.push({
src,
dest: target
const promise = new Promise((resolve, reject) => {
const pairs = [];
const destFiles = [];
files.forEach(file => {
const src = path.resolve(base, file);
const relative = path.relative(base, src);
const target = path.join(dest, relative);
pairs.push({
src,
dest: target
});
destFiles.push(target);
});
destFiles.push(target);
});

async.eachSeries(dirsToCreate(destFiles), makeDir, err => {
if (err) {
return deferred.reject(err);
}
async.each(pairs, copyFile, err => {
async.eachSeries(dirsToCreate(destFiles), makeDir, err => {
if (err) {
return deferred.reject(err);
return reject(err);
}
return deferred.resolve();
async.each(pairs, copyFile, err => {
if (err) {
return reject(err);
}
return resolve();
});
});
});

return deferred.promise;
return promise;
};
Loading

0 comments on commit ac63890

Please sign in to comment.