rc-config-loader 2.0.3 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # rc-config-loader [![Build Status](https://travis-ci.org/azu/rc-config-loader.svg?branch=master)](https://travis-ci.org/azu/rc-config-loader)
1
+ # rc-config-loader [![Actions Status: test](https://github.com/azu/rc-config-loader/workflows/test/badge.svg)](https://github.com/azu/rc-config-loader/actions?query=workflow%3A"test")
2
2
 
3
3
  Load config from `.{product}rc.{json,yml,js}` file.
4
4
 
@@ -26,6 +26,10 @@ Find and load a configuration object from:
26
26
 
27
27
  - <del>Sync loading</del>
28
28
  - [cosmiconfig@3+](https://github.com/davidtheclark/cosmiconfig/blob/master/CHANGELOG.md#300) support `sync` option
29
+ - <del>Built-in TypeScript support</del>
30
+ - [comisconfig@6*](https://github.com/davidtheclark/cosmiconfig/blob/master/CHANGELOG.md#600) is written by TypeScript
31
+
32
+ If you want to async support and customize loader, recommended to use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig).
29
33
 
30
34
  ## Install
31
35
 
@@ -40,34 +44,75 @@ Install with [npm](https://www.npmjs.com/):
40
44
  ```ts
41
45
  export interface rcConfigLoaderOption {
42
46
  // does look for `package.json`
43
- packageJSON?: boolean,
47
+ packageJSON?:
48
+ | boolean
49
+ | {
50
+ fieldName: string;
51
+ };
44
52
  // if config file name is not same with packageName, set the name
45
53
  configFileName?: string;
46
54
  // treat default(no ext file) as some extension
47
- defaultExtension?: string | string[],
55
+ defaultExtension?: string | string[];
48
56
  // where start to load
49
57
  cwd?: string;
50
58
  }
51
- export default function rcConfigLoader(packageName: string, options?: rcConfigLoaderOption): Object;
59
+ /**
60
+ * Find and load rcfile, return { config, filePath }
61
+ * If not found any rcfile, throw an Error.
62
+ * @param {string} pkgName
63
+ * @param {rcConfigLoaderOption} [opts]
64
+ * @returns {{ config: Object, filePath:string } | undefined}
65
+ */
66
+ export declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption): {
67
+ config: R;
68
+ filePath: string;
69
+ } | undefined;
70
+
52
71
  ```
53
72
 
54
- `rcConfigLoader` return `{ config, filePath }` object.
73
+ `rcFile` return `{ config, filePath }` object.
55
74
 
56
75
  - `config`: it is config object
57
76
  - `filePath`: absolute path to config file
58
77
 
59
- If not found config file, return `undefined`.
78
+ **Note:**
79
+
80
+ - `rcFile` function return `undefined` if the config file is not found
81
+ - `rcFile` throw an Error if the config file content is malformed (causing a parsing error)
82
+
83
+ Recommenced usage:
84
+
85
+ ```js
86
+ import { rcFile } from "rc-config-loader"
87
+
88
+ function loadRcFile(rcFileName){
89
+ try {
90
+ const results = rcFile(rcFileName);
91
+ // Not Found
92
+ if (!results) {
93
+ return {};
94
+ }
95
+ return results.config;
96
+ } catch (error) {
97
+ // Found it, but it is parsing error
98
+ return {} ; // default value
99
+ }
100
+ }
101
+ // load config
102
+ const config = loadRcFile("your-application");
103
+ console.log(config); // => rcfile content
104
+ ```
60
105
 
61
106
  ### Example
62
107
 
63
108
  ```js
64
109
  "use strict";
65
- const rcfile = require("rc-config-loader");
110
+ import { rcFile } from "rc-config-loader"
66
111
  // load .eslintrc from current dir
67
- console.log(rcfile("eslint"));
112
+ console.log(rcFile("eslint"));
68
113
 
69
114
  // load .eslintrc from specific path
70
- console.log(rcfile("eslint", {
115
+ console.log(rcFile("eslint", {
71
116
  configFileName: `${__dirname}/test/fixtures/.eslintrc`
72
117
  }));
73
118
  /*
@@ -79,7 +124,7 @@ filePath: ${__dirname}/test/fixtures/.eslintrc
79
124
  */
80
125
 
81
126
  // load property from pacakge.json
82
- console.log(rcfile("rc-config-loader", {
127
+ console.log(rcFile("rc-config-loader", {
83
128
  packageJSON: {
84
129
  fieldName: "directories"
85
130
  }
@@ -90,27 +135,30 @@ filePath: /path/to/package.json
90
135
  */
91
136
 
92
137
  // load .eslintrc from specific dir
93
- console.log(rcfile("eslint", {
138
+ console.log(rcFile("eslint", {
94
139
  cwd: `${__dirname}/test/fixtures`
95
140
  }));
96
141
 
97
142
  // load specific filename from current dir
98
- console.log(rcfile("travis", {configFileName: ".travis"}));
143
+ console.log(rcFile("travis", {configFileName: ".travis"}));
99
144
  /*
100
145
  config: { sudo: false, language: 'node_js', node_js: 'stable' }
101
146
  filePath: /path/to/.travis
102
147
  */
103
148
 
104
149
  // try to load as .json, .yml, js
105
- console.log(rcfile("bar", {
150
+ console.log(rcFile("bar", {
106
151
  configFileName: `${__dirname}/test/fixtures/.barrc`,
107
152
  defaultExtension: [".json", ".yml", ".js"]
108
153
  }));
109
154
 
110
- // try to load as .yml, but it is not json
111
- // throw Error
155
+ // try to load as foobar, but .foobarrc is not found
156
+ console.log(rcFile("foorbar")); // => undefined
157
+
158
+ // try to load as .json, but it is not json
159
+ // throw SyntaxError
112
160
  try {
113
- rcfile("unknown", {
161
+ rcFile("unknown", {
114
162
  // This is not json
115
163
  configFileName: `${__dirname}/test/fixtures/.unknownrc`,
116
164
  defaultExtension: ".json"
@@ -121,7 +169,6 @@ try {
121
169
  SyntaxError: Cannot read config file: /test/fixtures/.unknownrc
122
170
  */
123
171
  }
124
-
125
172
  ```
126
173
 
127
174
  ## Users
@@ -1,11 +1,19 @@
1
1
  export interface rcConfigLoaderOption {
2
- // does look for `package.json`
3
- packageJSON?: boolean;
4
- // if config file name is not same with packageName, set the name
2
+ packageJSON?: boolean | {
3
+ fieldName: string;
4
+ };
5
5
  configFileName?: string;
6
- // treat default(no ext file) as some extension
7
6
  defaultExtension?: string | string[];
8
- // where start to load
9
7
  cwd?: string;
10
8
  }
11
- export default function rcConfigLoader(packageName: string, options?: rcConfigLoaderOption): Object;
9
+ /**
10
+ * Find and load rcfile, return { config, filePath }
11
+ * If not found any rcfile, throw an Error.
12
+ * @param {string} pkgName
13
+ * @param {rcConfigLoaderOption} [opts]
14
+ * @returns {{ config: Object, filePath:string } | undefined}
15
+ */
16
+ export declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption): {
17
+ config: R;
18
+ filePath: string;
19
+ } | undefined;
@@ -1,185 +1,172 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.rcFile = void 0;
1
7
  // MIT © 2017 azu
2
8
  // MIT © Zoltan Kochan
3
9
  // Original https://github.com/zkochan/rcfile
4
- "use strict";
5
-
6
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
7
-
8
- var path = require("path");
9
- var debug = require("debug")("rc-config-loader");
10
- var requireFromString = require("require-from-string");
11
- var JSON5 = require("json5");
12
- var fs = require("fs");
13
- var pathExists = require("path-exists");
14
- var objectAssign = require("object-assign");
15
- var keys = require("object-keys");
16
-
17
- var defaultLoaderByExt = {
10
+ const path_1 = __importDefault(require("path"));
11
+ const fs_1 = __importDefault(require("fs"));
12
+ const require_from_string_1 = __importDefault(require("require-from-string"));
13
+ const json5_1 = __importDefault(require("json5"));
14
+ const debug = require("debug")("rc-config-loader");
15
+ const defaultLoaderByExt = {
18
16
  ".js": loadJSConfigFile,
19
17
  ".json": loadJSONConfigFile,
20
18
  ".yaml": loadYAMLConfigFile,
21
- ".yml": loadYAMLConfigFile
19
+ ".yml": loadYAMLConfigFile,
22
20
  };
23
-
24
- var defaultOptions = {
21
+ const defaultOptions = {
25
22
  // does look for `package.json`
26
23
  packageJSON: false,
27
24
  // treat default(no ext file) as some extension
28
25
  defaultExtension: [".json", ".yaml", ".yml", ".js"],
29
- cwd: process.cwd()
26
+ cwd: process.cwd(),
27
+ };
28
+ const selectLoader = (defaultLoaderByExt, extension) => {
29
+ if (![".json", ".yaml", ".yml", ".js"].includes(extension)) {
30
+ throw new Error(`${extension} is not supported.`);
31
+ }
32
+ return defaultLoaderByExt[extension];
30
33
  };
31
-
32
34
  /**
35
+ * Find and load rcfile, return { config, filePath }
36
+ * If not found any rcfile, throw an Error.
33
37
  * @param {string} pkgName
34
38
  * @param {rcConfigLoaderOption} [opts]
35
39
  * @returns {{ config: Object, filePath:string } | undefined}
36
40
  */
37
- module.exports = function rcConfigLoader(pkgName) {
38
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
39
-
41
+ function rcFile(pkgName, opts = {}) {
40
42
  // path/to/config or basename of config file.
41
- var configFileName = opts.configFileName || "." + pkgName + "rc";
42
- var defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
43
- var cwd = opts.cwd || defaultOptions.cwd;
44
- var packageJSON = opts.packageJSON || defaultOptions.packageJSON;
45
- var packageJSONFieldName = (typeof packageJSON === "undefined" ? "undefined" : _typeof(packageJSON)) === "object" ? packageJSON.fieldName : pkgName;
46
-
47
- var parts = splitPath(cwd);
48
-
49
- var loaders = Array.isArray(defaultExtension) ? defaultExtension.map(function (extension) {
50
- return defaultLoaderByExt[extension];
51
- }) : defaultLoaderByExt[defaultExtension];
52
-
53
- var loaderByExt = objectAssign({}, defaultLoaderByExt, {
54
- "": loaders
43
+ const configFileName = opts.configFileName || `.${pkgName}rc`;
44
+ const defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
45
+ const cwd = opts.cwd || defaultOptions.cwd;
46
+ const packageJSON = opts.packageJSON || defaultOptions.packageJSON;
47
+ const packageJSONFieldName = typeof packageJSON === "object" ? packageJSON.fieldName : pkgName;
48
+ const parts = splitPath(cwd);
49
+ const loadersByOrder = Array.isArray(defaultExtension)
50
+ ? defaultExtension.map((extension) => selectLoader(defaultLoaderByExt, extension))
51
+ : selectLoader(defaultLoaderByExt, defaultExtension);
52
+ const loaderByExt = Object.assign(Object.assign({}, defaultLoaderByExt), { "": loadersByOrder });
53
+ return findConfig({
54
+ parts,
55
+ loaderByExt,
56
+ loadersByOrder,
57
+ configFileName,
58
+ packageJSON,
59
+ packageJSONFieldName,
55
60
  });
56
-
57
- return findConfig({ parts: parts, loaderByExt: loaderByExt, configFileName: configFileName, packageJSON: packageJSON, packageJSONFieldName: packageJSONFieldName });
58
- };
59
-
61
+ }
62
+ exports.rcFile = rcFile;
60
63
  /**
61
64
  *
62
- * @param {string[]} parts
63
- * @param {Object} loaderByExt
64
- * @param {string} configFileName
65
- * @param {boolean|Object} packageJSON
66
- * @param {string} packageJSONFieldName
67
65
  * @returns {{
68
66
  * config: string,
69
67
  * filePath: string
70
- * }|undefined}
68
+ * }}
71
69
  */
72
- function findConfig(_ref) {
73
- var parts = _ref.parts,
74
- loaderByExt = _ref.loaderByExt,
75
- configFileName = _ref.configFileName,
76
- packageJSON = _ref.packageJSON,
77
- packageJSONFieldName = _ref.packageJSONFieldName;
78
-
79
- var exts = keys(loaderByExt);
80
- while (exts.length) {
81
- var ext = exts.shift();
82
- var configLocation = join(parts, configFileName + ext);
83
- if (!pathExists.sync(configLocation)) {
70
+ function findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName, }) {
71
+ const extensions = Object.keys(loaderByExt);
72
+ while (extensions.length) {
73
+ const ext = extensions.shift();
74
+ // may be ext is "". if it .<product>rc
75
+ const configLocation = join(parts, configFileName + ext);
76
+ if (!fs_1.default.existsSync(configLocation)) {
84
77
  continue;
85
78
  }
86
- var loaders = loaderByExt[ext];
79
+ // if ext === ""(empty string):, use ordered loaders
80
+ const loaders = ext ? loaderByExt[ext] : loadersByOrder;
87
81
  if (!Array.isArray(loaders)) {
88
- var loader = loaders;
89
- var result = loader(configLocation);
82
+ const loader = loaders;
83
+ const result = loader(configLocation, false);
90
84
  if (!result) {
91
85
  continue;
92
86
  }
93
87
  return {
94
88
  config: result,
95
- filePath: configLocation
89
+ filePath: configLocation,
96
90
  };
97
91
  }
98
- for (var i = 0; i < loaders.length; i++) {
99
- var _loader = loaders[i];
100
- var _result = _loader(configLocation, true);
101
- if (!_result) {
92
+ for (let i = 0; i < loaders.length; i++) {
93
+ const loader = loaders[i];
94
+ const result = loader(configLocation, true);
95
+ if (!result) {
102
96
  continue;
103
97
  }
104
98
  return {
105
- config: _result,
106
- filePath: configLocation
99
+ config: result,
100
+ filePath: configLocation,
107
101
  };
108
102
  }
109
103
  }
110
-
111
104
  if (packageJSON) {
112
- var pkgJSONLoc = join(parts, "package.json");
113
- if (pathExists.sync(pkgJSONLoc)) {
114
- var pkgJSON = require(pkgJSONLoc);
105
+ const pkgJSONLoc = join(parts, "package.json");
106
+ if (fs_1.default.existsSync(pkgJSONLoc)) {
107
+ const pkgJSON = require(pkgJSONLoc);
115
108
  if (pkgJSON[packageJSONFieldName]) {
116
109
  return {
117
110
  config: pkgJSON[packageJSONFieldName],
118
- filePath: pkgJSONLoc
111
+ filePath: pkgJSONLoc,
119
112
  };
120
113
  }
121
114
  }
122
115
  }
123
116
  if (parts.pop()) {
124
- return findConfig({ parts: parts, loaderByExt: loaderByExt, configFileName: configFileName, packageJSON: packageJSON, packageJSONFieldName: packageJSONFieldName });
117
+ return findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName });
125
118
  }
126
- return undefined;
119
+ return;
127
120
  }
128
-
129
121
  function splitPath(x) {
130
- return path.resolve(x || "").split(path.sep);
122
+ return path_1.default.resolve(x || "").split(path_1.default.sep);
131
123
  }
132
-
133
124
  function join(parts, filename) {
134
- return path.resolve(parts.join(path.sep) + path.sep, filename);
125
+ return path_1.default.resolve(parts.join(path_1.default.sep) + path_1.default.sep, filename);
135
126
  }
136
-
137
127
  function loadJSConfigFile(filePath, suppress) {
138
- debug("Loading JavaScript config file: " + filePath);
128
+ debug(`Loading JavaScript config file: ${filePath}`);
139
129
  try {
140
- var content = fs.readFileSync(filePath, "utf-8");
141
- return requireFromString(content, filePath);
142
- } catch (e) {
143
- debug("Error reading JavaScript file: " + filePath);
130
+ const content = fs_1.default.readFileSync(filePath, "utf-8");
131
+ return require_from_string_1.default(content, filePath);
132
+ }
133
+ catch (error) {
134
+ debug(`Error reading JavaScript file: ${filePath}`);
144
135
  if (!suppress) {
145
- e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
146
- throw e;
136
+ error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
137
+ throw error;
147
138
  }
148
139
  }
149
140
  }
150
-
151
141
  function loadJSONConfigFile(filePath, suppress) {
152
- debug("Loading JSON config file: " + filePath);
153
-
142
+ debug(`Loading JSON config file: ${filePath}`);
154
143
  try {
155
- return JSON5.parse(readFile(filePath));
156
- } catch (e) {
157
- debug("Error reading JSON file: " + filePath);
144
+ return json5_1.default.parse(readFile(filePath));
145
+ }
146
+ catch (error) {
147
+ debug(`Error reading JSON file: ${filePath}`);
158
148
  if (!suppress) {
159
- e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
160
- throw e;
149
+ error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
150
+ throw error;
161
151
  }
162
152
  }
163
153
  }
164
-
165
154
  function readFile(filePath) {
166
- return fs.readFileSync(filePath, "utf8");
155
+ return fs_1.default.readFileSync(filePath, "utf8");
167
156
  }
168
-
169
157
  function loadYAMLConfigFile(filePath, suppress) {
170
- debug("Loading YAML config file: " + filePath);
171
-
158
+ debug(`Loading YAML config file: ${filePath}`);
172
159
  // lazy load YAML to improve performance when not used
173
- var yaml = require("js-yaml");
174
-
160
+ const yaml = require("js-yaml");
175
161
  try {
176
162
  // empty YAML file can be null, so always use
177
- return yaml.safeLoad(readFile(filePath)) || {};
178
- } catch (e) {
179
- debug("Error reading YAML file: " + filePath);
163
+ return yaml.load(readFile(filePath)) || {};
164
+ }
165
+ catch (error) {
166
+ debug(`Error reading YAML file: ${filePath}`);
180
167
  if (!suppress) {
181
- e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
182
- throw e;
168
+ error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
169
+ throw error;
183
170
  }
184
171
  }
185
172
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/rc-config-loader.js"],"names":["path","require","debug","requireFromString","JSON5","fs","pathExists","objectAssign","keys","defaultLoaderByExt","loadJSConfigFile","loadJSONConfigFile","loadYAMLConfigFile","defaultOptions","packageJSON","defaultExtension","cwd","process","module","exports","rcConfigLoader","pkgName","opts","configFileName","packageJSONFieldName","fieldName","parts","splitPath","loaders","Array","isArray","map","extension","loaderByExt","findConfig","exts","length","ext","shift","configLocation","join","sync","loader","result","config","filePath","i","pkgJSONLoc","pkgJSON","pop","undefined","x","resolve","split","sep","filename","suppress","content","readFileSync","e","message","parse","readFile","yaml","safeLoad"],"mappings":"AAAA;AACA;AACA;AACA;;;;AACA,IAAMA,OAAOC,QAAQ,MAAR,CAAb;AACA,IAAMC,QAAQD,QAAQ,OAAR,EAAiB,kBAAjB,CAAd;AACA,IAAME,oBAAoBF,QAAQ,qBAAR,CAA1B;AACA,IAAMG,QAAQH,QAAQ,OAAR,CAAd;AACA,IAAMI,KAAKJ,QAAQ,IAAR,CAAX;AACA,IAAMK,aAAaL,QAAQ,aAAR,CAAnB;AACA,IAAMM,eAAeN,QAAQ,eAAR,CAArB;AACA,IAAMO,OAAOP,QAAQ,aAAR,CAAb;;AAEA,IAAMQ,qBAAqB;AACvB,WAAOC,gBADgB;AAEvB,aAASC,kBAFc;AAGvB,aAASC,kBAHc;AAIvB,YAAQA;AAJe,CAA3B;;AAOA,IAAMC,iBAAiB;AACnB;AACAC,iBAAa,KAFM;AAGnB;AACAC,sBAAkB,CAAC,OAAD,EAAU,OAAV,EAAmB,MAAnB,EAA2B,KAA3B,CAJC;AAKnBC,SAAKC,QAAQD,GAAR;AALc,CAAvB;;AAQA;;;;;AAKAE,OAAOC,OAAP,GAAiB,SAASC,cAAT,CAAwBC,OAAxB,EAA4C;AAAA,QAAXC,IAAW,uEAAJ,EAAI;;AACzD;AACA,QAAMC,iBAAiBD,KAAKC,cAAL,UAA2BF,OAA3B,OAAvB;AACA,QAAMN,mBAAmBO,KAAKP,gBAAL,IAAyBF,eAA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,QAAL,EAAe;AACXG,cAAEC,OAAF,iCAAwCf,QAAxC,iBAA4Dc,EAAEC,OAA9D;AACA,kBAAMD,CAAN;AACH;AACJ;AACJ","file":"rc-config-loader.js","sourcesContent":["// MIT © 2017 azu\n// MIT © Zoltan Kochan\n// Original https://github.com/zkochan/rcfile\n\"use strict\";\nconst path = require(\"path\");\nconst debug = require(\"debug\")(\"rc-config-loader\");\nconst requireFromString = require(\"require-from-string\");\nconst JSON5 = require(\"json5\");\nconst fs = require(\"fs\");\nconst pathExists = require(\"path-exists\");\nconst objectAssign = require(\"object-assign\");\nconst keys = require(\"object-keys\");\n\nconst defaultLoaderByExt = {\n \".js\": loadJSConfigFile,\n \".json\": loadJSONConfigFile,\n \".yaml\": loadYAMLConfigFile,\n \".yml\": loadYAMLConfigFile\n};\n\nconst defaultOptions = {\n // does look for `package.json`\n packageJSON: false,\n // treat default(no ext file) as some extension\n defaultExtension: [\".json\", \".yaml\", \".yml\", \".js\"],\n cwd: process.cwd()\n};\n\n/**\n * @param {string} pkgName\n * @param {rcConfigLoaderOption} [opts]\n * @returns {{ config: Object, filePath:string } | undefined}\n */\nmodule.exports = function rcConfigLoader(pkgName, opts = {}) {\n // path/to/config or basename of config file.\n const configFileName = opts.configFileName || `.${pkgName}rc`;\n const defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;\n const cwd = opts.cwd || defaultOptions.cwd;\n const packageJSON = opts.packageJSON || defaultOptions.packageJSON;\n const packageJSONFieldName = typeof packageJSON === \"object\" ? packageJSON.fieldName : pkgName;\n\n const parts = splitPath(cwd);\n\n const loaders = Array.isArray(defaultExtension)\n ? defaultExtension.map(extension => defaultLoaderByExt[extension])\n : defaultLoaderByExt[defaultExtension];\n\n const loaderByExt = objectAssign({}, defaultLoaderByExt, {\n \"\": loaders\n });\n\n return findConfig({ parts, loaderByExt, configFileName, packageJSON, packageJSONFieldName });\n};\n\n/**\n *\n * @param {string[]} parts\n * @param {Object} loaderByExt\n * @param {string} configFileName\n * @param {boolean|Object} packageJSON\n * @param {string} packageJSONFieldName\n * @returns {{\n * config: string,\n * filePath: string\n * }|undefined}\n */\nfunction findConfig({ parts, loaderByExt, configFileName, packageJSON, packageJSONFieldName }) {\n const exts = keys(loaderByExt);\n while (exts.length) {\n const ext = exts.shift();\n const configLocation = join(parts, configFileName + ext);\n if (!pathExists.sync(configLocation)) {\n continue;\n }\n const loaders = loaderByExt[ext];\n if (!Array.isArray(loaders)) {\n const loader = loaders;\n const result = loader(configLocation);\n if (!result) {\n continue;\n }\n return {\n config: result,\n filePath: configLocation\n };\n }\n for (let i = 0; i < loaders.length; i++) {\n const loader = loaders[i];\n const result = loader(configLocation, true);\n if (!result) {\n continue;\n }\n return {\n config: result,\n filePath: configLocation\n };\n }\n }\n\n if (packageJSON) {\n const pkgJSONLoc = join(parts, \"package.json\");\n if (pathExists.sync(pkgJSONLoc)) {\n const pkgJSON = require(pkgJSONLoc);\n if (pkgJSON[packageJSONFieldName]) {\n return {\n config: pkgJSON[packageJSONFieldName],\n filePath: pkgJSONLoc\n };\n }\n }\n }\n if (parts.pop()) {\n return findConfig({ parts, loaderByExt, configFileName, packageJSON, packageJSONFieldName });\n }\n return undefined;\n}\n\nfunction splitPath(x) {\n return path.resolve(x || \"\").split(path.sep);\n}\n\nfunction join(parts, filename) {\n return path.resolve(parts.join(path.sep) + path.sep, filename);\n}\n\nfunction loadJSConfigFile(filePath, suppress) {\n debug(`Loading JavaScript config file: ${filePath}`);\n try {\n const content = fs.readFileSync(filePath, \"utf-8\");\n return requireFromString(content, filePath);\n } catch (e) {\n debug(`Error reading JavaScript file: ${filePath}`);\n if (!suppress) {\n e.message = `Cannot read config file: ${filePath}\\nError: ${e.message}`;\n throw e;\n }\n }\n}\n\nfunction loadJSONConfigFile(filePath, suppress) {\n debug(`Loading JSON config file: ${filePath}`);\n\n try {\n return JSON5.parse(readFile(filePath));\n } catch (e) {\n debug(`Error reading JSON file: ${filePath}`);\n if (!suppress) {\n e.message = `Cannot read config file: ${filePath}\\nError: ${e.message}`;\n throw e;\n }\n }\n}\n\nfunction readFile(filePath) {\n return fs.readFileSync(filePath, \"utf8\");\n}\n\nfunction loadYAMLConfigFile(filePath, suppress) {\n debug(`Loading YAML config file: ${filePath}`);\n\n // lazy load YAML to improve performance when not used\n const yaml = require(\"js-yaml\");\n\n try {\n // empty YAML file can be null, so always use\n return yaml.safeLoad(readFile(filePath)) || {};\n } catch (e) {\n debug(`Error reading YAML file: ${filePath}`);\n if (!suppress) {\n e.message = `Cannot read config file: ${filePath}\\nError: ${e.message}`;\n throw e;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"rc-config-loader.js","sourceRoot":"","sources":["../src/rc-config-loader.ts"],"names":[],"mappings":";;;;;;AAAA,iBAAiB;AACjB,sBAAsB;AACtB,6CAA6C;AAC7C,gDAAwB;AACxB,4CAAoB;AACpB,8EAAoD;AACpD,kDAA0B;AAE1B,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,kBAAkB,CAAC,CAAC;AACnD,MAAM,kBAAkB,GAAG;IACvB,KAAK,EAAE,gBAAgB;IACvB,OAAO,EAAE,kBAAkB;IAC3B,OAAO,EAAE,kBAAkB;IAC3B,MAAM,EAAE,kBAAkB;CAC7B,CAAC;AAEF,MAAM,cAAc,GAAG;IACnB,+BAA+B;IAC/B,WAAW,EAAE,KAAK;IAClB,+CAA+C;IAC/C,gBAAgB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC;IACnD,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;CACrB,CAAC;AAmBF,MAAM,YAAY,GAAG,CAAC,kBAA+C,EAAE,SAAiB,EAAE,EAAE;IACxF,IAAI,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE;QACxD,MAAM,IAAI,KAAK,CAAC,GAAG,SAAS,oBAAoB,CAAC,CAAC;KACrD;IAC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AC,6BAA6B,QAAQ,EAAE,CAAC,CAAC;IAE/C,IAAI;QACA,OAAO,eAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;KAC1C;IAAC,OAAO,KAAK,EAAE;QACZ,KAAK,CAAC,4BAA4B,QAAQ,EAAE,CAAC,CAAC;QAC9C,IAAI,CAAC,QAAQ,EAAE;YACX,KAAK,CAAC,OAAO,GAAG,4BAA4B,QAAQ,YAAY,KAAK,CAAC,OAAO,EAAE,CAAC;YAChF,MAAM,KAAK,CAAC;SACf;KACJ;AACL,CAAC;AAED,SAAS,QAAQ,CAAC,QAAgB;IAC9B,OAAO,YAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC7C,CAAC;AAED,SAAS,kBAAkB,CAAC,QAAgB,EAAE,QAAiB;IAC3D,KAAK,CAAC,6BAA6B,QAAQ,EAAE,CAAC,CAAC;IAC/C,sDAAsD;IACtD,MAAM,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,IAAI;QACA,6CAA6C;QAC7C,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;KAC9C;IAAC,OAAO,KAAK,EAAE;QACZ,KAAK,CAAC,4BAA4B,QAAQ,EAAE,CAAC,CAAC;QAC9C,IAAI,CAAC,QAAQ,EAAE;YACX,KAAK,CAAC,OAAO,GAAG,4BAA4B,QAAQ,YAAY,KAAK,CAAC,OAAO,EAAE,CAAC;YAChF,MAAM,KAAK,CAAC;SACf;KACJ;AACL,CAAC"}
package/package.json CHANGED
@@ -1,76 +1,76 @@
1
1
  {
2
- "directories": {
3
- "test": "test"
4
- },
5
- "author": "azu",
6
- "license": "MIT",
7
- "files": [
8
- "bin/",
9
- "lib/",
10
- "src/"
11
- ],
12
2
  "name": "rc-config-loader",
13
- "version": "2.0.3",
3
+ "version": "4.0.0",
14
4
  "description": "load config file from .{product}rc.{json,yml,js}",
15
- "main": "lib/rc-config-loader.js",
16
- "scripts": {
17
- "test": "mocha test/",
18
- "build": "cross-env NODE_ENV=production babel src --out-dir lib --copy-files --source-maps",
19
- "watch": "babel src --out-dir lib --watch --source-maps",
20
- "prepublish": "npm run --if-present build",
21
- "prettier": "prettier --write '**/*.{js,jsx,ts,tsx,css}'",
22
- "precommit": "lint-staged",
23
- "postcommit": "git reset"
24
- },
25
5
  "keywords": [
26
- "rc",
27
6
  "config",
28
7
  "configuration",
29
- "loader",
30
8
  "json",
9
+ "loader",
10
+ "rc",
31
11
  "yaml",
32
12
  "yml"
33
13
  ],
14
+ "homepage": "https://github.com/azu/rc-config-loader",
15
+ "bugs": {
16
+ "url": "https://github.com/azu/rc-config-loader/issues"
17
+ },
34
18
  "repository": {
35
19
  "type": "git",
36
20
  "url": "https://github.com/azu/rc-config-loader.git"
37
21
  },
38
- "bugs": {
39
- "url": "https://github.com/azu/rc-config-loader/issues"
22
+ "license": "MIT",
23
+ "author": "azu",
24
+ "files": [
25
+ "bin/",
26
+ "lib/",
27
+ "src/"
28
+ ],
29
+ "main": "lib/rc-config-loader.js",
30
+ "types": "lib/rc-config-loader.d.ts",
31
+ "directories": {
32
+ "test": "test"
40
33
  },
41
- "homepage": "https://github.com/azu/rc-config-loader",
42
- "dependencies": {
43
- "debug": "^3.1.0",
44
- "js-yaml": "^3.12.0",
45
- "json5": "^1.0.1",
46
- "object-assign": "^4.1.0",
47
- "object-keys": "^1.0.12",
48
- "path-exists": "^3.0.0",
49
- "require-from-string": "^2.0.2"
34
+ "scripts": {
35
+ "build": "tsc -p .",
36
+ "prettier": "prettier --write '**/*.{js,jsx,ts,tsx,css}'",
37
+ "prepublish": "npm run --if-present build",
38
+ "test": "mocha \"test/**/*.{js,ts}\"",
39
+ "watch": "tsc -p . --watch"
50
40
  },
51
- "devDependencies": {
52
- "babel-cli": "^6.22.2",
53
- "babel-preset-jsdoc-to-assert": "^4.0.0",
54
- "babel-preset-latest": "^6.22.0",
55
- "babel-preset-power-assert": "^1.0.0",
56
- "babel-register": "^6.22.0",
57
- "chai": "^3.5.0",
58
- "cross-env": "^3.1.4",
59
- "eslint": "^3.15.0",
60
- "husky": "^0.14.3",
61
- "lint-staged": "^5.0.0",
62
- "mocha": "^3.2.0",
63
- "power-assert": "^1.4.2",
64
- "prettier": "^1.8.2"
41
+ "husky": {
42
+ "hooks": {
43
+ "post-commit": "git reset",
44
+ "pre-commit": "lint-staged"
45
+ }
46
+ },
47
+ "lint-staged": {
48
+ "*.{js,jsx,ts,tsx,css}": [
49
+ "prettier --write"
50
+ ]
65
51
  },
66
52
  "prettier": {
67
53
  "printWidth": 120,
68
54
  "tabWidth": 4
69
55
  },
70
- "lint-staged": {
71
- "*.{js,jsx,ts,tsx,css}": [
72
- "prettier --write",
73
- "git add"
74
- ]
56
+ "dependencies": {
57
+ "debug": "^4.1.1",
58
+ "js-yaml": "^4.0.0",
59
+ "json5": "^2.1.2",
60
+ "require-from-string": "^2.0.2"
61
+ },
62
+ "devDependencies": {
63
+ "@types/json5": "^0.0.30",
64
+ "@types/mocha": "^8.2.0",
65
+ "@types/node": "^14.14.20",
66
+ "@types/require-from-string": "^1.2.0",
67
+ "chai": "^4.2.0",
68
+ "husky": "^4.2.3",
69
+ "lint-staged": "^10.1.1",
70
+ "mocha": "^8.2.1",
71
+ "prettier": "^2.0.2",
72
+ "ts-node": "^9.1.1",
73
+ "ts-node-test-register": "^9.0.1",
74
+ "typescript": "^4.1.3"
75
75
  }
76
76
  }
@@ -0,0 +1,226 @@
1
+ // MIT © 2017 azu
2
+ // MIT © Zoltan Kochan
3
+ // Original https://github.com/zkochan/rcfile
4
+ import path from "path";
5
+ import fs from "fs";
6
+ import requireFromString from "require-from-string";
7
+ import JSON5 from "json5";
8
+
9
+ const debug = require("debug")("rc-config-loader");
10
+ const defaultLoaderByExt = {
11
+ ".js": loadJSConfigFile,
12
+ ".json": loadJSONConfigFile,
13
+ ".yaml": loadYAMLConfigFile,
14
+ ".yml": loadYAMLConfigFile,
15
+ };
16
+
17
+ const defaultOptions = {
18
+ // does look for `package.json`
19
+ packageJSON: false,
20
+ // treat default(no ext file) as some extension
21
+ defaultExtension: [".json", ".yaml", ".yml", ".js"],
22
+ cwd: process.cwd(),
23
+ };
24
+
25
+ export interface rcConfigLoaderOption {
26
+ // does look for `package.json`
27
+ packageJSON?:
28
+ | boolean
29
+ | {
30
+ fieldName: string;
31
+ };
32
+ // if config file name is not same with packageName, set the name
33
+ configFileName?: string;
34
+ // treat default(no ext file) as some extension
35
+ defaultExtension?: string | string[];
36
+ // where start to load
37
+ cwd?: string;
38
+ }
39
+
40
+ type Loader = <R extends object>(fileName: string, suppress: boolean) => R;
41
+
42
+ const selectLoader = (defaultLoaderByExt: { [index: string]: Loader }, extension: string) => {
43
+ if (![".json", ".yaml", ".yml", ".js"].includes(extension)) {
44
+ throw new Error(`${extension} is not supported.`);
45
+ }
46
+ return defaultLoaderByExt[extension];
47
+ };
48
+
49
+ /**
50
+ * Find and load rcfile, return { config, filePath }
51
+ * If not found any rcfile, throw an Error.
52
+ * @param {string} pkgName
53
+ * @param {rcConfigLoaderOption} [opts]
54
+ * @returns {{ config: Object, filePath:string } | undefined}
55
+ */
56
+ export function rcFile<R extends {}>(
57
+ pkgName: string,
58
+ opts: rcConfigLoaderOption = {}
59
+ ):
60
+ | {
61
+ config: R;
62
+ filePath: string;
63
+ }
64
+ | undefined {
65
+ // path/to/config or basename of config file.
66
+ const configFileName = opts.configFileName || `.${pkgName}rc`;
67
+ const defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
68
+ const cwd = opts.cwd || defaultOptions.cwd;
69
+ const packageJSON = opts.packageJSON || defaultOptions.packageJSON;
70
+ const packageJSONFieldName = typeof packageJSON === "object" ? packageJSON.fieldName : pkgName;
71
+
72
+ const parts = splitPath(cwd);
73
+ const loadersByOrder = Array.isArray(defaultExtension)
74
+ ? defaultExtension.map((extension) => selectLoader(defaultLoaderByExt, extension))
75
+ : selectLoader(defaultLoaderByExt, defaultExtension);
76
+
77
+ const loaderByExt = {
78
+ ...defaultLoaderByExt,
79
+ "": loadersByOrder,
80
+ };
81
+ return findConfig<R>({
82
+ parts,
83
+ loaderByExt,
84
+ loadersByOrder,
85
+ configFileName,
86
+ packageJSON,
87
+ packageJSONFieldName,
88
+ });
89
+ }
90
+
91
+ /**
92
+ *
93
+ * @returns {{
94
+ * config: string,
95
+ * filePath: string
96
+ * }}
97
+ */
98
+ function findConfig<R extends {}>({
99
+ parts,
100
+ loaderByExt,
101
+ loadersByOrder,
102
+ configFileName,
103
+ packageJSON,
104
+ packageJSONFieldName,
105
+ }: {
106
+ parts: string[];
107
+ loaderByExt: {
108
+ [index: string]: Loader | Loader[];
109
+ };
110
+ loadersByOrder: Loader | Loader[];
111
+ configFileName: string;
112
+ packageJSON: boolean | { fieldName: string };
113
+ packageJSONFieldName: string;
114
+ }):
115
+ | {
116
+ config: R;
117
+ filePath: string;
118
+ }
119
+ | undefined {
120
+ const extensions = Object.keys(loaderByExt);
121
+ while (extensions.length) {
122
+ const ext = extensions.shift();
123
+ // may be ext is "". if it .<product>rc
124
+ const configLocation = join(parts, configFileName + ext);
125
+ if (!fs.existsSync(configLocation)) {
126
+ continue;
127
+ }
128
+ // if ext === ""(empty string):, use ordered loaders
129
+ const loaders = ext ? loaderByExt[ext] : loadersByOrder;
130
+ if (!Array.isArray(loaders)) {
131
+ const loader = loaders;
132
+ const result = loader<R>(configLocation, false);
133
+ if (!result) {
134
+ continue;
135
+ }
136
+ return {
137
+ config: result,
138
+ filePath: configLocation,
139
+ };
140
+ }
141
+ for (let i = 0; i < loaders.length; i++) {
142
+ const loader = loaders[i];
143
+ const result = loader<R>(configLocation, true);
144
+ if (!result) {
145
+ continue;
146
+ }
147
+ return {
148
+ config: result,
149
+ filePath: configLocation,
150
+ };
151
+ }
152
+ }
153
+
154
+ if (packageJSON) {
155
+ const pkgJSONLoc = join(parts, "package.json");
156
+ if (fs.existsSync(pkgJSONLoc)) {
157
+ const pkgJSON = require(pkgJSONLoc);
158
+ if (pkgJSON[packageJSONFieldName]) {
159
+ return {
160
+ config: pkgJSON[packageJSONFieldName],
161
+ filePath: pkgJSONLoc,
162
+ };
163
+ }
164
+ }
165
+ }
166
+ if (parts.pop()) {
167
+ return findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName });
168
+ }
169
+ return;
170
+ }
171
+
172
+ function splitPath(x: string): string[] {
173
+ return path.resolve(x || "").split(path.sep);
174
+ }
175
+
176
+ function join(parts: string[], filename: string) {
177
+ return path.resolve(parts.join(path.sep) + path.sep, filename);
178
+ }
179
+
180
+ function loadJSConfigFile(filePath: string, suppress: boolean) {
181
+ debug(`Loading JavaScript config file: ${filePath}`);
182
+ try {
183
+ const content = fs.readFileSync(filePath, "utf-8");
184
+ return requireFromString(content, filePath);
185
+ } catch (error) {
186
+ debug(`Error reading JavaScript file: ${filePath}`);
187
+ if (!suppress) {
188
+ error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
189
+ throw error;
190
+ }
191
+ }
192
+ }
193
+
194
+ function loadJSONConfigFile(filePath: string, suppress: boolean) {
195
+ debug(`Loading JSON config file: ${filePath}`);
196
+
197
+ try {
198
+ return JSON5.parse(readFile(filePath));
199
+ } catch (error) {
200
+ debug(`Error reading JSON file: ${filePath}`);
201
+ if (!suppress) {
202
+ error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
203
+ throw error;
204
+ }
205
+ }
206
+ }
207
+
208
+ function readFile(filePath: string) {
209
+ return fs.readFileSync(filePath, "utf8");
210
+ }
211
+
212
+ function loadYAMLConfigFile(filePath: string, suppress: boolean) {
213
+ debug(`Loading YAML config file: ${filePath}`);
214
+ // lazy load YAML to improve performance when not used
215
+ const yaml = require("js-yaml");
216
+ try {
217
+ // empty YAML file can be null, so always use
218
+ return yaml.load(readFile(filePath)) || {};
219
+ } catch (error) {
220
+ debug(`Error reading YAML file: ${filePath}`);
221
+ if (!suppress) {
222
+ error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
223
+ throw error;
224
+ }
225
+ }
226
+ }
@@ -1,11 +0,0 @@
1
- export interface rcConfigLoaderOption {
2
- // does look for `package.json`
3
- packageJSON?: boolean;
4
- // if config file name is not same with packageName, set the name
5
- configFileName?: string;
6
- // treat default(no ext file) as some extension
7
- defaultExtension?: string | string[];
8
- // where start to load
9
- cwd?: string;
10
- }
11
- export default function rcConfigLoader(packageName: string, options?: rcConfigLoaderOption): Object;
@@ -1,174 +0,0 @@
1
- // MIT © 2017 azu
2
- // MIT © Zoltan Kochan
3
- // Original https://github.com/zkochan/rcfile
4
- "use strict";
5
- const path = require("path");
6
- const debug = require("debug")("rc-config-loader");
7
- const requireFromString = require("require-from-string");
8
- const JSON5 = require("json5");
9
- const fs = require("fs");
10
- const pathExists = require("path-exists");
11
- const objectAssign = require("object-assign");
12
- const keys = require("object-keys");
13
-
14
- const defaultLoaderByExt = {
15
- ".js": loadJSConfigFile,
16
- ".json": loadJSONConfigFile,
17
- ".yaml": loadYAMLConfigFile,
18
- ".yml": loadYAMLConfigFile
19
- };
20
-
21
- const defaultOptions = {
22
- // does look for `package.json`
23
- packageJSON: false,
24
- // treat default(no ext file) as some extension
25
- defaultExtension: [".json", ".yaml", ".yml", ".js"],
26
- cwd: process.cwd()
27
- };
28
-
29
- /**
30
- * @param {string} pkgName
31
- * @param {rcConfigLoaderOption} [opts]
32
- * @returns {{ config: Object, filePath:string } | undefined}
33
- */
34
- module.exports = function rcConfigLoader(pkgName, opts = {}) {
35
- // path/to/config or basename of config file.
36
- const configFileName = opts.configFileName || `.${pkgName}rc`;
37
- const defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
38
- const cwd = opts.cwd || defaultOptions.cwd;
39
- const packageJSON = opts.packageJSON || defaultOptions.packageJSON;
40
- const packageJSONFieldName = typeof packageJSON === "object" ? packageJSON.fieldName : pkgName;
41
-
42
- const parts = splitPath(cwd);
43
-
44
- const loaders = Array.isArray(defaultExtension)
45
- ? defaultExtension.map(extension => defaultLoaderByExt[extension])
46
- : defaultLoaderByExt[defaultExtension];
47
-
48
- const loaderByExt = objectAssign({}, defaultLoaderByExt, {
49
- "": loaders
50
- });
51
-
52
- return findConfig({ parts, loaderByExt, configFileName, packageJSON, packageJSONFieldName });
53
- };
54
-
55
- /**
56
- *
57
- * @param {string[]} parts
58
- * @param {Object} loaderByExt
59
- * @param {string} configFileName
60
- * @param {boolean|Object} packageJSON
61
- * @param {string} packageJSONFieldName
62
- * @returns {{
63
- * config: string,
64
- * filePath: string
65
- * }|undefined}
66
- */
67
- function findConfig({ parts, loaderByExt, configFileName, packageJSON, packageJSONFieldName }) {
68
- const exts = keys(loaderByExt);
69
- while (exts.length) {
70
- const ext = exts.shift();
71
- const configLocation = join(parts, configFileName + ext);
72
- if (!pathExists.sync(configLocation)) {
73
- continue;
74
- }
75
- const loaders = loaderByExt[ext];
76
- if (!Array.isArray(loaders)) {
77
- const loader = loaders;
78
- const result = loader(configLocation);
79
- if (!result) {
80
- continue;
81
- }
82
- return {
83
- config: result,
84
- filePath: configLocation
85
- };
86
- }
87
- for (let i = 0; i < loaders.length; i++) {
88
- const loader = loaders[i];
89
- const result = loader(configLocation, true);
90
- if (!result) {
91
- continue;
92
- }
93
- return {
94
- config: result,
95
- filePath: configLocation
96
- };
97
- }
98
- }
99
-
100
- if (packageJSON) {
101
- const pkgJSONLoc = join(parts, "package.json");
102
- if (pathExists.sync(pkgJSONLoc)) {
103
- const pkgJSON = require(pkgJSONLoc);
104
- if (pkgJSON[packageJSONFieldName]) {
105
- return {
106
- config: pkgJSON[packageJSONFieldName],
107
- filePath: pkgJSONLoc
108
- };
109
- }
110
- }
111
- }
112
- if (parts.pop()) {
113
- return findConfig({ parts, loaderByExt, configFileName, packageJSON, packageJSONFieldName });
114
- }
115
- return undefined;
116
- }
117
-
118
- function splitPath(x) {
119
- return path.resolve(x || "").split(path.sep);
120
- }
121
-
122
- function join(parts, filename) {
123
- return path.resolve(parts.join(path.sep) + path.sep, filename);
124
- }
125
-
126
- function loadJSConfigFile(filePath, suppress) {
127
- debug(`Loading JavaScript config file: ${filePath}`);
128
- try {
129
- const content = fs.readFileSync(filePath, "utf-8");
130
- return requireFromString(content, filePath);
131
- } catch (e) {
132
- debug(`Error reading JavaScript file: ${filePath}`);
133
- if (!suppress) {
134
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
135
- throw e;
136
- }
137
- }
138
- }
139
-
140
- function loadJSONConfigFile(filePath, suppress) {
141
- debug(`Loading JSON config file: ${filePath}`);
142
-
143
- try {
144
- return JSON5.parse(readFile(filePath));
145
- } catch (e) {
146
- debug(`Error reading JSON file: ${filePath}`);
147
- if (!suppress) {
148
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
149
- throw e;
150
- }
151
- }
152
- }
153
-
154
- function readFile(filePath) {
155
- return fs.readFileSync(filePath, "utf8");
156
- }
157
-
158
- function loadYAMLConfigFile(filePath, suppress) {
159
- debug(`Loading YAML config file: ${filePath}`);
160
-
161
- // lazy load YAML to improve performance when not used
162
- const yaml = require("js-yaml");
163
-
164
- try {
165
- // empty YAML file can be null, so always use
166
- return yaml.safeLoad(readFile(filePath)) || {};
167
- } catch (e) {
168
- debug(`Error reading YAML file: ${filePath}`);
169
- if (!suppress) {
170
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
171
- throw e;
172
- }
173
- }
174
- }