-
-
Notifications
You must be signed in to change notification settings - Fork 425
/
runAll.js
65 lines (59 loc) · 1.86 KB
/
runAll.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
'use strict'
const sgf = require('staged-git-files')
const Listr = require('listr')
const runScript = require('./runScript')
const generateTasks = require('./generateTasks')
const resolveGitDir = require('./resolveGitDir')
/**
* Executes all tasks and either resolves or rejects the promise
* @param packageJson
* @param config
* @returns {Promise}
*/
module.exports = function runAll(packageJson, config) {
// Config validation
if (!config || !config.gitDir || !config.concurrent || !config.renderer) {
throw new Error('Invalid config provided to runAll! Use getConfig instead.')
}
const gitDir = config.gitDir
const concurrent = config.concurrent
const renderer = config.renderer
sgf.cwd = resolveGitDir(gitDir)
return new Promise((resolve, reject) => {
sgf('ACM', (err, files) => {
if (err) {
reject(err)
}
/* files is an Object{ filename: String, status: String } */
const filenames = files.map(file => file.filename)
const tasks = generateTasks(config, filenames).map(task => ({
title: `Running tasks for ${task.pattern}`,
task: () =>
new Listr(runScript(task.commands, task.fileList, packageJson, config), {
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
concurrent: false,
exitOnError: true
}),
skip: () => {
if (task.fileList.length === 0) {
return `No staged files match ${task.pattern}`
}
return false
}
}))
if (tasks.length) {
new Listr(tasks, {
concurrent,
renderer,
exitOnError: !concurrent // Wait for all errors when running concurrently
})
.run()
.then(resolve)
.catch(reject)
} else {
resolve('No tasks to run.')
}
})
})
}