Skip to content

Commit

Permalink
refactor: generateTasks is not async
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj authored and okonet committed Aug 17, 2019
1 parent 23019a5 commit f77cefa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions src/generateTasks.js
Expand Up @@ -29,39 +29,38 @@ 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,
files,
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 }
Expand Down
2 changes: 1 addition & 1 deletion src/runAll.js
Expand Up @@ -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(
Expand Down

0 comments on commit f77cefa

Please sign in to comment.