gmi-web/cli.js

131 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-01-28 06:16:46 +00:00
#!/usr/bin/env node
2021-03-29 01:43:20 +00:00
import { readFileSync } from "fs";
import path from "path";
import fs from "vinyl-fs";
import yargs from "yargs";
2021-02-15 20:56:41 +00:00
import * as CSS from "./css.js";
2021-02-15 22:31:08 +00:00
import { streamHTML, toHTML } from "./html.js";
2021-02-25 20:44:09 +00:00
2021-02-25 20:46:53 +00:00
const internal = (file) =>
2021-02-25 20:44:09 +00:00
path.resolve(path.dirname(new URL(import.meta.url).pathname), file);
const pkg = JSON.parse(readFileSync(internal("package.json"), "utf-8"));
const cli = yargs(process.argv.slice(2))
.config("config", function (path) {
2021-02-15 20:56:41 +00:00
return JSON.parse(readFileSync(path), "utf-8");
})
.scriptName("gmi-web")
2021-02-25 18:17:38 +00:00
.command("$0 [files..]", pkg.description, (yargs) =>
yargs
2021-02-25 17:55:48 +00:00
.example("$0 --body < doc.gmi")
.example("$0 --html en $(find ~/my-capsule -name '*.gmi')")
2021-02-25 18:17:38 +00:00
.example("$0 --foreground '#9EEBCF' --html en < doc.gmi")
2021-02-11 23:49:39 +00:00
.example("$0 --image jpg --audio mp3 --image png --body < doc.gmi")
2021-02-16 22:16:11 +00:00
.epilog("See the gmi-web(1) man page for more information.")
)
.options({
2021-02-11 23:49:39 +00:00
body: {
type: "boolean",
2021-02-19 19:52:02 +00:00
group: "Core:",
2021-02-11 23:49:39 +00:00
},
html: {
type: "string",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Core:",
},
css: {
requiresArg: true,
group: "Core:",
},
2021-02-15 17:17:16 +00:00
dir: {
type: "string",
2021-02-19 19:32:28 +00:00
hidden: true,
2021-02-15 17:17:16 +00:00
choices: ["rtl", "ltr"],
default: "ltr",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "HTML:",
2021-02-15 17:17:16 +00:00
},
author: {
2021-02-12 01:59:02 +00:00
type: "string",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "HTML:",
2021-02-12 01:59:02 +00:00
},
2021-02-24 20:23:28 +00:00
description: {
2021-02-19 19:52:02 +00:00
coerce: (arg) => (typeof arg === "number" ? arg : arg ? Infinity : 0),
group: "HTML:",
},
2021-02-19 19:52:02 +00:00
charset: {
type: "string",
2021-02-19 19:32:28 +00:00
hidden: true,
2021-02-19 19:52:02 +00:00
default: "utf-8",
requiresArg: true,
group: "HTML:",
},
2021-02-15 20:56:41 +00:00
image: {
type: "array",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Media:",
2021-02-15 20:56:41 +00:00
},
audio: {
type: "array",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Media:",
2021-02-15 20:56:41 +00:00
},
video: {
type: "array",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Media:",
2021-02-15 20:56:41 +00:00
},
2021-02-19 19:52:02 +00:00
inline: {
type: "boolean",
2021-02-15 22:31:08 +00:00
hidden: true,
2021-02-19 19:52:02 +00:00
group: "CSS:",
2021-02-15 22:31:08 +00:00
},
2021-02-19 19:52:02 +00:00
});
2021-02-16 20:41:27 +00:00
2021-02-24 18:15:32 +00:00
const css =
2021-02-25 18:17:38 +00:00
yargs(process.argv.slice(2))
.version(false)
.help(false)
.exitProcess(false)
.parse().css || "gmi-web.css";
2021-02-22 00:26:56 +00:00
const CSS_VARS = CSS.rootVariables(CSS.load({ css }));
2021-02-20 21:37:35 +00:00
Object.keys(CSS_VARS).forEach((key) => {
2021-02-25 17:11:25 +00:00
cli.option(key, { default: CSS_VARS[key] });
cli.conflicts(key, "core");
cli.conflicts(key, "none");
});
2021-02-25 17:19:27 +00:00
cli.group(Object.keys(CSS_VARS), `${css}:`);
2021-01-29 22:32:30 +00:00
const argv = cli
.conflicts("author", "body")
.conflicts("html", "body")
.alias("html", "language")
.alias("html", "lang")
.showHelpOnFail(true)
2021-02-25 18:17:38 +00:00
.version(pkg.version)
2021-01-28 06:16:46 +00:00
.help().argv;
2021-02-15 20:56:41 +00:00
if (!argv.html && !argv.body) {
cli.showHelp();
console.error(`\nMissing required argument: --html or --body`);
cli.exit(1);
}
if (!argv.files) {
let gemtext;
try {
2021-02-15 20:56:41 +00:00
gemtext = readFileSync(process.stdin.fd, "utf-8");
} catch (e) {
2021-02-12 01:59:02 +00:00
cli.showHelp();
console.error("\nMissing files: pipe from stdin or provide [files..]");
cli.exit(1);
}
2021-02-15 22:31:08 +00:00
console.log(toHTML(gemtext, argv));
} else {
fs.src(argv.files)
2021-02-15 22:31:08 +00:00
.pipe(streamHTML(argv))
.pipe(fs.dest((file) => path.dirname(file.path)));
}