-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·131 lines (119 loc) · 2.73 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
//-
//- Usage
//- lj [init|bench|build|help|test]
//-
//- build options
//- --banner, -b Add comment banner to output
//- --input, -i Input file
//- --output, -o Output file
//- --readme, -r Replase readme tags in file
//-
//- Examples
//- lj b -r README.md -i ui/dev.html -o ui/index.html
//- lj r
//-
var fs = require("fs")
, path = require("path")
, opts = require("./opts.js").opts(mergeOpts({
bench: {
samples: 10,
sampleTime: 500,
warmup: 2000
},
build_b: {
banner: "",
cat: true,
fetch: true,
out: "",
readme: "",
worker: ""
},
init_i: {},
lint: {
fix: false
},
release_r: {
build: true,
commit: true,
global: "",
install: true,
lint: true,
rewrite: false,
tag:true,
test: true,
update: true,
upstream: true
},
serve: {
port: 8080
},
static: {
base: "",
cat: true,
fetch: true,
min: true,
script: true,
out: "_site/",
},
test_t: {
coverage: false,
lcov: true,
sources: "./*.js",
status: true,
tz: "",
up: false,
watch: false
},
color: true,
help: false,
version: true
}, [ "package.json", "litejs", ".github/litejs.json", null ]))
, libFile = opts._cmd && "./lib/" + opts._cmd + ".js"
, package = require("./package.json")
, userPackage = {}
, hasOwn = userPackage.hasOwnProperty
try {
userPackage = require(path.resolve("package.json"))
} catch(e) {}
if (opts.tz) process.env.TZ = opts.tz
if (opts._unknown[0] && opts._cmd !== "test") {
console.error("\nError: Unknown option: " + opts._unknown)
usage(true)
process.exit(1)
} else if (libFile && !opts.help) {
if (opts.version) console.error("# %s %s@%s with %s@%s", opts._cmd, userPackage.name, userPackage.version, package.name, package.version)
require(libFile)(opts)
} else {
usage()
}
function usage(err) {
if (!err && opts.version) console.log("%s v%s", package.name, package.version)
var helpFile = libFile ? path.resolve(module.filename, "." + libFile) : module.filename
console.log(fs.readFileSync(helpFile, "utf8").match(/^\/\/-.*/gm).join("\n").replace(/^.../gm, ""))
}
function mergeOpts(opts, searchList) {
var file = searchList.shift()
, key = searchList.shift()
if (file) try {
var conf = require(path.resolve(file))
if (key) conf = conf[key]
if (conf) {
return assignOpts(opts, conf)
}
} catch(e) {}
if (searchList[0]) mergeOpts(opts, searchList)
return opts
}
function assignOpts(to, from) {
var key, val, tmp
for (key in to) if (hasOwn.call(to, key)) {
tmp = key.split("_")
val = hasOwn.call(from, tmp[0]) ? from[tmp[0]] : hasOwn.call(from, tmp[1]) ? from[tmp[1]] : null
if (val !== null) to[key] = isObj(val) ? assignOpts(to[key], val) : val
}
return to
}
function isObj(obj) {
return !!obj && obj.constructor === Object
}