-
-
Notifications
You must be signed in to change notification settings - Fork 782
/
display.ts
45 lines (37 loc) · 1.22 KB
/
display.ts
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
import {through} from './streams'
import File from 'vinyl'
import {log} from '@blitzjs/display'
import chalk from 'chalk'
import {Event, FILE_WRITTEN, INIT, ERROR_THROWN, READY} from './events'
/**
* Display is a stream that converts build status events and prepares them for the console.
* A good way to think about this is as the root of the "view" component of the application.
*/
export function createDisplay() {
let lastEvent: Event<any> = {type: INIT, payload: null}
let spinner = log.spinner('Preparing for launch').start()
const stream = through({objectMode: true}, (event: Event<File>, _, next) => {
switch (event.type) {
case FILE_WRITTEN: {
const filePath = event.payload.history[0].replace(process.cwd(), '')
spinner.text = filePath
break
}
case ERROR_THROWN: {
// Tidy up if operational error is encountered
if (lastEvent.type === FILE_WRITTEN) {
spinner.fail('Uh oh something broke')
}
break
}
case READY: {
spinner.succeed(chalk.green.bold('Prepped for launch'))
break
}
}
// Capture last event incase we need to tidy up the console
lastEvent = event
next()
})
return {stream}
}