Skip to content

Commit

Permalink
feat: support .aegir.ts config files
Browse files Browse the repository at this point in the history
Fix #943
  • Loading branch information
SgtPooki committed Jun 23, 2022
1 parent d7608ad commit bffecd9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .aegir.js → .aegir.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/** @type {import("./src/types").PartialOptions} */
export default {
docs: {
entryPoint: 'utils'
}
}
} as import("./src/types").PartialOptions
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ aegir test --ts-repo
## Release steps
1. Run linting
1. Run linting `npm run lint`
2. Run type check
3. Run tests
3. Run tests `npm run test`
4. Bump the version in `package.json`
5. Build everything
6. Update contributors based on the git history
5. Build everything `npm run build`
6. Update contributors based on the git history (deprecated?)
7. Generate a changelog based on the git log
8. Commit the version change & `CHANGELOG.md`
9. Create a git tag
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
"strip-bom": "^5.0.0",
"strip-json-comments": "^4.0.0",
"tempy": "^2.0.0",
"ts-import": "^3.0.0-beta.3",
"typescript": "^4.6.3",
"uint8arrays": "^3.0.0",
"undici": "^5.0.0",
Expand Down
74 changes: 55 additions & 19 deletions src/config/user.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-console */

// @ts-check
import { lilconfig } from 'lilconfig'
import merge from 'merge-options'
import { pathToFileURL } from 'url'

import tsImport from 'ts-import'
/**
* @typedef {import("./../types").Options} Options
*/
Expand Down Expand Up @@ -125,39 +125,75 @@ const defaults = {
}
}

/**
* @typedef {(filepath: string) => Promise<Options | {}>} Loader
*/

/**
*
* @param {{default?: Options} | Options} mod
* @returns {Options | {}}
*/
const handleConfigImport = (mod) => {
/**
* @type {*}
*/
const modWithDefaultExport = mod
if (modWithDefaultExport.default != null) {
return modWithDefaultExport.default
}

if (typeof mod.toString === 'function') {
return mod
}

// if there's no toString function, this was an ES module that didn't export anything
return {}
}

/**
* @type {Loader}
*/
const loadEsm = async (filepath) => {
const res = await import(pathToFileURL(filepath).toString())

return handleConfigImport(res)
}

/**
* @type {Loader}
*/
const loadTs = async (filepath) => {
try {
const res = await tsImport.load(filepath)

return handleConfigImport(res)
} catch (err) {
return {}
}
}

/**
* Search for local user config
*
* @param {string | undefined} [searchFrom]
* @returns {Promise<Options>}
*/
export const config = async (searchFrom) => {
console.log('searchFrom: ', searchFrom)
let userConfig
try {
const loadEsm = async (/** @type {string} */ filepath) => {
/** @type {any} */
const res = await import(pathToFileURL(filepath).toString())

if (res.default != null) {
return res.default
}

if (typeof res.toString === 'function') {
return res
}

// if there's no toString function, this was an ES module that didn't export anything
return {}
}
const loadedConfig = await lilconfig('aegir', {
loaders: {
'.js': loadEsm,
'.mjs': loadEsm
'.mjs': loadEsm,
'.ts': loadTs
},
searchPlaces: [
'package.json',
'.aegir.js',
'.aegir.cjs'
'.aegir.cjs',
'.aegir.ts'
]
})
.search(searchFrom)
Expand Down

0 comments on commit bffecd9

Please sign in to comment.