You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.7 KiB
60 lines
1.7 KiB
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);
|
|
}
|
|
|