-
Notifications
You must be signed in to change notification settings - Fork 8
/
choose-dirs.js
executable file
路186 lines (162 loc) 路 5.01 KB
/
choose-dirs.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
'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 path = require('path');
const fs = require('fs');
//npm
const colors = require('colors/safe');
const inquirer = require('suman-inquirer');
//project
const _suman = global.__suman = (global.__suman || {});
const sumanUtils = require('suman-utils');
const rejectionHandler = require('../interactive-rejection-handler');
///////////////////////////////////////////////////////////
module.exports = function chooseDirs (opts, backspaceCB) {
if (opts.allowChoose === false) {
if(_suman.backspacing){
backspaceCB();
return 'backspacing';
}
else{
return Promise.resolve(Object.assign(opts, {
pathsToRun: []
}));
}
}
const onlyOneFile = opts.onlyOneFile;
const rootDir = opts.rootDir;
const output = [];
return (function ask (rootDir) {
const p = inquirer.prompt([
{
type: 'directory',
name: 'filePath',
message: 'Please select a test file that you would like to run.',
includeFiles: true,
basePath: rootDir,
onlyOneFile: onlyOneFile,
onLeftKey: function () {
_interactiveDebug('promise???',p);
// p.ui.close();
_suman.onBackspace(backspaceCB);
},
mapValue: function (p) {
return sumanUtils.removePath(p, _suman.projectRoot)
},
filterItems: function () {
return true;
},
validate: function (p) {
if (fs.statSync(p).isFile()) {
return true;
}
return 'Please select a file not a directory.';
},
when: function () {
if (onlyOneFile === true) {
console.log('\n\n ----------------------------------------------- \n\n');
_suman.backspacing = false;
return true;
}
}
},
{
type: 'directory',
name: 'dirOrFilePath',
message: 'Please select a test file or directory that you would like to run.\n',
includeFiles: true,
basePath: rootDir,
onLeftKey: function () {
// p.ui.close();
_interactiveDebug('backspaceCB in choose dirs', String(backspaceCB));
_suman.onBackspace(backspaceCB);
},
mapValue: function (p) {
return sumanUtils.removePath(p, _suman.projectRoot)
},
when: function () {
if (onlyOneFile !== true) {
console.log('\n\n ----------------------------------------------- \n\n');
_suman.backspacing = false;
return true;
}
},
validate: function (p) {
if (fs.statSync(p).isFile()) {
return true;
}
return 'Please select a file not a directory.';
},
filterItems: function () {
return true;
}
},
{
type: 'confirm',
name: 'askAgain',
onLeftKey: function () {
// p.ui.close();
_suman.onBackspace(backspaceCB);
},
message: 'Would you like to choose another file or directory (enter = yes) ?\n',
when: function () {
if (onlyOneFile !== true) {
console.log('\n\n ----------------------------------------------- \n\n');
_suman.backspacing = false;
return true;
}
},
default: true
}
]).then(function (answers) {
if (answers.dirOrFilePath) {
output.push(answers.dirOrFilePath);
}
else if (answers.filePath) {
output.push(answers.filePath);
}
else {
throw new Error(' No option selected.');
}
if (answers.askAgain) {
return ask(rootDir);
} else {
const $output = output.map(function (item) {
return path.isAbsolute(item) ? item : path.resolve(rootDir + '/' + item);
}).map((i, index) => ( (index + 1) + ' => "' + i + '"')).join(',\n');
return inquirer.prompt([
{
type: 'confirm',
name: 'confirmOutput',
onLeftKey: function () {
_suman.onBackspace(backspaceCB);
},
message: 'Are these the files/directories you wish to use? (To skip use the --fast option)\n\n' +
colors.magenta($output) + '\n',
when: function () {
console.log('\n\n ----------------------------------------------- \n\n');
_suman.backspacing = false;
return true;
},
default: true
}
]).then(function (respuestas) {
if (respuestas.confirmOutput) {
return Object.assign(opts, {
pathsToRun: output,
});
}
else {
output.pop();
return ask(rootDir);
}
});
}
}).catch(rejectionHandler);
return p;
})(rootDir);
};