-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (79 loc) · 2.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @author Stanislav Kalashnik <[email protected]>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/
'use strict';
const
name = 'repl',
log = require('runner-logger').wrap(name);
function start ( config, done ) {
const
readline = require('readline'),
instance = readline.createInterface(config.readline);
//readline.setPrompt(config.readline.prompt);
instance.on('line', function ( line ) {
if ( line ) {
// silent mode for future
//readline.moveCursor(process.stdout, 0, -1);
//readline.clearLine();
config.runner.run(line);
}
});
instance.on('close', function () {
done();
});
// ctrl+c
instance.on('SIGINT', function onSIGINT () {
/* eslint-disable no-process-exit */
process.exit(0);
});
return instance;
}
function stop ( instance ) {
if ( instance ) {
instance.close();
}
}
function generator ( config = {}, options = {} ) {
const
tasks = {},
{prefix = name + ':', suffix = ''} = options;
let instance;
// sanitize and extend defaults
config = Object.assign({
readline: {
input: process.stdin,
output: process.stdout,
prompt: '',
historySize: 100,
removeHistoryDuplicates: true,
completer: function ( line ) {
const
taskList = Object.keys(config.runner.tasks).sort(),
hits = taskList.filter(function ( task ) {
return task.startsWith(line);
});
// show all completions if none found
return [hits.length ? hits : taskList, line];
}
}
}, config);
tasks[prefix + 'config' + suffix] = function () {
log.inspect(config, log);
};
tasks[prefix + 'start' + suffix] = function ( done ) {
instance = start(config, done);
};
tasks[prefix + 'stop' + suffix] = function () {
stop(instance);
instance = null;
};
return tasks;
}
// export main actions
generator.methods = {
start: start,
stop: stop
};
// public
module.exports = generator;