kusamoji
Version:
Japanese morphological analyzer for Node.js — Viterbi tokenizer with mmap dict loading and pluggable POS-source strategy
68 lines (59 loc) • 2.29 kB
JavaScript
;
/**
* kusamoji rebuild-native — compile the mmap addon from source.
*
* Usage:
* npx kusamoji rebuild-native
*
* Requires: C compiler (gcc/clang), Python 3, node-gyp.
* The compiled binary is cached at ~/.kusamoji/ for future use.
*/
let path = require("path");
let fs = require("fs");
let { execSync } = require("child_process");
const NATIVE_DIR = path.resolve(__dirname, "..", "src", "native");
const BINARY = path.join(NATIVE_DIR, "build", "Release", "mmap_addon.node");
console.log("[kusamoji] Compiling mmap native addon from source...");
console.log(" Source:", path.join(NATIVE_DIR, "mmap_addon.c"));
console.log(" Platform:", process.platform + "-" + process.arch);
console.log(" Node:", process.version, "(N-API", process.versions.napi + ")");
console.log();
try {
execSync("npx node-gyp rebuild", {
cwd: NATIVE_DIR,
stdio: "inherit",
timeout: 120000,
});
} catch (e) {
console.error("\n[kusamoji] Build failed. Ensure you have:");
console.error(" - A C compiler (gcc, clang, or MSVC)");
console.error(" - Python 3");
console.error(" - node-gyp (npx node-gyp should work)");
process.exit(1);
}
if (!fs.existsSync(BINARY)) {
console.error("[kusamoji] Build succeeded but binary not found at:", BINARY);
process.exit(1);
}
// Cache to ~/.kusamoji/
try {
const { CACHE_DIR } = require("../src/native/loader");
const NAPI_VER = process.versions.napi;
const cacheName = `mmap_addon-${process.platform}-${process.arch}-napi${NAPI_VER}.node`;
fs.mkdirSync(CACHE_DIR, { recursive: true });
fs.copyFileSync(BINARY, path.join(CACHE_DIR, cacheName));
fs.writeFileSync(path.join(CACHE_DIR, "config.json"), JSON.stringify({
builtAt: new Date().toISOString(),
nodeVersion: process.version,
napiVersion: NAPI_VER,
platform: process.platform,
arch: process.arch,
source: "compiled",
}, null, 2) + "\n");
console.log("\n[kusamoji] Binary compiled and cached at:", CACHE_DIR);
} catch (e) {
console.log("\n[kusamoji] Binary compiled at:", BINARY);
console.log(" (could not cache to ~/.kusamoji/:", e.message + ")");
}
console.log("[kusamoji] Done. mmap dict loading is now active.");