Skip to content

Commit 1b3d3c3

Browse files
authored
Merge pull request #220 from docsbydoxdox/hotfix/node-modules-missing
[hotfix] Check both local and global node_modules for packages.
2 parents abe5981 + 5c5a86c commit 1b3d3c3

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

packages/doxdox-cli/src/index.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env node
22

3-
import { EOL } from 'os';
3+
import { EOL, homedir } 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,31 @@ 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(
122+
execSync('npm get prefix', { cwd: homedir() }).toString().trim(),
123+
'./lib/node_modules'
124+
)
125+
);
126+
127+
if (!localNodeModulesDir || !globalNodeModulesDir) {
119128
throw new Error('node_modules directory was not found');
120129
}
121130

122131
const loadedParser = await loadPlugin<
123132
(cwd: string, path: string) => Promise<File>
124-
>(nodeModulesDir, 'doxdox-parser-', overrideParser.toLowerCase());
133+
>(
134+
[localNodeModulesDir, globalNodeModulesDir].filter(Boolean),
135+
'doxdox-parser-',
136+
overrideParser.toLowerCase()
137+
);
125138

126139
const loadedRenderer = await loadPlugin<(doc: Doc) => Promise<string>>(
127-
nodeModulesDir,
140+
[localNodeModulesDir, globalNodeModulesDir].filter(Boolean),
128141
'doxdox-renderer-',
129142
overrideRenderer.toLowerCase()
130143
);

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)