Skip to content

Commit

Permalink
chore: minor adjustments
Browse files Browse the repository at this point in the history
chore: wip
  • Loading branch information
chrisbbreuer committed Nov 8, 2024
1 parent 9ae2d92 commit 22fc820
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 101 deletions.
Binary file modified bun.lockb
Binary file not shown.
75 changes: 0 additions & 75 deletions commitlint.config.js

This file was deleted.

17 changes: 2 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,23 @@
"lint": "bunx eslint .",
"lint:fix": "bunx eslint . --fix",
"fresh": "bunx rimraf node_modules/ bun.lock && bun i",
"commit": "git cz",
"changelog": "bunx changelogen --output CHANGELOG.md",
"prepublishOnly": "bun --bun run build",
"release": "bun run changelog && bunx bumpp package.json --all",
"test": "bun test",
"typecheck": "bun --bun tsc --noEmit"
},
"devDependencies": {
"@commitlint/cli": "^19.5.0",
"@stacksjs/eslint-config": "^3.8.1-beta.2",
"@types/bun": "^1.1.13",
"bumpp": "^9.8.1",
"bun-plugin-dts-auto": "^0.20.6",
"bun-plugin-dtsx": "^0.21.0",
"changelogen": "^0.5.7",
"commitizen": "^4.3.1",
"cz-git": "^1.10.1",
"lint-staged": "^15.2.10",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.6.3"
},
"simple-git-hooks": {
"pre-commit": "bun lint-staged",
"commit-msg": "bunx --no -- commitlint --edit $1"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": "bunx eslint . --fix"
},
"config": {
"commitizen": {
"path": "node_modules/cz-git"
}
"*.{js,ts}": "bunx eslint . --fix"
}
}
2 changes: 1 addition & 1 deletion pkgx.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dependencies:
bun.sh: ^1.1.33
bun.sh: ^1.1.34
26 changes: 16 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import type { ConfigOptions } from './types'
import { resolve } from 'node:path'
import process from 'node:process'
import { deepMerge } from './utils'

export interface ConfigOptions<T> {
name: string
cwd?: string
defaultConfig: T
}

/**
* Load Config
*
* @param {object} options - The configuration options.
* @param {string} options.name - The name of the configuration file.
* @param {string} [options.cwd] - The current working directory. If not provided, defaults to the current process's working directory.
* @param {T} options.defaultConfig - The default configuration.
* @returns {Promise<T>} The merged configuration object.
* @example ```ts
* // imports from example.config.{ts,js} and merges with default config
* await loadConfig({ name: 'example', defaultConfig: { foo: 'bar' } })
*/
export async function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: ConfigOptions<T>): Promise<T> {
const c = cwd ?? process.cwd()
const configPath = resolve(c, `${name}.config`)
const configPath = resolve(cwd || process.cwd(), `${name}.config`)

try {
const importedConfig = await import(configPath)
const loadedConfig = importedConfig.default || importedConfig
return deepMerge(defaultConfig, loadedConfig)
}
catch (error) {
console.error(`Error loading config from ${configPath}:`, error)
// eslint-disable-next-line unused-imports/no-unused-vars
catch (error: any) {
return defaultConfig
}
}
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Config Options
*
* @param name - The name of the configuration file.
* @param cwd - The current working directory.
* @param defaultConfig - The default configuration.
*/
export interface ConfigOptions<T> {
name: string
cwd?: string
defaultConfig: T
}
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Deep merge objects
*
* @param target - The target object
* @param sources - The source objects
* @returns The merged object
* @example deepMerge({ foo: 'bar' }, { bar: 'baz' }) // { foo: 'bar', bar: 'baz' }
*/
export function deepMerge<T extends object>(target: T, ...sources: Array<Partial<T>>): T {
if (!sources.length)
return target
Expand Down

0 comments on commit 22fc820

Please sign in to comment.