diff --git a/docs/pages/docs/configuration/autorc.mdx b/docs/pages/docs/configuration/autorc.mdx index cd5c0f0be..25b350804 100644 --- a/docs/pages/docs/configuration/autorc.mdx +++ b/docs/pages/docs/configuration/autorc.mdx @@ -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 @@ -411,7 +412,7 @@ 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 { @@ -419,7 +420,16 @@ Will use the package `@YOUR_SCOPE/auto-config` } ``` -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 diff --git a/packages/core/src/__tests__/config.test.ts b/packages/core/src/__tests__/config.test.ts index 2da667d23..91832785f 100644 --- a/packages/core/src/__tests__/config.test.ts +++ b/packages/core/src/__tests__/config.test.ts @@ -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", () => { @@ -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, + }); + }); }); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index a5c6034c7..4f5f4aa16 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -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}`); }