Skip to content

Commit 842e0b3

Browse files
author
刘祺
committed
update eslint config & add JSDoc
1 parent 3f19d33 commit 842e0b3

30 files changed

+211
-160
lines changed

.eslintrc.json

+3-16
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,9 @@
4444
50
4545
],
4646
"new-cap": 0,
47-
"no-console": [
48-
"off",
49-
{
50-
"allow": [
51-
"warn",
52-
"error",
53-
"info",
54-
"dir",
55-
"time",
56-
"debug"
57-
]
58-
}
59-
],
47+
"no-console": "off",
6048
"no-extend-native": "error",
61-
"no-undef": "error",
6249
"no-unused-expressions": "error",
63-
"no-unused-vars": "off",
6450
"quotes": [
6551
"error",
6652
"single",
@@ -87,6 +73,7 @@
8773
"strict": [
8874
"error",
8975
"safe"
90-
]
76+
],
77+
"valid-jsdoc": "error"
9178
}
9279
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ results
1515
npm-debug.log
1616
node_modules
1717
*.sublime*
18+
docs/

examples/gulpfile.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ gulp.task('commitmultiline', function() {
6161
// Clone remote repo to current directory ($CWD/git-test)
6262
gulp.task('clone', function() {
6363
git.clone('https://github.com/stevelacy/git-test', function(err) {
64-
// handle err
64+
if (err) {
65+
console.error(err);
66+
}
6567
});
6668
});
6769

6870
// Clone remote repo to sub folder ($CWD/sub/folder/git-test)
6971
gulp.task('clonesub', function() {
7072
git.clone('https://github.com/stevelacy/git-test', {args: './sub/folder'}, function(err) {
71-
// handle err
73+
if (err) {
74+
console.error(err);
75+
}
7276
});
7377
});
7478

@@ -93,7 +97,9 @@ gulp.task('precommit', function() {
9397

9498
gulp.task('remote', function() {
9599
git.addRemote('origin', 'https://github.com/stevelacy/git-test', function (err) {
96-
// if (err) ...
100+
if (err) {
101+
console.error(err);
102+
}
97103
});
98104
});
99105

@@ -102,7 +108,9 @@ gulp.task('remote', function() {
102108

103109
gulp.task('push', function() {
104110
git.push('origin', 'master', function (err) {
105-
// if (err) ...
111+
if (err) {
112+
console.error(err);
113+
}
106114
});
107115
});
108116

@@ -143,20 +151,26 @@ gulp.task('pull-array', function() {
143151

144152
gulp.task('tag', function() {
145153
git.tag('v1.1.1', 'Version message', function (err) {
146-
// if (err) ...
154+
if (err) {
155+
console.error(err);
156+
}
147157
});
148158
});
149159

150160
// Tag the repo WITH signed key
151161
gulp.task('tagsec', function() {
152162
git.tag('v1.1.1', 'Version message with signed key', {signed:true}, function (err) {
153-
// if (err) ...
163+
if (err) {
164+
console.error(err);
165+
}
154166
});
155167
});
156168

157169
gulp.task('push-tag', function() {
158170
git.push('origin', 'v1.1.1', function (err) {
159-
// if (err) ...
171+
if (err) {
172+
console.error(err);
173+
}
160174
});
161175
});
162176

examples/test.js

-3
This file was deleted.

index.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
'use strict';
2-
2+
/**
3+
* git
4+
* @exports gulp-git
5+
* @property {function} add {@link module:gulp-git/lib/add}
6+
* @property {function} addRemote {@link module:gulp-git/lib/addRemote}
7+
* @property {function} addSubmodule {@link module:gulp-git/lib/addSubmodule}
8+
* @property {function} branch {@link module:gulp-git/lib/branch}
9+
* @property {function} catFile {@link module:gulp-git/lib/catFile}
10+
* @property {function} checkout {@link module:gulp-git/lib/checkout}
11+
* @property {function} checkoutFiles {@link module:gulp-git/lib/checkoutFiles}
12+
* @property {function} clean {@link module:gulp-git/lib/clean}
13+
* @property {function} clone {@link module:gulp-git/lib/clone}
14+
* @property {function} commit {@link module:gulp-git/lib/commit}
15+
* @property {function} diff {@link module:gulp-git/lib/diff}
16+
* @property {function} exec {@link module:gulp-git/lib/exec}
17+
* @property {function} fetch {@link module:gulp-git/lib/fetch}
18+
* @property {function} init {@link module:gulp-git/lib/init}
19+
* @property {function} merge {@link module:gulp-git/lib/merge}
20+
* @property {function} pull {@link module:gulp-git/lib/pull}
21+
* @property {function} push {@link module:gulp-git/lib/push}
22+
* @property {function} removeRemote {@link module:gulp-git/lib/removeRemote}
23+
* @property {function} reset {@link module:gulp-git/lib/reset}
24+
* @property {function} revParse {@link module:gulp-git/lib/revParse}
25+
* @property {function} rm {@link module:gulp-git/lib/rm}
26+
* @property {function} stash {@link module:gulp-git/lib/stash}
27+
* @property {function} status {@link module:gulp-git/lib/status}
28+
* @property {function} tag {@link module:gulp-git/lib/tag}
29+
* @property {function} updateSubmodule {@link module:gulp-git/lib/updateSubmodule}
30+
*/
331
var requireDir = require('require-dir');
432
module.exports = requireDir('./lib');

lib/catFile.js

+49-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
'use strict';
2+
/**
3+
* catFile
4+
* @module gulp-git/lib/catFile
5+
*/
26

37
var through = require('through2');
48
var gutil = require('gulp-util');
59
var spawn = require('child_process').spawn;
6-
var escape = require('any-shell-escape');
710
var stripBom = require('strip-bom-stream');
811

12+
/**
13+
* get a buffer.
14+
* @callback requestCallback
15+
* @param {buffer} buf
16+
*/
17+
18+
/**
19+
* Convert stream to buffer
20+
*
21+
* @param {Stream} stream stream that what to read
22+
* @param {readStreamCallback} callback function that receive buffer
23+
* @returns {void}
24+
*/
925
function readStream(stream, callback) {
1026
var buf;
1127
stream.on('data', function(data) {
@@ -22,26 +38,51 @@ function readStream(stream, callback) {
2238
});
2339
}
2440

41+
/**
42+
* @typedef {object} catFileOptions
43+
* @property {boolean} stripBOM {@link https://github.com/gulpjs/vinyl-fs#optionsstripbom}
44+
* @property {boolean} buffer {@link https://github.com/gulpjs/vinyl-fs#optionsbuffer}
45+
*/
46+
47+
/**
48+
* read vinyl file contents
49+
* @param {catFileOptions} opt [catFileOptions]{@link module:gulp-git/lib/catFile~catFileOptions}
50+
* @returns {stream} stream of vinyl `File` objects.
51+
*/
2552
module.exports = function (opt) {
2653
if (!opt) opt = {};
27-
// https://github.com/gulpjs/vinyl-fs#optionsstripbom
2854
if (undefined === opt.stripBOM || null === opt.stripBOM) opt.stripBOM = true;
29-
// https://github.com/gulpjs/vinyl-fs#optionsbuffer
3055
if (undefined === opt.buffer || null === opt.buffer) opt.buffer = true;
3156

57+
/**
58+
* transform function of stream {@link https://nodejs.org/docs/latest/api/stream.html#stream_transform_transform_chunk_encoding_callback}
59+
*
60+
* @param {vinyl} file The file to be transformed.
61+
* @param {any} enc encoding type.
62+
* @param {function} cb A callback function (optionally with an error argument and data) to be called after the supplied `file` has been processed.
63+
* @returns {void}
64+
*/
3265
var write = function(file, enc, cb) {
3366

3467
var hash = file.git && file.git.hash;
3568

36-
if (!hash || /^0+$/.test(hash)) {
37-
return sendFile();
38-
}
39-
69+
/**
70+
* set file contents and send file to stream
71+
*
72+
* @param {Buffer} contents file contents
73+
* @returns {void}
74+
*/
4075
var sendFile = function(contents) {
41-
file.contents = contents;
76+
if (contents) {
77+
file.contents = contents;
78+
}
4279
return cb(null, file);
4380
};
4481

82+
if (!hash || /^0+$/.test(hash)) {
83+
return sendFile();
84+
}
85+
4586
var catFile = spawn('git', ['cat-file', 'blob', hash], {
4687
cwd: file.cwd
4788
});

lib/diff.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict';
2+
/**
3+
* diff
4+
* @module gulp-git/lib/diff
5+
*/
26

37
var Vinyl = require('vinyl');
48
var through = require('through2');
59
var gutil = require('gulp-util');
610
var path = require('path');
711
var exec = require('child_process').exec;
8-
var escape = require('any-shell-escape');
912
var catFile = require('./catFile');
1013

1114
// https://git-scm.com/docs/git-diff#_raw_output_format
@@ -39,6 +42,31 @@ function getReaslt(data) {
3942
return result;
4043
}
4144

45+
/**
46+
* @typedef {Object} diffOptions
47+
* @property {string} cwd {@link https://github.com/gulpjs/vinyl-fs#optionscwd}
48+
* @property {string} base {@link https://github.com/gulpjs/vinyl-fs#optionsbase}
49+
* @property {boolean} read {@link https://github.com/gulpjs/vinyl-fs#optionsread}
50+
* @property {boolean} buffer {@link https://github.com/gulpjs/vinyl-fs#optionsbuffer}
51+
* @property {boolean} stripBOM {@link https://github.com/gulpjs/vinyl-fs#optionsstripbom}
52+
* @property {boolean} log show log in console
53+
* @property {string[]} args Command parameter for `git diff`
54+
*/
55+
56+
/**
57+
* get git diff result as a stream of vinyl `File` objects.
58+
*
59+
* @example
60+
const git = require('gulp-git');
61+
const eslint = require('gulp-eslint');
62+
git.diff('--cached', {
63+
args: '-- *.js'
64+
})
65+
.pipe(eslint())
66+
* @param {string} compare compare arg for `git diff`
67+
* @param {diffOptions} opt [diffOptions]{@link module:gulp-git/lib/diff~diffOptions}
68+
* @returns {stream} stream of vinyl `File` objects.
69+
*/
4270
module.exports = function (compare, opt) {
4371
if (!opt) opt = {};
4472
if (!opt.cwd) opt.cwd = process.cwd();
@@ -55,7 +83,7 @@ module.exports = function (compare, opt) {
5583
if (opt.args) {
5684
cmd += ' ' + opt.args;
5785
}
58-
exec('git diff --raw -z ' + cmd, {cwd: opt.cwd}, function(err, stdout, stderr) {
86+
exec('git diff --raw -z ' + cmd, {cwd: opt.cwd}, function(err, stdout) {
5987
if (err) return srcStream.emit('error', err);
6088
var files = getReaslt(stdout);
6189

lib/stash.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var gutil = require('gulp-util');
44
var exec = require('child_process').exec;
5-
var escape = require('any-shell-escape');
65

76
module.exports = function(opt, cb) {
87

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
"main": "./index.js",
1212
"dependencies": {
1313
"any-shell-escape": "^0.1.1",
14-
"gulp-util": "^3.0.6",
14+
"gulp-util": "^3.0.8",
1515
"require-dir": "^0.3.1",
1616
"strip-bom-stream": "^3.0.0",
1717
"through2": "^2.0.3",
1818
"vinyl": "^2.0.1"
1919
},
2020
"devDependencies": {
21-
"eslint": "^3.15.0",
21+
"eslint": "^3.17.0",
2222
"mocha": "^3.2.0",
23-
"rimraf": "^2.4.3",
23+
"rimraf": "^2.6.1",
2424
"should": "^11.2.0"
2525
},
2626
"scripts": {
27-
"test": "mocha --reporter spec --timeout 6000 test/main.js && npm run lint",
28-
"lint": "eslint ./index.js ./examples/. ./lib/. ./test/."
27+
"docs": "rimraf docs/* && jsdoc ./index.js ./lib --recurse --destination ./docs",
28+
"pretest": "rimraf test/repo test/tmp && eslint ./index.js ./examples/ ./lib/ ./test/",
29+
"test": "mocha --reporter spec --timeout 6000 test/main.js"
2930
},
3031
"engines": {
3132
"node": ">= 0.9.0"

test/_util.js

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
var fs = require('fs');
44
var path = require('path');
5-
var rimraf = require('rimraf');
6-
var should = require('should');
7-
var gutil = require('gulp-util');
85
var Vinyl = require('vinyl');
96

107
var repo = path.join(__dirname, 'repo');

test/add.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

33
var fs = require('fs');
4-
var path = require('path');
5-
var rimraf = require('rimraf');
64
var should = require('should');
75
var gutil = require('gulp-util');
86

@@ -13,7 +11,7 @@ module.exports = function(git, util) {
1311
var gitS = git.add();
1412
gitS.on('data', function(newFile) {
1513
should.exist(newFile);
16-
fs.stat('test/repo/.git/objects/', function(err, stats) {
14+
fs.stat('test/repo/.git/objects/', function(err) {
1715
should.not.exist(err);
1816
done();
1917
});
@@ -30,7 +28,7 @@ module.exports = function(git, util) {
3028
var gitS = git.add();
3129
gitS.on('data', function(newFile) {
3230
should.exist(newFile);
33-
fs.stat('test/repo/.git/objects/', function(err, stats) {
31+
fs.stat('test/repo/.git/objects/', function(err) {
3432
should.not.exist(err);
3533
});
3634
});

test/addRemote.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
'use strict';
22

33
var fs = require('fs');
4-
var path = require('path');
5-
var rimraf = require('rimraf');
6-
var should = require('should');
7-
var gutil = require('gulp-util');
84

9-
module.exports = function(git, util) {
5+
module.exports = function(git) {
106

117
it('should add a Remote to the git repo', function(done) {
128
var opt = {cwd: './test/repo/'};

0 commit comments

Comments
 (0)