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

fix: Plugin resolution for relative paths #1194

Merged
merged 1 commit into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 26 additions & 1 deletion src/lib/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class PluginHost extends AbstractComponent<Application> {
*/
load(): boolean {
const logger = this.application.logger;
const plugins = this.plugins.length ? this.plugins : this.discoverNpmPlugins();
const plugins = this.plugins.length ? this.resolvePluginPaths(this.plugins) : this.discoverNpmPlugins();

if (plugins.some(plugin => plugin.toLowerCase() === 'none')) {
return true;
Expand Down Expand Up @@ -133,4 +133,29 @@ export class PluginHost extends AbstractComponent<Application> {
return false;
}
}

/**
* Resolves plugin paths to absolute paths from the current working directory
* (`process.cwd()`).
*
* ```txt
* ./plugin -> resolve
* ../plugin -> resolve
* plugin -> don't resolve (module resolution)
* /plugin -> don't resolve (already absolute path)
* c:\plugin -> don't resolve (already absolute path)
* ```
*
* @param plugins
*/
private resolvePluginPaths(plugins: string[]) {
const cwd = process.cwd();
return plugins.map(plugin => {
// treat plugins that start with `.` as relative, requiring resolution
if (plugin.startsWith('.')) {
return Path.resolve(cwd, plugin);
}
return plugin;
});
}
}
24 changes: 24 additions & 0 deletions src/test/plugin-host.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Application } from '..';
import Assert = require('assert');
import * as mockery from 'mockery';
import * as path from 'path';

describe('PluginHost', function () {
before (function () {
Expand All @@ -27,4 +28,27 @@ describe('PluginHost', function () {
'typedoc-plugin-2'
]);
});

it('loads a plugin with relative path', function () {
const app = new Application();
app.bootstrap({
plugin: ['./dist/test/plugins/relative']
});

Assert.deepEqual(app.plugins.plugins, [
'./dist/test/plugins/relative'
]);
});

it('loads a plugin with absolute path', function () {
const app = new Application();
const absolutePath = path.resolve(__dirname, './plugins/absolute');
app.bootstrap({
plugin: [absolutePath]
});

Assert.deepEqual(app.plugins.plugins, [
absolutePath
]);
});
});
4 changes: 4 additions & 0 deletions src/test/plugins/absolute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Application } from '../..';

module.exports = (pluginHost: Application) => {
};
4 changes: 4 additions & 0 deletions src/test/plugins/relative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Application } from '../..';

module.exports = (pluginHost: Application) => {
};