Skip to content

Commit fef0437

Browse files
committed
fix: use "dir" as plugin key to have a consistent tree
1 parent 5ae6fe6 commit fef0437

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lib/find-plugins.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async function findPlugins (dir, options) {
88
const { opts, prefix } = options
99

1010
const pluginTree = {
11-
[prefix || '/']: { hooks: [], plugins: [] }
11+
[dir]: { hooks: [], plugins: [] }
1212
}
1313

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

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

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

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

68-
pluginTree[prefix || '/'].hooks = currentDirHooks
69+
pluginTree[dir].hooks = currentDirHooks
6970

7071
return currentDirHooks
7172
}
@@ -78,7 +79,7 @@ function processIndexDirEntryIfExists (pluginTree, { opts, dirEntries, dir, pref
7879
const file = join(dir, indexDirEntry.name)
7980
const { language, type } = getScriptType(file, opts.packageType)
8081
handleTypeScriptSupport(file, language, true)
81-
accumulatePlugin({ file, type, opts, pluginTree, prefix })
82+
accumulatePlugin({ dir, file, type, opts, pluginTree, prefix })
8283

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

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

122-
function processFile (pluginTree, { file, opts, dirEntry, prefix }) {
123+
function processFile (pluginTree, { dir, file, opts, dirEntry, prefix }) {
123124
const { language, type } = getScriptType(file, opts.packageType)
124125
handleTypeScriptSupport(file, language)
125126

126127
// Don't place hook in plugin queue
127128
if (!opts.autoHooksPattern.test(dirEntry.name)) {
128-
accumulatePlugin({ file, type, opts, pluginTree, prefix })
129+
accumulatePlugin({ dir, file, type, opts, pluginTree, prefix })
129130
}
130131
}
131132

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

143-
pluginTree[prefix || '/'].plugins.push({ file, type, prefix })
144+
pluginTree[dir].plugins.push({ file, type, prefix })
144145
}
145146

146147
function handleTypeScriptSupport (file, language, isHook = false) {

0 commit comments

Comments
 (0)