Files
go/build-pipeline.js

76 lines
2.4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const AdmZip = require('adm-zip');
const { execSync } = require('child_process');
const BUILD_DIR = path.join(__dirname, 'dist-build');
const REPOS = ['frontend', 'player'];
const GITLAB_GROUP = 'vyla-entertainment';
const REPO_CONFIG = {
'frontend': { name: 'frontend', branch: 'main' },
'player': { name: 'player', branch: 'main' },
};
function cleanBuildDir() {
if (fs.existsSync(BUILD_DIR)) {
fs.rmSync(BUILD_DIR, { recursive: true, force: true });
}
fs.mkdirSync(BUILD_DIR, { recursive: true });
}
function clonePrivateRepo(repoConfig, targetDir) {
if (fs.existsSync(targetDir)) {
fs.rmSync(targetDir, { recursive: true, force: true });
}
const token = process.env.GITLAB_TOKEN || process.env.VYLA_GITLAB_TOKEN;
const auth = token ? `oauth2:${token}@` : '';
const repoUrl = `https://${auth}gitlab.com/${GITLAB_GROUP}/${repoConfig.name}.git`;
execSync(`git clone --depth 1 --branch ${repoConfig.branch} ${repoUrl} ${targetDir}`, { stdio: 'inherit' });
}
async function build() {
cleanBuildDir();
for (const [key, config] of Object.entries(REPO_CONFIG)) {
const targetDir = path.join(__dirname, key);
clonePrivateRepo(config, targetDir);
execSync('npm install --omit=dev', { cwd: targetDir, stdio: 'inherit' });
}
const zip = new AdmZip();
const configDir = path.join(__dirname, 'config');
if (fs.existsSync(configDir)) {
zip.addLocalFolder(configDir, 'config');
}
for (const repo of REPOS) {
const srcRepo = path.join(__dirname, repo);
zip.addLocalFolder(srcRepo, repo);
}
const zipPath = path.join(__dirname, 'update-payload.zip');
if (fs.existsSync(zipPath)) fs.rmSync(zipPath, { force: true });
zip.writeZip(zipPath);
const hash = crypto.createHash('sha256');
hash.update(fs.readFileSync(zipPath));
const checksum = hash.digest('hex');
const manifest = {
version: JSON.parse(fs.readFileSync('package.json', 'utf8')).version,
sha256: checksum,
timestamp: new Date().toISOString()
};
fs.writeFileSync(path.join(__dirname, 'manifest.json'), JSON.stringify(manifest, null, 2), 'utf8');
for (const repo of REPOS) {
fs.rmSync(path.join(__dirname, repo), { recursive: true, force: true });
}
}
build().catch(err => {
console.error(err);
process.exit(1);
});