/
ui.js
151 lines (129 loc) · 3.36 KB
/
ui.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
'use strict';
const readline = require('readline');
const os = require('os');
const fs = require('./file-system');
const transform = require('./colors/transform');
const createLines = require('./string').createLines;
const tty = require('tty');
const UIEnquirer = require('./ui-enquirer');
exports.UI = class { };
exports.ConsoleUI = class {
constructor(cliOptions) {
this.cliOptions = cliOptions;
this.uiEnquirer = new UIEnquirer(this);
}
open() {
if (this.rl) {
return;
}
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
}
close() {
if (this.rl) {
this.rl.close();
this.rl = null;
}
}
log(text, createLinesIndentation) {
if (createLinesIndentation !== undefined) {
text = createLines(text, createLinesIndentation, this.getWidth());
}
return new Promise((resolve, reject) => {
console.log(text);
resolve();
});
}
ensureAnswer(answer, question, suggestion) {
if (answer) {
return Promise.resolve(answer);
}
return this.question(question, suggestion);
}
async question(question, optionsOrSuggestion, defaultValue) {
if (!optionsOrSuggestion || typeof optionsOrSuggestion === 'string') {
const answer = await this.uiEnquirer.prompt(
'input',
'noNameNeeded',
question,
optionsOrSuggestion,
);
if (answer && answer.length) {
return answer;
} else {
return this.question(question, optionsOrSuggestion);
}
} else {
optionsOrSuggestion = optionsOrSuggestion.filter(x => includeOption(this.cliOptions, x));
let autoSubmit = false;
if (optionsOrSuggestion.length === 1) {
defaultValue = optionsOrSuggestion[0].displayName;
optionsOrSuggestion[0].name = optionsOrSuggestion[0].value.id;
autoSubmit = true;
}
return this.uiEnquirer.prompt(
'select',
'noNameNeeded',
question,
defaultValue,
optionsOrSuggestion,
autoSubmit,
);
}
}
multiselect(question, options, defaultValue) {
return this.uiEnquirer.prompt(
'multiselect',
'noNameNeeded',
question,
defaultValue,
options
);
}
getWidth() {
return getWindowSize().width;
}
getHeight() {
return getWindowSize().height;
}
displayLogo() {
if (this.getWidth() < 50) {
return this.log('Aurelia CLI' + os.EOL);
}
let logoLocation = require.resolve('./resources/logo.txt');
return fs.readFile(logoLocation).then(logo => {
this.log(logo.toString());
});
}
};
function includeOption(cliOptions, option) {
if (option.disabled) {
return false;
}
if (option.flag) {
return cliOptions.hasFlag(option.flag);
}
return true;
}
function getWindowSize() {
let width;
let height;
if (tty.isatty(1) && tty.isatty(2)) {
if (process.stdout.getWindowSize) {
width = process.stdout.getWindowSize(1)[0];
height = process.stdout.getWindowSize(1)[1];
} else if (tty.getWindowSize) {
width = tty.getWindowSize()[1];
height = tty.getWindowSize()[0];
} else if (process.stdout.columns && process.stdout.rows) {
height = process.stdout.rows;
width = process.stdout.columns;
}
} else {
width = 80;
height = 100;
}
return { height: height, width: width };
}