Skip to content

Commit 91eaa4e

Browse files
committed
add main files
1 parent f38c2bf commit 91eaa4e

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
try {
4+
var gulp = require('gulp');
5+
} catch (err){
6+
var gutil = require('gulp-util');
7+
gutil.log('gulp is not installed locally');
8+
gutil.log('try `npm install gulp`');
9+
process.exit(1);
10+
}
11+
12+
var readline = require('readline');
13+
var completer = require('./lib/completer');
14+
15+
// create a simple readline interface
16+
//
17+
gulp.repl = readline.createInterface({
18+
input: process.stdin,
19+
output: process.stdout,
20+
completer: function(line){
21+
return completer(gulp, line);
22+
}
23+
});
24+
25+
// dispatch tasks only if line wasn't empty
26+
//
27+
gulp.repl.on('line', function(line){
28+
line = line.trim();
29+
if(!line){ return gulp.repl.prompt(); }
30+
var runner = gulp.parallel || gulp.start;
31+
var result = runner.apply(gulp, line.split(/[ ]+/));
32+
if(typeof result === 'function'){ // gulp#4.0
33+
result();
34+
}
35+
});
36+
37+
exports = module.exports = gulp;

lib/completer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
// ## completer(line, callback)
4+
//
5+
// arguments
6+
// - gulp, gulp instance
7+
// - line, `string`, passed from readline's `line` event
8+
// - callback, `function` passed from readline's
9+
//
10+
// returns undefined
11+
//
12+
// --
13+
// api.private
14+
// --
15+
16+
function completer(gulp, line){
17+
var completion;
18+
19+
if(gulp.tasks){ // gulp#3.x
20+
completion = Object.keys(gulp.tasks);
21+
} else if(gulp._registry && gulp._registry._tasks){ // gulp#4.0
22+
completion = Object.keys(gulp._registry._tasks);
23+
} else { completion = []; }
24+
25+
var match = line.match(/([ ]+|^)\S+$/);
26+
line = line.substring(match.index, line.length).trim();
27+
28+
var hits = completion.filter(function(elem){
29+
return !elem.indexOf(line);
30+
});
31+
32+
// TODO: add async path completion
33+
// read: nodejs.org/api/readline.html
34+
return [hits.length ? hits : completion, line];
35+
}
36+
37+
exports = module.exports = completer;

readme.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## gulp-repl [![build][b-build]][x-travis][![NPM version][b-version]][gulp-repl]
2+
3+
### usage
4+
5+
```js
6+
var gulp = require('gulp-repl');
7+
8+
gulp.task('foo', function (cb) {
9+
// do foo stuff
10+
cb();
11+
});
12+
13+
gulp.task('bar', function (cb) {
14+
// do bar stuff
15+
cb();
16+
});
17+
```
18+
19+
then, on your terminal you'll have a repl. Press
20+
21+
1. <kbd>Enter</kbd> to see the prompt
22+
1. write the tasks you want to run
23+
1. or <kbd>Tab</kbd> to see completion
24+
25+
```
26+
$ gulp
27+
$ # some task logging here
28+
$ (press Enter)
29+
$ > foo bar
30+
[10:39:11] Starting 'foo'...
31+
[10:39:11] Finished 'foo' after 13 μs
32+
[10:39:11] Starting 'bar'...
33+
[10:39:11] Finished 'bar' after 5.52 μs
34+
```
35+
36+
### API
37+
38+
#### gulp.repl
39+
40+
The module adds a `gulp.repl` property to the `gulp` instance. This
41+
property is a readline instance. [See node core `realine` module documentation][readline] for more detail on its methods.
42+
43+
### install
44+
45+
```
46+
$ npm install --save-dev gulp-repl
47+
```
48+
49+
### license
50+
51+
The MIT License (MIT)
52+
53+
Copyright (c) 2015 Javier Carrillo
54+
55+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
56+
57+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
58+
59+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60+
61+
<!-- links -->
62+
63+
[x-travis]: https://travis-ci.org/stringparser/gulp-repl/builds
64+
[b-build]: https://travis-ci.org/stringparser/gulp-repl.svg?branch=master
65+
[b-version]: http://img.shields.io/npm/v/gulp-repl.svg?style=flat-square
66+
67+
[gulp-repl]: https://npmjs.com/gulp-repl
68+
[kramed-issues]: https://github.com/GitbookIO/kramed/issues

test/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
require('should');
4+
5+
describe('gulp-repl', function(){
6+
var gulp = require('../');
7+
8+
it('repl property that is a readline Interface', function(){
9+
var readline = require('readline');
10+
gulp.repl.constructor.should.be.eql(readline.Interface);
11+
});
12+
13+
it('should dispatch registered tasks', function(done){
14+
var one, two;
15+
gulp.task('one', function(cb){ one = true; cb(); });
16+
gulp.task('two', function(cb){ two = true; done(); cb(); });
17+
gulp.repl.emit('line', 'one two');
18+
});
19+
});

0 commit comments

Comments
 (0)