Skip to the content

Capsid menu

update-prose-engine.mjs

44 lines and 1 functions in DrDustinEdwards/site-runtime, at commit 89e9d58 on 2026-10-24. This file on GitHub.

Functions

The functions of update-prose-engine.mjs
FunctionLinesComplexityCoveredCRAPMutation
<anonymous:40>40 to 402No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet

Imports

It imports no other module of this repo. No module of this repo imports it.

Source

#!/usr/bin/env node
/**
 * Refreshes the package's copy of the avoid-ai-writing detection engine, so `./prose` keeps no dependency:
 *
 *   node update-prose-engine.mjs 3.38.0
 *
 * It packs `avoid-ai-writing-detector@<version>` from the npm registry (https://github.com/conorbronsdon/avoid-ai-writing,
 * MIT), and writes prose-engine.mjs beside this file: the package's LICENSE as a header, then detector/patterns.js
 * verbatim except its CommonJS export, which becomes `export default`. Exits non-zero when the pack or either file is
 * missing, or the export it replaces is not where it was, so a failed refresh never leaves a half-written engine.
 */

import { execFileSync } from "node:child_process";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";

const version = process.argv[2];
if (!version || !/^\d+\.\d+\.\d+$/.test(version)) {
  console.error("usage: node update-prose-engine.mjs <exact version, such as 3.38.0>");
  process.exit(2);
}

const spec = `avoid-ai-writing-detector@${version}`;
const dir = await mkdtemp(join(tmpdir(), "prose-engine-"));
try {
  const [packed] = JSON.parse(execFileSync("npm", ["pack", spec, "--json", "--pack-destination", dir], { encoding: "utf8" }));
  execFileSync("tar", ["-xzf", join(dir, packed.filename), "-C", dir]);
  const license = await readFile(join(dir, "package", "LICENSE"), "utf8");
  const engine = await readFile(join(dir, "package", "detector", "patterns.js"), "utf8");

  const cjs = /\nif \(typeof module !== 'undefined' && module\.exports\) \{\n\s*module\.exports = AIDetector;\n\}\s*$/;
  if (!cjs.test(engine)) throw new Error(`${spec}: detector/patterns.js no longer ends with its CommonJS export`);

  const header = [
    "// GENERATED by update-prose-engine.mjs: do not edit. Change the version there and run it again.",
    `// ${spec}, detector/patterns.js (https://github.com/conorbronsdon/avoid-ai-writing), under its licence:`,
    "//",
    ...license.trim().split("\n").map((l) => (l ? `// ${l}` : "//")),
    "",
    "",
  ].join("\n");
  const out = `${header}${engine.replace(cjs, "\n")}\nexport default AIDetector;\n`;
  await writeFile(fileURLToPath(new URL("prose-engine.mjs", import.meta.url)), out);
  console.log(`prose-engine.mjs: ${spec}`);
} finally {
  await rm(dir, { recursive: true, force: true });
}