-
Notifications
You must be signed in to change notification settings - Fork 3
/
bin.js
executable file
·111 lines (96 loc) · 3.27 KB
/
bin.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
#!/usr/bin/env node
process.title = 'jalla'
var path = require('path')
var chalk = require('chalk')
var assert = require('assert')
var dedent = require('dedent')
var getPort = require('get-port')
var minimist = require('minimist')
var jalla = require('./index')
var COMMANDS = ['start', 'build', 'serve']
var argv = minimist(process.argv.slice(2), {
alias: {
'service-worker': 'sw',
dir: 'd',
quiet: 'q',
inspect: 'i',
skip: 's',
base: 'b',
watch: 'w',
port: 'p',
help: 'h',
version: 'v'
},
default: {
port: process.env.PORT || 8080
},
boolean: [
'help',
'quiet',
'version'
]
})
if (argv.help) {
console.log('\n', dedent`
${chalk.dim('usage')}
${chalk.cyan.bold('jalla')} [command] [opts] <entry>
${chalk.dim('commands')}
start start server and compile assets (default)
build build assets to disk
serve start server and serve built assets
${chalk.dim('options')}
--css entry point for CSS
--service-worker, --sw entry point for service worker
--base, -b base path where app will be mounted
--skip, -s skip transform for file/glob (excluding optimizations)
--watch, -w enable watch mode (default in development)
--dir, -d output directory, use with ${chalk.bold('build')} and ${chalk.bold('serve')}
--quiet, -q disable printing to console
--inspect, -i enable node inspector, accepts port
--port, -p server port
--version, -v print version
--help, -h show this help text
${chalk.dim('examples')}
${chalk.bold('start development server')}
jalla index.js
${chalk.bold('start development server with CSS and service worker entries')}
jalla index.js --sw sw.js --css index.css
${chalk.bold('build and start production server')}
NODE_ENV=production jalla build index.js && jalla serve index.js
`)
process.exit(0)
}
if (argv.version) {
console.log(require('./package.json').version)
process.exit(0)
}
var entry = argv._[argv._.length - 1]
var command = argv._.length > 1 ? argv._[0] : 'start'
assert(COMMANDS.includes(command), `jalla: command "${command}" not recognized`)
assert(entry, 'jalla: entry file should be supplied')
if (argv.inspect) {
if (typeof argv.inspect === 'number') process.debugPort = argv.inspect
process.kill(process.pid, 'SIGUSR1')
}
var opts = {}
if (argv.css) opts.css = argv.css
if (argv.base) opts.base = argv.base
if (argv.quiet) opts.quiet = argv.quiet
if (command === 'serve') opts.serve = argv.dir || true
if (argv['service-worker']) opts.sw = argv['service-worker']
if (typeof argv.watch !== 'undefined') opts.watch = Boolean(argv.watch)
if (command === 'build') {
opts.watch = false
const app = jalla(path.resolve(process.cwd(), entry), opts)
const dir = typeof argv.dir === 'string' ? argv.dir : 'dist'
app.build(path.resolve(process.cwd(), dir)).then(function () {
process.exit(0)
}, function () {
process.exit(1)
})
} else {
const app = jalla(path.resolve(process.cwd(), entry), opts)
getPort({ port: argv.port || 8080 }).then(function (port) {
app.listen(port)
})
}