1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.resolveConfig = resolveConfig;
|
7 | exports.loadConfig = loadConfig;
|
8 |
|
9 | var _path = _interopRequireDefault(require("path"));
|
10 |
|
11 | var _clone = _interopRequireDefault(require("clone"));
|
12 |
|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14 |
|
15 | const PARSERS = {
|
16 | json: require('json5').parse,
|
17 | toml: require('@iarna/toml').parse
|
18 | };
|
19 | const existsCache = new Map();
|
20 |
|
21 | async function resolveConfig(fs, filepath, filenames, opts, root = _path.default.parse(filepath).root) {
|
22 | filepath = _path.default.dirname(filepath);
|
23 |
|
24 | if (filepath === root || _path.default.basename(filepath) === 'node_modules') {
|
25 | return null;
|
26 | }
|
27 |
|
28 | for (const filename of filenames) {
|
29 | let file = _path.default.join(filepath, filename);
|
30 |
|
31 | if (await fs.exists(file)) {
|
32 | return file;
|
33 | }
|
34 | }
|
35 |
|
36 | return resolveConfig(fs, filepath, filenames, opts);
|
37 | }
|
38 |
|
39 | async function loadConfig(fs, filepath, filenames, opts) {
|
40 | let configFile = await resolveConfig(fs, filepath, filenames, opts);
|
41 |
|
42 | if (configFile) {
|
43 | try {
|
44 | let extname = _path.default.extname(configFile).slice(1);
|
45 |
|
46 | if (extname === 'js') {
|
47 | return {
|
48 |
|
49 | config: (0, _clone.default)(require(configFile)),
|
50 | files: [{
|
51 | filePath: configFile
|
52 | }]
|
53 | };
|
54 | }
|
55 |
|
56 | let configContent = await fs.readFile(configFile, 'utf8');
|
57 |
|
58 | if (!configContent) {
|
59 | return null;
|
60 | }
|
61 |
|
62 | let config;
|
63 |
|
64 | if (opts && opts.parse === false) {
|
65 | config = configContent;
|
66 | } else {
|
67 | let parse = PARSERS[extname] || PARSERS.json;
|
68 | config = parse(configContent);
|
69 | }
|
70 |
|
71 | return {
|
72 | config: config,
|
73 | files: [{
|
74 | filePath: configFile
|
75 | }]
|
76 | };
|
77 | } catch (err) {
|
78 | if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
|
79 | existsCache.delete(configFile);
|
80 | return null;
|
81 | }
|
82 |
|
83 | throw err;
|
84 | }
|
85 | }
|
86 |
|
87 | return null;
|
88 | } |
\ | No newline at end of file |