-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
路323 lines (252 loc) 路 9.09 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
//polyfills
const process = require('suman-browser-polyfills/modules/process');
const global = require('suman-browser-polyfills/modules/global');
//core
const assert = require('assert');
const util = require('util');
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
//npm
const async = require('async');
const debug = require('suman-debug')('suman:interactive');
const colors = require('colors/safe');
//project
const _suman = global.__suman = (global.__suman || {});
const rejectionHandler = require('./interactive-rejection-handler');
const choices = require('./helpers/choices');
//////////////////////////////////////////////////////
process.on('warning', function (w) {
if (weAreDebugging) {
// if we are debugging, log all warnings
console.log(' => Suman interactive warning => ', w.stack || w);
}
else if (!(/deprecated/i.test(String(w)))) {
//there were some really useless warnings about deprecation
console.log(' => Suman interactive warning => ', w.stack || w);
}
});
//////////////////////////////////////////////////////
process.stdin.setMaxListeners(20);
///////////////////////////////////////////////////////
const interactiveDebugPath = path.resolve(__dirname + '/helpers/debug-interactive.log');
fs.writeFileSync(interactiveDebugPath, '\n new run \n', {flag: 'w'});
_suman._interactiveDebug = function () {
fs.writeFileSync(interactiveDebugPath, '\n\n', {flag: 'a'});
const args = Array.from(arguments);
const data = args.map(function (a) {
return (typeof a === 'string' ? a : util.inspect(a));
}).join('\n');
fs.writeFileSync(interactiveDebugPath, data, {flag: 'a'})
};
_suman.onBackspace = function (cb) {
process.stdin.listeners('readable').forEach(function (fn) {
if (process.stdin.listenerCount('readable') > 4)
process.stdin.removeListener('readable', fn);
});
process.stdin.listeners('close').forEach(function (fn) {
if (process.stdin.listenerCount('close') > 4)
process.stdin.removeListener('close', fn);
});
process.stdin.listeners('keypress').forEach(function (fn) {
if (process.stdin.listenerCount('keypress') > 4)
process.stdin.removeListener('keypress', fn);
});
process.stdin.listeners('exit').forEach(function (fn) {
if (process.stdin.listenerCount('exit') > 4)
process.stdin.removeListener('exit', fn);
});
process.stdin.listeners('end').forEach(function (fn) {
if (process.stdin.listenerCount('end') > 4)
process.stdin.removeListener('end', fn);
});
process.nextTick(cb);
};
_suman._implementationError = function (msg) {
msg = msg || '';
msg = typeof msg === 'string' ? msg : util.inspect(msg);
throw new Error(colors.red(' => Suman interactive internal implementation problem ' + (msg || '.')));
};
_interactiveDebug(' => beginning of interactive session => ');
//////////////////////////////////////////////////////
const testDir = _suman.sumanConfig.testDir;
let rootDir;
try {
rootDir = path.resolve(_suman.projectRoot + '/' + testDir);
if (!(fs.statSync(rootDir).isDirectory())) {
throw new Error('Path given by => "' + rootDir + '" is not a directory');
}
}
catch (err) {
rootDir = _suman.projectRoot;
}
//TODO: we can validate that all the choices are actually files in a directory
const loadingMessage = colors.bgYellow.blue(' => Loading suman interactive mode...');
console.log(loadingMessage);
const _cwd = path.resolve(process.env.HOME + '/.suman');
async.parallel({
installSumanInquirer: function (cb) {
try {
require.resolve('suman-inquirer');
return process.nextTick(cb);
}
catch (err) {
cp.exec('npm install suman-inquirer@latest', {
cwd: _cwd
}, cb);
}
},
installSumanInquirerDirectory: function (cb) {
try {
require.resolve('suman-inquirer-directory');
return process.nextTick(cb);
}
catch (err) {
cp.exec('npm install suman-inquirer-directory@latest', {
cwd: _cwd
}, cb);
}
}
}, function (err) {
if (err) {
throw err;
}
const inquirer = require('suman-inquirer');
const inqDir = require('suman-inquirer-directory');
inquirer.registerPrompt('directory', inqDir);
let firstQuestionSeen = false;
const firstSetOfQuestions = [
{
type: 'confirm',
name: 'suman',
message: colors.yellow.underline(' => Welcome to Suman land!') + '\n' +
colors.blue(' This interactive utility allows you to familiarize yourself with Suman, as well as keep up to date with the API.\n' +
' You can generate a terminal command with this tool which you can then go run yourself.\n' +
' This tool can also help you troubleshoot or debug tests.') + '\n' +
' \n To skip this messsage the future, just use ' + colors.magenta('"suman --interactive --fast"') + '.\n\n' +
' To ' + colors.green('continue') + ', hit enter, or type "y" or "yes".\n\n ',
when: function () {
if (!firstQuestionSeen) {
firstQuestionSeen = true;
if (process.argv.indexOf('--fast') < 0) {
console.log('\n\n ---------------------------------------------------- \n\n');
return true;
}
}
}
},
{
type: 'list',
name: 'action',
message: 'What would you like to do?',
default: 0,
onLeftKey: function () {
_interactiveDebug('left key fired in top level!');
_suman.onBackspace(start);
},
choices: Object.values(choices.topLevelOpts), //add empty option for formatting purposes
when: function (d) {
if (d.suman === false) {
console.log('\n\n');
console.log('\n => Confirmation was false...ok, we will exit then!');
process.exit(1);
}
else {
console.log('\n\n ---------------------------------------------------- \n\n');
return true;
}
}
}
];
const secondSetOfQuestions = [
{
type: 'list',
name: 'firstAction',
message: 'What would you like to do?',
onLeftKey: function () {
_interactiveDebug('left key fired in generate list!');
_suman.onBackspace(start);
},
default: 0,
choices: fs.readdirSync(path.resolve(__dirname + '/generate-command')),
when: function () {
console.log('\n\n ----------------------------------------------------- \n\n');
return true;
}
}
];
function generateList(obj, onBackspace) {
const dir = obj.firstAction;
const root = path.resolve(__dirname + '/generate-command');
const items = fs.readdirSync(root + '/' + dir);
_interactiveDebug('ITEMS => ', items);
return inquirer.prompt([
{
type: 'list',
name: 'secondAction',
message: 'What would you like to do?',
onLeftKey: function () {
_interactiveDebug('left key fired in generate list!');
_suman.onBackspace(onBackspace);
},
default: 0,
choices: items.map(function (item) {
return String(item).slice(0, -3); // get rid of ".js"
}),
when: function () {
console.log('\n\n ----------------------------------------------------- \n\n');
return true;
},
filter: function (items) {
return items;
// return items.map(function (item) {
// return path.resolve(root + '/' + item);
// });
}
}
]).then(function (answers) {
return Object.assign(obj, answers);
});
}
function secondSet() {
return inquirer.prompt(secondSetOfQuestions).then(function (obj) {
return thirdSet(obj, secondSet);
});
}
function thirdSet(obj, cb) {
_interactiveDebug('in THIRD SET => ', obj);
const _thirdSet = thirdSet.bind(null, obj, cb);
return generateList(obj, cb).then(function (obj) {
_interactiveDebug('obj in SECOND => ', obj);
const pth = path.resolve(__dirname + '/generate-command/' + obj.firstAction + '/' + obj.secondAction + '.js');
_interactiveDebug('RESOLVED PATH => ', pth);
return require(pth)({
rootDir: rootDir
}, _thirdSet);
});
}
function start() {
// process.stdin.removeAllListeners('keypress');
// process.stdin.removeAllListeners('end');
_interactiveDebug('readable count:', process.stdin.listenerCount('readable'));
_interactiveDebug('keypress count:', process.stdin.listenerCount('keypress'));
_interactiveDebug('keypress count:', process.stdin.listenerCount('keypress'));
inquirer.restoreDefaultPrompts();
inquirer.prompt(firstSetOfQuestions).then(function (respuestas) {
if (respuestas.action === choices.topLevelOpts.GenerateCommand) {
return secondSet(start);
}
else if (respuestas.action === choices.topLevelOpts.Learn) {
throw new Error('Learn the Suman API is not implemented yet.');
}
else if (respuestas.action === choices.topLevelOpts.Troubleshoot) {
throw new Error('Troubleshoot is not implemented yet.');
}
else {
throw new Error('Action not recognized.');
}
}).catch(rejectionHandler);
}
start();
});