Skip to content

Commit

Permalink
feat: allow load npm module as extends
Browse files Browse the repository at this point in the history
  • Loading branch information
jBouyoud committed Mar 7, 2022
1 parent 8fd6684 commit 04e679c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/pages/docs/configuration/autorc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,15 @@ 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)

> :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,
});
});
});
5 changes: 5 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ export default class Config {
}
}

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

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

0 comments on commit 04e679c

Please sign in to comment.