UNPKG

4.04 kBJavaScriptView Raw
1#! /usr/bin/env node
2
3process.title = 'bankai'
4
5require('v8-compile-cache')
6
7var ansi = require('ansi-escape-sequences')
8var minimist = require('minimist')
9var dedent = require('dedent')
10var path = require('path')
11
12var USAGE = `
13 $ ${clr('bankai', 'bold')} ${clr('<command> [entry]', 'green')} [options]
14
15 Commands:
16
17 build compile all files to ${clr('dist/', 'green')}
18 inspect inspect the bundle dependencies
19 start start a development server
20
21 Options:
22
23 -d, --debug output lots of logs
24 -h, --help print usage
25 -q, --quiet don't output any logs
26 -v, --version print version
27
28 Examples:
29
30 Start a development server
31 ${clr('$ bankai start index.js', 'cyan')}
32
33 Visualize all dependencies in your project
34 ${clr('$ bankai inspect index.js', 'cyan')}
35
36 Compile all files in the project to disk
37 ${clr('$ bankai build index.js', 'cyan')}
38
39 Running into trouble? Feel free to file an issue:
40 ${clr('https://github.com/choojs/bankai/issues/new', 'cyan')}
41
42 Do you enjoy using this software? Become a backer:
43 ${clr('https://opencollective.com/choo', 'cyan')}
44`.replace(/\n$/, '').replace(/^\n/, '')
45
46var NOCOMMAND = `
47 Please specify a bankai command:
48 ${clr('$ bankai', 'cyan')} ${clr('<command>', 'green')}
49
50 For example:
51 ${clr('$ bankai start', 'cyan')} ${clr('index.js', 'green')}
52
53 Run ${clr('bankai --help', 'cyan')} to see all options.
54`.replace(/\n$/, '').replace(/^\n/, '')
55
56var argv = minimist(process.argv.slice(2), {
57 alias: {
58 help: 'h',
59 quiet: 'q',
60 version: 'v',
61 base: 'b'
62 },
63 boolean: [
64 'help',
65 'quiet',
66 'version'
67 ]
68})
69
70;(function main (argv) {
71 var cmd = argv._[0]
72 var entry = argv._[1]
73
74 if (entry) {
75 if (!path.isAbsolute(entry)) entry = path.join(process.cwd(), entry)
76 } else {
77 entry = process.cwd()
78 }
79
80 if (argv.help) {
81 console.log(USAGE)
82 } else if (argv.version) {
83 console.log(require('./package.json').version)
84 } else if (cmd === 'build') {
85 var outdir = argv._[2]
86 require('./lib/cmd-build')(path.join(entry), outdir, argv)
87 } else if (cmd === 'inspect') {
88 require('./lib/cmd-inspect')(path.join(entry), argv)
89 } else if (cmd === 'start') {
90 if (!argv.q) alternateBuffer()
91 require('./lib/cmd-start')(path.join(entry), argv)
92 } else {
93 console.log(NOCOMMAND)
94 process.exit(1)
95 }
96})(argv)
97
98function clr (text, color) {
99 return process.stdout.isTTY ? ansi.format(text, color) : text
100}
101
102// Switch to an alternate terminal buffer,
103// switch back to the main terminal buffer on exit.
104function alternateBuffer () {
105 var q = Buffer.from('q')
106 var esc = Buffer.from([0x1B])
107
108 process.stdout.write('\x1b[?1049h') // Enter alternate buffer.
109 process.stdout.write('\x1b[H') // Reset screen to top.
110 process.stdout.write('\x1b[?25l') // Hide cursor
111
112 process.on('unhandledRejection', onexit)
113 process.on('uncaughtException', onexit)
114 process.on('SIGTERM', onexit)
115 process.on('SIGINT', onexit)
116 process.on('exit', onexit)
117 process.stdin.on('data', handleKey)
118
119 function handleKey (buf) {
120 if (buf.compare(q) === 0 || buf.compare(esc) === 0) {
121 onexit()
122 }
123 }
124
125 function onexit (statusCode) {
126 process.stdout.write('\x1b[?1049l') // Enter to main buffer.
127 process.stdout.write('\x1b[?25h') // Restore cursor
128
129 if (statusCode instanceof Error) {
130 console.error('A critical error occured, forcing Bankai to abort:\n')
131 console.error(clr(statusCode.stack, 'red') + '\n')
132 console.error(dedent`
133 If you think this might be a bug in Bankai, please consider helping
134 improve Bankai's stability by submitting an error to:
135
136 ${' ' + clr('https://github.com/choojs/bankai/issues/new', 'underline')}
137
138 Please include the steps to reproduce this error, the stack trace
139 printed above, your version of Node, and your version of npm. Thanks!
140 ${clr('— Team Choo', 'italic')}
141 ` + '\n')
142 statusCode = 1
143 }
144
145 process.exit(statusCode)
146 }
147}