From 4cc56570c37223a6ffc846e38eee7e80e2c0e03d Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:20:06 -0700 Subject: [PATCH] feat: support .aegir config files without extension --- src/config/user.js | 22 +++++++++++++++++- .../fixtures/custom-config-no-ext/.aegir | 3 +++ .../custom-config-no-ext/package.json | 3 +++ .../fixtures/custom-ts-no-ext-config/.aegir | 12 ++++++++++ .../custom-ts-no-ext-config/.eslintignore | 1 + .../custom-ts-no-ext-config/.eslintrc.json | 8 +++++++ .../fixtures/custom-ts-no-ext-config/index.ts | 1 + .../custom-ts-no-ext-config/package.json | 4 ++++ .../custom-ts-no-ext-config/tsconfig.json | 11 +++++++++ test/config/user.js | 23 +++++++++++++++++++ 10 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/config/fixtures/custom-config-no-ext/.aegir create mode 100644 test/config/fixtures/custom-config-no-ext/package.json create mode 100644 test/config/fixtures/custom-ts-no-ext-config/.aegir create mode 100644 test/config/fixtures/custom-ts-no-ext-config/.eslintignore create mode 100644 test/config/fixtures/custom-ts-no-ext-config/.eslintrc.json create mode 100644 test/config/fixtures/custom-ts-no-ext-config/index.ts create mode 100644 test/config/fixtures/custom-ts-no-ext-config/package.json create mode 100644 test/config/fixtures/custom-ts-no-ext-config/tsconfig.json diff --git a/src/config/user.js b/src/config/user.js index 22b7edf17..d92a804fc 100644 --- a/src/config/user.js +++ b/src/config/user.js @@ -67,6 +67,25 @@ const loadTs = async (filepath) => { return handleConfigImport(res, filepath) } +/** + * @type {Loader} + */ +const noExt = async (filepath) => { + console.log('noExt') + try { + return await loadEsm(filepath) + } catch (err) { + try { + return await loadTs(filepath) + } catch (err) { + console.error(`Could not load your config file at '${filepath}'`) + console.error(err, err) + } + } + + return null +} + /** * We should only be grabbing the config once per search location * @@ -94,7 +113,8 @@ export const config = async (searchFrom) => { loaders: { '.js': loadEsm, '.mjs': loadEsm, - '.ts': loadTs + '.ts': loadTs, + noExt }, searchPlaces: [ 'package.json', diff --git a/test/config/fixtures/custom-config-no-ext/.aegir b/test/config/fixtures/custom-config-no-ext/.aegir new file mode 100644 index 000000000..a07a84c50 --- /dev/null +++ b/test/config/fixtures/custom-config-no-ext/.aegir @@ -0,0 +1,3 @@ +module.exports = { + +}; diff --git a/test/config/fixtures/custom-config-no-ext/package.json b/test/config/fixtures/custom-config-no-ext/package.json new file mode 100644 index 000000000..1503ae7af --- /dev/null +++ b/test/config/fixtures/custom-config-no-ext/package.json @@ -0,0 +1,3 @@ +{ + "name": "custom-config" +} \ No newline at end of file diff --git a/test/config/fixtures/custom-ts-no-ext-config/.aegir b/test/config/fixtures/custom-ts-no-ext-config/.aegir new file mode 100644 index 000000000..39b0457b8 --- /dev/null +++ b/test/config/fixtures/custom-ts-no-ext-config/.aegir @@ -0,0 +1,12 @@ +import type { PartialOptions } from '../../../../src/types'; + +const config: PartialOptions = { + debug: true, + test: { + before: async (opts) => { + return { env: { res: '1234' } } + } + } +} + +export default config diff --git a/test/config/fixtures/custom-ts-no-ext-config/.eslintignore b/test/config/fixtures/custom-ts-no-ext-config/.eslintignore new file mode 100644 index 000000000..c49db0865 --- /dev/null +++ b/test/config/fixtures/custom-ts-no-ext-config/.eslintignore @@ -0,0 +1 @@ +!.aegir diff --git a/test/config/fixtures/custom-ts-no-ext-config/.eslintrc.json b/test/config/fixtures/custom-ts-no-ext-config/.eslintrc.json new file mode 100644 index 000000000..d6e753c88 --- /dev/null +++ b/test/config/fixtures/custom-ts-no-ext-config/.eslintrc.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../package.json", + "parserOptions": { + "sourceType": "module", + "project": ["./tsconfig.json"] + }, + "parser": "@typescript-eslint/parser" +} diff --git a/test/config/fixtures/custom-ts-no-ext-config/index.ts b/test/config/fixtures/custom-ts-no-ext-config/index.ts new file mode 100644 index 000000000..336ce12bb --- /dev/null +++ b/test/config/fixtures/custom-ts-no-ext-config/index.ts @@ -0,0 +1 @@ +export {} diff --git a/test/config/fixtures/custom-ts-no-ext-config/package.json b/test/config/fixtures/custom-ts-no-ext-config/package.json new file mode 100644 index 000000000..80612d988 --- /dev/null +++ b/test/config/fixtures/custom-ts-no-ext-config/package.json @@ -0,0 +1,4 @@ +{ + "name": "custom-config", + "type": "module" +} diff --git a/test/config/fixtures/custom-ts-no-ext-config/tsconfig.json b/test/config/fixtures/custom-ts-no-ext-config/tsconfig.json new file mode 100644 index 000000000..cc66116b5 --- /dev/null +++ b/test/config/fixtures/custom-ts-no-ext-config/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../../../src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist", + "noEmit": true + }, + "include": [ + "./.aegir", + "./index.ts" + ] +} diff --git a/test/config/user.js b/test/config/user.js index e585b7afc..a07293409 100644 --- a/test/config/user.js +++ b/test/config/user.js @@ -26,6 +26,17 @@ describe('config - user', () => { expect(res).to.eql(undefined) }) + it('custom config without extension', async () => { + const conf = await config(getConfigSearchPath('fixtures/custom-config')) + expect(conf).to.have.property('debug').eql(false) + expect(conf).to.have.nested.property('test.before') + expect(conf).to.have.nested.property('test.after') + + // @ts-ignore + const res = await conf.test.before() + expect(res).to.eql(undefined) + }) + it('custom ts config', async () => { const conf = await config(getConfigSearchPath('fixtures/custom-ts-config')) expect(conf).to.have.property('debug').eql(true) @@ -38,6 +49,18 @@ describe('config - user', () => { expect(res && res.env?.res).to.eql('1234') }) + it('custom ts config without extension', async () => { + const conf = await config(getConfigSearchPath('fixtures/custom-ts-no-ext-config')) + expect(conf).to.have.property('debug').eql(true) + expect(conf).to.have.nested.property('test.before') + expect(conf).to.have.nested.property('test.after') + + // @ts-ignore + const res = await conf.test.before() + expect(res).not.to.be.undefined() + expect(res && res.env?.res).to.eql('1234') + }) + it('supports async hooks', async () => { const conf = await config(getConfigSearchPath('fixtures/custom-user-async-hooks')) // @ts-ignore