@thi.ng/tangle
Version:
Literate programming code block tangling / codegen utility, inspired by org-mode & noweb
69 lines (66 loc) • 1.4 kB
JavaScript
import {
ARG_DRY_RUN,
ARG_VERBOSE,
THING_HEADER,
cliApp,
configureLogLevel,
flag
} from "@thi.ng/args";
import { readJSON, writeText } from "@thi.ng/file-io";
import { join } from "node:path";
import { tangleFile } from "./tangle.js";
const PKG = readJSON(join(process.argv[2], "package.json"));
const APP_NAME = PKG.name.split("/")[1];
const HEADER = THING_HEADER(
PKG.name,
PKG.version,
"Literate programming code block tangling"
);
const CMD = {
desc: "",
opts: {},
inputs: [1, Infinity],
fn: async ({ opts, inputs, logger }) => {
let ctx = {
logger,
opts: {
comments: opts.noComments !== true
}
};
for (const file of inputs) {
ctx = tangleFile(file, ctx);
}
for (const out in ctx.outputs) {
writeText(out, ctx.outputs[out], ctx.logger, opts.dryRun);
}
}
};
cliApp({
name: "tangle",
start: 3,
opts: {
...ARG_DRY_RUN,
...ARG_VERBOSE,
noComments: flag({ default: false, desc: "don't generate comments" })
},
commands: { CMD },
single: true,
usage: {
lineWidth: process.stdout.columns,
prefix: `${HEADER}
usage: ${APP_NAME} [OPTS] SOURCE-FILES(S) ...
${APP_NAME} --help
`,
showGroupNames: true,
paramWidth: 20
},
ctx: async (ctx) => {
configureLogLevel(ctx.logger, ctx.opts.verbose);
return ctx;
}
});
export {
APP_NAME,
HEADER,
PKG
};