1 | 'use strict'
|
2 |
|
3 | const sgf = require('staged-git-files')
|
4 | const Listr = require('listr')
|
5 | const has = require('lodash/has')
|
6 | const pify = require('pify')
|
7 | const makeCmdTasks = require('./makeCmdTasks')
|
8 | const generateTasks = require('./generateTasks')
|
9 | const resolveGitDir = require('./resolveGitDir')
|
10 | const git = require('./gitWorkflow')
|
11 |
|
12 | const debug = require('debug')('lint-staged:run')
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | module.exports = function runAll(config) {
|
20 | debug('Running all linter scripts')
|
21 |
|
22 | if (!config || !has(config, 'concurrent') || !has(config, 'renderer')) {
|
23 | throw new Error('Invalid config provided to runAll! Use getConfig instead.')
|
24 | }
|
25 |
|
26 | const { concurrent, renderer, chunkSize, subTaskConcurrency } = config
|
27 | const gitDir = resolveGitDir()
|
28 | debug('Resolved git directory to be `%s`', gitDir)
|
29 |
|
30 | sgf.cwd = gitDir
|
31 | return pify(sgf)('ACM').then(files => {
|
32 |
|
33 | const filenames = files.map(file => file.filename)
|
34 | debug('Loaded list of staged files in git:\n%O', filenames)
|
35 |
|
36 | const tasks = generateTasks(config, filenames).map(task => ({
|
37 | title: `Running tasks for ${task.pattern}`,
|
38 | task: () =>
|
39 | new Listr(
|
40 | makeCmdTasks(task.commands, task.fileList, {
|
41 | chunkSize,
|
42 | subTaskConcurrency
|
43 | }),
|
44 | {
|
45 |
|
46 |
|
47 | dateFormat: false,
|
48 | concurrent: false,
|
49 | exitOnError: true
|
50 | }
|
51 | ),
|
52 | skip: () => {
|
53 | if (task.fileList.length === 0) {
|
54 | return `No staged files match ${task.pattern}`
|
55 | }
|
56 | return false
|
57 | }
|
58 | }))
|
59 |
|
60 | const listrBaseOptions = {
|
61 | dateFormat: false,
|
62 | renderer
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 | if (tasks.every(task => task.skip())) {
|
68 | console.log('No staged files match any of provided globs.')
|
69 | return 'No tasks to run.'
|
70 | }
|
71 |
|
72 |
|
73 | process.on('SIGINT', () => {})
|
74 |
|
75 | return new Listr(
|
76 | [
|
77 | {
|
78 | title: 'Stashing changes...',
|
79 | skip: async () => {
|
80 | const hasPSF = await git.hasPartiallyStagedFiles()
|
81 | if (!hasPSF) {
|
82 | return 'No partially staged files found...'
|
83 | }
|
84 | return false
|
85 | },
|
86 | task: ctx => {
|
87 | ctx.hasStash = true
|
88 | return git.gitStashSave()
|
89 | }
|
90 | },
|
91 | {
|
92 | title: 'Running linters...',
|
93 | task: () =>
|
94 | new Listr(tasks, {
|
95 | ...listrBaseOptions,
|
96 | concurrent,
|
97 | exitOnError: !concurrent
|
98 | })
|
99 | },
|
100 | {
|
101 | title: 'Updating stash...',
|
102 | enabled: ctx => ctx.hasStash,
|
103 | skip: ctx => ctx.hasErrors && 'Skipping stash update since some tasks exited with errors',
|
104 | task: () => git.updateStash()
|
105 | },
|
106 | {
|
107 | title: 'Restoring local changes...',
|
108 | enabled: ctx => ctx.hasStash,
|
109 | task: () => git.gitStashPop()
|
110 | }
|
111 | ],
|
112 | listrBaseOptions
|
113 | ).run()
|
114 | })
|
115 | }
|