Files
DiunaBI/.github/workflows/buildScripts/replaceTokens.js
Michał Zieliński c6736cd01d WIP: Build & Release
2025-02-12 17:59:01 +01:00

39 lines
1.4 KiB
JavaScript

module.exports = async ({ github, context, core, jobId }) => {
const frontendPath = `./${jobId}/frontend/diunaBI/browser/`;
const files = (require('fs').readdirSync(frontendPath).filter(file => file.endsWith('.js')))
.map(file => `${frontendPath}${file}`);
if (files.length === 0) {
core.setFailed("Frontend JS files not found");
return false;
}
files.push(`./${jobId}/webapi/appsettings.json`);
files.push(`./${jobId}/webapi/client_secrets.json`);
files.forEach(file => {
const data = require('fs').readFileSync(file, 'utf8');
const regex = /#{(.*?)}/g;
let match;
while (match = regex.exec(data)) {
const token = match[1].replace(/-/g, '_').toUpperCase();
console.log(token);
const value = getValue(token, jobId);
console.log(value);
if (!value) {
core.setFailed(`Token ${token} not found`);
return false;
}
// replace all occurrences of the ${token} with the value in file
const result = data.replace(new RegExp(`#{${token}}`, 'g'), value);
require('fs').writeFileSync(file, result, 'utf8');
}
});
}
function getValue(token, jobId) {
if (token == 'BUILDID') { return jobId; }
const secrets = JSON.parse(process.env.SECRETS);
const variables = JSON.parse(process.env.VARIABLES);
return variables[token] || secrets[token];
}