-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (69 loc) · 2.15 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
'use strict'
const args = require('get-them-args')(process.argv.slice(2))
const chalk = require('chalk')
const shell = require('shelljs')
const npmRunPath = require('npm-run-path')
const exec = (cmd, options = {}, cb) => shell.exec(cmd, Object.assign({env: npmRunPath.env()}, options, cb))
function ok () {
const _tasks = []
const _ok = {
args,
exec,
tasks: _tasks,
task,
run}
function task (name, handler) {
if (typeof name !== 'string') {
console.error(`🚨 ${chalk.red.bold('Error!')} Task(${name}) must have a name.`)
return _ok
}
if (typeof handler === 'string') {
_tasks.push({
name,
handler: () => {
console.log(`⌨ cmd - ${chalk.green(handler)}`)
exec(handler)
}
})
} else if (typeof handler === 'function') {
_tasks.push({
name,
handler})
} else {
console.error(`🚨 ${chalk.red.bold('Error!')} Please provide a valid handler for task ${name}.`)
}
return _ok
}
function run (name, handler) {
if (typeof name !== 'string') {
console.error(`🚨 ${chalk.red.bold('Error!')} Task(${name}) must have a name.`)
return _ok
}
if (name !== 'all' && (!_tasks[name] && !handler)) {
console.error(`🚨 ${chalk.red.bold('Error!')}Task ${chalk.blue.bold(name)} doesn't exist. Please provide a handler or create the task.`)
return _ok
}
// Create & start timer for running task
const timerName = `\n⚡ ${chalk.blue.bold(name)} ran in`
console.time(timerName)
if (name === 'all') {
console.log(`🎉 ${chalk.magenta('Running all tasks')}`)
_tasks.forEach((task) => {
console.log(`✨ Task - ${chalk.blue.bold(task.name)}`)
task.handler(_ok)
})
} else {
let currentTask = _tasks.filter((task) => task.name === name)
if (!currentTask.length) {
_ok.task(name, handler)
}
currentTask = _tasks.filter((task) => task.name === name)
console.log(`✨ Task - ${chalk.blue.bold(name)}`)
currentTask[0].handler(_ok)
}
console.timeEnd(timerName)
return _ok
}
return _ok
}
module.exports = ok()