Skip to content

Commit

Permalink
Merge pull request #220 from docsbydoxdox/hotfix/node-modules-missing
Browse files Browse the repository at this point in the history
[hotfix] Check both local and global node_modules for packages.
  • Loading branch information
neogeek authored Apr 13, 2024
2 parents abe5981 + 5c5a86c commit 1b3d3c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
25 changes: 19 additions & 6 deletions packages/doxdox-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env node

import { EOL } from 'os';
import { EOL, homedir } from 'os';

import { promises as fs } from 'fs';

import { dirname, join } from 'path';
import { dirname, join, resolve } from 'path';

import { execSync } from 'child_process';

import { fileURLToPath } from 'url';

Expand Down Expand Up @@ -111,20 +113,31 @@ const overridePackage = String(

const cliConfig = parseConfigFromCLI(args.raw);

const nodeModulesDir = await findParentNodeModules(
const localNodeModulesDir = await findParentNodeModules(
dirname(fileURLToPath(import.meta.url))
);

if (!nodeModulesDir) {
const globalNodeModulesDir = resolve(
join(
execSync('npm get prefix', { cwd: homedir() }).toString().trim(),
'./lib/node_modules'
)
);

if (!localNodeModulesDir || !globalNodeModulesDir) {
throw new Error('node_modules directory was not found');
}

const loadedParser = await loadPlugin<
(cwd: string, path: string) => Promise<File>
>(nodeModulesDir, 'doxdox-parser-', overrideParser.toLowerCase());
>(
[localNodeModulesDir, globalNodeModulesDir].filter(Boolean),
'doxdox-parser-',
overrideParser.toLowerCase()
);

const loadedRenderer = await loadPlugin<(doc: Doc) => Promise<string>>(
nodeModulesDir,
[localNodeModulesDir, globalNodeModulesDir].filter(Boolean),
'doxdox-renderer-',
overrideRenderer.toLowerCase()
);
Expand Down
31 changes: 14 additions & 17 deletions packages/doxdox-core/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ export const loadPluginFromFile = async <T>(
/**
* Load plugin from file or directory.
*
* const plugin = await loadPlugin(process.cwd(), null, './renderer.js');
* const plugin = await loadPlugin('../node_modules', 'doxdox-renderer-', 'json');
* const plugin = await loadPlugin([process.cwd()], null, './renderer.js');
* const plugin = await loadPlugin(['../node_modules'], 'doxdox-renderer-', 'json');
*
* @param {string} [directory] Root directory to load plugin from.
* @param {string[]} [directories] Root directories to load plugin from.
* @param {string} [prefix] Optional prefix to attach to the pathOrPackage.
* @param {string} [pathOrPackage] Path or package name.
* @return {Promise<T | null>} Plugin default method.
* @public
*/

export const loadPlugin = async <T>(
directory: string,
directories: string[],
prefix: string | null,
pathOrPackage: string
): Promise<T | null> => {
Expand All @@ -83,20 +83,17 @@ export const loadPlugin = async <T>(
return await loadPluginFromFile(pathOrPackage);
} else if (await isDirectory(pathOrPackage)) {
return await loadPluginFromPackagePath(pathOrPackage);
} else if (
await isDirectory(
join(
directory,
} else {
for (let i = 0; i < directories?.length; i += 1) {
const path = join(
directories[i],
`${prefix}${pathOrPackage.replace(prefixPattern, '')}`
)
)
) {
return await loadPluginFromPackagePath(
join(
directory,
`${prefix}${pathOrPackage.replace(prefixPattern, '')}`
)
);
);

if (await isDirectory(path)) {
return await loadPluginFromPackagePath(path);
}
}
}
} catch (err) {
if (process.env.DEBUG) {
Expand Down

0 comments on commit 1b3d3c3

Please sign in to comment.