Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve dirNameRoutePrefix interferes with auto-hooks #454

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/find-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function findPlugins (dir, options) {
const { opts, prefix } = options

const pluginTree = {
[prefix || '/']: { hooks: [], plugins: [] }
[dir]: { hooks: [], plugins: [] }
}

await buildTree(pluginTree, dir, { prefix, opts, depth: 0, hooks: [] })
Expand All @@ -18,8 +18,8 @@ async function findPlugins (dir, options) {

async function buildTree (pluginTree, dir, { prefix, opts, depth, hooks }) {
// check to see if hooks or plugins have been added to this prefix, initialize if not
if (!pluginTree[prefix]) {
pluginTree[prefix] = { hooks: [], plugins: [] }
if (!pluginTree[dir]) {
pluginTree[dir] = { hooks: [], plugins: [] }
}

const dirEntries = await readdir(dir, { withFileTypes: true })
Expand Down Expand Up @@ -47,6 +47,7 @@ function findCurrentDirHooks (pluginTree, { dir, dirEntries, hooks, opts, prefix
let currentDirHooks = []
// Hooks were passed in, create new array specific to this plugin item
for (const hook of hooks) {
console.log(hook)
currentDirHooks.push(hook)
}

Expand All @@ -65,7 +66,7 @@ function findCurrentDirHooks (pluginTree, { dir, dirEntries, hooks, opts, prefix
currentDirHooks.push({ file, type })
}

pluginTree[prefix || '/'].hooks = currentDirHooks
pluginTree[dir].hooks = currentDirHooks

return currentDirHooks
}
Expand All @@ -78,7 +79,7 @@ function processIndexDirEntryIfExists (pluginTree, { opts, dirEntries, dir, pref
const file = join(dir, indexDirEntry.name)
const { language, type } = getScriptType(file, opts.packageType)
handleTypeScriptSupport(file, language, true)
accumulatePlugin({ file, type, opts, pluginTree, prefix })
accumulatePlugin({ dir, file, type, opts, pluginTree, prefix })

const hasNoDirectory = dirEntries.every((dirEntry) => !dirEntry.isDirectory())

Expand All @@ -98,7 +99,7 @@ async function processDirContents (pluginTree, { dirEntries, opts, indexDirEntry
} else if (indexDirEntry) {
// An index.js file is present in the directory so we ignore the others modules (but not the subdirectories)
} else if (dirEntry.isFile() && opts.scriptPattern.test(dirEntry.name)) {
processFile(pluginTree, { file, opts, dirEntry, pluginTree, prefix })
processFile(pluginTree, { dir, file, opts, dirEntry, pluginTree, prefix })
}
}
}
Expand All @@ -119,17 +120,17 @@ async function processDirectory (pluginTree, { prefix, opts, dirEntry, dir, file
await buildTree(pluginTree, file, { opts, prefix: prefixBreadCrumb, depth: depth + 1, hooks })
}

function processFile (pluginTree, { file, opts, dirEntry, prefix }) {
function processFile (pluginTree, { dir, file, opts, dirEntry, prefix }) {
const { language, type } = getScriptType(file, opts.packageType)
handleTypeScriptSupport(file, language)

// Don't place hook in plugin queue
if (!opts.autoHooksPattern.test(dirEntry.name)) {
accumulatePlugin({ file, type, opts, pluginTree, prefix })
accumulatePlugin({ dir, file, type, opts, pluginTree, prefix })
}
}

function accumulatePlugin ({ file, type, opts, pluginTree, prefix }) {
function accumulatePlugin ({ dir, file, type, opts, pluginTree, prefix }) {
// Replace backward slash to forward slash for consistent behavior between windows and posix.
const filePath = '/' + relative(opts.dir, file).replace(/\\/gu, '/')
if (opts.matchFilter && !filterPath(filePath, opts.matchFilter)) {
Expand All @@ -140,7 +141,7 @@ function accumulatePlugin ({ file, type, opts, pluginTree, prefix }) {
return
}

pluginTree[prefix || '/'].plugins.push({ file, type, prefix })
pluginTree[dir].plugins.push({ file, type, prefix })
}

function handleTypeScriptSupport (file, language, isHook = false) {
Expand Down
Loading