-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
156 lines (137 loc) · 3.99 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
const path = require('path')
const chalk = require('chalk')
const logger = require('./logger')
const readMaidFile = require('./readMaidFile')
const MaidError = require('./MaidError')
const runCLICommand = require('./runCLICommand')
class Maid {
constructor(opts = {}) {
this.maidfile = readMaidFile(opts.section)
logger.setOptions({ quiet: opts.quiet })
if (!this.maidfile) {
throw new MaidError('No maidfile was found. Stop.')
}
}
async runTasks(taskNames, inParallel) {
if (!taskNames || taskNames.length === 0) return
if (inParallel) {
await Promise.all(
taskNames.map(taskName => {
return this.runTask(taskName)
})
)
} else {
for (const taskName of taskNames) {
await this.runTask(taskName)
}
}
}
async runFile(taskName) {
await this.runTask('beforeAll', false)
await this.runTask(taskName)
await this.runTask('afterAll', false)
}
async runTask(taskName, throwWhenNoMatchedTask = true) {
const task =
taskName &&
this.maidfile &&
this.maidfile.tasks.find(task => task.name === taskName)
if (!task) {
if (throwWhenNoMatchedTask) {
throw new MaidError(`No task called "${taskName}" was found. Stop.`)
} else {
return
}
}
await this.runTaskHooks(task, 'before')
const start = Date.now()
logger.log(`Starting '${chalk.cyan(task.name)}'...`)
await new Promise((resolve, reject) => {
const handleError = err => {
throw new MaidError(`Task '${task.name}' failed.\n${err.stack}`)
}
if (checkTypes(task, ['sh', 'bash'])) {
return runCLICommand({ task, resolve, reject })
}
if (checkTypes(task, ['py', 'python'])) {
return runCLICommand({ type: 'python', task, resolve, reject })
}
if (checkTypes(task, ['js', 'javascript'])) {
let res
try {
res = require('require-from-string')(
task.script,
this.maidfile.filepath
)
} catch (err) {
return handleError(err)
}
res = res.default || res
return resolve(
typeof res === 'function'
? Promise.resolve(res()).catch(handleError)
: res
)
}
return resolve()
})
logger.log(
`Finished '${chalk.cyan(task.name)}' ${chalk.magenta(
`after ${Date.now() - start} ms`
)}...`
)
await this.runTaskHooks(task, 'after')
}
async runTaskHooks(task, when) {
const prefix = when === 'before' ? 'pre' : 'post'
const tasks = this.maidfile.tasks.filter(({ name }) => {
return name === `${prefix}${task.name}`
})
await this.runTasks(tasks.map(task => task.name))
for (const item of task[when]) {
const { taskNames, inParallel } = item
await this.runTasks(taskNames, inParallel)
}
}
getHelp(patterns) {
const mm = require('micromatch')
patterns = [].concat(patterns)
const tasks =
patterns.length > 0
? this.maidfile.tasks.filter(task => {
return mm.some(task.name, patterns)
})
: this.maidfile.tasks
if (tasks.length === 0) {
throw new MaidError(
`No tasks for pattern "${patterns.join(' ')}" was found. Stop.`
)
}
console.log(
`\n ${chalk.magenta.bold(
`Task${tasks.length > 1 ? 's' : ''} in ${path.relative(
process.cwd(),
this.maidfile.filepath
)}:`
)}\n\n` +
tasks
.map(
task =>
` ${chalk.bold(task.name)}\n${chalk.dim(
task.description
? task.description
.split('\n')
.map(v => ` ${v.trim()}`)
.join('\n')
: ' No description'
)}`
)
.join('\n\n') +
'\n'
)
}
}
function checkTypes(task, types) {
return types.some(type => type === task.type)
}
module.exports = opts => new Maid(opts)