2022-12-01 12:03:37 +01:00
|
|
|
import path from "path";
|
|
|
|
|
import * as fs from "fs";
|
|
|
|
|
import deepmerge from "deepmerge";
|
2022-12-30 12:12:04 +01:00
|
|
|
import { ConfigShape } from "TokensGenerator";
|
|
|
|
|
import { workPath } from "Paths";
|
2023-01-02 12:06:00 +01:00
|
|
|
import { register } from "ts-node";
|
2022-12-01 12:03:37 +01:00
|
|
|
import Config from "./Config";
|
|
|
|
|
|
|
|
|
|
const getLocalConfig = async () => {
|
|
|
|
|
const filename = Config.configurationFilenames
|
|
|
|
|
.map((filename_) => path.join(workPath(), filename_))
|
|
|
|
|
.find((filename_) => fs.existsSync(filename_));
|
|
|
|
|
|
|
|
|
|
if (!filename) {
|
|
|
|
|
console.log("No local config found, using default config.");
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-01-02 12:06:00 +01:00
|
|
|
console.log("Found local config file: " + filename);
|
|
|
|
|
|
|
|
|
|
const ext = path.extname(filename);
|
|
|
|
|
if (ext === ".ts") {
|
|
|
|
|
registerTypescriptLoader();
|
|
|
|
|
}
|
2022-12-01 12:03:37 +01:00
|
|
|
|
|
|
|
|
const config = await import(filename);
|
|
|
|
|
return config.default;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-04 15:49:37 +01:00
|
|
|
/**
|
|
|
|
|
* Register ts-node to load typescript config files with import. ( In fact, with `require` after transpiling ).
|
|
|
|
|
*/
|
2023-01-02 12:06:00 +01:00
|
|
|
const registerTypescriptLoader = () => {
|
2023-01-04 15:49:37 +01:00
|
|
|
// Specifying to load all .ts files as CJS is really important, otherwise ts-node will try to load them as ESM in
|
|
|
|
|
// projects where cunningham is used as a dependency where package.json contains "type": "module".
|
|
|
|
|
// So, by doing this we tell ts-node "Load all .ts files as CJS, even if they are in a project with ESM".
|
2023-01-02 12:06:00 +01:00
|
|
|
register({
|
|
|
|
|
moduleTypes: {
|
|
|
|
|
"**/*.ts": "cjs",
|
|
|
|
|
},
|
|
|
|
|
compilerOptions: {
|
|
|
|
|
module: "commonjs",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-01 12:03:37 +01:00
|
|
|
const getDistConfig = async () => {
|
2023-01-12 15:00:45 +01:00
|
|
|
const config = await import("./cunningham");
|
2022-12-01 12:03:37 +01:00
|
|
|
return config.default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getConfig = async () => {
|
|
|
|
|
const localConfig = await getLocalConfig();
|
|
|
|
|
const distConfig = await getDistConfig();
|
|
|
|
|
const config: ConfigShape = deepmerge(distConfig, localConfig);
|
|
|
|
|
return config;
|
|
|
|
|
};
|