// Copyright (C) 2026 Zaher Salman // SPDX-License-Identifier: GPL-3.0-or-later const fs = require('fs'); const path = require('path'); const { LocalRuntime } = require('../local-runtime'); async function restoreRuntime(bundleDirectory, installationDirectory) { const source = path.resolve(bundleDirectory); const descriptors = []; // upload-artifact retains the content-addressed bundle directory. Accept a // direct bundle too, but never silently choose between multiple runtimes. for (const entry of await fs.promises.readdir(source, { withFileTypes: true })) { const filename = path.join(source, entry.name); if (entry.isFile() && entry.name === 'runtime-download.json') { descriptors.push(filename); } else if (entry.isDirectory()) { const descriptor = path.join(filename, 'runtime-download.json'); if (fs.existsSync(descriptor)) descriptors.push(descriptor); } } if (descriptors.length !== 1) { throw new Error('Expected exactly one runtime-download.json in the selected artifact; found ' + descriptors.length); } // Reuse the desktop installer's checksum, safe extraction, manifest, ELF, // executable-mode and startup checks rather than trusting an arbitrary tar. const installer = new LocalRuntime({ directory: installationDirectory, timeout: 15000 }); const plan = await installer.inspectBundle(descriptors[0]); const result = await installer.install(plan, new AbortController().signal); return result.root; } if (require.main === module) { const [source, destination] = process.argv.slice(2); if (!source || !destination || !process.env.GITHUB_ENV) { console.error('Usage: GITHUB_ENV= node scripts/ci-restore-runtime.js '); process.exitCode = 1; } else { restoreRuntime(source, destination).then(function(root) { if (/[\r\n]/.test(root)) throw new Error('Invalid runtime path'); fs.appendFileSync(process.env.GITHUB_ENV, 'RUNTIME_DIR=' + root + '\n'); console.log('Verified runtime restored: ' + root); }).catch(function(error) { console.error(error.message); process.exitCode = 1; }); } } module.exports = { restoreRuntime };