UNPKG

4.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.loadEnv = exports.loadParentConfig = exports.getConfig = exports.loadConfig = exports.orIfFileNotExist = exports.orNullIfFileNotExist = exports.findAndReadConfig = void 0;
4const fs_1 = require("fs");
5const js_yaml_1 = require("js-yaml");
6const path = require("path");
7const dotenv_1 = require("dotenv");
8const config_file_ts_1 = require("config-file-ts");
9async function readConfig(configFile, request) {
10 const data = await fs_1.promises.readFile(configFile, "utf8");
11 let result;
12 if (configFile.endsWith(".json5") || configFile.endsWith(".json")) {
13 result = require("json5").parse(data);
14 }
15 else if (configFile.endsWith(".js") || configFile.endsWith(".cjs")) {
16 result = require(configFile);
17 if (result.default != null) {
18 result = result.default;
19 }
20 if (typeof result === "function") {
21 result = result(request);
22 }
23 result = await Promise.resolve(result);
24 }
25 else if (configFile.endsWith(".ts")) {
26 result = (0, config_file_ts_1.loadTsConfig)(configFile);
27 if (typeof result === "function") {
28 result = result(request);
29 }
30 result = await Promise.resolve(result);
31 }
32 else if (configFile.endsWith(".toml")) {
33 result = require("toml").parse(data);
34 }
35 else {
36 result = (0, js_yaml_1.load)(data);
37 }
38 return { result, configFile };
39}
40async function findAndReadConfig(request) {
41 const prefix = request.configFilename;
42 for (const configFile of [`${prefix}.yml`, `${prefix}.yaml`, `${prefix}.json`, `${prefix}.json5`, `${prefix}.toml`, `${prefix}.js`, `${prefix}.cjs`, `${prefix}.ts`]) {
43 const data = await orNullIfFileNotExist(readConfig(path.join(request.projectDir, configFile), request));
44 if (data != null) {
45 return data;
46 }
47 }
48 return null;
49}
50exports.findAndReadConfig = findAndReadConfig;
51function orNullIfFileNotExist(promise) {
52 return orIfFileNotExist(promise, null);
53}
54exports.orNullIfFileNotExist = orNullIfFileNotExist;
55function orIfFileNotExist(promise, fallbackValue) {
56 return promise
57 .catch(e => {
58 if (e.code === "ENOENT" || e.code === "ENOTDIR") {
59 return fallbackValue;
60 }
61 throw e;
62 });
63}
64exports.orIfFileNotExist = orIfFileNotExist;
65async function loadConfig(request) {
66 let packageMetadata = request.packageMetadata == null ? null : await request.packageMetadata.value;
67 if (packageMetadata == null) {
68 const json = await orNullIfFileNotExist(fs_1.promises.readFile(path.join(request.projectDir, "package.json"), "utf8"));
69 packageMetadata = json == null ? null : JSON.parse(json);
70 }
71 const data = packageMetadata == null ? null : packageMetadata[request.packageKey];
72 return data == null ? findAndReadConfig(request) : { result: data, configFile: null };
73}
74exports.loadConfig = loadConfig;
75function getConfig(request, configPath) {
76 if (configPath == null) {
77 return loadConfig(request);
78 }
79 else {
80 return readConfig(path.resolve(request.projectDir, configPath), request);
81 }
82}
83exports.getConfig = getConfig;
84async function loadParentConfig(request, spec) {
85 let isFileSpec;
86 if (spec.startsWith("file:")) {
87 spec = spec.substring("file:".length);
88 isFileSpec = true;
89 }
90 let parentConfig = await orNullIfFileNotExist(readConfig(path.resolve(request.projectDir, spec), request));
91 if (parentConfig == null && isFileSpec !== true) {
92 let resolved = null;
93 try {
94 resolved = require.resolve(spec);
95 }
96 catch (e) {
97 // ignore
98 }
99 if (resolved != null) {
100 parentConfig = await readConfig(resolved, request);
101 }
102 }
103 if (parentConfig == null) {
104 throw new Error(`Cannot find parent config file: ${spec}`);
105 }
106 return parentConfig;
107}
108exports.loadParentConfig = loadParentConfig;
109async function loadEnv(envFile) {
110 const data = await orNullIfFileNotExist(fs_1.promises.readFile(envFile, "utf8"));
111 if (data == null) {
112 return null;
113 }
114 const parsed = (0, dotenv_1.parse)(data);
115 for (const key of Object.keys(parsed)) {
116 if (!process.env.hasOwnProperty(key)) {
117 process.env[key] = parsed[key];
118 }
119 }
120 require("dotenv-expand")(parsed);
121 return parsed;
122}
123exports.loadEnv = loadEnv;
124//# sourceMappingURL=main.js.map
\No newline at end of file