From a106320c1795d26b475d96617a218bf17d0b276f Mon Sep 17 00:00:00 2001 From: Jake Bentvelzen Date: Tue, 29 Jan 2019 14:22:26 +1100 Subject: [PATCH] fix: replace Array.forEach with Array.every, add message if lint-staged is skipped and update snapshots Fixes #570 --- src/runAll.js | 94 ++++++++++++-------------- test/__snapshots__/runAll.spec.js.snap | 39 ++++++++++- 2 files changed, 82 insertions(+), 51 deletions(-) diff --git a/src/runAll.js b/src/runAll.js index e956e7f25..98f1eddd8 100644 --- a/src/runAll.js +++ b/src/runAll.js @@ -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() }) } diff --git a/test/__snapshots__/runAll.spec.js.snap b/test/__snapshots__/runAll.spec.js.snap index 3e8e5514e..edad8ad85 100644 --- a/test/__snapshots__/runAll.spec.js.snap +++ b/test/__snapshots__/runAll.spec.js.snap @@ -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`] = ` " @@ -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`] = ` "