Skip to content

Commit

Permalink
Remove Q as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Apr 16, 2020
1 parent 97cc3b1 commit 63477f8
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 130 deletions.
46 changes: 23 additions & 23 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const cp = require('child_process');
const path = require('path');
const util = require('util');
const fse = require('fs-extra');
const Q = require('q');

let git = 'git';

Expand All @@ -25,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 @@ -49,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 Down Expand Up @@ -96,7 +96,7 @@ exports.clone = function clone(repo, dir, branch, options) {
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 @@ -197,7 +197,7 @@ exports.commit = function commit(message, cwd) {
// nothing to commit
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;
};
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"async": "^3.2.0",
"fs-extra": "^8.1.0",
"graceful-fs": "^4.2.3",
"q": "^1.5.1",
"url-safe": "^2.0.0"
},
"devDependencies": {
Expand Down
44 changes: 18 additions & 26 deletions tasks/gh-pages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require('path');
const fse = require('fs-extra');
const Q = require('q');
const urlSafe = require('url-safe');
const copy = require('../lib/util').copy;
const git = require('../lib/git');
Expand All @@ -11,22 +10,17 @@ function getCacheDir() {
}

function getRemoteUrl(dir, remote) {
let repo;
return git(['config', '--get', `remote.${remote}.url`], dir)
.progress(chunk => {
repo = String(chunk)
.split(/[\n\r]/)
.shift();
})
.then(() => {
.then(data => {
const repo = data.split(/[\n\r]/).shift();
if (repo) {
return Promise.resolve(repo);
}
return Promise.reject(
new Error('Failed to get repo URL from options or current directory.')
);
})
.fail(err => {
.catch(err => {
return Promise.reject(
new Error(
'Failed to get remote.origin.url (task must either be run in a ' +
Expand Down Expand Up @@ -189,21 +183,22 @@ module.exports = function(grunt) {
.then(() => {
if (options.tag) {
log('Tagging');
const deferred = Q.defer();
git
.tag(options.tag, options.clone)
.then(() => {
return deferred.resolve();
})
.fail(error => {
// tagging failed probably because this tag alredy exists
log('Tagging failed, continuing');
grunt.log.debug(error);
return deferred.resolve();
});
return deferred.promise;
const promise = new Promise((resolve, reject) => {
git
.tag(options.tag, options.clone)
.then(() => {
return resolve();
})
.catch(error => {
// tagging failed probably because this tag alredy exists
log('Tagging failed, continuing');
grunt.log.debug(error);
return resolve();
});
});
return promise;
}
return Q.resolve();
return Promise.resolve();
})
.then(() => {
if (options.push) {
Expand All @@ -223,9 +218,6 @@ module.exports = function(grunt) {
);
}
done(error);
},
progress => {
grunt.verbose.writeln(progress);
}
);
});
Expand Down
20 changes: 6 additions & 14 deletions test/add.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ describe('add', () => {
});

it('creates a gh-pages branch', done => {
let branch;
helper
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo1)
.progress(chunk => {
branch = String(chunk);
})
.then(() => {
.then(branch => {
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
done();
})
.fail(done);
.catch(done);
});

it('copies source files relative to the base', () => {
Expand All @@ -71,7 +67,7 @@ describe('add', () => {
.then(() => {
done();
})
.fail(done);
.catch(done);
});

/**
Expand All @@ -89,17 +85,13 @@ describe('add', () => {
});

it('creates a gh-pages branch', done => {
let branch;
helper
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo2)
.progress(chunk => {
branch = String(chunk);
})
.then(() => {
.then(branch => {
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
done();
})
.fail(done);
.catch(done);
});

it('overwrites, but does not remove existing', () => {
Expand All @@ -123,6 +115,6 @@ describe('add', () => {
.then(() => {
done();
})
.fail(done);
.catch(done);
});
});
2 changes: 1 addition & 1 deletion test/custom-clone-dir.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ describe('custom-clone-dir', () => {
.then(() => {
done();
})
.fail(done);
.catch(done);
});
});
10 changes: 3 additions & 7 deletions test/deep-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ describe('deep-base', () => {
});

it('creates a gh-pages branch', done => {
let branch;
helper
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo)
.progress(chunk => {
branch = String(chunk);
})
.then(() => {
.then(branch => {
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
done();
})
.fail(done);
.catch(done);
});

it('copies source files relative to the base', done => {
Expand All @@ -63,6 +59,6 @@ describe('deep-base', () => {
.then(() => {
done();
})
.fail(done);
.catch(done);
});
});
2 changes: 1 addition & 1 deletion test/deep-clone-dir.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ describe('deep-clone-dir', () => {
.then(() => {
done();
})
.fail(done);
.catch(done);
});
});
10 changes: 3 additions & 7 deletions test/different-repo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ describe('different-repo', () => {
});

it('creates a gh-pages branch', done => {
let branch;
helper
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo)
.progress(chunk => {
branch = String(chunk);
})
.then(() => {
.then(branch => {
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
done();
})
.fail(done);
.catch(done);
});

it('copies source files', done => {
Expand Down Expand Up @@ -74,6 +70,6 @@ describe('different-repo', () => {
.then(() => {
done();
})
.fail(done);
.catch(done);
});
});
Loading

0 comments on commit 63477f8

Please sign in to comment.