-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
39 additions
and
101 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters