-
Notifications
You must be signed in to change notification settings - Fork 0
/
break-manager.js
191 lines (168 loc) · 4.68 KB
/
break-manager.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const prompts = require('prompts');
/**
* @class CLIBreakManager
*
* This class abstracts out the act of managing the breaking of a continuous looping
* workflow by providing ability to insert CLI options for user select on break.
*/
class CLIBreakManager {
/**
* @constructor
*
* @param {Object=} [options] Accepts CLIBreak Options. This can also be passed using
* the {@link CLIBreakManager#setup} function.
*/
constructor (options) {
/**
* @private
* @type {Object[]}
*/
this.operations = [];
/**
* @private
* @type {Number}
*/
this.lastOperationIndex = 0;
/**
* @private
* @type {Boolean}
*/
this.broken = false;
/**
* @private
* @type {Function(callback)}
*/
this.onBreak = undefined;
/**
* @private
* @type {Function(callback)}
*/
this.onContinue = undefined;
if (options) {
this.setup(options);
}
}
/**
* Setup the CLIBreakManager to be operational
*
* @param {Object} options Accepts `onBreak` and `onContinue` properties. These are functions
* that are called when the break manager needs to break or continue. Both these function
* should accept a callback as a parameter that needs to be called to proceed.
*
*/
setup (options) {
if (!options) {
return;
}
if (typeof options.onBreak === 'function') {
this.onBreak = options.onBreak;
}
if (typeof options.onContinue === 'function') {
this.onContinue = options.onContinue;
}
if (options.signal) {
let sigint = 0;
process.on('SIGINT', () => {
try {
sigint += 1;
if (sigint > 1) {
process.exit();
}
if (!this.broken) {
sigint = 0;
this.break('CTRL+C interrupt');
}
} catch (e) {
console.error(e);
process.exit(1);
}
});
this.add('Continue (press CTRL+C to break anytime after)', function (done) {
done(null, true);
});
}
}
/**
* Cause the break manager to execute breaking function and show options to proceed.
*
* @param {String=} message This is a message that will be shown as the reason for breaking
*/
break (message = '') {
this.broken = true;
if (!this.onBreak) {
this.present(message);
return;
}
this.onBreak(() => {
this.present(message);
});
}
/**
* Cause the break manager to execute 'continue' function and show options to proceed.
*
* @param {String=} message This is a message that will be shown as the reason for 'continue'
*/
continue (message = '') {
this.broken = false;
if (!this.onContinue) {
message && console.log(message);
return;
}
this.onContinue(() => {
message && console.log(message);
});
}
/**
* Add a break operation. This operation will be presented on 'break'
*
* @param {String} title The name of the operation that will be presented as part of the
* list of things that can be done upon 'break'
*
* @param {Function(callback(err?:Error, continue=false:Boolean))} The action is a function
* that will be called when user chooses this from list on break. This function will get a
* callback that needs to be executed after completion of the operation. It can return an error
* as part of the callback's first parameter and a boolean as second parameter indicating to the
* break manager whether to continue or to repeat break options after this.
*/
add (title, action) {
this.operations.push({
title: title,
value: {
action: action,
// record the position of the op on list. this will be used to re-position
// to the same option on consecutive breaks
index: this.operations.length
}
});
}
/**
* Function that presents the break op list
* @private
*
* @param {String} message
*/
present (message) {
this.constructor.prompt(message, this.operations, this.lastOperationIndex, (choice) => {
this.lastOperationIndex = choice.index;
choice.action((err, forward) => {
if (err) { console.error(err); }
forward ? this.continue() : this.present();
});
});
}
/**
* A function that makes presenting the CLI options from an array
* @private
*/
static prompt (message, choices = [], initial = 0, callback) {
console.log(''); // add newline spacer
prompts({
type: 'select',
name: 'value',
message: 'Run paused' + (message ? ` on ${message}` : ''),
choices: choices,
initial: initial
}, { onSubmit: (p, a) => callback(a) });
}
}
module.exports = CLIBreakManager;