1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.loadEnv = exports.loadParentConfig = exports.getConfig = exports.loadConfig = exports.orIfFileNotExist = exports.orNullIfFileNotExist = exports.findAndReadConfig = void 0;
|
4 | const fs_1 = require("fs");
|
5 | const js_yaml_1 = require("js-yaml");
|
6 | const path = require("path");
|
7 | const dotenv_1 = require("dotenv");
|
8 | const config_file_ts_1 = require("config-file-ts");
|
9 | async 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 | }
|
40 | async 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 | }
|
50 | exports.findAndReadConfig = findAndReadConfig;
|
51 | function orNullIfFileNotExist(promise) {
|
52 | return orIfFileNotExist(promise, null);
|
53 | }
|
54 | exports.orNullIfFileNotExist = orNullIfFileNotExist;
|
55 | function 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 | }
|
64 | exports.orIfFileNotExist = orIfFileNotExist;
|
65 | async 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 | }
|
74 | exports.loadConfig = loadConfig;
|
75 | function 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 | }
|
83 | exports.getConfig = getConfig;
|
84 | async 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 |
|
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 | }
|
108 | exports.loadParentConfig = loadParentConfig;
|
109 | async 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 | }
|
123 | exports.loadEnv = loadEnv;
|
124 |
|
\ | No newline at end of file |