This repository was archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreateUI.ts
More file actions
211 lines (192 loc) · 5.83 KB
/
Copy pathcreateUI.ts
File metadata and controls
211 lines (192 loc) · 5.83 KB
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
import chalk from 'chalk'
import util from 'util'
import prettyFormatStep from './prettyFormatStep'
export default function createUI() {
const ui = {
testLoadStarted() {
console.log(
chalk.bold.yellow('## Loading test and generating test plan...')
)
},
testLoadCompleted(test) {
console.log(
chalk.dim('* ') + chalk.green('Test plan generated successfully!')
)
console.log()
},
testLoadError(e: Error) {
console.log(chalk.bold.red('Cannot load the test file.'))
console.log(chalk.red(e.stack || 'Unknown stack trace...'))
console.log()
},
moduleUncacheStarted() {
console.log(chalk.bold.yellow('## Clearing Node module cache...'))
},
moduleUncached(key: string) {
console.log(chalk.dim('*'), 'Reloading', chalk.cyan(key))
},
moduleUncacheCompleted() {
console.log()
},
onUserInterrupted() {
console.log(chalk.yellow('Interrupted by user.'))
},
developmentModeStarted(environment) {
startInteractiveMode(environment)
}
}
function startInteractiveMode(environment) {
console.log(chalk.bold.yellow('## Entering development mode...'))
console.log('Welcome to prescript development mode.')
console.log()
announceStatus()
hint('help', 'for more information')
console.log()
const vorpal = require('vorpal')()
vorpal
.command('inspect')
.alias('i')
.description('Inspect the test state')
.action(function(args, callback) {
console.log('This is current test state:')
console.log(util.inspect(environment.getState()))
console.log()
callback()
})
vorpal
.command('status')
.alias('s')
.description('Show the test status')
.action(function(args, callback) {
console.log('This is the test plan with current test status:')
const currentStepNumber = environment.getCurrentStepNumber()
const previousResult = environment.getPreviousResult()
let printed = false
environment.forEachStep(step => {
const prefix =
step.number === currentStepNumber
? chalk.bold.blue('次は')
: previousResult && step.number === previousResult.stepNumber
? previousResult.error
? chalk.bold.bgRed(' NG ')
: chalk.bold.bgGreen(' OK ')
: ' '
printed = true
console.log(prefix, prettyFormatStep(step))
})
if (!printed) {
console.log(chalk.yellow('The test plan is empty.'))
}
console.log()
announceStatus()
console.log()
callback()
})
vorpal
.command('reload')
.alias('r')
.description('Reload the test file')
.action(function(args, callback) {
const reloadResult = environment.reload()
console.log('Test file is reloaded.')
const jump = reloadResult.jump
if (jump) {
if (jump.success) {
console.log(
'Jumping to',
prettyFormatStep(environment.getCurrentStep())
)
} else {
console.log('Cannot jump to previously run step ' + jump.target)
}
}
console.log()
announceStatus()
console.log()
callback()
})
vorpal
.command('continue')
.alias('c')
.description('Continue running the test until there is an error')
.action(function(args, callback) {
handleRun(environment.continue(), callback)
})
.cancel(() => {
environment.cancel()
})
vorpal
.command('next')
.alias('n')
.description('Run the next step.')
.action(function(args, callback) {
handleRun(environment.nextStep(), callback)
})
vorpal
.command('jump <stepNumber>')
.alias('j')
.description('Jump to a step number')
.action(function(args, callback) {
environment.jumpTo(String(args.stepNumber))
announceStatus()
console.log()
callback()
})
vorpal
.command('runto <stepNumber>')
.description('Run from current step to step number')
.action(function(args, callback) {
handleRun(environment.runTo(String(args.stepNumber)), callback)
})
vorpal.delimiter('prescript>').show()
function handleRun(promise, callback) {
return promise.then(
() => {
announcePrevious()
announceStatus()
console.log()
callback && callback()
},
err => {
callback && callback(err)
}
)
}
function announcePrevious() {
const previousResult = environment.getPreviousResult()
if (previousResult) {
if (previousResult.error) {
console.log(
chalk.bold.red(
'Step ' + previousResult.stepNumber + ' encountered an error'
)
)
}
}
}
function announceStatus() {
const previousResult = environment.getPreviousResult()
const current = environment.getCurrentStepNumber()
if (previousResult && previousResult.error) {
hint('reload', 'after fixing the test to reload the test file')
}
if (current) {
console.log(
chalk.bold.blue('次は'),
prettyFormatStep(environment.getCurrentStep())
)
hint('next', 'to run this step only')
hint('continue', 'to run from this step until next error')
hint('runto', 'to run into specific step')
} else {
console.log(chalk.yellow('Nothing to be run.'))
hint('status', 'to see the test plan')
hint('jump', 'to jump to a step number')
}
}
}
function hint(commandName, description) {
console.log('Type', chalk.cyan(commandName), description)
}
return ui
}