From 5119e0be0e79d7813d4cedaff105f9134203aa8f Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 12 Dec 2022 18:42:02 -0500 Subject: [PATCH] Command line tool for generating djenterator templates instead of using the web interface. --- api/devtools/djenterator.js | 4 -- commands/djent.js | 60 ++++++++++++++++++++++++++++++ static/djenterator/command.js | 46 +++++++++++++++++++++++ static/djenterator/command.js.vars | 1 + 4 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 commands/djent.js create mode 100644 static/djenterator/command.js create mode 100644 static/djenterator/command.js.vars diff --git a/api/devtools/djenterator.js b/api/devtools/djenterator.js index 25da6e5..affa132 100644 --- a/api/devtools/djenterator.js +++ b/api/devtools/djenterator.js @@ -1,11 +1,7 @@ -import logging from '../../lib/logging.js'; import { API } from '../../lib/api.js'; import glob from "fast-glob"; import path from "path"; -const log = logging.create("/api/devtools/djenterator.js"); - - export const get = async (req, res) => { const api = new API(req, res); diff --git a/commands/djent.js b/commands/djent.js new file mode 100644 index 0000000..d58946d --- /dev/null +++ b/commands/djent.js @@ -0,0 +1,60 @@ +import fs from "fs"; +import assert from "assert"; +import logging from '../lib/logging.js'; +import glob from "fast-glob"; +import path from "path"; +import template from "lodash/template.js"; + +const log = logging.create(import.meta.url); + +export const description = "Describe your command here." + +// your command uses the npm package commander's options format +export const options = [ + ["--list", "List the available templates to use."], + ["--force", "Overwrite the output if it exists."], + ["--output ", "Save to file rather than stdout."], + ["--template ", "Name of the template to generate. Use --list to see."] +] + +const check = (test, fail_message) => { + if(!test) { + log.error(fail_message); + process.exit(1); + } +} + +export const main = async (opts) => { + try { + if(opts.list) { + const available = glob.sync("./static/djenterator/!(*.vars|*.swp)"); + for(let name of available) { + console.log(name); + } + } else { + check(opts.template, "--template is required."); + check(opts.output, "--output is required."); + + check(fs.existsSync(opts.template), `template file ${opts.template} does not exist`); + + const in_template = fs.readFileSync(opts.template).toString(); + + const vars_file = `${opts.template}.vars`; + const in_vars = fs.existsSync(vars_file) ? fs.readFileSync(vars_file) : {}; + + const renderer = template(in_template); + const result = renderer(JSON.parse(in_vars)); + + if(!opts.force) { + check(!fs.existsSync(opts.output), `output file ${opts.output} exists already won't overwrite.`); + } + + fs.writeFileSync(opts.output, result); + } + } catch(error) { + log.error(error); + process.exit(1); + } + + process.exit(0); +} diff --git a/static/djenterator/command.js b/static/djenterator/command.js new file mode 100644 index 0000000..bd2650a --- /dev/null +++ b/static/djenterator/command.js @@ -0,0 +1,46 @@ +// you may not need all of these but they come up a lot +import fs from "fs"; +import assert from "assert"; +import logging from '../lib/logging.js'; +import glob from "fast-glob"; +import path from "path"; +import template from "lodash/template.js"; + +const log = logging.create(import.meta.url); + +export const description = "Describe your command here." + +// your command uses the npm package commander's options format +export const options = [ + ["--input ", "A string option with a default.", "default.json"], + ["--min ", "A number option, no default."], +] + +// put required options in the required variable +export const required = [ + ["--output ", "Save to file rather than stdout."], +] + +// handy function for checking things are good and aborting +const check = (test, fail_message) => { + if(!test) { + log.error(fail_message); + process.exit(1); + } +} + +export const main = async (opts) => { + // if they give an numeric option this is how you can convert it + // yes, this is annoying and commander should handle it but oh well + if(opts.min !== undefined) { + // commander doesn't do integer conversions sadly + opts.min = parseInt(opts.min, 10); + check(!isNaN(opts.min), "--min must be a number"); + } + + // it's easier to debug options with console + console.log("OPTIONS", opts); + + // due to how async/await works it's just easier to manually exit with exit codes + process.exit(0); +} diff --git a/static/djenterator/command.js.vars b/static/djenterator/command.js.vars new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/static/djenterator/command.js.vars @@ -0,0 +1 @@ +{}