/
runAll.js
148 lines (128 loc) · 4.62 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
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
'use strict'
/** @typedef {import('./index').Logger} Logger */
const chalk = require('chalk')
const Listr = require('listr')
const symbols = require('log-symbols')
const generateTasks = require('./generateTasks')
const getStagedFiles = require('./getStagedFiles')
const GitWorkflow = require('./gitWorkflow')
const makeCmdTasks = require('./makeCmdTasks')
const resolveGitDir = require('./resolveGitDir')
const debugLog = require('debug')('lint-staged:run')
/**
* https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x
* https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation
* https://unix.stackexchange.com/a/120652
*/
const MAX_ARG_LENGTH =
(process.platform === 'darwin' && 262144) || (process.platform === 'win32' && 8191) || 131072
/**
* Executes all tasks and either resolves or rejects the promise
*
* @param {object} options
* @param {Object} [options.config] - Task configuration
* @param {Object} [options.cwd] - Current working directory
* @param {boolean} [options.relative] - Pass relative filepaths to tasks
* @param {boolean} [options.shell] - Skip parsing of tasks for better shell support
* @param {boolean} [options.quiet] - Disable lint-staged’s own console output
* @param {boolean} [options.debug] - Enable debug mode
* @param {Logger} logger
* @returns {Promise}
*/
module.exports = async function runAll(
{ config, cwd = process.cwd(), debug = false, quiet = false, relative = false, shell = false },
logger = console
) {
debugLog('Running all linter scripts')
const gitDir = await resolveGitDir({ cwd })
if (!gitDir) {
throw new Error('Current directory is not a git directory!')
}
debugLog('Resolved git directory to be `%s`', gitDir)
const files = await getStagedFiles({ cwd: gitDir })
if (!files) {
throw new Error('Unable to get staged files!')
}
debugLog('Loaded list of staged files in git:\n%O', files)
const argLength = files.join(' ').length
if (argLength > MAX_ARG_LENGTH) {
logger.warn(`
${symbols.warning} ${chalk.yellow(
`lint-staged generated an argument string of ${argLength} characters, and commands might not run correctly on your platform.
It is recommended to use functions as linters and split your command based on the number of staged files. For more info, please visit:
https://github.com/okonet/lint-staged#using-js-functions-to-customize-linter-commands`
)}
`)
}
const tasks = generateTasks({ config, cwd, gitDir, files, relative }).map(task => ({
title: `Running tasks for ${task.pattern}`,
task: async () =>
new Listr(
await makeCmdTasks({ commands: task.commands, files: task.fileList, gitDir, shell }),
{
// 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 listrOptions = {
dateFormat: false,
renderer: (quiet && 'silent') || (debug && 'verbose') || 'update'
}
// If all of the configured "linters" should be skipped
// avoid executing any lint-staged logic
if (tasks.every(task => task.skip())) {
logger.log('No staged files match any of provided globs.')
return 'No tasks to run.'
}
const git = new GitWorkflow(gitDir)
try {
await new Listr(
[
{
title: 'Preparing...',
task: () => git.stashBackup()
},
{
title: 'Running tasks...',
task: () => new Listr(tasks, { ...listrOptions, concurrent: true, exitOnError: false })
},
{
title: 'Applying modifications...',
skip: ctx => ctx.hasErrors && 'Skipped because of errors from tasks',
task: () => git.applyModifications()
},
{
title: 'Reverting to original state...',
enabled: ctx => ctx.hasErrors,
task: () => git.restoreOriginalState()
},
{
title: 'Cleaning up...',
task: () => git.dropBackup()
}
],
listrOptions
).run()
} catch (error) {
if (error.message.includes('Another git process seems to be running in this repository')) {
logger.error(`
${symbols.error} ${chalk.red(`lint-staged failed due to a git error.
Any lost modifications can be restored from a git stash:
> git stash list
stash@{0}: On master: automatic lint-staged backup
> git stash pop stash@{0}`)}
`)
}
throw error
}
}