Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
Find and load configuration from a package.json property, rc file, or CommonJS module.
License
jaywcjlove/auto-config-loader
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Find and load configuration from apackage.json property,rc file, orCommonJS module. It has smart default based on traditional expectations in the JavaScript ecosystem. But it's also flexible enough to search anywhere you want and load whatever you want.
- SupportJSON,JSONC,JSON5,YAML,TOML,INI,CJS,Typescript, and ESM config load.
- Reads config from the nearest
package.jsonfile
$ npm i auto-config-loader
constautoConf=require('auto-config-loader');import{autoConf}from'auto-config-loader';// will look for:// process.cwd() + '.namespacerc'// process.cwd() + '.namespacerc.js'// process.cwd() + '.namespacerc.ts'// process.cwd() + '.namespacerc.mjs'// process.cwd() + '.namespacerc.cjs'// process.cwd() + '.namespacerc.json'// process.cwd() + '.namespacerc.json5'// process.cwd() + '.namespacerc.jsonc'// process.cwd() + '.namespacerc.yaml'// process.cwd() + '.namespacerc.yml'// process.cwd() + '.namespacerc.toml'// process.cwd() + 'namespace.config.mjs'// process.cwd() + 'namespace.config.cjs'// process.cwd() + 'namespace.config.js'// ........constdata=awaitautoConf('namespace',{default:{testItem2:'some value'}});
Load the JS file and return the result, support.js,.cjs,.mjs,.ts.
// => ./app/app.config.jsexportdefault{name:'app'}
import{loadConf}from'auto-config-loader/load-conf';interfaceConfig{name:string;}constresult=awaitloadConf<Config>('./app/app.config.js');// => { name: 'app' }
import{LoadConfOption}from'auto-config-loader';exporttypeLoaderFunc<T>=(filepath:string,content:string,jsOption?:LoadConfOption)=>T|Promise<T>;exporttypeLoader<T>=Record<string,LoaderFunc<T>>;exportinterfaceAutoConfOption<T>{searchPlaces?:string[];/** An object that maps extensions to the loader functions responsible for loading and parsing files with those extensions. */loaders?:Loader<T>;/** Specify default configuration. It has the lowest priority and is applied after extending config. */default?:T;/** Resolve configuration from this working directory. The default is `process.cwd()` */cwd?:string;/** Default transform js configuration */jsOption?:LoadConfOption;/**@deprecated use `mustExist` instead */ignoreLog?:boolean;mustExist?:boolean;}exportdeclareconstgetConfigPath:()=>string;/** * Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module. *@param namespace {string} Configuration base name. The default is `autoconf`. *@param option */exportdeclarefunctionautoConf<T>(namespace?:string,option?:AutoConfOption<T>):Promise<{}&T>;exportdefaultautoConf;
Discover configurations in the specified directory order. When configuring a tool, you can use multiple file formats and put these in multiple places. Usually, a tool would mention this in its own README file, but by default, these are the following places, where${moduleName} represents the name of the tool:
DefaultsearchPlaces:
['package.json',`.${moduleName}rc`,`.${moduleName}rc.json`,`.${moduleName}rc.json5`,`.${moduleName}rc.jsonc`,`.${moduleName}rc.yaml`,`.${moduleName}rc.yml`,`.${moduleName}rc.toml`,`.${moduleName}rc.ini`,`.${moduleName}rc.js`,`.${moduleName}rc.ts`,`.${moduleName}rc.cjs`,`.${moduleName}rc.mjs`,`.config/${moduleName}rc`,`.config/${moduleName}rc.json`,`.config/${moduleName}rc.json5`,`.config/${moduleName}rc.jsonc`,`.config/${moduleName}rc.yaml`,`.config/${moduleName}rc.yml`,`.config/${moduleName}rc.toml`,`.config/${moduleName}rc.ini`,`.config/${moduleName}rc.js`,`.config/${moduleName}rc.ts`,`.config/${moduleName}rc.cjs`,`.config/${moduleName}rc.mjs`,`${moduleName}.config.js`,`${moduleName}.config.ts`,`${moduleName}.config.cjs`,`${moduleName}.config.mjs`,]
Configurations are loaded sequentially, and the configuration file search is terminated when a configuration file exists.
The content of these files is defined by the tool. For example, you can add asemi configuration value tofalse using a file called.config/autoconfig.yml:
semi:true
Additionally, you have the option to put a property named after the tool in yourpackage.json file, with the contents of that property being the same as the file contents. To use the same example as above:
{"name":"your-project","autoconfig":{"semi":true}}
This has the advantage that you can put the configuration of all tools (at least the ones that useauto-config-loader) in one file.
importtypejitifrom'jiti';import{Options}from'sucrase';typeJiti=ReturnType<typeofjiti>;typeJITIOptions=Jiti['options'];exportinterfaceLoadConfOption{jiti?:boolean;jitiOptions?:JITIOptions;transformOption?:Options;}exportdeclarefunctionloadConf<T>(path:string,option?:LoadConfOption):Promise<T>;exportdeclarefunctionjsLoader<T>(filepath:string,content:string,option?:LoadConfOption):Promise<T>;
Modify default.js,.ts,.cjs,.mjs loader parameters.
importload,{jsLoader}from'auto-config-loader';functionloadJS(filepath,content){returnjsLoader(filepath,content,{// change option...});}constdata=awaitload('namespace',{loaders:{'.js':loadJS,'.ts':loadJS,'.cjs':loadJS,'.mjs':loadJS,},default:{testItem2:'some value'}});
example:
import{jsLoader}from'auto-config-loader';constdata=jsLoader('/path/to/file/name.js')
exportdeclarefunctioniniLoader<T>(_:string,content:string):T;
example:
import{iniLoader}from'auto-config-loader';constdata=iniLoader(undefined,`...`)
exportdeclarefunctionjsonLoader<T>(_:string,content:string):T;
example:
import{jsonLoader}from'auto-config-loader';constdata=jsonLoader(undefined,`{ "a": 123 }`)
exportdeclarefunctiontomlLoader<T>(_:string,content:string):T;
example:
import{tomlLoader}from'auto-config-loader';constdata=tomlLoader(undefined,`...`)
exportdeclarefunctionyamlLoader<T>(_:string,content:string):T;
example:
import{yamlLoader}from'auto-config-loader';constdata=yamlLoader(undefined,`...`)
This is an example, the defaultyaml/yml does not require a loader.
importloadfrom'auto-config-loader';importyamlfrom'yaml';functionloadYaml(filepath,content){returnyaml.parse(content);}constdata=awaitload('namespace',{searchPlaces:['.namespacerc.yaml','.namespacerc.yml',],loaders:{'.yaml':loadYaml,'.yml':loadYaml,},default:{testItem2:'some value'}});
exportdeclareconstmerge:{<TObject,TSource>(object:TObject,source:TSource):TObject&TSource;<TObject,TSource1,TSource2>(object:TObject,source1:TSource1,source2:TSource2):TObject&TSource1&TSource2;<TObject,TSource1,TSource2,TSource3>(object:TObject,source1:TSource1,source2:TSource2,source3:TSource3):TObject&TSource1&TSource2&TSource3;<TObject,TSource1,TSource2,TSource3,TSource4>(object:TObject,source1:TSource1,source2:TSource2,source3:TSource3,source4:TSource4):TObject&TSource1&TSource2&TSource3&TSource4;(object:any, ...otherArgs:any[]):any;};
exportdeclarefunctionfindConfigFile(moduleName:string,root:string,searchPlaces?:string[]):string;
exportdeclareconstgetConfigPath:()=>string;
Example:
import{autoConf,getConfigPath}from'auto-config-loader';constdata=autoConf<Config>('idoc');constconfigPath=getConfigPath();// => /.autoconfrc.js
This guide provides the steps to migrate to the latest version of the configuration loader API.
Loader Functions Support Async
LoaderFunc<T>now supports returningTorPromise<T>.- Update custom loaders to handle asynchronous operations if needed.
Example:
exporttypeLoaderFunc<T>=(filepath:string,content:string,jsOption?:LoadConfOption)=>T|Promise<T>;
autoConfReturns a Promise- The
autoConffunction now returns aPromiseinstead of a synchronous result. - Update your code to handle asynchronous calls.
Example:
exportdeclarefunctionautoConf<T>(namespace?:string,option?:AutoConfOption<T>):Promise<{}&T>;
- The
If you have custom loaders, update their return types to support asynchronous operations:
Example:
constjsonLoader:LoaderFunc<MyConfig>=async(filepath,content)=>JSON.parse(content);
Update all calls toautoConf to useawait or.then to handle Promises:
Example Usingawait:
constconfig=awaitautoConf('myNamespace',options);console.log(config);
Example Using.then:
autoConf('myNamespace',options).then(config=>{console.log(config);});
- cosmiconfig Find and load configuration from a package.json property, rc file, or CommonJS module
- cjson Comments enabled json loader (Commented JavaScript Object Notation)
- Config Loader Load user config files for node js projects.
- Lilconfig Zero-dependency nodejs config seeker.
- proload Searches for and loads your tool's JavaScript configuration files with full support for CJS, ESM, TypeScript and more.
- rc The non-configurable configuration loader for lazy people.
As always, thanks to our amazing contributors!
Made withcontributors.
This package is licensed under the MIT License.
About
Find and load configuration from a package.json property, rc file, or CommonJS module.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.