Skip to content

Commit

Permalink
Attempt to resolve relative plugin paths from extended config location
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Jan 14, 2021
1 parent f29b6ed commit 4c5bdd2
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 10 deletions.
25 changes: 24 additions & 1 deletion docs/pages/docs/configuration/autorc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ The following removes the default internal and documentation label sections:

## Extending

If you want to share your auto configuration between projects you can use the `extends` property. This property will load from a module's package.json or from a custom path. It's expected that the extended configuration be under the `auto` key in the package.json file.
If you want to share your auto configuration between projects you can use the `extends` property.
This property will load from a module's `package.json` or from a custom path.
It's expected that the extended configuration be under the `auto` key in the `package.json` file.

Auto can load `extends` configs in the following ways:

Expand All @@ -371,6 +373,8 @@ Auto can load `extends` configs in the following ways:
- from a package `auto-config-YOUR_NAME`
- from a url `https://yourdomain.com/auto-config.json` (must return the content type `application/json`)

### Formats

```json
{
"extends": "@YOUR_SCOPE"
Expand Down Expand Up @@ -402,3 +406,22 @@ If you're extending from a local file it can be any file in JSON format or a `pa
"extends": "./path/to/other/package.json"
}
```

### Custom Plugins

An `extends` configuration can include custom plugins in the NPM package.
This is useful if you have a shared plugin that doesn't necessarily need to be published as it's own package.

> NOTE: This does not work for `auto` configs stored in a url.
To include custom plugins just use a relative path in the extended configuration/

**`package.json`:**

```json
{
"auto": {
"plugins": ["./plugins/some-plugin.js"]
}
}
```
10 changes: 9 additions & 1 deletion packages/core/src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2057,7 +2057,15 @@ export default class Auto {
extendedLocation = require.resolve(config.extends);
}
} catch (error) {
this.logger.veryVerbose.error(error);
try {
if (config.extends) {
extendedLocation = require.resolve(
path.join(process.cwd(), config.extends)
);
}
} catch (error) {
this.logger.veryVerbose.error(error);
}
}

return extendedLocation;
Expand Down
26 changes: 25 additions & 1 deletion packages/core/src/utils/__tests__/load-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import endent from "endent";
import { execSync } from "child_process";

import { getInstalledPlugins, loadPlugin, listPlugins } from "../load-plugins";
import { dummyLog } from "../logger";
import { dummyLog, setLogLevel } from "../logger";

const logger = dummyLog();

Expand Down Expand Up @@ -40,6 +40,10 @@ jest.mock(
);

describe("loadPlugins", () => {
beforeEach(() => {
setLogLevel('quiet');
});

test("should load official plugins", () => {
expect(loadPlugin(["baz", {}], logger)?.name).toBe("baz");
expect(loadPlugin(["@auto-it/baz", {}], logger)?.name).toBe("baz");
Expand All @@ -50,6 +54,26 @@ describe("loadPlugins", () => {
expect(loadPlugin(["auto-plugin-foo", {}], logger)?.name).toBe("foo");
});

test("should load plugin stored relative to extended config package.json", () => {
expect(
loadPlugin(
["./some-plugin.js", {}],
logger,
path.join(__dirname, "../test-config/package.json")
)?.name
).toBe("test-1");
});

test("should load plugin stored relative to extended config dir", () => {
expect(
loadPlugin(
["./some-other-plugin.js", {}],
logger,
path.join(__dirname, "../test-config")
)?.name
).toBe("test-2");
});

test("should load scoped plugins", () => {
expect(loadPlugin(["@my-scope/auto-plugin-bar", {}], logger)?.name).toBe(
"bar"
Expand Down
24 changes: 17 additions & 7 deletions packages/core/src/utils/load-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,24 @@ export function findPlugin(

// Try requiring a path from cwd
if (isLocal) {
const localPath = path.join(process.cwd(), pluginPath);

if (!exists(localPath)) {
logger.log.warn(`Could not find plugin from path: ${localPath}`);
return;
let localPath = path.join(process.cwd(), pluginPath);

if (exists(localPath)) {
return localPath;
}

if (extendedLocation) {
localPath = path.join(
extendedLocation.endsWith("package.json")
? path.dirname(extendedLocation)
: extendedLocation,
pluginPath
);

if (exists(localPath)) {
return localPath;
}
}

return localPath;
}

// For pkg bundle
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/utils/test-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"auto": {
"plugins": [
"./some-other-plugin.js",
["./some-plugin.js", {"foo": "bar"}]
]
}
}
13 changes: 13 additions & 0 deletions packages/core/src/utils/test-config/some-other-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = class TestPlugin {
/** */
constructor() {
this.name = "test-2";
}

/**
* Tap into auto plugin points.
*/
apply() {
/** */
}
};
13 changes: 13 additions & 0 deletions packages/core/src/utils/test-config/some-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = class TestPlugin {
/** */
constructor() {
this.name = "test-1";
}

/**
* Tap into auto plugin points.
*/
apply() {
/** */
}
};

0 comments on commit 4c5bdd2

Please sign in to comment.