Skip to content

Commit 63477f8

Browse files
committed
Remove Q as a dependency
1 parent 97cc3b1 commit 63477f8

14 files changed

+87
-130
lines changed

lib/git.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const cp = require('child_process');
22
const path = require('path');
33
const util = require('util');
44
const fse = require('fs-extra');
5-
const Q = require('q');
65

76
let git = 'git';
87

@@ -25,9 +24,8 @@ util.inherits(ProcessError, Error);
2524
* Execute a git command.
2625
* @param {Array.<string>} args Arguments (e.g. ['remote', 'update']).
2726
* @param {string} cwd Repository directory.
28-
* @return {Promise} A promise. The promise will be resolved with the exit code
29-
* or rejected with an error. To get stdout, use a progress listener (e.g.
30-
* `promise.progress(function(chunk) {console.log(String(chunk);}))`).
27+
* @return {Promise} A promise. The promise will be resolved with stdout as a string
28+
* or rejected with an error.
3129
*/
3230
exports = module.exports = function(args, cwd) {
3331
return spawn(git, args, cwd);
@@ -49,24 +47,26 @@ exports.exe = function(exe) {
4947
* @return {Promise} A promise.
5048
*/
5149
function spawn(exe, args, cwd) {
52-
const deferred = Q.defer();
53-
const child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
54-
const buffer = [];
55-
child.stderr.on('data', chunk => {
56-
buffer.push(chunk.toString());
57-
});
58-
child.stdout.on('data', chunk => {
59-
deferred.notify(chunk);
60-
});
61-
child.on('close', code => {
62-
if (code) {
63-
const msg = buffer.join('') || `Process failed: ${code}`;
64-
deferred.reject(new ProcessError(code, msg));
65-
} else {
66-
deferred.resolve(code);
67-
}
50+
const promise = new Promise((resolve, reject) => {
51+
const child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
52+
const stderrBuffer = [];
53+
const stdoutBuffer = [];
54+
child.stderr.on('data', chunk => {
55+
stderrBuffer.push(chunk.toString());
56+
});
57+
child.stdout.on('data', chunk => {
58+
stdoutBuffer.push(chunk.toString());
59+
});
60+
child.on('close', code => {
61+
if (code) {
62+
const msg = stderrBuffer.join('') || `Process failed: ${code}`;
63+
reject(new ProcessError(code, msg));
64+
} else {
65+
resolve(stdoutBuffer.join(''));
66+
}
67+
});
6868
});
69-
return deferred.promise;
69+
return promise;
7070
}
7171

7272
/**
@@ -96,7 +96,7 @@ exports.clone = function clone(repo, dir, branch, options) {
9696
if (options.depth) {
9797
args.push('--depth', options.depth);
9898
}
99-
return spawn(git, args).fail(err => {
99+
return spawn(git, args).catch(err => {
100100
// try again without branch options
101101
return spawn(git, ['clone', repo, dir]);
102102
});
@@ -197,7 +197,7 @@ exports.commit = function commit(message, cwd) {
197197
// nothing to commit
198198
return Promise.resolve();
199199
})
200-
.fail(() => {
200+
.catch(() => {
201201
return spawn(git, ['commit', '-m', message], cwd);
202202
});
203203
};

lib/util.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const path = require('path');
22
const async = require('async');
33
const fs = require('graceful-fs');
4-
const Q = require('q');
54

65
/**
76
* Generate a list of unique directory paths given a list of file paths.
@@ -126,32 +125,32 @@ function makeDir(path, callback) {
126125
* @return {Promise} A promise.
127126
*/
128127
exports.copy = function(files, base, dest) {
129-
const deferred = Q.defer();
130-
131-
const pairs = [];
132-
const destFiles = [];
133-
files.forEach(file => {
134-
const src = path.resolve(base, file);
135-
const relative = path.relative(base, src);
136-
const target = path.join(dest, relative);
137-
pairs.push({
138-
src,
139-
dest: target
128+
const promise = new Promise((resolve, reject) => {
129+
const pairs = [];
130+
const destFiles = [];
131+
files.forEach(file => {
132+
const src = path.resolve(base, file);
133+
const relative = path.relative(base, src);
134+
const target = path.join(dest, relative);
135+
pairs.push({
136+
src,
137+
dest: target
138+
});
139+
destFiles.push(target);
140140
});
141-
destFiles.push(target);
142-
});
143141

144-
async.eachSeries(dirsToCreate(destFiles), makeDir, err => {
145-
if (err) {
146-
return deferred.reject(err);
147-
}
148-
async.each(pairs, copyFile, err => {
142+
async.eachSeries(dirsToCreate(destFiles), makeDir, err => {
149143
if (err) {
150-
return deferred.reject(err);
144+
return reject(err);
151145
}
152-
return deferred.resolve();
146+
async.each(pairs, copyFile, err => {
147+
if (err) {
148+
return reject(err);
149+
}
150+
return resolve();
151+
});
153152
});
154153
});
155154

156-
return deferred.promise;
155+
return promise;
157156
};

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"async": "^3.2.0",
2323
"fs-extra": "^8.1.0",
2424
"graceful-fs": "^4.2.3",
25-
"q": "^1.5.1",
2625
"url-safe": "^2.0.0"
2726
},
2827
"devDependencies": {

tasks/gh-pages.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const path = require('path');
22
const fse = require('fs-extra');
3-
const Q = require('q');
43
const urlSafe = require('url-safe');
54
const copy = require('../lib/util').copy;
65
const git = require('../lib/git');
@@ -11,22 +10,17 @@ function getCacheDir() {
1110
}
1211

1312
function getRemoteUrl(dir, remote) {
14-
let repo;
1513
return git(['config', '--get', `remote.${remote}.url`], dir)
16-
.progress(chunk => {
17-
repo = String(chunk)
18-
.split(/[\n\r]/)
19-
.shift();
20-
})
21-
.then(() => {
14+
.then(data => {
15+
const repo = data.split(/[\n\r]/).shift();
2216
if (repo) {
2317
return Promise.resolve(repo);
2418
}
2519
return Promise.reject(
2620
new Error('Failed to get repo URL from options or current directory.')
2721
);
2822
})
29-
.fail(err => {
23+
.catch(err => {
3024
return Promise.reject(
3125
new Error(
3226
'Failed to get remote.origin.url (task must either be run in a ' +
@@ -189,21 +183,22 @@ module.exports = function(grunt) {
189183
.then(() => {
190184
if (options.tag) {
191185
log('Tagging');
192-
const deferred = Q.defer();
193-
git
194-
.tag(options.tag, options.clone)
195-
.then(() => {
196-
return deferred.resolve();
197-
})
198-
.fail(error => {
199-
// tagging failed probably because this tag alredy exists
200-
log('Tagging failed, continuing');
201-
grunt.log.debug(error);
202-
return deferred.resolve();
203-
});
204-
return deferred.promise;
186+
const promise = new Promise((resolve, reject) => {
187+
git
188+
.tag(options.tag, options.clone)
189+
.then(() => {
190+
return resolve();
191+
})
192+
.catch(error => {
193+
// tagging failed probably because this tag alredy exists
194+
log('Tagging failed, continuing');
195+
grunt.log.debug(error);
196+
return resolve();
197+
});
198+
});
199+
return promise;
205200
}
206-
return Q.resolve();
201+
return Promise.resolve();
207202
})
208203
.then(() => {
209204
if (options.push) {
@@ -223,9 +218,6 @@ module.exports = function(grunt) {
223218
);
224219
}
225220
done(error);
226-
},
227-
progress => {
228-
grunt.verbose.writeln(progress);
229221
}
230222
);
231223
});

test/add.spec.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ describe('add', () => {
4141
});
4242

4343
it('creates a gh-pages branch', done => {
44-
let branch;
4544
helper
4645
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo1)
47-
.progress(chunk => {
48-
branch = String(chunk);
49-
})
50-
.then(() => {
46+
.then(branch => {
5147
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
5248
done();
5349
})
54-
.fail(done);
50+
.catch(done);
5551
});
5652

5753
it('copies source files relative to the base', () => {
@@ -71,7 +67,7 @@ describe('add', () => {
7167
.then(() => {
7268
done();
7369
})
74-
.fail(done);
70+
.catch(done);
7571
});
7672

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

9187
it('creates a gh-pages branch', done => {
92-
let branch;
9388
helper
9489
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo2)
95-
.progress(chunk => {
96-
branch = String(chunk);
97-
})
98-
.then(() => {
90+
.then(branch => {
9991
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
10092
done();
10193
})
102-
.fail(done);
94+
.catch(done);
10395
});
10496

10597
it('overwrites, but does not remove existing', () => {
@@ -123,6 +115,6 @@ describe('add', () => {
123115
.then(() => {
124116
done();
125117
})
126-
.fail(done);
118+
.catch(done);
127119
});
128120
});

test/custom-clone-dir.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ describe('custom-clone-dir', () => {
4141
.then(() => {
4242
done();
4343
})
44-
.fail(done);
44+
.catch(done);
4545
});
4646
});

test/deep-base.spec.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@ describe('deep-base', () => {
3434
});
3535

3636
it('creates a gh-pages branch', done => {
37-
let branch;
3837
helper
3938
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo)
40-
.progress(chunk => {
41-
branch = String(chunk);
42-
})
43-
.then(() => {
39+
.then(branch => {
4440
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
4541
done();
4642
})
47-
.fail(done);
43+
.catch(done);
4844
});
4945

5046
it('copies source files relative to the base', done => {
@@ -63,6 +59,6 @@ describe('deep-base', () => {
6359
.then(() => {
6460
done();
6561
})
66-
.fail(done);
62+
.catch(done);
6763
});
6864
});

test/deep-clone-dir.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ describe('deep-clone-dir', () => {
4141
.then(() => {
4242
done();
4343
})
44-
.fail(done);
44+
.catch(done);
4545
});
4646
});

test/different-repo.spec.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@ describe('different-repo', () => {
3434
});
3535

3636
it('creates a gh-pages branch', done => {
37-
let branch;
3837
helper
3938
.git(['rev-parse', '--abbrev-ref', 'HEAD'], repo)
40-
.progress(chunk => {
41-
branch = String(chunk);
42-
})
43-
.then(() => {
39+
.then(branch => {
4440
assert.strictEqual(branch, 'gh-pages\n', 'branch created');
4541
done();
4642
})
47-
.fail(done);
43+
.catch(done);
4844
});
4945

5046
it('copies source files', done => {
@@ -74,6 +70,6 @@ describe('different-repo', () => {
7470
.then(() => {
7571
done();
7672
})
77-
.fail(done);
73+
.catch(done);
7874
});
7975
});

0 commit comments

Comments
 (0)