Command line tool for generating djenterator templates instead of using the web interface.

main
Zed A. Shaw 2 years ago
parent 81bcff0441
commit 5119e0be0e
  1. 4
      api/devtools/djenterator.js
  2. 60
      commands/djent.js
  3. 46
      static/djenterator/command.js
  4. 1
      static/djenterator/command.js.vars

@ -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);

@ -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 <string>", "Save to file rather than stdout."],
["--template <name>", "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);
}

@ -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 <string>", "A string option with a default.", "default.json"],
["--min <number>", "A number option, no default."],
]
// put required options in the required variable
export const required = [
["--output <string>", "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);
}
Loading…
Cancel
Save