Skip to content

Commit

Permalink
add scoped plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Feb 25, 2020
1 parent 3a746ad commit fdd1658
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
9 changes: 7 additions & 2 deletions docs/pages/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ Unofficial plugins pulled from NPM should be named in the format `auto-plugin-PL

That name is provided to auto to use that particular plugin.

```json
```jsonc
{
"plugins": ["auto-plugin-my-cool-plugin", "some-package"]
"plugins": [
"auto-plugin-my-cool-plugin",
// or
"@my-scope/auto-plugin-my-cool-plugin",
"some-package"
]
}
```

Expand Down
54 changes: 51 additions & 3 deletions packages/core/src/utils/__tests__/load-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,56 @@ import { dummyLog } from '../logger';

const logger = dummyLog();

jest.mock(
'auto-plugin-foo',
() => ({
default: class {
name = 'foo';
}
}),
{
virtual: true
}
);
jest.mock(
'@my-scope/auto-plugin-bar',
() => ({
default: class {
name = 'bar';
}
}),
{ virtual: true }
);
jest.mock(
'@auto-it/baz',
() => ({
default: class {
name = 'baz';
}
}),
{
virtual: true
}
);

describe('loadPlugins', () => {
test('should require custom plugins -- fallback to cwd', async () => {
test('should load official plugins', () => {
expect(loadPlugin(['baz', {}], logger)?.name).toBe('baz');
expect(loadPlugin(['@auto-it/baz', {}], logger)?.name).toBe('baz');
});

test('should load community plugins', () => {
expect(loadPlugin(['foo', {}], logger)?.name).toBe('foo');
expect(loadPlugin(['auto-plugin-foo', {}], logger)?.name).toBe('foo');
});

test('should load scoped plugins', () => {
expect(
loadPlugin(['@my-scope/auto-plugin-bar', {}], logger)?.name
).toBe('bar');
});

test('should require custom plugins -- fallback to cwd', () => {
expect(
loadPlugin([path.join(__dirname, './test-plugin.ts'), {}], logger)
).toStrictEqual(
Expand All @@ -16,7 +64,7 @@ describe('loadPlugins', () => {
);
});

test('should require custom plugins -- surface errors', async () => {
test('should require custom plugins -- surface errors', () => {
expect(() =>
loadPlugin(
[path.join(__dirname, './test-plugin-malformed.js'), {}],
Expand All @@ -25,7 +73,7 @@ describe('loadPlugins', () => {
).toThrow();
});

test('should load config', async () => {
test('should load config', () => {
expect(
loadPlugin(
[path.join(__dirname, './test-plugin.ts'), 'do the thing'],
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/utils/load-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ export default function loadPlugin(
plugin = requirePlugin(path.join('@auto-it', pluginPath), logger);
}

// Try requiring a package
if (
!plugin &&
(pluginPath.includes('/auto-plugin-') ||
pluginPath.startsWith('auto-plugin-') ||
pluginPath.startsWith('@auto-it'))
) {
plugin = requirePlugin(pluginPath, logger);
}

if (!plugin) {
logger.log.warn(`Could not find plugin: ${pluginPath}`);
return;
Expand Down

0 comments on commit fdd1658

Please sign in to comment.