/
runAll.js
120 lines (106 loc) Β· 3.3 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
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
'use strict'
const generateTasks = require('./generateTasks')
const git = require('./gitWorkflow')
const getStagedFiles = require('./getStagedFiles')
const Listr = require('listr')
const has = require('lodash/has')
const makeCmdTasks = require('./makeCmdTasks')
const resolveGitDir = require('./resolveGitDir')
const debug = require('debug')('lint-staged:run')
/**
* Executes all tasks and either resolves or rejects the promise
* @param config {Object}
* @returns {Promise}
*/
module.exports = async function runAll(config) {
debug('Running all linter scripts')
// Config validation
if (!config || !has(config, 'concurrent') || !has(config, 'renderer')) {
throw new Error('Invalid config provided to runAll! Use getConfig instead.')
}
const { concurrent, renderer, chunkSize, subTaskConcurrency } = config
const gitDir = await resolveGitDir(config)
if (!gitDir) {
throw new Error('Current directory is not a git directory!')
}
debug('Resolved git directory to be `%s`', gitDir)
const files = await getStagedFiles({ cwd: gitDir })
if (!files) {
throw new Error('Unable to get staged files!')
}
debug('Loaded list of staged files in git:\n%O', files)
const tasks = (await generateTasks(config, gitDir, files)).map(task => ({
title: `Running tasks for ${task.pattern}`,
task: async () =>
new Listr(
await makeCmdTasks(task.commands, gitDir, task.fileList, {
chunkSize,
subTaskConcurrency
}),
{
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
dateFormat: false,
concurrent: false,
exitOnError: true
}
),
skip: () => {
if (task.fileList.length === 0) {
return `No staged files match ${task.pattern}`
}
return false
}
}))
const listrBaseOptions = {
dateFormat: false,
renderer
}
// If all of the configured "linters" should be skipped
// avoid executing any lint-staged logic
if (tasks.every(task => task.skip())) {
console.log('No staged files match any of provided globs.')
return 'No tasks to run.'
}
// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})
return new Listr(
[
{
title: 'Stashing changes...',
skip: async () => {
const hasPSF = await git.hasPartiallyStagedFiles({ cwd: gitDir })
if (!hasPSF) {
return 'No partially staged files found...'
}
return false
},
task: ctx => {
ctx.hasStash = true
return git.gitStashSave({ cwd: gitDir })
}
},
{
title: 'Running linters...',
task: () =>
new Listr(tasks, {
...listrBaseOptions,
concurrent,
exitOnError: !concurrent // Wait for all errors when running concurrently
})
},
{
title: 'Updating stash...',
enabled: ctx => ctx.hasStash,
skip: ctx => ctx.hasErrors && 'Skipping stash update since some tasks exited with errors',
task: () => git.updateStash({ cwd: gitDir })
},
{
title: 'Restoring local changes...',
enabled: ctx => ctx.hasStash,
task: () => git.gitStashPop({ cwd: gitDir })
}
],
listrBaseOptions
).run()
}