rc-config-loader 2.0.5 → 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 +60 -16
- package/lib/rc-config-loader.d.ts +14 -10
- package/lib/rc-config-loader.js +135 -175
- package/lib/rc-config-loader.js.map +1 -1
- package/package.json +51 -56
- package/src/rc-config-loader.ts +228 -0
- package/src/rc-config-loader.d.ts +0 -15
- package/src/rc-config-loader.js +0 -174
package/README.md
CHANGED
|
@@ -26,6 +26,9 @@ 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
|
+
- Built-in TypeScript support
|
|
30
|
+
|
|
31
|
+
If you want to async support and customize loader, recommenced to use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig).
|
|
29
32
|
|
|
30
33
|
## Install
|
|
31
34
|
|
|
@@ -40,9 +43,11 @@ Install with [npm](https://www.npmjs.com/):
|
|
|
40
43
|
```ts
|
|
41
44
|
export interface rcConfigLoaderOption {
|
|
42
45
|
// does look for `package.json`
|
|
43
|
-
packageJSON?:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
packageJSON?:
|
|
47
|
+
| boolean
|
|
48
|
+
| {
|
|
49
|
+
fieldName: string;
|
|
50
|
+
};
|
|
46
51
|
// if config file name is not same with packageName, set the name
|
|
47
52
|
configFileName?: string;
|
|
48
53
|
// treat default(no ext file) as some extension
|
|
@@ -50,26 +55,63 @@ export interface rcConfigLoaderOption {
|
|
|
50
55
|
// where start to load
|
|
51
56
|
cwd?: string;
|
|
52
57
|
}
|
|
53
|
-
|
|
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
|
+
|
|
54
70
|
```
|
|
55
71
|
|
|
56
|
-
`
|
|
72
|
+
`rcFile` return `{ config, filePath }` object.
|
|
57
73
|
|
|
58
74
|
- `config`: it is config object
|
|
59
75
|
- `filePath`: absolute path to config file
|
|
60
76
|
|
|
61
|
-
|
|
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
|
+
```
|
|
62
104
|
|
|
63
105
|
### Example
|
|
64
106
|
|
|
65
107
|
```js
|
|
66
108
|
"use strict";
|
|
67
|
-
|
|
109
|
+
import { rcFile } from "rc-config-loader"
|
|
68
110
|
// load .eslintrc from current dir
|
|
69
|
-
console.log(
|
|
111
|
+
console.log(rcFile("eslint"));
|
|
70
112
|
|
|
71
113
|
// load .eslintrc from specific path
|
|
72
|
-
console.log(
|
|
114
|
+
console.log(rcFile("eslint", {
|
|
73
115
|
configFileName: `${__dirname}/test/fixtures/.eslintrc`
|
|
74
116
|
}));
|
|
75
117
|
/*
|
|
@@ -81,7 +123,7 @@ filePath: ${__dirname}/test/fixtures/.eslintrc
|
|
|
81
123
|
*/
|
|
82
124
|
|
|
83
125
|
// load property from pacakge.json
|
|
84
|
-
console.log(
|
|
126
|
+
console.log(rcFile("rc-config-loader", {
|
|
85
127
|
packageJSON: {
|
|
86
128
|
fieldName: "directories"
|
|
87
129
|
}
|
|
@@ -92,27 +134,30 @@ filePath: /path/to/package.json
|
|
|
92
134
|
*/
|
|
93
135
|
|
|
94
136
|
// load .eslintrc from specific dir
|
|
95
|
-
console.log(
|
|
137
|
+
console.log(rcFile("eslint", {
|
|
96
138
|
cwd: `${__dirname}/test/fixtures`
|
|
97
139
|
}));
|
|
98
140
|
|
|
99
141
|
// load specific filename from current dir
|
|
100
|
-
console.log(
|
|
142
|
+
console.log(rcFile("travis", {configFileName: ".travis"}));
|
|
101
143
|
/*
|
|
102
144
|
config: { sudo: false, language: 'node_js', node_js: 'stable' }
|
|
103
145
|
filePath: /path/to/.travis
|
|
104
146
|
*/
|
|
105
147
|
|
|
106
148
|
// try to load as .json, .yml, js
|
|
107
|
-
console.log(
|
|
149
|
+
console.log(rcFile("bar", {
|
|
108
150
|
configFileName: `${__dirname}/test/fixtures/.barrc`,
|
|
109
151
|
defaultExtension: [".json", ".yml", ".js"]
|
|
110
152
|
}));
|
|
111
153
|
|
|
154
|
+
// try to load as foobar, but .foobarrc is not found
|
|
155
|
+
console.log(rcFile("foorbar")); // => undefined
|
|
156
|
+
|
|
112
157
|
// try to load as .json, but it is not json
|
|
113
|
-
// throw
|
|
158
|
+
// throw SyntaxError
|
|
114
159
|
try {
|
|
115
|
-
|
|
160
|
+
rcFile("unknown", {
|
|
116
161
|
// This is not json
|
|
117
162
|
configFileName: `${__dirname}/test/fixtures/.unknownrc`,
|
|
118
163
|
defaultExtension: ".json"
|
|
@@ -123,7 +168,6 @@ try {
|
|
|
123
168
|
SyntaxError: Cannot read config file: /test/fixtures/.unknownrc
|
|
124
169
|
*/
|
|
125
170
|
}
|
|
126
|
-
|
|
127
171
|
```
|
|
128
172
|
|
|
129
173
|
## Users
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
export interface rcConfigLoaderOption {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
| {
|
|
6
|
-
fieldName: string;
|
|
7
|
-
};
|
|
8
|
-
// if config file name is not same with packageName, set the name
|
|
2
|
+
packageJSON?: boolean | {
|
|
3
|
+
fieldName: string;
|
|
4
|
+
};
|
|
9
5
|
configFileName?: string;
|
|
10
|
-
// treat default(no ext file) as some extension
|
|
11
6
|
defaultExtension?: string | string[];
|
|
12
|
-
// where start to load
|
|
13
7
|
cwd?: string;
|
|
14
8
|
}
|
|
15
|
-
|
|
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;
|
package/lib/rc-config-loader.js
CHANGED
|
@@ -1,216 +1,176 @@
|
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
7
|
-
|
|
8
|
-
var path = require("path");
|
|
9
|
-
|
|
9
|
+
var path_1 = __importDefault(require("path"));
|
|
10
|
+
var fs_1 = __importDefault(require("fs"));
|
|
10
11
|
var debug = require("debug")("rc-config-loader");
|
|
11
|
-
|
|
12
12
|
var requireFromString = require("require-from-string");
|
|
13
|
-
|
|
14
13
|
var JSON5 = require("json5");
|
|
15
|
-
|
|
16
|
-
var fs = require("fs");
|
|
17
|
-
|
|
18
|
-
var pathExists = require("path-exists");
|
|
19
|
-
|
|
20
|
-
var objectAssign = require("object-assign");
|
|
21
|
-
|
|
22
|
-
var keys = require("object-keys");
|
|
23
|
-
|
|
24
14
|
var defaultLoaderByExt = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
".js": loadJSConfigFile,
|
|
16
|
+
".json": loadJSONConfigFile,
|
|
17
|
+
".yaml": loadYAMLConfigFile,
|
|
18
|
+
".yml": loadYAMLConfigFile
|
|
29
19
|
};
|
|
30
20
|
var defaultOptions = {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
21
|
+
// does look for `package.json`
|
|
22
|
+
packageJSON: false,
|
|
23
|
+
// treat default(no ext file) as some extension
|
|
24
|
+
defaultExtension: [".json", ".yaml", ".yml", ".js"],
|
|
25
|
+
cwd: process.cwd()
|
|
26
|
+
};
|
|
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];
|
|
36
32
|
};
|
|
37
33
|
/**
|
|
34
|
+
* Find and load rcfile, return { config, filePath }
|
|
35
|
+
* If not found any rcfile, throw an Error.
|
|
38
36
|
* @param {string} pkgName
|
|
39
37
|
* @param {rcConfigLoaderOption} [opts]
|
|
40
38
|
* @returns {{ config: Object, filePath:string } | undefined}
|
|
41
39
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
40
|
+
function rcFile(pkgName, opts) {
|
|
41
|
+
if (opts === void 0) { opts = {}; }
|
|
42
|
+
// path/to/config or basename of config file.
|
|
43
|
+
var configFileName = opts.configFileName || "." + pkgName + "rc";
|
|
44
|
+
var defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
|
|
45
|
+
var cwd = opts.cwd || defaultOptions.cwd;
|
|
46
|
+
var packageJSON = opts.packageJSON || defaultOptions.packageJSON;
|
|
47
|
+
var packageJSONFieldName = typeof packageJSON === "object" ? packageJSON.fieldName : pkgName;
|
|
48
|
+
var parts = splitPath(cwd);
|
|
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
|
|
54
|
+
});
|
|
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;
|
|
66
65
|
/**
|
|
67
66
|
*
|
|
68
|
-
* @param {string[]} parts
|
|
69
|
-
* @param {Object} loaderByExt
|
|
70
|
-
* @param {string} configFileName
|
|
71
|
-
* @param {boolean|Object} packageJSON
|
|
72
|
-
* @param {string} packageJSONFieldName
|
|
73
67
|
* @returns {{
|
|
74
68
|
* config: string,
|
|
75
69
|
* filePath: string
|
|
76
|
-
* }
|
|
70
|
+
* }}
|
|
77
71
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
|
78
|
+
var configLocation = join(parts, configFileName + ext);
|
|
79
|
+
if (!fs_1.default.existsSync(configLocation)) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
// if ext === ""(empty string):, use ordered loaders
|
|
83
|
+
var loaders = ext ? loaderByExt[ext] : loadersByOrder;
|
|
84
|
+
if (!Array.isArray(loaders)) {
|
|
85
|
+
var loader = loaders;
|
|
86
|
+
var result = loader(configLocation, false);
|
|
87
|
+
if (!result) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
config: result,
|
|
92
|
+
filePath: configLocation
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
for (var i = 0; i < loaders.length; i++) {
|
|
96
|
+
var loader = loaders[i];
|
|
97
|
+
var result = loader(configLocation, true);
|
|
98
|
+
if (!result) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
config: result,
|
|
103
|
+
filePath: configLocation
|
|
104
|
+
};
|
|
105
|
+
}
|
|
94
106
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
config: result,
|
|
108
|
-
filePath: configLocation
|
|
109
|
-
};
|
|
107
|
+
if (packageJSON) {
|
|
108
|
+
var pkgJSONLoc = join(parts, "package.json");
|
|
109
|
+
if (fs_1.default.existsSync(pkgJSONLoc)) {
|
|
110
|
+
var pkgJSON = require(pkgJSONLoc);
|
|
111
|
+
if (pkgJSON[packageJSONFieldName]) {
|
|
112
|
+
return {
|
|
113
|
+
config: pkgJSON[packageJSONFieldName],
|
|
114
|
+
filePath: pkgJSONLoc
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
110
118
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
var _loader = loaders[i];
|
|
114
|
-
|
|
115
|
-
var _result = _loader(configLocation, true);
|
|
116
|
-
|
|
117
|
-
if (!_result) {
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return {
|
|
122
|
-
config: _result,
|
|
123
|
-
filePath: configLocation
|
|
124
|
-
};
|
|
119
|
+
if (parts.pop()) {
|
|
120
|
+
return findConfig({ parts: parts, loaderByExt: loaderByExt, loadersByOrder: loadersByOrder, configFileName: configFileName, packageJSON: packageJSON, packageJSONFieldName: packageJSONFieldName });
|
|
125
121
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (packageJSON) {
|
|
129
|
-
var pkgJSONLoc = join(parts, "package.json");
|
|
130
|
-
|
|
131
|
-
if (pathExists.sync(pkgJSONLoc)) {
|
|
132
|
-
var pkgJSON = require(pkgJSONLoc);
|
|
133
|
-
|
|
134
|
-
if (pkgJSON[packageJSONFieldName]) {
|
|
135
|
-
return {
|
|
136
|
-
config: pkgJSON[packageJSONFieldName],
|
|
137
|
-
filePath: pkgJSONLoc
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (parts.pop()) {
|
|
144
|
-
return findConfig({
|
|
145
|
-
parts: parts,
|
|
146
|
-
loaderByExt: loaderByExt,
|
|
147
|
-
configFileName: configFileName,
|
|
148
|
-
packageJSON: packageJSON,
|
|
149
|
-
packageJSONFieldName: packageJSONFieldName
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return undefined;
|
|
122
|
+
return;
|
|
154
123
|
}
|
|
155
|
-
|
|
156
124
|
function splitPath(x) {
|
|
157
|
-
|
|
125
|
+
return path_1.default.resolve(x || "").split(path_1.default.sep);
|
|
158
126
|
}
|
|
159
|
-
|
|
160
127
|
function join(parts, filename) {
|
|
161
|
-
|
|
128
|
+
return path_1.default.resolve(parts.join(path_1.default.sep) + path_1.default.sep, filename);
|
|
162
129
|
}
|
|
163
|
-
|
|
164
130
|
function loadJSConfigFile(filePath, suppress) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
131
|
+
debug("Loading JavaScript config file: " + filePath);
|
|
132
|
+
try {
|
|
133
|
+
var content = fs_1.default.readFileSync(filePath, "utf-8");
|
|
134
|
+
return requireFromString(content, filePath);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
debug("Error reading JavaScript file: " + filePath);
|
|
138
|
+
if (!suppress) {
|
|
139
|
+
error.message = "Cannot read config file: " + filePath + "\nError: " + error.message;
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
176
142
|
}
|
|
177
|
-
}
|
|
178
143
|
}
|
|
179
|
-
|
|
180
144
|
function loadJSONConfigFile(filePath, suppress) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
145
|
+
debug("Loading JSON config file: " + filePath);
|
|
146
|
+
try {
|
|
147
|
+
return JSON5.parse(readFile(filePath));
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
debug("Error reading JSON file: " + filePath);
|
|
151
|
+
if (!suppress) {
|
|
152
|
+
error.message = "Cannot read config file: " + filePath + "\nError: " + error.message;
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
191
155
|
}
|
|
192
|
-
}
|
|
193
156
|
}
|
|
194
|
-
|
|
195
157
|
function readFile(filePath) {
|
|
196
|
-
|
|
158
|
+
return fs_1.default.readFileSync(filePath, "utf8");
|
|
197
159
|
}
|
|
198
|
-
|
|
199
160
|
function loadYAMLConfigFile(filePath, suppress) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
161
|
+
debug("Loading YAML config file: " + filePath);
|
|
162
|
+
// lazy load YAML to improve performance when not used
|
|
163
|
+
var yaml = require("js-yaml");
|
|
164
|
+
try {
|
|
165
|
+
// empty YAML file can be null, so always use
|
|
166
|
+
return yaml.safeLoad(readFile(filePath)) || {};
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
debug("Error reading YAML file: " + filePath);
|
|
170
|
+
if (!suppress) {
|
|
171
|
+
error.message = "Cannot read config file: " + filePath + "\nError: " + error.message;
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
213
174
|
}
|
|
214
|
-
}
|
|
215
175
|
}
|
|
216
176
|
//# sourceMappingURL=rc-config-loader.js.map
|
|
@@ -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,IAAI,GAAGC,OAAO,CAAC,MAAD,CAApB;;AACA,IAAMC,KAAK,GAAGD,OAAO,CAAC,OAAD,CAAP,CAAiB,kBAAjB,CAAd;;AACA,IAAME,iBAAiB,GAAGF,OAAO,CAAC,qBAAD,CAAjC;;AACA,IAAMG,KAAK,GAAGH,OAAO,CAAC,OAAD,CAArB;;AACA,IAAMI,EAAE,GAAGJ,OAAO,CAAC,IAAD,CAAlB;;AACA,IAAMK,UAAU,GAAGL,OAAO,CAAC,aAAD,CAA1B;;AACA,IAAMM,YAAY,GAAGN,OAAO,CAAC,eAAD,CAA5B;;AACA,IAAMO,IAAI,GAAGP,OAAO,CAAC,aAAD,CAApB;;AAEA,IAAMQ,kBAAkB,GAAG;AACvB,SAAOC,gBADgB;AAEvB,WAASC,kBAFc;AAGvB,WAASC,kBAHc;AAIvB,UAAQA;AAJe,CAA3B;AAOA,IAAMC,cAAc,GAAG;AACnB;AACAC,EAAAA,WAAW,EAAE,KAFM;AAGnB;AACAC,EAAAA,gBAAgB,EAAE,CAAC,OAAD,EAAU,OAAV,EAAmB,MAAnB,EAA2B,KAA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","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"],"file":"rc-config-loader.js"}
|
|
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,qBAAqB,CAAC,CAAC;AACzD,IAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAE/B,IAAM,kBAAkB,GAAG;IACvB,KAAK,EAAE,gBAAgB;IACvB,OAAO,EAAE,kBAAkB;IAC3B,OAAO,EAAE,kBAAkB;IAC3B,MAAM,EAAE,kBAAkB;CAC7B,CAAC;AAEF,IAAM,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,IAAM,YAAY,GAAG,UAAC,kBAA+C,EAAE,SAAiB;IACpF,IAAI,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE;QACxD,MAAM,IAAI,KAAK,CAAI,SAAS,uBAAoB,CAAC,CAAC;KACrD;IACD,OAAO,kBAAkB,CAAC,SAAS,CAAC,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
|
|
7
|
-
"files": [
|
|
8
|
-
"bin/",
|
|
9
|
-
"lib/",
|
|
10
|
-
"src/"
|
|
11
|
-
],
|
|
12
2
|
"name": "rc-config-loader",
|
|
13
|
-
"version": "
|
|
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
|
-
},
|
|
23
5
|
"keywords": [
|
|
24
|
-
"rc",
|
|
25
6
|
"config",
|
|
26
7
|
"configuration",
|
|
27
|
-
"loader",
|
|
28
8
|
"json",
|
|
9
|
+
"loader",
|
|
10
|
+
"rc",
|
|
29
11
|
"yaml",
|
|
30
12
|
"yml"
|
|
31
13
|
],
|
|
14
|
+
"homepage": "https://github.com/azu/rc-config-loader",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/azu/rc-config-loader/issues"
|
|
17
|
+
},
|
|
32
18
|
"repository": {
|
|
33
19
|
"type": "git",
|
|
34
20
|
"url": "https://github.com/azu/rc-config-loader.git"
|
|
35
21
|
},
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
"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"
|
|
48
33
|
},
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"babel-preset-power-assert": "^3.0.0",
|
|
56
|
-
"chai": "^4.2.0",
|
|
57
|
-
"cross-env": "^5.2.0",
|
|
58
|
-
"eslint": "^6.0.1",
|
|
59
|
-
"husky": "^3.0.0",
|
|
60
|
-
"lint-staged": "^9.1.0",
|
|
61
|
-
"mocha": "^6.1.4",
|
|
62
|
-
"power-assert": "^1.4.2",
|
|
63
|
-
"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"
|
|
64
40
|
},
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
|
|
41
|
+
"husky": {
|
|
42
|
+
"hooks": {
|
|
43
|
+
"post-commit": "git reset",
|
|
44
|
+
"pre-commit": "lint-staged"
|
|
45
|
+
}
|
|
68
46
|
},
|
|
69
47
|
"lint-staged": {
|
|
70
48
|
"*.{js,jsx,ts,tsx,css}": [
|
|
@@ -72,10 +50,27 @@
|
|
|
72
50
|
"git add"
|
|
73
51
|
]
|
|
74
52
|
},
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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"
|
|
80
75
|
}
|
|
81
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,15 +0,0 @@
|
|
|
1
|
-
export interface rcConfigLoaderOption {
|
|
2
|
-
// does look for `package.json`
|
|
3
|
-
packageJSON?:
|
|
4
|
-
| boolean
|
|
5
|
-
| {
|
|
6
|
-
fieldName: string;
|
|
7
|
-
};
|
|
8
|
-
// if config file name is not same with packageName, set the name
|
|
9
|
-
configFileName?: string;
|
|
10
|
-
// treat default(no ext file) as some extension
|
|
11
|
-
defaultExtension?: string | string[];
|
|
12
|
-
// where start to load
|
|
13
|
-
cwd?: string;
|
|
14
|
-
}
|
|
15
|
-
export default function rcConfigLoader(packageName: string, options?: rcConfigLoaderOption): Object;
|
package/src/rc-config-loader.js
DELETED
|
@@ -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
|
-
}
|