Skip to content

Commit be44875

Browse files
committed
release 2.0.0
- docs: add new api docs - test: split test into files for each api method - dev: separate module into add, get, remove, reset and start
1 parent 64fbcfc commit be44875

File tree

4 files changed

+196
-86
lines changed

4 files changed

+196
-86
lines changed

README.md

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,40 @@ Simple repl for gulp compatible with gulp#3.x and the future gulp#4.x.
99
```js
1010
// gulpfile example
1111
var gulp = require('gulp');
12-
gulp.repl = require('gulp-repl')(gulp);
12+
var repl = require('gulp-repl');
1313

14-
gulp.task('foo', function (cb) {
15-
// do foo stuff
14+
gulp.task('repl-start', function (cb) {
15+
gulp.repl = repl(gulp);
16+
});
17+
18+
gulp.task('repl-stop', function (cb) {
19+
if (gulp.repl) {
20+
gulp.repl.close(); // same as nodejs.org/api/readline.html#readline_rl_close
21+
}
1622
cb();
1723
});
1824

25+
26+
gulp.task('foo', function (cb) {
27+
// do foo stuff
28+
cb();
29+
});
30+
1931
gulp.task('bar', function (cb) {
20-
// do bar stuff
21-
cb();
32+
// do bar stuff
33+
cb();
2234
});
2335

2436
gulp.task('default');
37+
```
2538

26-
// same as writing foo bar on the command line
27-
gulp.repl.emit('line', 'foo bar');
39+
Then, on your terminal write:
40+
41+
```
42+
gulp repl-start
2843
```
2944

30-
Then, on your terminal, you'll have a repl
45+
and you'll have a repl with:
3146

3247
1. Press <kbd>Enter</kbd> to see the prompt
3348
1. write the tasks you want to run
@@ -48,9 +63,69 @@ foo bar default
4863

4964
### API
5065

51-
The module exports a `readline` interface.
66+
The module exports a function
67+
68+
```js
69+
var repl = require('gulp-repl');
70+
```
71+
72+
Calling the function with `gulp` creates a `readline` instance using `gulp` tasks as commands for a REPL.
73+
74+
```js
75+
var gulp = require('gulp');
76+
var gulpREPL = require('repl');
77+
78+
// to start the repl
79+
gulp.repl = gulpREPL.start(gulp);
80+
```
81+
82+
#### gulpREPL.add
83+
84+
```js
85+
function add(Gulp gulp)
86+
```
87+
88+
Adds the given `gulp` instance for the REPL to be able to lookup and _returns_ the module again.
89+
90+
#### gulpREPL.remove
91+
92+
```js
93+
function remove(Gulp gulp)
94+
```
95+
96+
Removes the given `gulp` instance for the REPL to be able to lookup and _returns_ the module again.
97+
98+
#### gulpREPL.reset
99+
100+
```js
101+
function reset()
102+
```
103+
104+
Removes all of the previously added instances and _returns_ the module again.
105+
106+
#### gulpREPL.get
107+
108+
```js
109+
function get(Gulp gulp)
110+
```
111+
112+
Takes a `gulp` instance as argument
113+
114+
_returns_
115+
- `null` if the `gulp` instance wasn't stored yet
116+
- all of the stored instances if no arguments are given
117+
- metadata stored for the given `gulp` instance if was already stored
118+
119+
#### gulpREPL.start
120+
121+
```js
122+
function start(Gulp gulp)
123+
```
124+
125+
Starts a REPL listening on `stdin` and writing on `stdout` and _returns_ a `readline.Interface` instance. Each of the commands typed to the REPL are looked up in each of the instances given on `add`.
126+
127+
[See node's core module `readline` documentation about the `readline.Interface`](https://nodejs.org/api/readline.html).
52128

53-
For more information [see node's core module `readline` documentation](https://nodejs.org/api/readline.html).
54129

55130
### install
56131

@@ -60,6 +135,11 @@ $ npm install --save-dev gulp-repl
60135

61136
## Changelog
62137

138+
[v2.0.0][v2.0.0]:
139+
- docs: add new api docs
140+
- test: split test into files for each api method
141+
- dev: separate module into add, get, remove, reset and start
142+
63143
[v1.1.2][v1.1.2]:
64144

65145
- docs: add changelog
@@ -95,6 +175,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
95175
[badge-downloads]: http://img.shields.io/npm/dm/gulp-repl.svg?style=flat-square
96176

97177

178+
[v2.0.0]: https://github.com/stringparser/gulp-repl/commit/572df8ce7cd9d4edd3a2190de021381671a295f0
98179
[v1.1.2]: https://github.com/stringparser/gulp-repl/commit/572df8ce7cd9d4edd3a2190de021381671a295f0
99180
[v1.1.1]: https://github.com/stringparser/gulp-repl/commit/6f4655ca1a667ca04d2a668a175055f9b4437d65
100181
[v1.1.0]: https://github.com/stringparser/gulp-repl/commit/71a2301233a92d68dbfd7e7a1493a38be72d0a0e

index.js

Lines changed: 100 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,115 @@
22

33
var util = require('./lib/util');
44

5-
exports = module.exports = gulpRepl;
6-
exports.instances = [];
7-
8-
/**
9-
* create a readline interface
10-
**/
11-
var repl = require('readline').createInterface({
12-
input: process.stdin,
13-
output: process.stdout,
14-
completer: function onCompletion(line){
15-
return util.completer(line, exports.instances);
5+
var repl = null;
6+
var instances = [];
7+
8+
// add the instance to the repl and start it
9+
exports = module.exports = function (_gulp_) {
10+
return exports.start(_gulp_);
11+
};
12+
13+
// get the instance properties used by the REPL
14+
exports.get = function (gulp) {
15+
if (!arguments.length) {
16+
return instances.concat();
1617
}
17-
});
18-
19-
/**
20-
* queue tasks when line is not empty
21-
**/
22-
repl.on('line', function onLine(input){
23-
var line = input.trim();
24-
if(!line){ return repl.prompt(); }
25-
26-
var queue = {
27-
found: [],
28-
notFound: line.split(/[ ]+/)
29-
};
30-
31-
exports.instances.forEach(function(inst){
32-
var tasks = util.getQueue(queue.notFound.join(' '), inst.tasks);
33-
if(tasks.found.length){
34-
queue.found.push({
35-
inst: inst,
36-
tasks: tasks.found
37-
});
18+
19+
var length = instances.length;
20+
21+
for (var index = 0; index < length; ++index) {
22+
var instance = instances[index] || {};
23+
if (instance.gulp === gulp) {
24+
return instance;
3825
}
39-
queue.notFound = tasks.notFound;
40-
});
26+
}
4127

42-
if(queue.notFound.length){
43-
var plural = queue.notFound.length > 1;
28+
return null;
29+
};
4430

45-
console.log(' `%s` task%s %s not defined yet',
46-
queue.notFound.join(', '),
47-
plural ? 's' : '',
48-
plural ? 'are' : 'is'
49-
);
31+
// add the given instance to the REPL lookup
32+
exports.add = function (_gulp_) {
33+
if (_gulp_ && !this.get(_gulp_)) {
34+
var gulp = util.getGulp(_gulp_);
5035

51-
return repl.prompt();
36+
instances.push({
37+
gulp: gulp,
38+
index: instances.length,
39+
tasks: util.getTasks(gulp),
40+
runner: gulp.start || gulp.parallel
41+
});
5242
}
43+
return this;
44+
};
45+
46+
// reset the instances array
47+
exports.reset = function () {
48+
instances = [];
49+
return this;
50+
};
51+
52+
// remove the instance from the instances array
53+
exports.remove = function (_gulp_) {
54+
var instance = this.get(_gulp_);
55+
if (instance) {
56+
instances.splice(instance.index, 1);
57+
}
58+
return this;
59+
};
60+
61+
// create a readline instance if there is none
62+
exports.start = function (_gulp_) {
63+
exports.add(_gulp_);
5364

54-
queue.found.forEach(function(found){
55-
var result = found.inst.runner.apply(found.inst.gulp, found.tasks);
56-
if(typeof result === 'function'){
57-
result(); // gulp#4.0
65+
// only create one repl listening on stdin
66+
if (repl) { return repl; }
67+
68+
repl = require('readline').createInterface({
69+
input: process.stdin,
70+
output: process.stdout,
71+
completer: function onComplete (line) {
72+
return util.completer(line, instances);
73+
}
74+
});
75+
76+
// queue tasks when line is not empty
77+
repl.on('line', function onLine (input) {
78+
var line = input.trim();
79+
if (!line) { return repl.prompt(); }
80+
81+
var queue = {
82+
found: [],
83+
notFound: line.split(/[ ]+/)
84+
};
85+
86+
instances.forEach(function (inst) {
87+
var tasks = util.getQueue(queue.notFound.join(' '), inst.tasks);
88+
if (tasks.found.length) {
89+
queue.found.push({ inst: inst, tasks: tasks.found });
90+
}
91+
queue.notFound = tasks.notFound;
92+
});
93+
94+
if (queue.notFound.length) {
95+
console.log(' `%s` not found', queue.notFound.join(' '));
96+
return repl.prompt();
5897
}
98+
99+
queue.found.forEach(function (found) {
100+
var result = found.inst.runner.apply(found.inst.gulp, found.tasks);
101+
if (typeof result === 'function') {
102+
result(); // gulp#4.0
103+
}
104+
});
105+
106+
return this;
59107
});
60-
});
61-
62-
/**
63-
* exit on SIGINT with a timestamp
64-
**/
65-
repl.on('SIGINT', function onSIGINT(){
66-
process.stdout.write('\n');
67-
console.log(new Date());
68-
process.exit(0);
69-
});
70-
71-
/**
72-
* add the given gulp instance to the instances array
73-
**/
74-
function gulpRepl(_gulp_){
75-
var gulp = util.getGulp(_gulp_);
76-
77-
var inInstances = Boolean(
78-
exports.instances.filter(function(instance){
79-
return instance.gulp === gulp;
80-
}).length
81-
);
82-
if(inInstances){ return repl; }
83-
84-
exports.instances.push({
85-
gulp: gulp,
86-
tasks: util.getTasks(gulp),
87-
runner: gulp.start || gulp.parallel
108+
109+
// exit on SIGINT
110+
repl.on('SIGINT', function onSIGINT () {
111+
process.stdout.write('\n');
112+
process.exit(0);
88113
});
89114

90115
return repl;
91-
}
116+
};

lib/util.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var uniq = require('lodash.uniq');
44

55
exports = module.exports = {};
66

7+
// return a gulp-like instance, require gulp if not given
78
exports.getGulp = function (gulp) {
89
if (!gulp || typeof gulp.task !== 'function') {
910
try {
@@ -17,6 +18,7 @@ exports.getGulp = function (gulp) {
1718
return gulp;
1819
};
1920

21+
// get the tasks for the given instance
2022
exports.getTasks = function (gulp) {
2123
var tasks = {
2224
obj: (
@@ -42,6 +44,7 @@ exports.getTasks = function (gulp) {
4244
return tasks;
4345
};
4446

47+
// get an object with of found/not found tasks to run
4548
exports.getQueue = function (line, tasks) {
4649
var queue = {found: [], notFound: []};
4750

@@ -61,6 +64,7 @@ exports.getQueue = function (line, tasks) {
6164
return queue;
6265
};
6366

67+
// return all instances completion for the given line
6468
exports.completer = function (line, instances) {
6569
var match = line.match(/([\s]+|^)\S+$/);
6670

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "gulp-repl",
33
"author": "Javier Carrillo <[email protected]>",
4-
"version": "1.1.2",
4+
"version": "2.0.0",
55
"license": "MIT",
66
"repository": "https://github.com/stringparser/gulp-repl",
77
"description": "a simple repl for gulp",

0 commit comments

Comments
 (0)