UNPKG

@thi.ng/tangle

Version:

Literate programming code block tangling / codegen utility, inspired by org-mode & noweb

69 lines (67 loc) 1.93 kB
import { ParseError, flag, parse, usage } from "@thi.ng/args"; import { readJSON, writeText } from "@thi.ng/file-io"; import { ConsoleLogger } from "@thi.ng/logger"; import { join } from "node:path"; import { tangleFile } from "./tangle.js"; const argOpts = { noComments: flag({ default: false, desc: "don't generate comments" }), debug: flag({ alias: "d", default: false, desc: "enable debug output" }), dryRun: flag({ default: false, desc: "enable dry run (don't overwrite files)" }) }; const PKG = readJSON(join(process.argv[2], "package.json")); const APP_NAME = PKG.name.split("/")[1]; const HEADER = ` \u2588 \u2588 \u2588 \u2502 \u2588\u2588 \u2588 \u2502 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2502 ${PKG.name} ${PKG.version} \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2502 Literate programming code block tangling \u2588 \u2502 \u2588 \u2588 \u2502 `; const usageOpts = { lineWidth: process.stdout.columns, prefix: `${HEADER} usage: ${APP_NAME} [OPTS] SOURCE-FILES(S) ... ${APP_NAME} --help `, showGroupNames: true, paramWidth: 20 }; const showUsage = () => { process.stderr.write(usage(argOpts, usageOpts)); process.exit(1); }; try { const result = parse(argOpts, process.argv, { start: 3, usageOpts }); if (!result) process.exit(1); const { result: opts, rest } = result; if (!rest.length) showUsage(); let ctx = { logger: new ConsoleLogger("tangle", opts.debug ? "DEBUG" : "INFO"), opts: { comments: opts.noComments !== true } }; for (let file of rest) { ctx = tangleFile(file, ctx); } for (let out in ctx.outputs) { writeText(out, ctx.outputs[out], ctx.logger, opts.dryRun); } } catch (e) { if (!(e instanceof ParseError)) process.stderr.write(e.message); process.exit(1); } export { APP_NAME, HEADER, PKG };