-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·251 lines (222 loc) · 5.58 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
exports.command = command
exports.cp = cp
exports.debounce = debounce
exports.deepAssign = deepAssign
exports.dom = require("@litejs/dom")
exports.hold = hold
exports.isObj = isObj
exports.ls = ls
exports.mkdirp = mkdirp
exports.readFile = readFile
exports.rmrf = rmrf
exports.wait = wait
exports.watch = watch
exports.writeFile = writeFile
exports.writePackage = writePackage
exports.cols = +process.env.COLUMNS || process.stdout.columns || 80
exports.rows = +process.env.ROWS || process.stdout.rows || 24
if (parseInt(process.version.slice(1), 10) < 15) require("./lib/shim.js")
var child = require("child_process")
, fs = require("fs")
, path = require("path")
, hasOwn = {}.hasOwnProperty
function command(name) {
try {
return !!child.execSync((process.platform === "win32" ? "where " : "command -v ") + name)
} catch (e) {}
return false
}
function cp(src, dest) {
if (fs.statSync(src).isDirectory()) {
mkdirp(dest)
fs.readdirSync(src).forEach(function(file) {
cp(path.join(src, file), path.join(dest, file))
})
} else {
console.error("cp", src, dest)
fs.copyFileSync(src, dest)
}
}
function debounce(fn, time) {
var timer
return function() {
clearTimeout(timer)
timer = setTimeout(exec, time || 300, this, arguments)
}
function exec(scope, args) {
fn.apply(scope, args)
}
}
function deepAssign(to) {
if (to !== Object.prototype) for (var key, from, a = arguments, i = 1, len = a.length; i < len; ) {
if ((from = a[i++])) for (key in from) if (hasOwn.call(from, key)) {
if (from[key] === null) delete to[key]
else to[key] = (
isObj(from[key]) ?
deepAssign(isObj(to[key]) ? to[key] : {}, from[key]) :
from[key]
)
}
}
return to
}
function flat(arr) {
var out = []
return out.concat.apply(out, arr)
}
function isObj(obj) {
return !!obj && obj.constructor === Object
}
function ls() {
var key, dirRe, outRe, tmp, tmp2
, arr = flat(arguments)
, i = arr.length
, out = []
, paths = {}
, reEscRe = /[*.+^=:${}()|\/\\]/g
, opts = { absolute: false, cwd: process.cwd(), dir: true, dot: false, file: true, root: "", stat: false }
for (; i > 0; ) {
key = arr[--i]
if (isObj(key)) Object.assign(opts, key)
else if (typeof key === "string") {
tmp = path.resolve(opts.cwd, tmp2 = key.replace(/[^\/]*\*.*/, ""))
tmp = paths[tmp] || (paths[tmp] = [])
if (key !== tmp2) tmp.push(key.slice(tmp2.length))
}
}
for (key in paths) {
outRe = RegExp("^" + esc(key) + (
paths[key][0] ? "\\/(" + paths[key].map(esc).join("|") + ")$" : "$"
))
tmp = paths[key].map(dirname).filter(Boolean)
dirRe = RegExp("^" + esc(key) + (
tmp[0] ? "(?:\\/(" + tmp.map(esc).join("|") + ")|)$" : "$"
))
scan(key)
}
return out.sort()
function scan(name) {
try {
var stat = fs.statSync(name)
if (outRe.test(name)) {
if (stat.isDirectory() ? opts.dir : opts.file) out.push(
opts.stat ? Object.assign(stat, { name: opts.absolute ? name : opts.root + path.relative(opts.cwd, name) }) :
opts.absolute ? name :
opts.root + path.relative(opts.cwd, name)
)
}
if (stat.isDirectory() && dirRe.test(name)) {
fs.readdirSync(name).forEach(function(file) {
scan(path.resolve(name, file))
})
}
} catch(e) {}
}
function dirname(s) {
return s.indexOf("/") > -1 && path.dirname(s)
}
function esc(s) {
return (opts.dot || s.charAt(0) === "." ? "" : "(?!\\.)") +
s
.replace(reEscRe, "\\$&")
.replace(/\?/g, "[^\/]")
.replace(/\\\*\\\*(\\\/)?/g, "(.+$1)?")
.replace(/\\(?=\*)/g, "[^\/]")
}
}
function mkdirp(dir) {
try {
fs.statSync(dir)
} catch (e) {
mkdirp(path.dirname(dir))
console.error("mkdir", dir)
fs.mkdirSync(dir)
}
}
function readFile(fileName) {
return fs.readFileSync(path.resolve(fileName.split("?")[0]), "utf8")
}
function rmrf(dir) {
if (dir === "/") throw Error("Can not remove root")
fs.rmSync(dir, { force: true, recursive: true })
}
function writeFile(fileName, content) {
var name = path.resolve(fileName.split("?")[0])
mkdirp(path.dirname(name))
fs.writeFileSync(name, content)
}
function writePackage(obj) {
var undef
Object.assign(obj.litejs || {}, { cmd:undef, name:undef })
writeFile("package.json", JSON.stringify(obj, null, " ") + "\n")
}
function wait(fn) {
var pending = 1
function resume() {
if (!--pending && fn) fn.call(this)
}
resume.wait = function() {
pending++
return resume
}
return resume
}
function watch(paths, cb, delay) {
var watchers = {}
, changed = []
, fn = debounce(function() {
add(changed)
changed.length = 0
cb()
}, delay)
add(paths)
return {
add: add
}
function add(paths) {
paths.forEach(watch)
}
function watch(file) {
if (watchers[file]) return
try {
watchers[file] = fs.watch(file, function() {
if (watchers[file]) {
changed.push(file)
watchers[file].close()
watchers[file] = null
}
fn()
})
} catch (e) {}
}
}
function hold(ignore) {
var k
, obj = this
, hooks = []
, hooked = []
, _resume = wait(resume)
ignore = ignore || obj.syncMethods || []
for (k in obj) if (typeof obj[k] == "function" && ignore.indexOf(k) < 0) swap(k)
function swap(k) {
hooked.push(k, hasOwn.call(obj, k) && obj[k])
obj[k] = function() {
hooks.push(k, arguments)
return obj
}
}
// `wait` is already in hooked array, overrided method will be cleared on resume
obj.wait = _resume.wait
return _resume
function resume() {
for (var v, scope = obj, i = hooked.length; i--; i--) {
if (hooked[i]) obj[hooked[i-1]] = hooked[i]
else delete obj[hooked[i-1]]
}
// i == -1 from previous loop
for (; (v = hooks[++i]); ) {
scope = scope[v].apply(scope, hooks[++i]) || scope
}
hooks = hooked = null
}
}