Skip to content

Commit d1c8bf7

Browse files
committed
Check both local and global node_modules for packages.
1 parent abe5981 commit d1c8bf7

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

packages/doxdox-cli/src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { EOL } from 'os';
44

55
import { promises as fs } from 'fs';
66

7-
import { dirname, join } from 'path';
7+
import { dirname, join, resolve } from 'path';
8+
9+
import { execSync } from 'child_process';
810

911
import { fileURLToPath } from 'url';
1012

@@ -111,20 +113,28 @@ const overridePackage = String(
111113

112114
const cliConfig = parseConfigFromCLI(args.raw);
113115

114-
const nodeModulesDir = await findParentNodeModules(
116+
const localNodeModulesDir = await findParentNodeModules(
115117
dirname(fileURLToPath(import.meta.url))
116118
);
117119

118-
if (!nodeModulesDir) {
120+
const globalNodeModulesDir = resolve(
121+
join(execSync('npm get prefix').toString().trim(), './lib/node_modules')
122+
);
123+
124+
if (!localNodeModulesDir || !globalNodeModulesDir) {
119125
throw new Error('node_modules directory was not found');
120126
}
121127

122128
const loadedParser = await loadPlugin<
123129
(cwd: string, path: string) => Promise<File>
124-
>(nodeModulesDir, 'doxdox-parser-', overrideParser.toLowerCase());
130+
>(
131+
[localNodeModulesDir, globalNodeModulesDir].filter(Boolean),
132+
'doxdox-parser-',
133+
overrideParser.toLowerCase()
134+
);
125135

126136
const loadedRenderer = await loadPlugin<(doc: Doc) => Promise<string>>(
127-
nodeModulesDir,
137+
[localNodeModulesDir, globalNodeModulesDir].filter(Boolean),
128138
'doxdox-renderer-',
129139
overrideRenderer.toLowerCase()
130140
);

packages/doxdox-core/src/loader.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ export const loadPluginFromFile = async <T>(
6161
/**
6262
* Load plugin from file or directory.
6363
*
64-
* const plugin = await loadPlugin(process.cwd(), null, './renderer.js');
65-
* const plugin = await loadPlugin('../node_modules', 'doxdox-renderer-', 'json');
64+
* const plugin = await loadPlugin([process.cwd()], null, './renderer.js');
65+
* const plugin = await loadPlugin(['../node_modules'], 'doxdox-renderer-', 'json');
6666
*
67-
* @param {string} [directory] Root directory to load plugin from.
67+
* @param {string[]} [directories] Root directories to load plugin from.
6868
* @param {string} [prefix] Optional prefix to attach to the pathOrPackage.
6969
* @param {string} [pathOrPackage] Path or package name.
7070
* @return {Promise<T | null>} Plugin default method.
7171
* @public
7272
*/
7373

7474
export const loadPlugin = async <T>(
75-
directory: string,
75+
directories: string[],
7676
prefix: string | null,
7777
pathOrPackage: string
7878
): Promise<T | null> => {
@@ -83,20 +83,17 @@ export const loadPlugin = async <T>(
8383
return await loadPluginFromFile(pathOrPackage);
8484
} else if (await isDirectory(pathOrPackage)) {
8585
return await loadPluginFromPackagePath(pathOrPackage);
86-
} else if (
87-
await isDirectory(
88-
join(
89-
directory,
86+
} else {
87+
for (let i = 0; i < directories?.length; i += 1) {
88+
const path = join(
89+
directories[i],
9090
`${prefix}${pathOrPackage.replace(prefixPattern, '')}`
91-
)
92-
)
93-
) {
94-
return await loadPluginFromPackagePath(
95-
join(
96-
directory,
97-
`${prefix}${pathOrPackage.replace(prefixPattern, '')}`
98-
)
99-
);
91+
);
92+
93+
if (await isDirectory(path)) {
94+
return await loadPluginFromPackagePath(path);
95+
}
96+
}
10097
}
10198
} catch (err) {
10299
if (process.env.DEBUG) {

0 commit comments

Comments
 (0)