/
e2e-command-helper.js
330 lines (296 loc) · 11.4 KB
/
e2e-command-helper.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
323
324
325
326
327
328
329
330
// @flow
import rightpad from 'pad-right';
import chalk from 'chalk';
import path from 'path';
import childProcess from 'child_process';
import { expect } from 'chai';
import { NOTHING_TO_TAG_MSG } from '../cli/commands/public-cmds/tag-cmd';
import { removeChalkCharacters } from '../utils';
import runInteractive from '../interactive/utils/run-interactive-cmd';
import type { InteractiveInputs } from '../interactive/utils/run-interactive-cmd';
import ScopesData from './e2e-scopes';
import { CURRENT_UPSTREAM } from '../constants';
const DEFAULT_DEFAULT_INTERVAL_BETWEEN_INPUTS = 200;
export default class CommandHelper {
scopes: ScopesData;
debugMode: boolean;
bitBin: string;
constructor(scopes: ScopesData, debugMode: boolean) {
this.scopes = scopes;
this.debugMode = debugMode;
this.bitBin = process.env.npm_config_bit_bin || 'bit'; // e.g. npm run e2e-test --bit_bin=bit-dev
}
runCmd(cmd: string, cwd: string = this.scopes.localPath): string {
if (this.debugMode) console.log(rightpad(chalk.green('cwd: '), 20, ' '), cwd); // eslint-disable-line no-console
if (cmd.startsWith('bit ')) cmd = cmd.replace('bit', this.bitBin);
if (this.debugMode) console.log(rightpad(chalk.green('command: '), 20, ' '), cmd); // eslint-disable-line no-console
// $FlowFixMe
const cmdOutput = childProcess.execSync(cmd, { cwd, shell: true });
if (this.debugMode) console.log(rightpad(chalk.green('output: '), 20, ' '), chalk.cyan(cmdOutput.toString())); // eslint-disable-line no-console
return cmdOutput.toString();
}
listRemoteScope(raw: boolean = true, options: string = '') {
return this.runCmd(`bit list ${this.scopes.remote} ${options} ${raw ? '--raw' : ''}`);
}
listLocalScope(options: string = '') {
return this.runCmd(`bit list ${options}`);
}
listLocalScopeParsed(options: string = ''): Object[] {
const output = this.runCmd(`bit list --json ${options}`);
return JSON.parse(output);
}
listRemoteScopeParsed(options: string = '') {
const output = this.runCmd(`bit list ${this.scopes.remote} --json ${options}`);
return JSON.parse(output);
}
listScopeParsed(scope: string, options: string = '') {
const output = this.runCmd(`bit list ${scope} --json ${options}`);
return JSON.parse(output);
}
catScope(includeExtraData: boolean = false) {
const extraData = includeExtraData ? '--json-extra' : '';
const result = this.runCmd(`bit cat-scope --json ${extraData}`);
return JSON.parse(result);
}
catObject(hash: string, parse: boolean = false) {
const result = this.runCmd(`bit cat-object ${hash}`);
if (!parse) return result;
return JSON.parse(result);
}
catComponent(id: string, cwd?: string): Object {
const result = this.runCmd(`bit cat-component ${id}`, cwd);
return JSON.parse(result);
}
addComponent(filePaths: string, options: Object = {}, cwd: string = this.scopes.localPath) {
const value = Object.keys(options)
.map(key => `-${key} ${options[key]}`)
.join(' ');
return this.runCmd(`bit add ${filePaths} ${value}`, cwd);
}
getConfig(configName: string) {
return this.runCmd(`bit config get ${configName}`);
}
delConfig(configName: string) {
return this.runCmd(`bit config del ${configName}`);
}
setConfig(configName: string, configVal: string) {
return this.runCmd(`bit config set ${configName} ${configVal}`);
}
untrackComponent(id: string = '', all: boolean = false, cwd: string = this.scopes.localPath) {
return this.runCmd(`bit untrack ${id} ${all ? '--all' : ''}`, cwd);
}
removeComponent(id: string, flags: string = '') {
return this.runCmd(`bit remove ${id} ${flags}`);
}
deprecateComponent(id: string, flags: string = '') {
return this.runCmd(`bit deprecate ${id} ${flags}`);
}
undeprecateComponent(id: string, flags: string = '') {
return this.runCmd(`bit undeprecate ${id} ${flags}`);
}
tagComponent(id: string, tagMsg: string = 'tag-message', options: string = '') {
return this.runCmd(`bit tag ${id} -m ${tagMsg} ${options}`);
}
tagWithoutMessage(id: string, version: string = '', options: string = '') {
return this.runCmd(`bit tag ${id} ${version} ${options}`);
}
tagAllComponents(options: string = '', version: string = '', assertTagged: boolean = true) {
const result = this.runCmd(`bit tag -a ${version} ${options} `);
if (assertTagged) expect(result).to.not.have.string(NOTHING_TO_TAG_MSG);
return result;
}
tagScope(version: string, message: string = 'tag-message', options: string = '') {
return this.runCmd(`bit tag -s ${version} -m ${message} ${options}`);
}
untag(id: string) {
return this.runCmd(`bit untag ${id}`);
}
exportComponent(id: string, scope: string = this.scopes.remote, assert: boolean = true) {
const result = this.runCmd(`bit export ${scope} ${id} --force`);
if (assert) expect(result).to.not.have.string('nothing to export');
return result;
}
exportAllComponents(scope: string = this.scopes.remote) {
return this.runCmd(`bit export ${scope} --force`);
}
exportToCurrentScope(ids?: string) {
return this.runCmd(`bit export ${CURRENT_UPSTREAM} ${ids || ''}`);
}
export(options?: string = '') {
// --force just silents the prompt, which obviously needed for CIs
return this.runCmd(`bit export --force ${options}`);
}
ejectComponents(ids: string, flags?: string) {
return this.runCmd(`bit eject ${ids} ${flags || ''}`);
}
ejectComponentsParsed(ids: string, flags?: string) {
const result = this.runCmd(`bit eject ${ids} ${flags || ''} --json`);
const jsonStart = result.indexOf('{');
const jsonResult = result.substring(jsonStart);
return JSON.parse(jsonResult);
}
importComponent(id: string) {
return this.runCmd(`bit import ${this.scopes.remote}/${id}`);
}
importManyComponents(ids: string[]) {
const idsWithRemote = ids.map(id => `${this.scopes.remote}/${id}`);
return this.runCmd(`bit import ${idsWithRemote.join(' ')}`);
}
importComponentWithOptions(id: string = 'bar/foo.js', options: Object) {
const value = Object.keys(options)
.map(key => `-${key} ${options[key]}`)
.join(' ');
return this.runCmd(`bit import ${this.scopes.remote}/${id} ${value}`);
}
importAllComponents(writeToFileSystem: boolean = false) {
return this.runCmd(`bit import ${writeToFileSystem ? '--merge' : ''}`);
}
isolateComponent(id: string, flags: string): string {
const isolatedEnvOutput = this.runCmd(`bit isolate ${this.scopes.remote}/${id} ${this.scopes.remotePath} ${flags}`);
const isolatedEnvOutputArray = isolatedEnvOutput.split('\n').filter(str => str);
return isolatedEnvOutputArray[isolatedEnvOutputArray.length - 1];
}
isolateComponentWithCapsule(id: string, capsuleDir: string) {
return this.runCmd(`bit isolate ${id} --use-capsule --directory ${capsuleDir}`);
}
importExtension(id: string) {
return this.runCmd(`bit import ${id} --extension`);
}
build(id?: string = '') {
return this.runCmd(`bit build ${id}`);
}
buildComponentWithOptions(id: string = '', options: Object, cwd: string = this.scopes.localPath) {
const value = Object.keys(options)
.map(key => `-${key} ${options[key]}`)
.join(' ');
return this.runCmd(`bit build ${id} ${value}`, cwd);
}
testComponent(id: string = '') {
return this.runCmd(`bit test ${id}`);
}
testComponentWithOptions(id: string = '', options: Object, cwd: string = this.scopes.localPath) {
const value = Object.keys(options)
.map(key => `-${key} ${options[key]}`)
.join(' ');
return this.runCmd(`bit test ${id} ${value}`, cwd);
}
status() {
return this.runCmd('bit status');
}
statusJson() {
const status = this.runCmd('bit status --json');
return JSON.parse(status);
}
statusComponentIsStaged(id: string): boolean {
const status = this.statusJson();
return status.stagedComponents.includes(id);
}
statusComponentIsModified(id: string): boolean {
const status = this.statusJson();
return status.modifiedComponent.includes(id);
}
showComponent(id: string = 'bar/foo') {
return this.runCmd(`bit show ${id}`);
}
showComponentParsed(id: string = 'bar/foo') {
const output = this.runCmd(`bit show ${id} --json`);
return JSON.parse(output);
}
showComponentWithOptions(id: string = 'bar/foo', options: Object) {
const value = Object.keys(options)
.map(key => `-${key} ${options[key]}`)
.join(' ');
return this.runCmd(`bit show ${id} ${value}`);
}
checkoutVersion(version: string, ids: string, flags?: string, cwd?: string) {
return this.runCmd(`bit checkout ${version} ${ids} ${flags || ''}`, cwd);
}
checkout(values: string) {
return this.runCmd(`bit checkout ${values}`);
}
mergeVersion(version: string, ids: string, flags?: string) {
return this.runCmd(`bit merge ${version} ${ids} ${flags || ''}`);
}
diff(id?: string = '') {
const output = this.runCmd(`bit diff ${id}`);
return removeChalkCharacters(output);
}
log(id: string) {
return this.runCmd(`bit log ${id}`);
}
move(from: string, to: string) {
return this.runCmd(`bit move ${path.normalize(from)} ${path.normalize(to)}`);
}
ejectConf(id: string = 'bar/foo', options: ?Object) {
const value = options
? Object.keys(options) // $FlowFixMe
.map(key => `-${key} ${options[key]}`)
.join(' ')
: '';
return this.runCmd(`bit eject-conf ${id} ${value}`);
}
injectConf(id: string = 'bar/foo', options: ?Object) {
const value = options
? Object.keys(options) // $FlowFixMe
.map(key => `-${key} ${options[key]}`)
.join(' ')
: '';
return this.runCmd(`bit inject-conf ${id} ${value}`);
}
doctor(options: Object) {
const parsedOpts = this.parseOptions(options);
return this.runCmd(`bit doctor ${parsedOpts}`);
}
doctorOne(diagnosisName: string, options: Object, cwd?: string) {
const parsedOpts = this.parseOptions(options);
return this.runCmd(`bit doctor "${diagnosisName}" ${parsedOpts}`, cwd);
}
doctorList(options: Object) {
const parsedOpts = this.parseOptions(options);
return this.runCmd(`bit doctor --list ${parsedOpts}`);
}
doctorJsonParsed() {
const result = this.runCmd('bit doctor --json');
return JSON.parse(result);
}
parseOptions(options: Object): string {
const value = Object.keys(options)
.map((key) => {
const keyStr = key.length === 1 ? `-${key}` : `--${key}`;
return `${keyStr} ${options[key]}`;
})
.join(' ');
return value;
}
async runInteractiveCmd({
args = [],
inputs = [],
// Options for the process (execa)
processOpts = {
cwd: this.scopes.localPath
},
// opts for interactive
opts = {
defaultIntervalBetweenInputs: DEFAULT_DEFAULT_INTERVAL_BETWEEN_INPUTS,
verbose: false
}
}: {
args: string[],
inputs: InteractiveInputs,
processOpts: Object,
opts: {
// Default interval between inputs in case there is no specific interval
defaultIntervalBetweenInputs: number,
verbose: boolean
}
}) {
const processName = this.bitBin || 'bit';
opts.verbose = !!this.debugMode;
const { stdout } = await runInteractive({ processName, args, inputs, processOpts, opts });
if (this.debugMode) {
console.log(rightpad(chalk.green('output: \n'), 20, ' ')); // eslint-disable-line no-console
console.log(chalk.cyan(stdout)); // eslint-disable-line no-console
}
return stdout;
}
}