-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·88 lines (78 loc) · 2.29 KB
/
cli.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
#! /usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const exit = require('exit')
const app = require('commander')
const compiler = require('./index.js')
const { log, resolve, join } = require('./util.js')
app
.arguments('<in> <outDir>')
.option('-w, --watch', 'watch ur files')
.option('--jsx <pragma>', 'jsx pragma: --jsx preact.h (default: React.createElement)')
.option('--map <type>', 'any source map value supported by webpack: --map cheap-module-source-map (default: source-map)')
.option('--sass', 'use sass instead of postcss: --sass (default: false)')
.option('--reload', 'enable live-reloading after changes: --reload (default: false)')
.option('--config <config>', 'config file: --config config.js (default: spaghetti.config.js)')
.parse(process.argv)
const input = app.args[0] || null
const outDir = app.args[1] || null
const filename = (input ? path.basename(input, '.js') : null)
const watch = app.watch || false
const jsx = app.jsx || 'React.createElement'
const reload = app.reload || false
const conf = resolve(app.config || 'spaghetti.config.js')
let config = Object.assign({
in: input,
outDir,
filename,
jsx,
watch,
alias: {},
reload: false
}, (fs.existsSync(conf) ? require(conf) : {}))
/**
* If provided in config file
*/
config.in = resolve(config.in)
config.outDir = resolve(config.outDir)
config.filename = config.filename || path.basename(config.in, '.js')
if (!config.filename) {
log(c => ([
c.red('bad config'),
'- filename missing'
]))
exit()
}
const bundle = compiler(config)
log('compiling')
if (config.watch) {
bundle.watch()
.end(({ duration }) => {
log(c => ([
c.green(`built`),
`in ${duration}ms`
]))
})
.error(err => {
log(c => ([
c.red(`error`),
err ? err.message || err : ''
]))
})
} else {
bundle.build()
.end(({ duration, assets }) => {
log(c => `${c.green(`built`)} in ${duration}ms\n${assets.reduce((_, asset) => {
const size = asset.size.gzip ? asset.size.gzip + 'kb gzipped' : asset.size.raw + 'kb'
return _ += ` > ${c.green(asset.filename)} ${size}\n`
}, '')}
`)
})
.error(err => {
log(c => ([
c.red(`error`),
err ? err.message || err : ''
]))
})
}