2026-05-30 03:21:35 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
import { createRequire } from 'node:module';
|
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
|
const fs = require('node:fs');
|
|
|
|
|
const path = require('node:path');
|
|
|
|
|
const vm = require('node:vm');
|
|
|
|
|
|
2026-05-31 03:29:41 +08:00
|
|
|
import { DB_PATH, openDb } from './db.mjs';
|
|
|
|
|
import { buildIndex } from './indexer.mjs';
|
2026-06-12 22:20:29 +08:00
|
|
|
import { createQueryApi, createAttuneApi } from './query.mjs';
|
2026-05-30 03:21:35 +08:00
|
|
|
|
2026-06-10 02:05:27 +08:00
|
|
|
function executeScript(api, scriptContent) {
|
2026-05-30 03:21:35 +08:00
|
|
|
const sandbox = {
|
|
|
|
|
...api, JSON, Math, Array, Object, Set, Map, Date, RegExp,
|
|
|
|
|
parseInt, parseFloat, String, Number, Boolean, Error, Promise, console, setTimeout,
|
|
|
|
|
};
|
|
|
|
|
const ctx = vm.createContext(sandbox);
|
|
|
|
|
return vm.runInNewContext(`(async()=>{${scriptContent}})()`, ctx, { timeout: 30000 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 02:05:27 +08:00
|
|
|
function executeQuery(db, scriptContent) {
|
|
|
|
|
return executeScript(createQueryApi(db), scriptContent);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 22:20:29 +08:00
|
|
|
function executeAttune(db, scriptContent) {
|
|
|
|
|
return executeScript(createAttuneApi(db), scriptContent);
|
2026-06-10 02:05:27 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 03:21:35 +08:00
|
|
|
function main() {
|
|
|
|
|
const args = process.argv.slice(2);
|
2026-07-08 16:20:48 +08:00
|
|
|
// Uniform error envelope across all four verbs: a failure is reported as
|
|
|
|
|
// { error, stack } on stdout with exit code 1, never a raw crash on stderr.
|
|
|
|
|
const fail = (e) => {
|
|
|
|
|
process.stdout.write(JSON.stringify({ error: e.message, stack: e.stack }) + '\n');
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
|
};
|
2026-05-30 03:21:35 +08:00
|
|
|
if (args[0] === '--build') {
|
2026-07-08 16:20:48 +08:00
|
|
|
try {
|
|
|
|
|
buildIndex({ force: true });
|
|
|
|
|
process.stdout.write(JSON.stringify({ ok: true, db: DB_PATH }) + '\n');
|
|
|
|
|
} catch (e) { fail(e); }
|
2026-05-30 03:21:35 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (args[0] === '--search' && args[1]) {
|
2026-07-08 16:20:48 +08:00
|
|
|
let db;
|
|
|
|
|
try {
|
|
|
|
|
buildIndex();
|
|
|
|
|
db = openDb();
|
|
|
|
|
process.stdout.write(JSON.stringify(createQueryApi(db).search(args.slice(1).join(' ')), null, 2) + '\n');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
fail(e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (db) db.close();
|
|
|
|
|
}
|
2026-05-30 03:21:35 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (args[0] === '--query' && args[1]) {
|
|
|
|
|
buildIndex();
|
|
|
|
|
const db = openDb();
|
|
|
|
|
const script = fs.readFileSync(path.resolve(args[1]), 'utf8');
|
|
|
|
|
executeQuery(db, script)
|
|
|
|
|
.then(r => { process.stdout.write(JSON.stringify(r, null, 2) + '\n'); db.close(); })
|
|
|
|
|
.catch(e => { process.stdout.write(JSON.stringify({ error: e.message, stack: e.stack }) + '\n'); db.close(); process.exitCode = 1; });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-06-12 22:20:29 +08:00
|
|
|
if (args[0] === '--attune' && args[1]) {
|
2026-06-10 02:05:27 +08:00
|
|
|
buildIndex();
|
|
|
|
|
const db = openDb();
|
|
|
|
|
const script = fs.readFileSync(path.resolve(args[1]), 'utf8');
|
2026-06-12 22:20:29 +08:00
|
|
|
executeAttune(db, script)
|
2026-06-10 02:05:27 +08:00
|
|
|
.then(r => { process.stdout.write(JSON.stringify(r, null, 2) + '\n'); db.close(); })
|
|
|
|
|
.catch(e => { process.stdout.write(JSON.stringify({ error: e.message, stack: e.stack }) + '\n'); db.close(); process.exitCode = 1; });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-06-12 22:20:29 +08:00
|
|
|
process.stderr.write('Usage:\n node runtime.mjs --build\n node runtime.mjs --search "text"\n node runtime.mjs --query <file.js>\n node runtime.mjs --attune <file.js>\n');
|
2026-05-30 03:21:35 +08:00
|
|
|
process.exitCode = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|