rc-config-loader 2.0.2 → 3.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
@@ -12,7 +12,7 @@ Find and load a configuration object from:
12
12
  - a JSON or YAML, JS "rc file"
13
13
  - `.<product>rc` or `.<product>rc.json` or `.<product>rc.js` or`.<product>rc.yml`, `.<product>rc.yaml`
14
14
  - TypeScript support
15
- - Include `.d.ts`
15
+ - Includes `.d.ts`
16
16
 
17
17
  ## Difference
18
18
 
@@ -24,7 +24,11 @@ Find and load a configuration object from:
24
24
 
25
25
  ### with [cosmiconfig](https://github.com/davidtheclark/cosmiconfig "cosmiconfig")
26
26
 
27
- - Sync loading
27
+ - <del>Sync loading</del>
28
+ - [cosmiconfig@3+](https://github.com/davidtheclark/cosmiconfig/blob/master/CHANGELOG.md#300) support `sync` option
29
+ - Built-in TypeScript support
30
+
31
+ If you want to async support and customize loader, recommenced to use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig).
28
32
 
29
33
  ## Install
30
34
 
@@ -39,34 +43,75 @@ Install with [npm](https://www.npmjs.com/):
39
43
  ```ts
40
44
  export interface rcConfigLoaderOption {
41
45
  // does look for `package.json`
42
- packageJSON?: boolean,
46
+ packageJSON?:
47
+ | boolean
48
+ | {
49
+ fieldName: string;
50
+ };
43
51
  // if config file name is not same with packageName, set the name
44
52
  configFileName?: string;
45
53
  // treat default(no ext file) as some extension
46
- defaultExtension?: string | string[],
54
+ defaultExtension?: string | string[];
47
55
  // where start to load
48
56
  cwd?: string;
49
57
  }
50
- export default function rcConfigLoader(packageName: string, options?: rcConfigLoaderOption): Object;
58
+ /**
59
+ * Find and load rcfile, return { config, filePath }
60
+ * If not found any rcfile, throw an Error.
61
+ * @param {string} pkgName
62
+ * @param {rcConfigLoaderOption} [opts]
63
+ * @returns {{ config: Object, filePath:string } | undefined}
64
+ */
65
+ export declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption): {
66
+ config: R;
67
+ filePath: string;
68
+ } | undefined;
69
+
51
70
  ```
52
71
 
53
- `rcConfigLoader` return `{ config, filePath }` object.
72
+ `rcFile` return `{ config, filePath }` object.
54
73
 
55
74
  - `config`: it is config object
56
75
  - `filePath`: absolute path to config file
57
76
 
58
- If not found config file, return `undefined`.
77
+ **Note:**
78
+
79
+ - `rcFile` function return `undefined` if the config file is not found
80
+ - `rcFile` throw an Error if the config file content is malformed (causing a parsing error)
81
+
82
+ Recommenced usage:
83
+
84
+ ```js
85
+ import { rcFile } from "rc-config-loader"
86
+
87
+ function loadRcFile(rcFileName){
88
+ try {
89
+ const results = rcFile(rcFileName);
90
+ // Not Found
91
+ if (!results) {
92
+ return {};
93
+ }
94
+ return config;
95
+ } catch (error) {
96
+ // Found it, but it is parsing error
97
+ return {} ; // default value
98
+ }
99
+ }
100
+ // load config
101
+ const config = loadRcFile("your-application");
102
+ console.log(config); // => rcfile content
103
+ ```
59
104
 
60
105
  ### Example
61
106
 
62
107
  ```js
63
108
  "use strict";
64
- const rcfile = require("rc-config-loader");
109
+ import { rcFile } from "rc-config-loader"
65
110
  // load .eslintrc from current dir
66
- console.log(rcfile("eslint"));
111
+ console.log(rcFile("eslint"));
67
112
 
68
113
  // load .eslintrc from specific path
69
- console.log(rcfile("eslint", {
114
+ console.log(rcFile("eslint", {
70
115
  configFileName: `${__dirname}/test/fixtures/.eslintrc`
71
116
  }));
72
117
  /*
@@ -78,7 +123,7 @@ filePath: ${__dirname}/test/fixtures/.eslintrc
78
123
  */
79
124
 
80
125
  // load property from pacakge.json
81
- console.log(rcfile("rc-config-loader", {
126
+ console.log(rcFile("rc-config-loader", {
82
127
  packageJSON: {
83
128
  fieldName: "directories"
84
129
  }
@@ -89,27 +134,30 @@ filePath: /path/to/package.json
89
134
  */
90
135
 
91
136
  // load .eslintrc from specific dir
92
- console.log(rcfile("eslint", {
137
+ console.log(rcFile("eslint", {
93
138
  cwd: `${__dirname}/test/fixtures`
94
139
  }));
95
140
 
96
141
  // load specific filename from current dir
97
- console.log(rcfile("travis", {configFileName: ".travis"}));
142
+ console.log(rcFile("travis", {configFileName: ".travis"}));
98
143
  /*
99
144
  config: { sudo: false, language: 'node_js', node_js: 'stable' }
100
145
  filePath: /path/to/.travis
101
146
  */
102
147
 
103
148
  // try to load as .json, .yml, js
104
- console.log(rcfile("bar", {
149
+ console.log(rcFile("bar", {
105
150
  configFileName: `${__dirname}/test/fixtures/.barrc`,
106
151
  defaultExtension: [".json", ".yml", ".js"]
107
152
  }));
108
153
 
109
- // try to load as .yml, but it is not json
110
- // throw Error
154
+ // try to load as foobar, but .foobarrc is not found
155
+ console.log(rcFile("foorbar")); // => undefined
156
+
157
+ // try to load as .json, but it is not json
158
+ // throw SyntaxError
111
159
  try {
112
- rcfile("unknown", {
160
+ rcFile("unknown", {
113
161
  // This is not json
114
162
  configFileName: `${__dirname}/test/fixtures/.unknownrc`,
115
163
  defaultExtension: ".json"
@@ -120,7 +168,6 @@ try {
120
168
  SyntaxError: Cannot read config file: /test/fixtures/.unknownrc
121
169
  */
122
170
  }
123
-
124
171
  ```
125
172
 
126
173
  ## 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,26 +1,22 @@
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 });
1
6
  // MIT © 2017 azu
2
7
  // MIT © Zoltan Kochan
3
8
  // 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 path_1 = __importDefault(require("path"));
10
+ var fs_1 = __importDefault(require("fs"));
9
11
  var debug = require("debug")("rc-config-loader");
10
12
  var requireFromString = require("require-from-string");
11
13
  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
14
  var defaultLoaderByExt = {
18
15
  ".js": loadJSConfigFile,
19
16
  ".json": loadJSONConfigFile,
20
17
  ".yaml": loadYAMLConfigFile,
21
18
  ".yml": loadYAMLConfigFile
22
19
  };
23
-
24
20
  var defaultOptions = {
25
21
  // does look for `package.json`
26
22
  packageJSON: false,
@@ -28,89 +24,89 @@ var defaultOptions = {
28
24
  defaultExtension: [".json", ".yaml", ".yml", ".js"],
29
25
  cwd: process.cwd()
30
26
  };
31
-
27
+ var selectLoader = function (defaultLoaderByExt, extension) {
28
+ if (![".json", ".yaml", ".yml", ".js"].includes(extension)) {
29
+ throw new Error(extension + " is not supported.");
30
+ }
31
+ return defaultLoaderByExt[extension];
32
+ };
32
33
  /**
34
+ * Find and load rcfile, return { config, filePath }
35
+ * If not found any rcfile, throw an Error.
33
36
  * @param {string} pkgName
34
37
  * @param {rcConfigLoaderOption} [opts]
35
38
  * @returns {{ config: Object, filePath:string } | undefined}
36
39
  */
37
- module.exports = function rcConfigLoader(pkgName) {
38
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
39
-
40
+ function rcFile(pkgName, opts) {
41
+ if (opts === void 0) { opts = {}; }
40
42
  // path/to/config or basename of config file.
41
43
  var configFileName = opts.configFileName || "." + pkgName + "rc";
42
44
  var defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
43
45
  var cwd = opts.cwd || defaultOptions.cwd;
44
46
  var packageJSON = opts.packageJSON || defaultOptions.packageJSON;
45
- var packageJSONFieldName = (typeof packageJSON === "undefined" ? "undefined" : _typeof(packageJSON)) === "object" ? packageJSON.fieldName : pkgName;
46
-
47
+ var packageJSONFieldName = typeof packageJSON === "object" ? packageJSON.fieldName : pkgName;
47
48
  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
49
+ var loadersByOrder = Array.isArray(defaultExtension)
50
+ ? defaultExtension.map(function (extension) { return selectLoader(defaultLoaderByExt, extension); })
51
+ : selectLoader(defaultLoaderByExt, defaultExtension);
52
+ var loaderByExt = Object.assign({}, defaultLoaderByExt, {
53
+ "": loadersByOrder
55
54
  });
56
-
57
- return findConfig({ parts: parts, loaderByExt: loaderByExt, configFileName: configFileName, packageJSON: packageJSON, packageJSONFieldName: packageJSONFieldName });
58
- };
59
-
55
+ return findConfig({
56
+ parts: parts,
57
+ loaderByExt: loaderByExt,
58
+ loadersByOrder: loadersByOrder,
59
+ configFileName: configFileName,
60
+ packageJSON: packageJSON,
61
+ packageJSONFieldName: packageJSONFieldName
62
+ });
63
+ }
64
+ exports.rcFile = rcFile;
60
65
  /**
61
66
  *
62
- * @param {string[]} parts
63
- * @param {Object} loaderByExt
64
- * @param {string} configFileName
65
- * @param {boolean|Object} packageJSON
66
- * @param {string} packageJSONFieldName
67
67
  * @returns {{
68
68
  * config: string,
69
69
  * filePath: string
70
- * }|undefined}
70
+ * }}
71
71
  */
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();
72
+ function findConfig(_a) {
73
+ var parts = _a.parts, loaderByExt = _a.loaderByExt, loadersByOrder = _a.loadersByOrder, configFileName = _a.configFileName, packageJSON = _a.packageJSON, packageJSONFieldName = _a.packageJSONFieldName;
74
+ var extensions = Object.keys(loaderByExt);
75
+ while (extensions.length) {
76
+ var ext = extensions.shift();
77
+ // may be ext is "". if it .<product>rc
82
78
  var configLocation = join(parts, configFileName + ext);
83
- if (!pathExists.sync(configLocation)) {
79
+ if (!fs_1.default.existsSync(configLocation)) {
84
80
  continue;
85
81
  }
86
- var loaders = loaderByExt[ext];
82
+ // if ext === ""(empty string):, use ordered loaders
83
+ var loaders = ext ? loaderByExt[ext] : loadersByOrder;
87
84
  if (!Array.isArray(loaders)) {
88
85
  var loader = loaders;
89
- var result = loader(configLocation);
86
+ var result = loader(configLocation, false);
90
87
  if (!result) {
91
88
  continue;
92
89
  }
93
90
  return {
94
- config: loader(configLocation),
91
+ config: result,
95
92
  filePath: configLocation
96
93
  };
97
94
  }
98
95
  for (var i = 0; i < loaders.length; i++) {
99
- var _loader = loaders[i];
100
- var _result = _loader(configLocation, true);
101
- if (!_result) {
96
+ var loader = loaders[i];
97
+ var result = loader(configLocation, true);
98
+ if (!result) {
102
99
  continue;
103
100
  }
104
101
  return {
105
- config: _result,
102
+ config: result,
106
103
  filePath: configLocation
107
104
  };
108
105
  }
109
106
  }
110
-
111
107
  if (packageJSON) {
112
108
  var pkgJSONLoc = join(parts, "package.json");
113
- if (pathExists.sync(pkgJSONLoc)) {
109
+ if (fs_1.default.existsSync(pkgJSONLoc)) {
114
110
  var pkgJSON = require(pkgJSONLoc);
115
111
  if (pkgJSON[packageJSONFieldName]) {
116
112
  return {
@@ -121,65 +117,59 @@ function findConfig(_ref) {
121
117
  }
122
118
  }
123
119
  if (parts.pop()) {
124
- return findConfig({ parts: parts, loaderByExt: loaderByExt, configFileName: configFileName, packageJSON: packageJSON, packageJSONFieldName: packageJSONFieldName });
120
+ return findConfig({ parts: parts, loaderByExt: loaderByExt, loadersByOrder: loadersByOrder, configFileName: configFileName, packageJSON: packageJSON, packageJSONFieldName: packageJSONFieldName });
125
121
  }
126
- return undefined;
122
+ return;
127
123
  }
128
-
129
124
  function splitPath(x) {
130
- return path.resolve(x || "").split(path.sep);
125
+ return path_1.default.resolve(x || "").split(path_1.default.sep);
131
126
  }
132
-
133
127
  function join(parts, filename) {
134
- return path.resolve(parts.join(path.sep) + path.sep, filename);
128
+ return path_1.default.resolve(parts.join(path_1.default.sep) + path_1.default.sep, filename);
135
129
  }
136
-
137
130
  function loadJSConfigFile(filePath, suppress) {
138
131
  debug("Loading JavaScript config file: " + filePath);
139
132
  try {
140
- var content = fs.readFileSync(filePath, "utf-8");
133
+ var content = fs_1.default.readFileSync(filePath, "utf-8");
141
134
  return requireFromString(content, filePath);
142
- } catch (e) {
135
+ }
136
+ catch (error) {
143
137
  debug("Error reading JavaScript file: " + filePath);
144
138
  if (!suppress) {
145
- e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
146
- throw e;
139
+ error.message = "Cannot read config file: " + filePath + "\nError: " + error.message;
140
+ throw error;
147
141
  }
148
142
  }
149
143
  }
150
-
151
144
  function loadJSONConfigFile(filePath, suppress) {
152
145
  debug("Loading JSON config file: " + filePath);
153
-
154
146
  try {
155
147
  return JSON5.parse(readFile(filePath));
156
- } catch (e) {
148
+ }
149
+ catch (error) {
157
150
  debug("Error reading JSON file: " + filePath);
158
151
  if (!suppress) {
159
- e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
160
- throw e;
152
+ error.message = "Cannot read config file: " + filePath + "\nError: " + error.message;
153
+ throw error;
161
154
  }
162
155
  }
163
156
  }
164
-
165
157
  function readFile(filePath) {
166
- return fs.readFileSync(filePath, "utf8");
158
+ return fs_1.default.readFileSync(filePath, "utf8");
167
159
  }
168
-
169
160
  function loadYAMLConfigFile(filePath, suppress) {
170
161
  debug("Loading YAML config file: " + filePath);
171
-
172
162
  // lazy load YAML to improve performance when not used
173
163
  var yaml = require("js-yaml");
174
-
175
164
  try {
176
165
  // empty YAML file can be null, so always use
177
166
  return yaml.safeLoad(readFile(filePath)) || {};
178
- } catch (e) {
167
+ }
168
+ catch (error) {
179
169
  debug("Error reading YAML file: " + filePath);
180
170
  if (!suppress) {
181
- e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
182
- throw e;
171
+ error.message = "Cannot read config file: " + filePath + "\nError: " + error.message;
172
+ throw error;
183
173
  }
184
174
  }
185
175
  }
@@ -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,IAAM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EE,OAAOc,CAAP,EAAU;AACRzD,4CAAkC2C,QAAlC;AACA,YAAI,CAACW,QAAL,EAAe;AACXG,cAAEC,OAAF,iCAAwCf,QAAxC,iBAA4Dc,EAAEC,OAA9D;AACA,kBAAMD,CAAN;AACH;AACJ;AACJ;;AAED,SAASG,QAAT,CAAkBjB,QAAlB,EAA4B;AACxB,WAAOxC,GAAGqD,YAAH,CAAgBb,QAAhB,EAA0B,MAA1B,CAAP;AACH;;AAED,SAASjC,kBAAT,CAA4BiC,QAA5B,EAAsCW,QAAtC,EAAgD;AAC5CtD,yCAAmC2C,QAAnC;;AAEA;AACA,QAAMkB,OAAO9D,QAAQ,SAAR,CAAb;;AAEA,QAAI;AACA;AACA,eAAO8D,KAAKC,QAAL,CAAcF,SAASjB,QAAT,CAAd,KAAqC,EAA5C;AACH,KAHD,CAGE,OAAOc,CAAP,EAAU;AACRzD,4CAAkC2C,QAAlC;AACA,YAAI,CAACW,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: loader(configLocation),\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,8CAAwB;AACxB,0CAAoB;AAEpB,IAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,kBAAkB,CAAC,CAAC;AACnD,IAAM,iBAAiB,GAAG,OAAO,CAAC,q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,MAAM,CAAC,CAAC;AAC7C,CAAC;AAED,SAAS,kBAAkB,CAAC,QAAgB,EAAE,QAAiB;IAC3D,KAAK,CAAC,+BAA6B,QAAU,CAAC,CAAC;IAE/C,sDAAsD;IACtD,IAAM,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAEhC,IAAI;QACA,6CAA6C;QAC7C,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;KAClD;IAAC,OAAO,KAAK,EAAE;QACZ,KAAK,CAAC,8BAA4B,QAAU,CAAC,CAAC;QAC9C,IAAI,CAAC,QAAQ,EAAE;YACX,KAAK,CAAC,OAAO,GAAG,8BAA4B,QAAQ,iBAAY,KAAK,CAAC,OAAS,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.2",
3
+ "version": "3.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"
40
- },
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"
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"
50
33
  },
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"
34
+ "scripts": {
35
+ "build": "cross-env NODE_ENV=production 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"
65
40
  },
66
- "prettier": {
67
- "printWidth": 120,
68
- "tabWidth": 4
41
+ "husky": {
42
+ "hooks": {
43
+ "post-commit": "git reset",
44
+ "pre-commit": "lint-staged"
45
+ }
69
46
  },
70
47
  "lint-staged": {
71
48
  "*.{js,jsx,ts,tsx,css}": [
72
49
  "prettier --write",
73
50
  "git add"
74
51
  ]
52
+ },
53
+ "prettier": {
54
+ "printWidth": 120,
55
+ "tabWidth": 4
56
+ },
57
+ "dependencies": {
58
+ "debug": "^4.1.1",
59
+ "js-yaml": "^3.12.0",
60
+ "json5": "^2.1.1",
61
+ "require-from-string": "^2.0.2"
62
+ },
63
+ "devDependencies": {
64
+ "@types/mocha": "^5.2.7",
65
+ "@types/node": "^12.12.5",
66
+ "chai": "^4.2.0",
67
+ "cross-env": "^6.0.3",
68
+ "husky": "^3.0.9",
69
+ "lint-staged": "^9.4.2",
70
+ "mocha": "^6.2.2",
71
+ "prettier": "^1.8.2",
72
+ "ts-node": "^8.4.1",
73
+ "ts-node-test-register": "^8.0.1",
74
+ "typescript": "^3.6.4"
75
75
  }
76
76
  }
@@ -0,0 +1,228 @@
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
+
7
+ const debug = require("debug")("rc-config-loader");
8
+ const requireFromString = require("require-from-string");
9
+ const JSON5 = require("json5");
10
+
11
+ const defaultLoaderByExt = {
12
+ ".js": loadJSConfigFile,
13
+ ".json": loadJSONConfigFile,
14
+ ".yaml": loadYAMLConfigFile,
15
+ ".yml": loadYAMLConfigFile
16
+ };
17
+
18
+ const defaultOptions = {
19
+ // does look for `package.json`
20
+ packageJSON: false,
21
+ // treat default(no ext file) as some extension
22
+ defaultExtension: [".json", ".yaml", ".yml", ".js"],
23
+ cwd: process.cwd()
24
+ };
25
+
26
+ export interface rcConfigLoaderOption {
27
+ // does look for `package.json`
28
+ packageJSON?:
29
+ | boolean
30
+ | {
31
+ fieldName: string;
32
+ };
33
+ // if config file name is not same with packageName, set the name
34
+ configFileName?: string;
35
+ // treat default(no ext file) as some extension
36
+ defaultExtension?: string | string[];
37
+ // where start to load
38
+ cwd?: string;
39
+ }
40
+
41
+ type Loader = <R extends object>(fileName: string, suppress: boolean) => R;
42
+
43
+ const selectLoader = (defaultLoaderByExt: { [index: string]: Loader }, extension: string) => {
44
+ if (![".json", ".yaml", ".yml", ".js"].includes(extension)) {
45
+ throw new Error(`${extension} is not supported.`);
46
+ }
47
+ return defaultLoaderByExt[extension];
48
+ };
49
+
50
+ /**
51
+ * Find and load rcfile, return { config, filePath }
52
+ * If not found any rcfile, throw an Error.
53
+ * @param {string} pkgName
54
+ * @param {rcConfigLoaderOption} [opts]
55
+ * @returns {{ config: Object, filePath:string } | undefined}
56
+ */
57
+ export function rcFile<R extends {}>(
58
+ pkgName: string,
59
+ opts: rcConfigLoaderOption = {}
60
+ ):
61
+ | {
62
+ config: R;
63
+ filePath: string;
64
+ }
65
+ | undefined {
66
+ // path/to/config or basename of config file.
67
+ const configFileName = opts.configFileName || `.${pkgName}rc`;
68
+ const defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
69
+ const cwd = opts.cwd || defaultOptions.cwd;
70
+ const packageJSON = opts.packageJSON || defaultOptions.packageJSON;
71
+ const packageJSONFieldName = typeof packageJSON === "object" ? packageJSON.fieldName : pkgName;
72
+
73
+ const parts = splitPath(cwd);
74
+ const loadersByOrder = Array.isArray(defaultExtension)
75
+ ? defaultExtension.map(extension => selectLoader(defaultLoaderByExt, extension))
76
+ : selectLoader(defaultLoaderByExt, defaultExtension);
77
+
78
+ const loaderByExt = Object.assign({}, 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;
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
+
215
+ // lazy load YAML to improve performance when not used
216
+ const yaml = require("js-yaml");
217
+
218
+ try {
219
+ // empty YAML file can be null, so always use
220
+ return yaml.safeLoad(readFile(filePath)) || {};
221
+ } catch (error) {
222
+ debug(`Error reading YAML file: ${filePath}`);
223
+ if (!suppress) {
224
+ error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
225
+ throw error;
226
+ }
227
+ }
228
+ }
@@ -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: loader(configLocation),
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
- }