@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
138 lines • 5.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getJobOnlyEnvVarsResolved = exports.getEnvVarsResolved = exports.getGitlabVar = exports.getEnvironment = exports.getAllPipelineContexts = exports.getAllComponentsWithAllEnvsHierarchical = exports.getAllComponentsWithAllEnvsFlat = exports.getPipelineContextByChoice = exports.getProjectComponents = exports.getProjectConfig = exports.parseChoice = void 0;
const pipeline_1 = require("../../../pipeline/src/index.js");
const gitlab_1 = require("../utils/gitlab");
const projects_1 = require("../utils/projects");
const fs_1 = require("fs");
var parseChoice_1 = require("./parseChoice");
Object.defineProperty(exports, "parseChoice", { enumerable: true, get: function () { return parseChoice_1.parseChoice; } });
let currentConfig = null;
// reload the config on change
const reloadConfigAndObserve = async () => {
const gitRoot = await (0, projects_1.getGitRoot)();
if (!gitRoot) {
return;
}
const result = await (0, pipeline_1.readConfig)(gitRoot);
if (!result) {
// can't do anything, there is no config
return;
}
const { config, path } = result;
const watcher = (0, fs_1.watch)(path, () => {
watcher.close();
reloadConfigAndObserve();
});
currentConfig = config;
};
const getProjectConfig = async () => {
if (!currentConfig) {
// initially
await reloadConfigAndObserve();
}
return currentConfig;
};
exports.getProjectConfig = getProjectConfig;
const getProjectComponents = async () => {
const config = await (0, exports.getProjectConfig)();
if (!config)
return [];
return Object.keys(config.components);
};
exports.getProjectComponents = getProjectComponents;
const getPipelineContextByChoice = async (env, componentName) => {
const config = await (0, exports.getProjectConfig)();
return await (0, pipeline_1.createComponentContext)({
config,
componentName,
env,
});
};
exports.getPipelineContextByChoice = getPipelineContextByChoice;
const getAllComponentsWithAllEnvsFlat = async () => {
const config = await (0, exports.getProjectConfig)();
if (!config) {
return [];
}
return getAllComponentsWithAllEnvsFlatFromConfig(config);
};
exports.getAllComponentsWithAllEnvsFlat = getAllComponentsWithAllEnvsFlat;
const getAllComponentsWithAllEnvsHierarchical = async () => {
const config = await (0, exports.getProjectConfig)();
if (!config) {
return {};
}
return Object.fromEntries(Object.keys(config.components).map((componentName) => [
componentName,
(0, pipeline_1.getAllEnvs)(config, componentName),
]));
};
exports.getAllComponentsWithAllEnvsHierarchical = getAllComponentsWithAllEnvsHierarchical;
const getAllPipelineContexts = async (onlyComponent) => {
const onlyComponentsArray = onlyComponent
? Array.isArray(onlyComponent)
? onlyComponent
: [onlyComponent]
: null;
return Promise.all((await (0, exports.getAllComponentsWithAllEnvsFlat)())
.filter((c) => c.env !== "local")
.filter((c) => !onlyComponentsArray || onlyComponentsArray.includes(c.componentName))
.map(({ env, componentName }) => (0, exports.getPipelineContextByChoice)(env, componentName)));
};
exports.getAllPipelineContexts = getAllPipelineContexts;
const getEnvironment = async (env, componentName) => {
const config = await (0, exports.getProjectConfig)();
return (0, pipeline_1.getEnvironment)({ config, componentName, env });
};
exports.getEnvironment = getEnvironment;
const getGitlabVar = async (vorpal, env, componentName, variableName) => {
const rawVariableName = (0, pipeline_1.getSecretVarName)(env, componentName, variableName);
return await (0, gitlab_1.getVariableValueByRawName)(vorpal, rawVariableName);
};
exports.getGitlabVar = getGitlabVar;
const resolveSecrets = async (vorpal, varSets) => {
const allVariablesInGitlab = await (0, gitlab_1.getAllVariables)(vorpal);
return Object.fromEntries(varSets.flatMap(({ envVars, secretEnvVarKeys }) => Object.entries(envVars)
.filter(([key, value]) => {
var _a;
return value !== undefined &&
value !== null &&
!((_a = secretEnvVarKeys.find((k) => k.key === key)) === null || _a === void 0 ? void 0 : _a.hidden);
})
.map(([key, value]) => [
key,
allVariablesInGitlab.reduce((acc, curr) => acc.replace(new RegExp("\\$" + curr.key, "g"), curr.value), `${value}`),
])));
};
const getEnvVarsResolved = async (vorpal, env, componentName) => {
if (!componentName) {
return {};
}
const envionment = await (0, exports.getEnvironment)(env, componentName);
// in the pipeline the secrets alreadyy exists and bash will expand them
// but here we need to manually load them
return resolveSecrets(vorpal, [
{
envVars: envionment.envVars,
secretEnvVarKeys: envionment.secretEnvVarKeys,
},
]);
};
exports.getEnvVarsResolved = getEnvVarsResolved;
/**
*
* is used to get job only vars that should also be editable locally with catladder.
*/
const getJobOnlyEnvVarsResolved = async (vorpal, env, componentName) => {
const envionment = await (0, exports.getEnvironment)(env, componentName);
return resolveSecrets(vorpal, [
envionment.jobOnlyVars.build,
envionment.jobOnlyVars.deploy,
]);
};
exports.getJobOnlyEnvVarsResolved = getJobOnlyEnvVarsResolved;
function getAllComponentsWithAllEnvsFlatFromConfig(config) {
return Object.keys(config.components).flatMap((componentName) => (0, pipeline_1.getAllEnvs)(config, componentName).map((env) => ({ env, componentName })));
}
//# sourceMappingURL=getProjectConfig.js.map