Skip to content

Commit

Permalink
feat: support .aegir config files without extension
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Jun 13, 2022
1 parent b6f351e commit 4cc5657
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -94,7 +113,8 @@ export const config = async (searchFrom) => {
loaders: {
'.js': loadEsm,
'.mjs': loadEsm,
'.ts': loadTs
'.ts': loadTs,
noExt
},
searchPlaces: [
'package.json',
Expand Down
3 changes: 3 additions & 0 deletions test/config/fixtures/custom-config-no-ext/.aegir
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {

};
3 changes: 3 additions & 0 deletions test/config/fixtures/custom-config-no-ext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-config"
}
12 changes: 12 additions & 0 deletions test/config/fixtures/custom-ts-no-ext-config/.aegir
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/config/fixtures/custom-ts-no-ext-config/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.aegir
8 changes: 8 additions & 0 deletions test/config/fixtures/custom-ts-no-ext-config/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../../../package.json",
"parserOptions": {
"sourceType": "module",
"project": ["./tsconfig.json"]
},
"parser": "@typescript-eslint/parser"
}
1 change: 1 addition & 0 deletions test/config/fixtures/custom-ts-no-ext-config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
4 changes: 4 additions & 0 deletions test/config/fixtures/custom-ts-no-ext-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "custom-config",
"type": "module"
}
11 changes: 11 additions & 0 deletions test/config/fixtures/custom-ts-no-ext-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../../src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"noEmit": true
},
"include": [
"./.aegir",
"./index.ts"
]
}
23 changes: 23 additions & 0 deletions test/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 4cc5657

Please sign in to comment.