예제 소스
바둑 on-policy 학습: 대국 로그를 KataGo 라벨로 바꾸기 · 파일 169개
dev-with-ts.mjs
#!/usr/bin/env node
/**
* next 요청 로그에 시각을 붙입니다.
* [18:17:05.123] GET /play 200 in 21ms
*
* pnpm run dev:ts
* pnpm run start:ts
*/
import { spawn } from "node:child_process";
import { createRequire } from "node:module";
import path from "node:path";
const require = createRequire(import.meta.url);
const nextBin = require.resolve("next/dist/bin/next");
const args = process.argv.slice(2);
const child = spawn(process.execPath, [nextBin, ...args], {
stdio: ["inherit", "pipe", "pipe"],
env: process.env,
});
const pending = new Map();
function ts() {
const now = new Date();
const hh = String(now.getHours()).padStart(2, "0");
const mm = String(now.getMinutes()).padStart(2, "0");
const ss = String(now.getSeconds()).padStart(2, "0");
const ms = String(now.getMilliseconds()).padStart(3, "0");
return `[${hh}:${mm}:${ss}.${ms}]`;
}
function onData(chunk, out) {
const text = (pending.get(out) ?? "") + chunk.toString("utf8");
const parts = text.split(/\r?\n/);
pending.set(out, parts.pop() ?? "");
for (const line of parts) {
out.write(`${ts()} ${line}\n`);
}
}
child.stdout.on("data", (c) => onData(c, process.stdout));
child.stderr.on("data", (c) => onData(c, process.stderr));
function flush() {
for (const [out, buf] of pending) {
if (buf) out.write(`${ts()} ${buf}\n`);
}
pending.clear();
}
child.on("exit", (code, signal) => {
flush();
if (signal) {
process.kill(process.pid, signal);
return;
}
process.exit(code ?? 1);
});
process.on("SIGINT", () => child.kill("SIGINT"));
process.on("SIGTERM", () => child.kill("SIGTERM"));