opus-magnum-solution-watcher/index.mjs
2025-03-13 11:08:53 -04:00

36 lines
1.1 KiB
JavaScript

import {glob, readFile, stat, watch} from 'node:fs/promises';
import path from 'node:path';
const steamUserID = process.env.STEAM_USER_ID;
const homeDir = process.env.HOME;
const solutionsDir = `${homeDir}/.local/share/Opus Magnum/${steamUserID}`;
async function readSolutionName (solutionPath) {
const buf = await readFile(solutionPath);
return buf.subarray(0x0A, 0x0A + buf[0x09]).toString('utf-8');
}
const solutionNames = Object.create(null);
for await (const solutionPath of glob(path.join(solutionsDir, '*.solution'))) {
const solutionName = await readSolutionName(solutionPath);
solutionNames[solutionPath] = solutionName;
}
for await (const {filename} of watch(solutionsDir)) {
if (path.extname(filename) !== '.solution') continue; // ignore options etc
const solutionPath = path.join(solutionsDir, filename);
if (!(await stat(solutionPath)).isFile) continue; // ignore deleted
const solutionName = await readSolutionName(solutionPath);
if (solutionName !== solutionNames[solutionPath]) {
console.log(
'changed:', filename,
solutionNames[solutionPath], '->', solutionName
);
solutionNames[solutionPath] = solutionName;
}
}