From f77cefaac2908d37a36f8e0b8e6cf521b3204ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Sun, 28 Jul 2019 21:13:19 +0300 Subject: [PATCH] refactor: generateTasks is not async --- src/generateTasks.js | 25 ++++++++++++------------- src/runAll.js | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/generateTasks.js b/src/generateTasks.js index 8ab664768..1f3b8424e 100644 --- a/src/generateTasks.js +++ b/src/generateTasks.js @@ -29,7 +29,7 @@ const isPathInside = (parent, child) => { * @param {boolean} [options.relative] - Whether filepaths to should be relative to gitDir * @returns {Promise} */ -module.exports = async function generateTasks({ +module.exports = function generateTasks({ config, cwd = process.cwd(), gitDir, @@ -37,31 +37,30 @@ module.exports = async function generateTasks({ relative = false }) { debug('Generating linter tasks') - - const stagedFiles = files.map(file => path.resolve(gitDir, file)) - - return Object.keys(config).map(pattern => { + return Object.entries(config).map(([pattern, commands]) => { const isParentDirPattern = pattern.startsWith('../') - const commands = config[pattern] - const fileList = micromatch( - stagedFiles + files // Only worry about children of the CWD unless the pattern explicitly // specifies that it concerns a parent directory. - .filter(file => isParentDirPattern || isPathInside(cwd, file)) - // Make the paths relative to CWD for filtering - .map(file => path.relative(cwd, file)), + .filter(file => { + if (isParentDirPattern) return true + const absolutePath = path.resolve(gitDir, file) + return isPathInside(cwd, absolutePath) + }), pattern, { + cwd, + dot: true, // If pattern doesn't look like a path, enable `matchBase` to // match against filenames in every directory. This makes `*.js` // match both `test.js` and `subdirectory/test.js`. matchBase: !pattern.includes('/'), - dot: true + posixSlashes: true } ).map(file => // Return absolute path after the filter is run - relative ? path.normalize(file) : path.resolve(cwd, file) + relative ? file : cwd + '/' + file ) const task = { pattern, commands, fileList } diff --git a/src/runAll.js b/src/runAll.js index c104dec8a..fe1774749 100644 --- a/src/runAll.js +++ b/src/runAll.js @@ -69,7 +69,7 @@ https://github.com/okonet/lint-staged#using-js-functions-to-customize-linter-com ) } - const tasks = (await generateTasks({ config, cwd, gitDir, files, relative })).map(task => ({ + const tasks = generateTasks({ config, cwd, gitDir, files, relative }).map(task => ({ title: `Running tasks for ${task.pattern}`, task: async () => new Listr(