-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (53 loc) · 1.5 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
const { promisify } = require('util')
const fs = require('fs')
const mkdirp = require('mkdirp')
const path = require('path')
const _tasks = {}
function task (name, fn) {
_tasks[name] = fn
}
task.run = async function (name, wait) {
if (!name) {
return console.warn('Usage: node task', Object.keys(_tasks))
}
if (!_tasks[name]) {
console.warn(`Error: no task '${name}'`)
return process.exit(1)
}
console.info(`\n>> Running task: ${name}`)
return _tasks[name].call()
}
task.parallel = async function (tasks) {
await Promise.all(tasks.map(t => task.run(t)))
}
task.series = async function (tasks) {
for (const t of tasks) await task.run(t)
}
task.plugin = function (fn) {
return function (input, output, options) {
mkdirp.sync(path.dirname(output))
console.log(` ${input} -> ${output}`)
return fn(input, output, options)
}
}
const _start = Date.now()
process.on('exit', () => {
let delta = (Date.now() - _start) / 1000
console.log(`\nDone in ${delta.toFixed(2)}s`)
})
function cached (fn) {
return function (file, content) {
if (content) cached[file] = content
else if (cached[file]) return cached[file]
return fn(file, content)
}
}
const _read = promisify(fs.readFile)
const _write = promisify(fs.writeFile)
const read = cached((file) => _read(file).then(s => s.toString()))
const write = cached((file, content) => _write(file, content))
module.exports = {
task,
read,
write
}