@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
56 lines (54 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeDTsFiles = void 0;
const path_1 = require("path");
const getProjectConfig_1 = require("../../config/getProjectConfig");
const projects_1 = require("../../utils/projects");
const utils_1 = require("./utils");
const writeDTsFiles = async (context, choice) => {
const { env, currentComponent } = await (0, utils_1.getCurrentComponentAndEnvFromChoice)(context.config, choice);
const componentsWithEnabledEnvDTsWrite = Object.entries(context.config.components)
.filter(([, component]) => { var _a; return (_a = component === null || component === void 0 ? void 0 : component.envDTs) !== null && _a !== void 0 ? _a : true; })
.map(([componentName]) => componentName);
const componentsToActuallyWriteEnvDts = currentComponent
? componentsWithEnabledEnvDTsWrite.includes(currentComponent)
? [currentComponent]
: []
: componentsWithEnabledEnvDTsWrite;
const gitRoot = await (0, projects_1.getGitRoot)();
for (const componentName of componentsToActuallyWriteEnvDts) {
const envNames = await getEnvsForDTs(env, componentName);
const envDTsContent = createEnvDTsContent(envNames);
const componentDir = (0, utils_1.getComponentFullPath)(gitRoot, context.config, componentName);
const filePath = (0, path_1.join)(componentDir, "env.d.ts");
await context.fileWriter.writeGeneratedFile(filePath, envDTsContent, {
commentChar: "//",
});
}
};
exports.writeDTsFiles = writeDTsFiles;
async function getEnvsForDTs(env, componentName) {
const environment = await (0, getProjectConfig_1.getEnvironment)(env, componentName);
const allEnvKeys = Object.keys(environment.envVars);
const hiddenEnvKeys = new Set(environment.secretEnvVarKeys
.filter((s) => s.hidden)
.map((s) => s.key)
.concat(["_ALL_ENV_VAR_KEYS"]));
const dTsEnvKeys = allEnvKeys.filter((k) => !hiddenEnvKeys.has(k));
return dTsEnvKeys;
}
function createEnvDTsContent(envNames) {
return `/* tslint:disable prettier/prettier */
/* eslint-disable prettier/prettier */
/* prettier-ignore */
export {}
declare global {
namespace NodeJS {
interface ProcessEnv {
${envNames.map((name) => ` ${name}: string`).join("\n")}
}
}
}
`;
}
//# sourceMappingURL=writeEnvDTs.js.map