Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413/** VM configuration persistence and platform detection. @module */
import { mkdir, readFile, writeFile,} from 'node:fs/promises';import { homedir, } from 'node:os';import { join, } from 'node:path';
import { nonNullishOrThrow, } from '@monochromatic-dev/module-or-throw/ts';import { tagged, } from '@monochromatic-dev/module-logger/ts';
import { CONFIG_FILENAME, type Hypervisor, type SyncState, type VmsyncConfig,} from './types.ts';
/** Logger root for vmsync after removing the package log shim. @example ```ts const rl = tagged({ tag: someFunction.name, l, },); ``` */const l = tagged({ tag: 'vmsync', },);
//region Data directories
/** Root data directory for all vmsync-managed VMs. */export const DATA_DIR: string = join( homedir(), '.local', 'share', 'vmsync',);
/** Resolves the directory path for a named VM. @param name - VM name @returns Absolute path to the VM directory @example ```ts vmDir('alpine'); // "/home/user/.local/share/vmsync/alpine" ``` */export function vmDir(name: string,): string { return join( DATA_DIR, name, );}
/** Resolves the config file path for a named VM. @param name - VM name @returns Absolute path to vmsync.jsonc @example ```ts vmConfigPath('alpine'); // "/home/user/.local/share/vmsync/alpine/vmsync.jsonc" ``` */export function vmConfigPath(name: string,): string { return join( vmDir(name,), CONFIG_FILENAME, );}
//endregion Data directories
//region Name validation
/** Whether `c` is an ASCII alphanumeric character. @param c - single-character string to inspect @returns whether `c` is `[A-Za-z0-9]` @example ```ts isAlphaNum('a'); // true isAlphaNum('-'); // false ``` */function isAlphaNum(c: string,): boolean { return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9'));}
/** Whether `c` is allowed in non-leading positions of a VM name. @param c - single-character string to inspect @returns whether `c` is alphanumeric, underscore, or hyphen @example ```ts isNameBodyChar('_'); // true isNameBodyChar('.'); // false ``` */function isNameBodyChar(c: string,): boolean { return isAlphaNum(c,) || (c === '_') || (c === '-');}
/** Checks whether `name` matches the original regex `/^[A-Za-z0-9][A-Za-z0-9_-]*$/`. Empty input fails the leading-alphanumeric requirement; all subsequent characters must be alphanumeric, underscore, or hyphen. @param name - candidate VM name @returns whether name is a valid VM identifier @example ```ts isValidVmName('my-vm-01'); // true isValidVmName('-leading'); // false isValidVmName(''); // false ``` */function isValidVmName(name: string,): boolean { if (name.length === 0) return false; if (!isAlphaNum(name.charAt(0,),)) return false; for (const c of name.slice(1,)) { if (!isNameBodyChar(c,)) return false; } return true;}
/** Validates a VM name contains only safe characters. @param name - VM name to validate @throws Error when name contains invalid characters @example ```ts validateName('my-vm-01'); // OK validateName('../evil'); // throws ``` */export function validateName(name: string,): void { if (!isValidVmName(name,)) { throw new Error( `invalid VM name: ${name} (must start with alphanumeric, contain only alphanumerics, hyphens, underscores)`, ); }}
//endregion Name validation
//region JSONC parsing
/** Strips single-line (`//`) and multi-line (`/* *\/`) comments from a JSONC string, preserving string literals that contain comment-like sequences. @param text - JSONC source text @returns JSON-parseable string with comments removed @example ```ts stripJsoncComments('{ "a": 1 // comment\n}'); // '{ "a": 1 \n}' ``` */export function stripJsoncComments(text: string,): string { /** Result accumulator built character-by-character. */ const result: string[] = []; /** Current position in the source text. */ // oxlint-disable-next-line no-restricted-syntax/no-function-root-let -- parser cursor advances by 1 or 2 depending on which side-effecting branch fires let i = 0; /** Whether we are currently inside a double-quoted string. */ // oxlint-disable-next-line no-restricted-syntax/no-function-root-let -- string-mode flag toggled across multiple branches of the parser state machine let inString = false;
while (i < text .length) { /** Character at the current position. */ const ch = nonNullishOrThrow(text[i],);
if (inString) { if (ch === '\\') { /** Escaped character pair. */ const next = nonNullishOrThrow(text[i + 1],); result.push( ch, next, ); i += 2; } else { if (ch === '"') inString = false; result.push(ch,); i += 1; } } else if (ch === '"') { inString = true; result.push(ch,); i += 1; } else if ((ch === '/') && (text[i + 1] === '/')) { while ((i < text .length) && (text[i] !== '\n')) i += 1; } else { if ((ch === '/') && (text[i + 1] === '*')) { i += 2; while ((i < text .length) && (!((text[i - 1] === '*') && (text[i] === '/')))) i += 1; } else result.push(ch,); i += 1; } }
return result.join('',);}
//endregion JSONC parsing
//region Config read/write
/** Reads and parses the vmsync.jsonc config for a named VM. @param name - VM name @returns Parsed {@link VmsyncConfig} @throws Error when the config file does not exist or contains invalid JSON @example ```ts const cfg = await readConfig('alpine'); console.log(cfg.boot.memory); ``` */export async function readConfig(name: string,): Promise<VmsyncConfig> { /** Tagged logger so read-config entries are scoped to `readConfig` in the output. */ const rl = tagged({ tag: readConfig.name, l, },); /** Raw JSONC content from disk. */ const raw = await readFile( vmConfigPath(name,), 'utf8', ); /** Parsed configuration object. */ // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- trusted JSONC config we wrote const config = JSON.parse(stripJsoncComments(raw,),) as VmsyncConfig; rl.info(`loaded config for "${name}"`,); return config;}
/** Serializes and writes the vmsync.jsonc config for a named VM. Creates the VM directory if it does not exist. @param name - VM name @param config - {@link VmsyncConfig} to persist @mutates config - `JSON.stringify` may invoke `toJSON`, getters, or proxy traps. @example ```ts await writeConfig({ name: 'alpine', config }); ``` */export async function writeConfig( { name, config, }: { readonly name: string; config: Omit<VmsyncConfig, 'state'> & { readonly state: Readonly<SyncState>; }; },): Promise<void> { /** Tagged logger so write-config entries are scoped to `writeConfig` in the output. */ const rl = tagged({ tag: writeConfig.name, l, },); /** Target directory for this VM. */ const dir = vmDir(name,); await mkdir( dir, { recursive: true, }, ); /** Formatted JSON with 2-space indent. */ const serialized = JSON.stringify( config, undefined, 2, ); await writeFile( vmConfigPath(name,), serialized, 'utf8', ); rl.info(`wrote config for "${name}"`,);}
//endregion Config read/write
//region Platform detection
/** Detects the available hypervisor on the current platform. @returns {@link Hypervisor}: `'kvm'` on Linux, `'hyperv'` on Windows @throws Error when running on an unsupported platform @example ```ts const hv = detectHypervisor(); // 'kvm' on Linux, 'hyperv' on Windows ``` */export function detectHypervisor(): Hypervisor { if (process.platform === 'linux') return 'kvm'; if (process.platform === 'win32') return 'hyperv'; throw new Error( `unsupported platform: ${process.platform} (vmsync requires Linux with KVM or Windows with Hyper-V)`, );}
//endregion Platform detection
//region Default boot config
/** Default memory allocation for new VMs. */export const DEFAULT_MEMORY = '4G';
/** Default number of virtual CPUs for new VMs. */export const DEFAULT_CPUS = 4;
//endregion Default boot config