-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
492 lines (417 loc) · 15.5 KB
/
app.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env node
// **********************************************
// ** Bivariate is an opinionated interface **
// ** for writing and running BackstopJS tests **
// **********************************************
// jshint esversion: 6
let colors = require("colors") // pretty console colors
let inquirer = require('inquirer') // prompt questions and gather answers
let Spinner = require('cli-spinner').Spinner // cool console spinner (progress indicator)
let spawn = require('child_process').spawn // built in node module for spawning child processes
let path = require('path')
let pjson = require('./package.json')
let pressEnterToContinue = require('./app_modules/_pressEnterToContinue')
let cls = require('./app_modules/_clearConsole')
let blank = require('./app_modules/_blankLine')
let asciiLogo = require('./app_modules/_asciiLogo')
let checkIfDirectoryExists = require('./app_modules/_checkIfDirectoryExists')
let checkForExistingTests = require('./app_modules/_checkForExistingTests')
let checkForExistingReports = require('./app_modules/_checkForExistingReports')
let checkForExistingReferences = require('./app_modules/_checkForExistingReferences')
let testGroupActions = require('./app_modules/_testGroupActions')
let getTestGroups = require('./app_modules/_getTestGroups')
let getApproveGroups = require('./app_modules/_getApproveGroups')
let getPath = require('./app_modules/_getPath')
let deleteFolder = require('./app_modules/_deleteFolder')
let updateHeader = require('./app_modules/_updateHeader')
let testConfig
// options:
// runcmdoutput: true / false <-- log stdout/stderr
// -----------------
// - setup spinner -
// -----------------
let spinner = new Spinner(' ')
spinner.setSpinnerString(25)
// ----------------------------
// - abracadabra - it's Magic -
// ----------------------------
function abracadabra(msg) {
testConfig = require(getPath('bivariate_data/test_scripts/__config-common.js'))()
cls()
asciiLogo()
if (typeof msg === 'undefined' ) { msg = '' }
console.log('----------------------------------'.bgWhite.black + pjson.version.bgWhite.black + '-'.bgWhite.black)
console.log((msg).bgWhite.black)
let questionsTestType = require('./app_modules/_questionsTestType')
inquirer.prompt(questionsTestType).then(function (answerAction) {
if (answerAction.testType !== '') {
switch(answerAction.testType) {
// CREATE NEW BLANK TEST
case 'create-new-test':
require('./app_modules/_createNewTestFile')()
.then(function() {
blank()
pressEnterToContinue('test created, make sure to include it in the \n' + 'Scenarios'.yellow.bold + ' section of a ' + 'Test Group'.yellow.bold + ' to run it \n\n.press enter to continue...', abracadabra) // restart app
})
.catch(function(err) {
console.log(err.bgRed.white)
})
break
// CREATE NEW BLANK TEST
case 'create-new-test-group':
require('./app_modules/_createNewTestGroupFile')()
.then(function() {
blank()
pressEnterToContinue('blank test group created, make sure to include any tests you want it to run in the \n' + 'Scenarios'.yellow.bold + ' section. \n\n.press enter to continue...', abracadabra) // restart app
})
.catch(function(err) {
console.log(err.bgRed.white)
})
break
// UPDATE -all- TEST GROUP WITH EXISTING TEST FILES
case 'update-all-test-group':
require('./app_modules/_updateAllTestGroup')()
.then(function() {
// blank()
pressEnterToContinue('-all-tests-.js'.yellow.bold + ' scenario was updated/created with all ' + 'test files'.yellow.bold + '\n\n.press enter to continue...', abracadabra) // restart app
})
.catch(function(err) {
console.log(err.bgRed.white)
})
break
// OPEN REPORT
case 'open-report':
checkForExistingReferences(false)
.then(function([existingReferenceList, isLocked]) {
if(existingReferenceList.length > 0) {
checkForExistingTests(false).then(function(testList) {
if(testList.length > 0) {
testGroupActions(answerAction.testType, existingReferenceList, spinner, testConfig)
.then(function(message) {
abracadabra(message)
})
}
else {
blank()
console.log(('Please run a Test before attempting to open a Report.').bgRed.white)
blank()
pressEnterToContinue('press enter to continue...', abracadabra)
}
})
}
else {
blank()
console.log(('Please create a Reference and run a Test before attempting to open a Report.').bgRed.white)
blank()
pressEnterToContinue('press enter to continue...', abracadabra)
}
})
break
// DELETE Test directories
case 'delete-tests':
spinner.stop()
checkForExistingTests(false)
.then(function(testList) {
if(testList.length > 0) {
deleteFolder('Test Results', 'backstop_data/bitmaps_test')
.then(function() {
pressEnterToContinue('press enter to continue...', abracadabra)
})
}
else {
blank()
console.log(('!!! No Test Data Exists !!!').bgRed.white)
pressEnterToContinue('press enter to continue...', abracadabra)
}
})
break
// DELETE Report directories
case 'delete-reports':
spinner.stop()
checkForExistingReports(false)
.then(function(testList) {
if(testList.length > 0) {
deleteFolder('Reports', 'backstop_data/html_report', true)
.then(function() {
pressEnterToContinue('press enter to continue...', abracadabra)
})
}
else {
blank()
console.log(('!!! No Reports Exist !!!').bgRed.white)
pressEnterToContinue('press enter to continue...', abracadabra)
}
})
break
// DELETE REFERENCES
case 'delete-reference':
case 'delete-testgroup-reference':
checkForExistingReferences(false)
.then(function([existingReferenceList, isLocked]) {
blank()
if (existingReferenceList.length === 0) {
console.log(('You do not have a Current Reference to Delete.').bgRed.white)
pressEnterToContinue('press enter to continue...', abracadabra)
}
else if (isLocked) {
console.log(('The Current Reference is Locked.').bgRed.white)
console.log(('You must Unlock is before it can be Deleted or altered.').bgRed.white)
pressEnterToContinue('press enter to continue...', abracadabra)
}
else if(!isLocked && existingReferenceList.length > 0) {
// DELETE Reference Group
if(answerAction.testType === 'delete-testgroup-reference') {
testGroupActions(answerAction.testType, existingReferenceList, spinner, testConfig)
.then(function(message) {
pressEnterToContinue('press enter to continue...', function() {
abracadabra(message)
})
})
}
// DELETE Reference directories
else {
console.log("deleting references:", existingReferenceList)
deleteFolder('Current References', 'backstop_data/bitmaps_reference', true)
.then(function() {
pressEnterToContinue('Press enter to continue...', abracadabra)
})
}
}
})
break
// LIST existing references
case 'list-references':
checkForExistingReferences(true)
.then(function() {
pressEnterToContinue('press enter to continue...', abracadabra) // restart app
})
break
// LIST existing tests
case 'list-tests':
checkForExistingTests(true)
.then(function() {
pressEnterToContinue('press enter to continue...', abracadabra) // restart app
})
break
// ARCHIVE current References
case 'archive-reference':
require('./app_modules/_archiveReference')()
.then(function() {
blank()
pressEnterToContinue('press enter to continue...', abracadabra) // restart app
})
.catch(function(err) {
console.log(err.bgRed.white)
})
break
// RESTORE an Archived Reference
case 'restore-reference':
let restoreReference = require('./app_modules/_restoreReference')
restoreReference()
.then(function() {
blank()
pressEnterToContinue('press enter to continue...', abracadabra) // restart app
})
.catch(function(err) {
console.log(err.bgRed.white)
})
break
// LOCK / UNLOCK Current Reference
case 'lock-reference':
case 'unlock-reference':
checkIfDirectoryExists(getPath('backstop_data/bitmaps_reference'))
.then(function() {
let lockIt = (answerAction.testType === 'lock-reference' ? true : false)
require('./app_modules/_lockReference')(lockIt)
.then(function() {
pressEnterToContinue('press enter to continue...', abracadabra)
})
})
.catch(function() {
blank()
console.log(('No Reference exists to ' + answerAction.testType.toUpperCase() + ', please Create or Restore a REFERENCE first.').bgRed.white)
pressEnterToContinue('press enter to continue...', abracadabra)
})
break
// CREATE REFERENCE
case 'reference':
checkForExistingReferences(false)
.then(function([existingReferenceList, isLocked]) {
if(isLocked) {
blank()
console.log(('You must first Unlock the Reference before').bgRed.white)
console.log(('adding additional Test Groups to it.').bgRed.white)
pressEnterToContinue('press enter to continue...', abracadabra)
}
else {
testGroupActions(answerAction.testType, existingReferenceList, spinner, testConfig)
.then(function(message) {
pressEnterToContinue(message + "Press enter to return to the main menu...", abracadabra)
})
.catch(function() {
pressEnterToContinue('An Error Occurred, press enter to return to the main menu...', abracadabra)
})
}
})
break
// RUN APPROVE
case 'approve':
// test to see if a test exists
console.log(getPath('backstop_data/bitmaps_test'))
checkIfDirectoryExists(getPath('backstop_data/bitmaps_test'))
.then(function() {
// APPROVE
if(answerAction.testType === 'approve') {
getApproveGroups()
.then(function(approveGroups) {
// console.log("approveGroups", approveGroups)
if(approveGroups.length > 0) {
console.log(('When running this command, all images (with changes) from your').bgWhite.black)
console.log(('most recent test batch will be promoted to your reference collection.').bgWhite.black)
console.log(('Subsequent tests will be compared against your updated reference files.').bgWhite.black)
testGroupActions(answerAction.testType, approveGroups, spinner, testConfig)
.then(function(message) {
pressEnterToContinue(message + '"Press enter to continue...', abracadabra)
})
}
else {
blank()
console.log(('You don\'t have any matching REFERENCE & TEST Groups to Approve.').bgRed.white)
pressEnterToContinue('"Press enter to continue...', abracadabra)
}
})
}
})
.catch(function() {
blank()
console.log(('You don\'t have any matching REFERENCE & TEST Groups to Approve.').bgRed.white)
pressEnterToContinue('"Press enter to continue...', abracadabra)
})
break
// RUN TEST
case 'test':
// test to see if a reference exists
checkIfDirectoryExists(getPath('backstop_data/bitmaps_reference'))
.then(function() {
checkForExistingReferences(false)
.then(function([existingReferenceList, isLocked]) {
// TEST
getTestGroups()
.then(function(testGroups) {
if(testGroups.length > 0) {
testGroupActions(answerAction.testType, existingReferenceList, spinner, testConfig)
.then(function([message, testPath]) {
updateHeader(testPath)
message = (typeof message === "undefined") ? "" : message
pressEnterToContinue(message + '"Press enter to continue...', abracadabra)
})
}
else {
blank()
console.log(('You don\'t have any Test Groups to run a Test from.').bgRed.white)
}
})
})
})
.catch(function() {
blank()
console.log(('No Reference exists to ' + answerAction.testType.toUpperCase() + ', please Create or Restore a REFERENCE first.').bgRed.white)
pressEnterToContinue('press enter to continue...', abracadabra)
})
break
case 'delete-testgroup-tests':
checkForExistingTests(false)
.then(function(existingTests) {
testGroupActions(answerAction.testType, existingTests, spinner, testConfig)
.then(function(message) {
pressEnterToContinue('press enter to continue...', function() {
abracadabra(message)
})
})
})
break
default:
testGroupActions(answerAction.testType, [], spinner, testConfig)
.then(function(message) {
abracadabra(message)
})
} //\ end switch
}
else {
cls()
asciiLogo()
console.log('\nThanks for using '.green + 'Bivariate'.cyan)
console.log('Have a Great Day!\n'.green)
}
})
}
// *************
// ** Run App **
// *************
checkIfDirectoryExists(getPath('bivariate_data'))
.then(function() {
let libsSrc = path.join(__dirname, 'init-bivariate-data/libs/')
let libsDest = path.join(process.cwd(), 'bivariate_data/libs/')
let copy = require('recursive-copy')
copy(libsSrc, libsDest, { overwrite: true }, function(error, results) {
if (error) {
console.error('libs failed: ' + error)
} else {
abracadabra()
}
})
})
.catch(function() {
return inquirer.prompt(
[{
type: 'confirm',
name: 'confirmInit',
message: 'No "bivariate_data" folder found.\nWould like like to generate the base/example test and configuration files?',
default: true
}]
)
.then(function (answer) {
return new Promise(function(resolve, reject) {
if(answer.confirmInit) {
let src = path.join(__dirname, 'init-bivariate-data')
let desc = path.join(process.cwd(), 'bivariate_data')
console.log("GENERATE DATA......")
// console.log("src", src)
// console.log("desc", desc)
let copy = require('recursive-copy')
copy(src, desc, function(error, results) {
if (error) {
console.error('config file generation failed: ' + error)
} else {
console.log('Generated base/example test and configuration files.')
return inquirer.prompt(
[{
type: 'confirm',
name: 'confirmStartapp',
message: 'Would you like to run Bivariate with the newly generated example tests and configuration?',
default: true
}]
)
.then(function (answer) {
return new Promise(function(resolve, reject) {
if(answer.confirmStartapp) {
abracadabra()
} else {
cls()
asciiLogo()
console.log('\nThanks for using '.green + 'Bivariate'.cyan)
console.log('Have a Great Day!\n'.green)
resolve()
}
})
})
}
})
} else {
blank()
console.log('You can not run Bivariate without a "bivariate_data" folder containing your tests and configuration files')
resolve()
}
})
})
})