Skip to content

Commit

Permalink
Merge pull request #2164 from jBouyoud/auto-extends-module
Browse files Browse the repository at this point in the history
Allow load npm module as extends
  • Loading branch information
hipstersmoothie authored Mar 20, 2022
2 parents d3a3ab5 + 1149233 commit 34d4450
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
14 changes: 12 additions & 2 deletions docs/pages/docs/configuration/autorc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ Auto can load `extends` configs in the following ways:
- from a scoped package `@YOUR_SCOPE/auto-config` (under the `auto` key in the package.json)
- from a package `auto-config-YOUR_NAME`
- from a url `https://yourdomain.com/auto-config.json` (must return the content type `application/json`)
- from a node module `auto-config-module` (this default export must be a plain object or a Promise of plain object)

### Formats

Expand All @@ -411,15 +412,24 @@ Auto can load `extends` configs in the following ways:
}
```

Will use the package `@YOUR_SCOPE/auto-config`
Will use the package `@YOUR_SCOPE/auto-config` (the `auto` key in `package.json` file)

```json
{
"extends": "joe"
}
```

Will use the package `auto-config-joe`
Will use the package `auto-config-joe` (the `auto` key in `package.json` file)


```json
{
"extends": "auto-config-module"
}
```

Will use the default export of `auto-config-module` node package

> :warning: If extending from a config package make sure it's a dependency of your project
Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ beforeEach(() => {
const log = dummyLog();

const importMock = jest.fn();
jest.mock("import-cwd", () => (path: string) => importMock(path));
jest.mock("import-cwd", () => {
return (path: any) => importMock(path);
});

describe("normalizeLabel", () => {
test("should extend base label", () => {
Expand Down Expand Up @@ -149,4 +151,19 @@ describe("loadExtendConfig", () => {
noVersionPrefix: true,
});
});

test("should load an npm module", async () => {
const config = new Config(log);

importMock.mockImplementation((path) =>
path === "auto-config-from-module"
? { noVersionPrefix: true }
: undefined
);

expect(await config.loadExtendConfig("auto-config-from-module")).toStrictEqual({
extends: "auto-config-from-module",
noVersionPrefix: true,
});
});
});
9 changes: 9 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ export default class Config {
}
}

if (!config) {
config = tryRequire(extend);
this.logger.verbose.note(`${extend} found: ${config}`);

if (config) {
config.extends = extend;
}
}

if (!config) {
throw new Error(`Unable to load extended config ${extend}`);
}
Expand Down

0 comments on commit 34d4450

Please sign in to comment.