rc-config-loader 2.0.5 → 4.1.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 +72 -19
- package/lib/rc-config-loader.d.ts +14 -10
- package/lib/rc-config-loader.js +138 -180
- package/lib/rc-config-loader.js.map +1 -1
- package/package.json +52 -57
- package/src/rc-config-loader.ts +227 -0
- package/src/rc-config-loader.d.ts +0 -15
- package/src/rc-config-loader.js +0 -174
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# rc-config-loader [](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,9 +44,11 @@ Install with [npm](https://www.npmjs.com/):
|
|
|
40
44
|
```ts
|
|
41
45
|
export interface rcConfigLoaderOption {
|
|
42
46
|
// does look for `package.json`
|
|
43
|
-
packageJSON?:
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
packageJSON?:
|
|
48
|
+
| boolean
|
|
49
|
+
| {
|
|
50
|
+
fieldName: string;
|
|
51
|
+
};
|
|
46
52
|
// if config file name is not same with packageName, set the name
|
|
47
53
|
configFileName?: string;
|
|
48
54
|
// treat default(no ext file) as some extension
|
|
@@ -50,26 +56,71 @@ export interface rcConfigLoaderOption {
|
|
|
50
56
|
// where start to load
|
|
51
57
|
cwd?: string;
|
|
52
58
|
}
|
|
53
|
-
|
|
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
|
+
|
|
54
71
|
```
|
|
55
72
|
|
|
56
|
-
`
|
|
73
|
+
`rcFile` return `{ config, filePath }` object.
|
|
57
74
|
|
|
58
75
|
- `config`: it is config object
|
|
59
76
|
- `filePath`: absolute path to config file
|
|
60
77
|
|
|
61
|
-
|
|
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
|
+
```
|
|
105
|
+
|
|
106
|
+
It will check these files and return config file if found it.
|
|
107
|
+
|
|
108
|
+
- `.your-applicationrc.json`
|
|
109
|
+
- `.your-applicationrc.yml`
|
|
110
|
+
- `.your-applicationrc.yaml`
|
|
111
|
+
- `.your-applicationrc.js`
|
|
112
|
+
- [optional] `package.json`
|
|
113
|
+
- if `packageJSON` option is enabled
|
|
62
114
|
|
|
63
115
|
### Example
|
|
64
116
|
|
|
65
117
|
```js
|
|
66
|
-
|
|
67
|
-
const rcfile = require("rc-config-loader");
|
|
118
|
+
import { rcFile } from "rc-config-loader"
|
|
68
119
|
// load .eslintrc from current dir
|
|
69
|
-
console.log(
|
|
120
|
+
console.log(rcFile("eslint"));
|
|
70
121
|
|
|
71
122
|
// load .eslintrc from specific path
|
|
72
|
-
console.log(
|
|
123
|
+
console.log(rcFile("eslint", {
|
|
73
124
|
configFileName: `${__dirname}/test/fixtures/.eslintrc`
|
|
74
125
|
}));
|
|
75
126
|
/*
|
|
@@ -80,8 +131,8 @@ config: { extends: 'standard',
|
|
|
80
131
|
filePath: ${__dirname}/test/fixtures/.eslintrc
|
|
81
132
|
*/
|
|
82
133
|
|
|
83
|
-
// load property from
|
|
84
|
-
console.log(
|
|
134
|
+
// load property from package.json
|
|
135
|
+
console.log(rcFile("rc-config-loader", {
|
|
85
136
|
packageJSON: {
|
|
86
137
|
fieldName: "directories"
|
|
87
138
|
}
|
|
@@ -92,27 +143,30 @@ filePath: /path/to/package.json
|
|
|
92
143
|
*/
|
|
93
144
|
|
|
94
145
|
// load .eslintrc from specific dir
|
|
95
|
-
console.log(
|
|
146
|
+
console.log(rcFile("eslint", {
|
|
96
147
|
cwd: `${__dirname}/test/fixtures`
|
|
97
148
|
}));
|
|
98
149
|
|
|
99
150
|
// load specific filename from current dir
|
|
100
|
-
console.log(
|
|
151
|
+
console.log(rcFile("travis", {configFileName: ".travis"}));
|
|
101
152
|
/*
|
|
102
153
|
config: { sudo: false, language: 'node_js', node_js: 'stable' }
|
|
103
154
|
filePath: /path/to/.travis
|
|
104
155
|
*/
|
|
105
156
|
|
|
106
157
|
// try to load as .json, .yml, js
|
|
107
|
-
console.log(
|
|
158
|
+
console.log(rcFile("bar", {
|
|
108
159
|
configFileName: `${__dirname}/test/fixtures/.barrc`,
|
|
109
160
|
defaultExtension: [".json", ".yml", ".js"]
|
|
110
161
|
}));
|
|
111
162
|
|
|
163
|
+
// try to load as foobar, but .foobarrc is not found
|
|
164
|
+
console.log(rcFile("foorbar")); // => undefined
|
|
165
|
+
|
|
112
166
|
// try to load as .json, but it is not json
|
|
113
|
-
// throw
|
|
167
|
+
// throw SyntaxError
|
|
114
168
|
try {
|
|
115
|
-
|
|
169
|
+
rcFile("unknown", {
|
|
116
170
|
// This is not json
|
|
117
171
|
configFileName: `${__dirname}/test/fixtures/.unknownrc`,
|
|
118
172
|
defaultExtension: ".json"
|
|
@@ -123,7 +177,6 @@ try {
|
|
|
123
177
|
SyntaxError: Cannot read config file: /test/fixtures/.unknownrc
|
|
124
178
|
*/
|
|
125
179
|
}
|
|
126
|
-
|
|
127
180
|
```
|
|
128
181
|
|
|
129
182
|
## 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,174 @@
|
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
var defaultLoaderByExt = {
|
|
25
|
-
".js": loadJSConfigFile,
|
|
26
|
-
".json": loadJSONConfigFile,
|
|
27
|
-
".yaml": loadYAMLConfigFile,
|
|
28
|
-
".yml": loadYAMLConfigFile
|
|
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 = {
|
|
16
|
+
".cjs": loadJSConfigFile,
|
|
17
|
+
".js": loadJSConfigFile,
|
|
18
|
+
".json": loadJSONConfigFile,
|
|
19
|
+
".yaml": loadYAMLConfigFile,
|
|
20
|
+
".yml": loadYAMLConfigFile,
|
|
29
21
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
22
|
+
const defaultOptions = {
|
|
23
|
+
// does look for `package.json`
|
|
24
|
+
packageJSON: false,
|
|
25
|
+
// treat default(no ext file) as some extension
|
|
26
|
+
defaultExtension: [".json", ".yaml", ".yml", ".js", ".cjs"],
|
|
27
|
+
cwd: process.cwd(),
|
|
28
|
+
};
|
|
29
|
+
const selectLoader = (defaultLoaderByExt, extension) => {
|
|
30
|
+
if (!defaultOptions.defaultExtension.includes(extension)) {
|
|
31
|
+
throw new Error(`${extension} is not supported.`);
|
|
32
|
+
}
|
|
33
|
+
return defaultLoaderByExt[extension];
|
|
36
34
|
};
|
|
37
35
|
/**
|
|
36
|
+
* Find and load rcfile, return { config, filePath }
|
|
37
|
+
* If not found any rcfile, throw an Error.
|
|
38
38
|
* @param {string} pkgName
|
|
39
39
|
* @param {rcConfigLoaderOption} [opts]
|
|
40
40
|
* @returns {{ config: Object, filePath:string } | undefined}
|
|
41
41
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
});
|
|
65
|
-
};
|
|
42
|
+
function rcFile(pkgName, opts = {}) {
|
|
43
|
+
// path/to/config or basename of config file.
|
|
44
|
+
const configFileName = opts.configFileName || `.${pkgName}rc`;
|
|
45
|
+
const defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
|
|
46
|
+
const cwd = opts.cwd || defaultOptions.cwd;
|
|
47
|
+
const packageJSON = opts.packageJSON || defaultOptions.packageJSON;
|
|
48
|
+
const packageJSONFieldName = typeof packageJSON === "object" ? packageJSON.fieldName : pkgName;
|
|
49
|
+
const parts = splitPath(cwd);
|
|
50
|
+
const loadersByOrder = Array.isArray(defaultExtension)
|
|
51
|
+
? defaultExtension.map((extension) => selectLoader(defaultLoaderByExt, extension))
|
|
52
|
+
: selectLoader(defaultLoaderByExt, defaultExtension);
|
|
53
|
+
const loaderByExt = Object.assign(Object.assign({}, defaultLoaderByExt), { "": loadersByOrder });
|
|
54
|
+
return findConfig({
|
|
55
|
+
parts,
|
|
56
|
+
loaderByExt,
|
|
57
|
+
loadersByOrder,
|
|
58
|
+
configFileName,
|
|
59
|
+
packageJSON,
|
|
60
|
+
packageJSONFieldName,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
exports.rcFile = rcFile;
|
|
66
64
|
/**
|
|
67
65
|
*
|
|
68
|
-
* @param {string[]} parts
|
|
69
|
-
* @param {Object} loaderByExt
|
|
70
|
-
* @param {string} configFileName
|
|
71
|
-
* @param {boolean|Object} packageJSON
|
|
72
|
-
* @param {string} packageJSONFieldName
|
|
73
66
|
* @returns {{
|
|
74
67
|
* config: string,
|
|
75
68
|
* filePath: string
|
|
76
|
-
* }
|
|
69
|
+
* }}
|
|
77
70
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
71
|
+
function findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName, }) {
|
|
72
|
+
const extensions = Object.keys(loaderByExt);
|
|
73
|
+
while (extensions.length) {
|
|
74
|
+
const ext = extensions.shift();
|
|
75
|
+
// may be ext is "". if it .<product>rc
|
|
76
|
+
const configLocation = join(parts, configFileName + ext);
|
|
77
|
+
if (!fs_1.default.existsSync(configLocation)) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
// if ext === ""(empty string):, use ordered loaders
|
|
81
|
+
const loaders = ext ? loaderByExt[ext] : loadersByOrder;
|
|
82
|
+
if (!Array.isArray(loaders)) {
|
|
83
|
+
const loader = loaders;
|
|
84
|
+
const result = loader(configLocation, false);
|
|
85
|
+
if (!result) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
config: result,
|
|
90
|
+
filePath: configLocation,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
for (let i = 0; i < loaders.length; i++) {
|
|
94
|
+
const loader = loaders[i];
|
|
95
|
+
const result = loader(configLocation, true);
|
|
96
|
+
if (!result) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
config: result,
|
|
101
|
+
filePath: configLocation,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
94
104
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
config: result,
|
|
108
|
-
filePath: configLocation
|
|
109
|
-
};
|
|
105
|
+
if (packageJSON) {
|
|
106
|
+
const pkgJSONLoc = join(parts, "package.json");
|
|
107
|
+
if (fs_1.default.existsSync(pkgJSONLoc)) {
|
|
108
|
+
const pkgJSON = require(pkgJSONLoc);
|
|
109
|
+
if (pkgJSON[packageJSONFieldName]) {
|
|
110
|
+
return {
|
|
111
|
+
config: pkgJSON[packageJSONFieldName],
|
|
112
|
+
filePath: pkgJSONLoc,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
110
116
|
}
|
|
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
|
-
};
|
|
117
|
+
if (parts.pop()) {
|
|
118
|
+
return findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName });
|
|
125
119
|
}
|
|
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;
|
|
120
|
+
return;
|
|
154
121
|
}
|
|
155
|
-
|
|
156
122
|
function splitPath(x) {
|
|
157
|
-
|
|
123
|
+
return path_1.default.resolve(x || "").split(path_1.default.sep);
|
|
158
124
|
}
|
|
159
|
-
|
|
160
125
|
function join(parts, filename) {
|
|
161
|
-
|
|
126
|
+
return path_1.default.resolve(parts.join(path_1.default.sep) + path_1.default.sep, filename);
|
|
162
127
|
}
|
|
163
|
-
|
|
164
128
|
function loadJSConfigFile(filePath, suppress) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
129
|
+
debug(`Loading JavaScript config file: ${filePath}`);
|
|
130
|
+
try {
|
|
131
|
+
const content = fs_1.default.readFileSync(filePath, "utf-8");
|
|
132
|
+
return require_from_string_1.default(content, filePath);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
debug(`Error reading JavaScript file: ${filePath}`);
|
|
136
|
+
if (!suppress) {
|
|
137
|
+
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
176
140
|
}
|
|
177
|
-
}
|
|
178
141
|
}
|
|
179
|
-
|
|
180
142
|
function loadJSONConfigFile(filePath, suppress) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
143
|
+
debug(`Loading JSON config file: ${filePath}`);
|
|
144
|
+
try {
|
|
145
|
+
return json5_1.default.parse(readFile(filePath));
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
debug(`Error reading JSON file: ${filePath}`);
|
|
149
|
+
if (!suppress) {
|
|
150
|
+
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
191
153
|
}
|
|
192
|
-
}
|
|
193
154
|
}
|
|
194
|
-
|
|
195
155
|
function readFile(filePath) {
|
|
196
|
-
|
|
156
|
+
return fs_1.default.readFileSync(filePath, "utf8");
|
|
197
157
|
}
|
|
198
|
-
|
|
199
158
|
function loadYAMLConfigFile(filePath, suppress) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
159
|
+
debug(`Loading YAML config file: ${filePath}`);
|
|
160
|
+
// lazy load YAML to improve performance when not used
|
|
161
|
+
const yaml = require("js-yaml");
|
|
162
|
+
try {
|
|
163
|
+
// empty YAML file can be null, so always use
|
|
164
|
+
return yaml.load(readFile(filePath)) || {};
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
debug(`Error reading YAML file: ${filePath}`);
|
|
168
|
+
if (!suppress) {
|
|
169
|
+
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
213
172
|
}
|
|
214
|
-
}
|
|
215
173
|
}
|
|
216
174
|
//# 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,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;;AACA,MAAMkB,IAAI,GAAG9D,OAAO,CAAC,SAAD,CAApB;;AAEA,MAAI;AACA;AACA,WAAO8D,IAAI,CAACC,QAAL,CAAcF,QAAQ,CAACjB,QAAD,CAAtB,KAAqC,EAA5C;AACH,GAHD,CAGE,OAAOc,CAAP,EAAU;AACRzD,IAAAA,KAAK,oCAA6B2C,QAA7B,EAAL;;AACA,QAAI,CAACW,QAAL,EAAe;AACXG,MAAAA,CAAC,CAACC,OAAF,sCAAwCf,QAAxC,sBAA4Dc,CAAC,CAACC,OAA9D;AACA,YAAMD,CAAN;AACH;AACJ;AACJ","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,gDAAwB;AACxB,4CAAoB;AACpB,8EAAoD;AACpD,kDAA0B;AAE1B,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,kBAAkB,CAAC,CAAC;AACnD,MAAM,kBAAkB,GAAG;IACvB,MAAM,EAAE,gBAAgB;IACxB,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,EAAE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AAC;YAChF,MAAM,KAAK,CAAC;SACf;KACJ;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,81 +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": "
|
|
3
|
+
"version": "4.1.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
|
-
|
|
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"
|
|
38
33
|
},
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"object-keys": "^1.0.12",
|
|
46
|
-
"path-exists": "^3.0.0",
|
|
47
|
-
"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"
|
|
48
40
|
},
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
|
|
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"
|
|
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
|
+
]
|
|
64
51
|
},
|
|
65
52
|
"prettier": {
|
|
66
53
|
"printWidth": 120,
|
|
67
54
|
"tabWidth": 4
|
|
68
55
|
},
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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"
|
|
74
61
|
},
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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"
|
|
80
75
|
}
|
|
81
76
|
}
|
|
@@ -0,0 +1,227 @@
|
|
|
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
|
+
".cjs": loadJSConfigFile,
|
|
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", ".cjs"],
|
|
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 (!defaultOptions.defaultExtension.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 = {
|
|
79
|
+
...defaultLoaderByExt,
|
|
80
|
+
"": loadersByOrder,
|
|
81
|
+
};
|
|
82
|
+
return findConfig<R>({
|
|
83
|
+
parts,
|
|
84
|
+
loaderByExt,
|
|
85
|
+
loadersByOrder,
|
|
86
|
+
configFileName,
|
|
87
|
+
packageJSON,
|
|
88
|
+
packageJSONFieldName,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
*
|
|
94
|
+
* @returns {{
|
|
95
|
+
* config: string,
|
|
96
|
+
* filePath: string
|
|
97
|
+
* }}
|
|
98
|
+
*/
|
|
99
|
+
function findConfig<R extends {}>({
|
|
100
|
+
parts,
|
|
101
|
+
loaderByExt,
|
|
102
|
+
loadersByOrder,
|
|
103
|
+
configFileName,
|
|
104
|
+
packageJSON,
|
|
105
|
+
packageJSONFieldName,
|
|
106
|
+
}: {
|
|
107
|
+
parts: string[];
|
|
108
|
+
loaderByExt: {
|
|
109
|
+
[index: string]: Loader | Loader[];
|
|
110
|
+
};
|
|
111
|
+
loadersByOrder: Loader | Loader[];
|
|
112
|
+
configFileName: string;
|
|
113
|
+
packageJSON: boolean | { fieldName: string };
|
|
114
|
+
packageJSONFieldName: string;
|
|
115
|
+
}):
|
|
116
|
+
| {
|
|
117
|
+
config: R;
|
|
118
|
+
filePath: string;
|
|
119
|
+
}
|
|
120
|
+
| undefined {
|
|
121
|
+
const extensions = Object.keys(loaderByExt);
|
|
122
|
+
while (extensions.length) {
|
|
123
|
+
const ext = extensions.shift();
|
|
124
|
+
// may be ext is "". if it .<product>rc
|
|
125
|
+
const configLocation = join(parts, configFileName + ext);
|
|
126
|
+
if (!fs.existsSync(configLocation)) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
// if ext === ""(empty string):, use ordered loaders
|
|
130
|
+
const loaders = ext ? loaderByExt[ext] : loadersByOrder;
|
|
131
|
+
if (!Array.isArray(loaders)) {
|
|
132
|
+
const loader = loaders;
|
|
133
|
+
const result = loader<R>(configLocation, false);
|
|
134
|
+
if (!result) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
config: result,
|
|
139
|
+
filePath: configLocation,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
for (let i = 0; i < loaders.length; i++) {
|
|
143
|
+
const loader = loaders[i];
|
|
144
|
+
const result = loader<R>(configLocation, true);
|
|
145
|
+
if (!result) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
config: result,
|
|
150
|
+
filePath: configLocation,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (packageJSON) {
|
|
156
|
+
const pkgJSONLoc = join(parts, "package.json");
|
|
157
|
+
if (fs.existsSync(pkgJSONLoc)) {
|
|
158
|
+
const pkgJSON = require(pkgJSONLoc);
|
|
159
|
+
if (pkgJSON[packageJSONFieldName]) {
|
|
160
|
+
return {
|
|
161
|
+
config: pkgJSON[packageJSONFieldName],
|
|
162
|
+
filePath: pkgJSONLoc,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (parts.pop()) {
|
|
168
|
+
return findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName });
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function splitPath(x: string): string[] {
|
|
174
|
+
return path.resolve(x || "").split(path.sep);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function join(parts: string[], filename: string) {
|
|
178
|
+
return path.resolve(parts.join(path.sep) + path.sep, filename);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function loadJSConfigFile(filePath: string, suppress: boolean) {
|
|
182
|
+
debug(`Loading JavaScript config file: ${filePath}`);
|
|
183
|
+
try {
|
|
184
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
185
|
+
return requireFromString(content, filePath);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
debug(`Error reading JavaScript file: ${filePath}`);
|
|
188
|
+
if (!suppress) {
|
|
189
|
+
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function loadJSONConfigFile(filePath: string, suppress: boolean) {
|
|
196
|
+
debug(`Loading JSON config file: ${filePath}`);
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
return JSON5.parse(readFile(filePath));
|
|
200
|
+
} catch (error) {
|
|
201
|
+
debug(`Error reading JSON file: ${filePath}`);
|
|
202
|
+
if (!suppress) {
|
|
203
|
+
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function readFile(filePath: string) {
|
|
210
|
+
return fs.readFileSync(filePath, "utf8");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function loadYAMLConfigFile(filePath: string, suppress: boolean) {
|
|
214
|
+
debug(`Loading YAML config file: ${filePath}`);
|
|
215
|
+
// lazy load YAML to improve performance when not used
|
|
216
|
+
const yaml = require("js-yaml");
|
|
217
|
+
try {
|
|
218
|
+
// empty YAML file can be null, so always use
|
|
219
|
+
return yaml.load(readFile(filePath)) || {};
|
|
220
|
+
} catch (error) {
|
|
221
|
+
debug(`Error reading YAML file: ${filePath}`);
|
|
222
|
+
if (!suppress) {
|
|
223
|
+
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -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
|
-
}
|