Skip to content

Commit

Permalink
fix: replace Array.forEach with Array.every, add message if lint-stag…
Browse files Browse the repository at this point in the history
…ed is skipped and update snapshots

Fixes lint-staged#570
  • Loading branch information
silbinarywolf committed Jan 29, 2019
1 parent 316a7ed commit a106320
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 51 deletions.
94 changes: 45 additions & 49 deletions src/runAll.js
Expand Up @@ -63,59 +63,55 @@ module.exports = function runAll(config) {
}

// If all of the configured "linters" should be skipped
// avoid executing any "stashing changes..." logic
let isSkippingAllLinters = true
tasks.forEach(task => {
if (!task.skip()) {
isSkippingAllLinters = false
}
})
// avoid executing any lint-staged logic
if (tasks.every(task => task.skip())) {
console.log(
'No linters executed. No staged files match any of provided globs in "lint-staged.linters".'
)
return 'No tasks to run.'
}

if (!isSkippingAllLinters) {
// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})
// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})

return new Listr(
[
{
title: 'Stashing changes...',
skip: async () => {
const hasPSF = await git.hasPartiallyStagedFiles()
if (!hasPSF) {
return 'No partially staged files found...'
}
return false
},
task: ctx => {
ctx.hasStash = true
return git.gitStashSave()
return new Listr(
[
{
title: 'Stashing changes...',
skip: async () => {
const hasPSF = await git.hasPartiallyStagedFiles()
if (!hasPSF) {
return 'No partially staged files found...'
}
return false
},
{
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()
},
{
title: 'Restoring local changes...',
enabled: ctx => ctx.hasStash,
task: () => git.gitStashPop()
task: ctx => {
ctx.hasStash = true
return git.gitStashSave()
}
],
listrBaseOptions
).run()
}
return 'No tasks to run.'
},
{
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()
},
{
title: 'Restoring local changes...',
enabled: ctx => ctx.hasStash,
task: () => git.gitStashPop()
}
],
listrBaseOptions
).run()
})
}
39 changes: 37 additions & 2 deletions test/__snapshots__/runAll.spec.js.snap
Expand Up @@ -29,7 +29,10 @@ LOG Running tasks for *.js [completed]
LOG Running linters... [completed]"
`;

exports[`runAll should resolve the promise with no files 1`] = `""`;
exports[`runAll should resolve the promise with no files 1`] = `
"
LOG No linters executed. No staged files match any of provided globs in \\"lint-staged.linters\\"."
`;

exports[`runAll should skip linters and stash update but perform working copy restore if terminated 1`] = `
"
Expand Down Expand Up @@ -73,7 +76,39 @@ LOG Running tasks for *.js [completed]
LOG Running linters... [completed]"
`;

exports[`runAll should skip stashing changes if no lint-staged files are changed 1`] = `""`;
exports[`runAll should skip stashing changes if no lint-staged files are changed 1`] = `
"
LOG No linters executed. No staged files match any of provided globs in \\"lint-staged.linters\\"."
`;

exports[`runAll should skip updating stash if there are errors during linting 1`] = `
"
LOG Stashing changes... [started]
LOG Stashing changes... [completed]
LOG Running linters... [started]
LOG Running tasks for *.js [started]
LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [failed]
LOG →
LOG Running tasks for *.js [failed]
LOG →
LOG Running linters... [failed]
LOG Updating stash... [started]
LOG Updating stash... [skipped]
LOG → Skipping stash update since some tasks exited with errors
LOG Restoring local changes... [started]
LOG Restoring local changes... [completed]
LOG {
name: 'ListrError',
errors: [
{
privateMsg: '\\\\n\\\\n\\\\n× echo \\"sample\\" found some errors. Please fix them and try committing again.\\\\n\\\\nLinter finished with error',
context: {hasStash: true, hasErrors: true}
}
],
context: {hasStash: true, hasErrors: true}
}"
`;

exports[`runAll should skip updating stash if there are errors during linting 1`] = `
"
Expand Down

0 comments on commit a106320

Please sign in to comment.