|
| 1 | +#!/usr/bin/env node |
| 2 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 3 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 4 | + |
| 5 | +// Panel Harness — Bridge cartridges to BoJ Server and panll |
| 6 | + |
| 7 | +import { readFile, writeFile, mkdir, readdir } from 'node:fs/promises'; |
| 8 | +import { join, dirname } from 'node:path'; |
| 9 | +import { exec } from 'node:child_process'; |
| 10 | +import { promisify } from 'node:util'; |
| 11 | + |
| 12 | +const execAsync = promisify(exec); |
| 13 | + |
| 14 | +async function readJsonFile(filePath) { |
| 15 | + const content = await readFile(filePath, 'utf8'); |
| 16 | + return JSON.parse(content); |
| 17 | +} |
| 18 | + |
| 19 | +async function writeJsonFile(filePath, data) { |
| 20 | + await writeFile(filePath, JSON.stringify(data, null, 2), 'utf8'); |
| 21 | +} |
| 22 | + |
| 23 | +async function ensureDirectoryExists(directory) { |
| 24 | + await mkdir(directory, { recursive: true }); |
| 25 | +} |
| 26 | + |
| 27 | +async function registerCartridge(cartridgeDir, target, options = {}) { |
| 28 | + const { routes = [] } = options; |
| 29 | + |
| 30 | + // Read cartridge.json |
| 31 | + const cartridgeJsonPath = join(cartridgeDir, 'cartridge.json'); |
| 32 | + const cartridge = await readJsonFile(cartridgeJsonPath); |
| 33 | + |
| 34 | + // Ensure the target directory exists |
| 35 | + const targetDir = join('harness', target, cartridge.name); |
| 36 | + await ensureDirectoryExists(targetDir); |
| 37 | + |
| 38 | + // Register the cartridge with the target |
| 39 | + console.log(`Registering cartridge with ${target}...`); |
| 40 | + |
| 41 | + // In a real implementation, you would register the cartridge with BoJ Server or panll here |
| 42 | + // For now, we'll just log the registration |
| 43 | + const registration = { |
| 44 | + cartridge, |
| 45 | + target, |
| 46 | + routes, |
| 47 | + registeredAt: new Date().toISOString(), |
| 48 | + }; |
| 49 | + |
| 50 | + // Write the registration to a file |
| 51 | + const registrationPath = join(targetDir, 'registration.json'); |
| 52 | + await writeJsonFile(registrationPath, registration); |
| 53 | + |
| 54 | + return { |
| 55 | + cartridge, |
| 56 | + target, |
| 57 | + routes, |
| 58 | + registrationPath, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +async function main() { |
| 63 | + const args = process.argv.slice(2); |
| 64 | + |
| 65 | + if (args.length < 2) { |
| 66 | + console.error('Usage: node harness.js <cartridge-dir> <target> [--routes <routes-file>]'); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + const cartridgeDir = args[0]; |
| 71 | + const target = args[1]; |
| 72 | + const routesIndex = args.indexOf('--routes'); |
| 73 | + const routesFile = routesIndex !== -1 ? args[routesIndex + 1] : null; |
| 74 | + |
| 75 | + let routes = []; |
| 76 | + if (routesFile) { |
| 77 | + try { |
| 78 | + routes = await readJsonFile(routesFile); |
| 79 | + } catch (error) { |
| 80 | + console.error('Error reading routes file:', error); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + console.log(`Registering cartridge from ${cartridgeDir} with ${target}...`); |
| 87 | + const result = await registerCartridge(cartridgeDir, target, { routes }); |
| 88 | + |
| 89 | + console.log('Cartridge registered successfully:'); |
| 90 | + console.log(` Name: ${result.cartridge.name}`); |
| 91 | + console.log(` Target: ${result.target}`); |
| 92 | + console.log(` Routes: ${JSON.stringify(result.routes)}`); |
| 93 | + console.log(` Registration Path: ${result.registrationPath}`); |
| 94 | + } catch (error) { |
| 95 | + console.error('Error registering cartridge:', error); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 101 | + main(); |
| 102 | +} |
| 103 | + |
| 104 | +export { registerCartridge }; |
0 commit comments