-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbin.js
executable file
·93 lines (77 loc) · 2.04 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
#!/usr/bin/env node
var ansi = require('ansi-escape-sequences')
var subarg = require('subarg')
var path = require('path')
var pump = require('pump')
var Documentify = require('./')
var USAGE = `
$ ${clr('documentify', 'bold')} ${clr('[entry-file]', 'green')} [options]
Options:
-h, --help print usage
-v, --version print version
-t, --transform add a transform
Examples:
Start bundling HTML
${clr('$ documentify .', 'cyan')}
Running into trouble? Feel free to file an issue:
${clr('https://github.com/stackhtml/documentify/issues/new', 'cyan')}
Do you enjoy using this software? Become a backer:
${clr('https://opencollective.com/choo', 'cyan')}
`.replace(/\n$/, '').replace(/^\n/, '')
var argv = subarg(process.argv.slice(2), {
alias: {
help: 'h',
version: 'v',
transform: 't'
},
boolean: [
'help',
'version'
],
array: [
'transform'
]
})
;(function main (argv) {
var entry = argv._[0]
var html = null
// If entry isn't an absolute path, convert to absolute path
if (!entry) entry = process.cwd()
if (!/^\//.test(entry)) entry = path.join(process.cwd(), entry)
if (!process.stdin.isTTY) {
html = process.stdin
}
if (argv.help) {
console.log(USAGE)
} else if (argv.version) {
console.log(require('./package.json').version)
} else {
var bundler = Documentify(entry, html, {
transform: normalizeTransforms(argv.transform)
})
pump(bundler.bundle(), process.stdout, function (err) {
if (err) {
console.error(err)
process.exit(1)
}
})
}
})(argv)
function clr (text, color) {
return process.stdout.isTTY ? ansi.format(text, color) : text
}
function normalizeTransforms (transforms) {
if (!transforms) return []
if (!Array.isArray(transforms)) transforms = [transforms]
return transforms.map(function (t) {
if (typeof t === 'object' && Array.isArray(t._)) {
var name = t._[0]
delete t._
return [name, t]
}
if (Array.isArray(t)) {
return t
}
return [t, {}]
})
}