UNPKG

3.42 kBJavaScriptView Raw
1'use strict'
2
3const sgf = require('staged-git-files')
4const Listr = require('listr')
5const has = require('lodash/has')
6const pify = require('pify')
7const makeCmdTasks = require('./makeCmdTasks')
8const generateTasks = require('./generateTasks')
9const resolveGitDir = require('./resolveGitDir')
10const git = require('./gitWorkflow')
11
12const debug = require('debug')('lint-staged:run')
13
14/**
15 * Executes all tasks and either resolves or rejects the promise
16 * @param config {Object}
17 * @returns {Promise}
18 */
19module.exports = function runAll(config) {
20 debug('Running all linter scripts')
21 // Config validation
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 /* files is an Object{ filename: String, status: String } */
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 // In sub-tasks we don't want to run concurrently
46 // and we want to abort on errors
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 // If all of the configured "linters" should be skipped
66 // avoid executing any lint-staged logic
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 // Do not terminate main Listr process on SIGINT
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 // Wait for all errors when running concurrently
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}