Skip to content

Commit cae451d

Browse files
Move UI code out of http.js (#417)
* WIP - non-fancy log output * Split UI from HTTP * Move more UI things out of /http * Lint fixes * trim unused UI code * lint * Only switch to alternate buffer for fancy ui * Rerender terminal UI when SSR completes * Make progress work * Initialise UI earlier * Fix lint and extract fatal error formatter helper
1 parent 5134829 commit cae451d

File tree

8 files changed

+267
-133
lines changed

8 files changed

+267
-133
lines changed

bin.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require('v8-compile-cache')
66

77
var ansi = require('ansi-escape-sequences')
88
var minimist = require('minimist')
9-
var dedent = require('dedent')
109
var path = require('path')
1110

1211
var USAGE = `
@@ -133,7 +132,6 @@ var argv = minimist(process.argv.slice(2), {
133132
} else if (cmd === 'inspect') {
134133
require('./lib/cmd-inspect')(path.join(entry), argv)
135134
} else if (cmd === 'start') {
136-
if (!argv.q) alternateBuffer()
137135
require('./lib/cmd-start')(path.join(entry), argv)
138136
} else {
139137
console.log(NOCOMMAND)
@@ -144,50 +142,3 @@ var argv = minimist(process.argv.slice(2), {
144142
function clr (text, color) {
145143
return process.stdout.isTTY ? ansi.format(text, color) : text
146144
}
147-
148-
// Switch to an alternate terminal buffer,
149-
// switch back to the main terminal buffer on exit.
150-
function alternateBuffer () {
151-
var q = Buffer.from('q')
152-
var esc = Buffer.from([0x1B])
153-
154-
process.stdout.write('\x1b[?1049h') // Enter alternate buffer.
155-
process.stdout.write('\x1b[H') // Reset screen to top.
156-
process.stdout.write('\x1b[?25l') // Hide cursor
157-
158-
process.on('unhandledRejection', onexit)
159-
process.on('uncaughtException', onexit)
160-
process.on('SIGTERM', onexit)
161-
process.on('SIGINT', onexit)
162-
process.on('exit', onexit)
163-
process.stdin.on('data', handleKey)
164-
165-
function handleKey (buf) {
166-
if (buf.compare(q) === 0 || buf.compare(esc) === 0) {
167-
onexit()
168-
}
169-
}
170-
171-
function onexit (statusCode) {
172-
process.stdout.write('\x1b[?1049l') // Enter to main buffer.
173-
process.stdout.write('\x1b[?25h') // Restore cursor
174-
175-
if (statusCode instanceof Error) {
176-
console.error('A critical error occured, forcing Bankai to abort:\n')
177-
console.error(clr(statusCode.stack, 'red') + '\n')
178-
console.error(dedent`
179-
If you think this might be a bug in Bankai, please consider helping
180-
improve Bankai's stability by submitting an error to:
181-
182-
${' ' + clr('https://github.com/choojs/bankai/issues/new', 'underline')}
183-
184-
Please include the steps to reproduce this error, the stack trace
185-
printed above, your version of Node, and your version of npm. Thanks!
186-
${clr('— Team Choo', 'italic')}
187-
` + '\n')
188-
statusCode = 1
189-
}
190-
191-
process.exit(statusCode)
192-
}
193-
}

http.js

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
var EventEmitter = require('events').EventEmitter
22
var gzipMaybe = require('http-gzip-maybe')
3-
var gzipSize = require('gzip-size')
43
var assert = require('assert')
54
var path = require('path')
65
var pump = require('pump')
76
var send = require('send')
87

98
var Router = require('./lib/regex-router')
10-
var ui = require('./lib/ui')
119
var bankai = require('./')
1210

13-
var files = [
14-
'assets',
15-
'documents',
16-
'scripts',
17-
'manifest',
18-
'styles',
19-
'service-worker'
20-
]
21-
2211
module.exports = start
2312

2413
function start (entry, opts) {
@@ -27,73 +16,24 @@ function start (entry, opts) {
2716
assert.equal(typeof entry, 'string', 'bankai/http: entry should be type string')
2817
assert.equal(typeof opts, 'object', 'bankai/http: opts should be type object')
2918

30-
var quiet = !!opts.quiet
3119
opts = Object.assign({ reload: true }, opts)
3220
var compiler = bankai(entry, opts)
3321
var router = new Router()
3422
var emitter = new EventEmitter()
3523
var id = 0
3624
var state = {
37-
count: compiler.metadata.count,
38-
files: {},
39-
sse: 0,
40-
size: 0
25+
sse: 0
4126
}
4227

43-
files.forEach(function (filename) {
44-
state.files[filename] = {
45-
name: filename,
46-
progress: 0,
47-
timestamp: ' ',
48-
size: 0,
49-
status: 'pending',
50-
done: false
51-
}
52-
})
53-
54-
if (!quiet) var render = ui(state)
55-
compiler.on('error', function (topic, sub, err) {
56-
if (err.pretty) state.error = err.pretty
57-
else state.error = `${topic}:${sub} ${err.message}\n${err.stack}`
58-
if (!quiet) render()
59-
})
60-
61-
compiler.on('progress', function () {
62-
state.error = null
63-
if (!quiet) render()
64-
})
65-
6628
compiler.on('ssr', function (result) {
6729
state.ssr = result
6830
})
6931

7032
compiler.on('change', function (nodeName, edgeName, nodeState) {
7133
var node = nodeState[nodeName][edgeName]
7234
var name = nodeName + ':' + edgeName
73-
var data = {
74-
name: nodeName,
75-
progress: 100,
76-
timestamp: time(),
77-
size: 0,
78-
status: 'done',
79-
done: true
80-
}
81-
state.files[nodeName] = data
82-
8335
if (name === 'documents:index.html') emitter.emit('documents:index.html', node)
8436
if (name === 'styles:bundle') emitter.emit('styles:bundle', node)
85-
86-
// Only calculate the gzip size if there's a buffer. Apparently zipping
87-
// an empty file means it'll pop out with a 20B base size.
88-
if (node.buffer.length) {
89-
gzipSize(node.buffer)
90-
.then(function (size) { data.size = size })
91-
.catch(function () { data.size = node.buffer.length })
92-
.then(function () {
93-
if (!quiet) render()
94-
})
95-
}
96-
if (!quiet) render()
9737
})
9838

9939
router.route(/^\/manifest.json$/, function (req, res, params) {
@@ -182,7 +122,7 @@ function start (entry, opts) {
182122
emitter.on('documents:index.html', reloadScript)
183123
emitter.on('styles:bundle', reloadStyle)
184124
state.sse += 1
185-
if (!quiet) render()
125+
compiler.emit('sse-connect')
186126

187127
res.writeHead(200, {
188128
'Content-Type': 'text/event-stream',
@@ -208,7 +148,7 @@ function start (entry, opts) {
208148
emitter.removeListener('styles:bundle', reloadStyle)
209149
connected = false
210150
state.sse -= 1
211-
if (!quiet) render()
151+
compiler.emit('sse-disconnect')
212152
}
213153
}
214154

@@ -277,16 +217,3 @@ function gzip (buffer, req, res) {
277217
pump(zipper, res)
278218
zipper.end(buffer)
279219
}
280-
281-
function time () {
282-
var date = new Date()
283-
var hours = numPad(date.getHours())
284-
var minutes = numPad(date.getMinutes())
285-
var seconds = numPad(date.getSeconds())
286-
return `${hours}:${minutes}:${seconds}`
287-
}
288-
289-
function numPad (num) {
290-
if (num < 10) num = '0' + num
291-
return num
292-
}

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ module.exports = Bankai
2121

2222
function Bankai (entry, opts) {
2323
if (!(this instanceof Bankai)) return new Bankai(entry, opts)
24+
25+
Emitter.call(this)
26+
2427
opts = opts || {}
2528
this.local = localization(opts.language || 'en-US')
2629
this.log = pino(opts.logStream || process.stdout)

lib/cmd-start.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ var getPort = require('get-port')
33
var isElectronProject = require('./is-electron-project')
44
var http = require('./http-server')
55
var bankai = require('../http')
6+
var createTui = require('./ui')
7+
var createLogUi = require('./ui-basic')
68

79
module.exports = start
810

911
function start (entry, opts) {
1012
var handler = bankai(entry, opts)
13+
var state = handler.state
14+
15+
var createUi = opts.simple ? createLogUi : createTui
16+
var render = createUi(handler.compiler, state)
1117

1218
isElectronProject(handler.compiler.dirname, function (err, bool) {
1319
if (err) throw err
1420
opts.electron = bool
1521

16-
var state = handler.state // TODO: move all UI code into this file
1722
var server = http.createServer(function (req, res) {
1823
if (req.type === 'OPTIONS') return cors(req, res)
1924
handler(req, res, function () {
@@ -22,6 +27,8 @@ function start (entry, opts) {
2227
})
2328
})
2429

30+
render()
31+
2532
getPort({port: 8080})
2633
.then(function (port) {
2734
server.listen(port, function () {

lib/fatal-error.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var ansi = require('ansi-escape-sequences')
2+
var dedent = require('dedent')
3+
4+
function clr (text, color) {
5+
return process.stdout.isTTY ? ansi.format(text, color) : text
6+
}
7+
8+
module.exports = function fatalError (err) {
9+
return dedent`
10+
A critical error occured, forcing Bankai to abort:
11+
${clr(err.stack, 'red')}
12+
13+
If you think this might be a bug in Bankai, please consider helping
14+
improve Bankai's stability by submitting an error to:
15+
16+
${clr('https://github.com/choojs/bankai/issues/new', 'underline')}
17+
18+
Please include the steps to reproduce this error, the stack trace
19+
printed above, your version of Node, and your version of npm. Thanks!
20+
${clr('— Team Choo', 'italic')}
21+
22+
`
23+
}

lib/graph-assets.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var dirs = [
1717
// 3. Estimate total size of all files combined, and emit `size`.
1818
//
1919
// TODO: optimize assets (on the fly); e.g. convert images to webp, etc.
20-
// TODO: also emit `progress`.
2120

2221
module.exports = node
2322

@@ -43,7 +42,7 @@ function node (state, createEdge) {
4342
})
4443

4544
tracker.on('progress', function (progress) {
46-
// self.emit('progress', 'assets', progress)
45+
self.emit('progress', 'assets', progress)
4746
})
4847

4948
this.on('close', function () {

0 commit comments

Comments
 (0)