|
| 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 | +// Cartridge Configurator — Apply runtime configuration to cartridges dynamically |
| 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 applyConfig(cartridgeDir, config, options = {}) { |
| 28 | + const { validate = true } = options; |
| 29 | + |
| 30 | + // Read cartridge.json |
| 31 | + const cartridgeJsonPath = join(cartridgeDir, 'cartridge.json'); |
| 32 | + const cartridge = await readJsonFile(cartridgeJsonPath); |
| 33 | + |
| 34 | + // Validate configuration |
| 35 | + if (validate) { |
| 36 | + console.log('Validating configuration...'); |
| 37 | + // In a real implementation, you would validate the configuration against a schema |
| 38 | + } |
| 39 | + |
| 40 | + // Apply configuration |
| 41 | + const configPath = join(cartridgeDir, 'config.json'); |
| 42 | + await writeJsonFile(configPath, config); |
| 43 | + |
| 44 | + // Trigger hot-reload if supported (placeholder for actual hot-reload logic) |
| 45 | + console.log('Applying configuration...'); |
| 46 | + |
| 47 | + return { |
| 48 | + cartridge, |
| 49 | + config, |
| 50 | + configPath, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +async function main() { |
| 55 | + const args = process.argv.slice(2); |
| 56 | + |
| 57 | + if (args.length < 2) { |
| 58 | + console.error('Usage: node configurator.js <cartridge-dir> <config-file> [--no-validate]'); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + |
| 62 | + const cartridgeDir = args[0]; |
| 63 | + const configFile = args[1]; |
| 64 | + const validate = !args.includes('--no-validate'); |
| 65 | + |
| 66 | + let config; |
| 67 | + try { |
| 68 | + config = await readJsonFile(configFile); |
| 69 | + } catch (error) { |
| 70 | + console.error('Error reading config file:', error); |
| 71 | + process.exit(1); |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + console.log(`Applying configuration to cartridge from ${cartridgeDir}...`); |
| 76 | + const result = await applyConfig(cartridgeDir, config, { validate }); |
| 77 | + |
| 78 | + console.log('Configuration applied successfully:'); |
| 79 | + console.log(` Name: ${result.cartridge.name}`); |
| 80 | + console.log(` Config: ${JSON.stringify(result.config)}`); |
| 81 | + console.log(` Config Path: ${result.configPath}`); |
| 82 | + } catch (error) { |
| 83 | + console.error('Error applying configuration:', error); |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 89 | + main(); |
| 90 | +} |
| 91 | + |
| 92 | +export { applyConfig }; |
0 commit comments