You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Loren West edited this page May 16, 2023
·
12 revisions
Using NODE_CONFIG_PARSER you can provide node-config with an alternative path from which to load the parser definitions and file types resolution order.
You'll need to create a new file which imports from config/parser and exports the same Parser object after applying modifications.
Modifying the default parser is done using the following methods:
constParser=require('config/parser');// Modify using Parser APImodule.exports=Parser;
It may look simple (because it is!) but with it you can define any parser your heart desires.
Parser Examples
importParserfrom'config/parser';// coffeescript parserimport'coffeescript/register';// once registered, coffee files are loaded as any other js file Parser.setParser('coffee',Parser.getParser('js'));// custom yaml parserimport{safeLoad}from'yaml-parser';functionyamlParser(filename,content){returnsafeLoad(content);}// note that when you override supported types// their resolution order doesn't changeParser.setParser('yml',yamlParser);Parser.setParser('yaml',yamlParser);// Hocon parserimporthoconParserfrom'hocon-parser';Parser.setParser('hocon',(filename,content)=>hoconParser(content));// override json parser so json files are parsed as json5Parser.setParser('json',Parser.getParser('json5'));exportdefaultParser;// must be exported!
Both filename and content are provided to support both use cases, but normally you will only use one of the two. filename to file types which are required, such as coffee and ts files, while content can be used by any string parser available to you. (ini, hocon or you own custom parser, you name it!)
Note that when adding support for a previously unknown file extension it is automatically appended to the files resolution
order, giving it priority over all other file types in the resolution order, while existing exntesions keep their previous resolution order.
setFileOrder can be used to override default file resolution order heuristics.
param
type
description
extension
string
name of the corresponding file extension
parser
function
provide a parser function, which expects filename and content as arguments, and returns an object containing parsed results to be merged.
Gets information about the order in which file types are resulted and merged.
Returns the entire files order array, unless called with an extension argument in which case it will return the extension's index in the files order array. This can be used with setFilesOrder to make specific changes.
Sets which extensions will be support by node-config (automatically loaded) and their resolution order.
setFilesOrder(order)
param
type
description
order
array
an array containing file extensions in the desired order of resolution
constParser=require('config/parser');Parser.getFilesOrder();// ['js', 'json', 'json5', 'yaml', ... ]Parser.setFilesOrder(['yaml','yml','json','json5','js']);// file types not present will not be parsedmodule.exports=Parser;
setFilesOrder(extension, index)
param
type
description
extension
string
file extension name
index
integer
an index to move extension to in the files order array
// make sure json5 files are resulted before json filesParser.setFilesOrder('json5',Parser.getFilesOrder('json'));
parse(filename, content)
Used internally to match and execute a parser according to filename's extracted extension.