2020-08-18 05:17:27 +00:00
|
|
|
import * as _ from "lodash";
|
|
|
|
import * as yargs from "yargs";
|
|
|
|
import * as scanner from "../scanner";
|
2023-01-27 04:19:45 +00:00
|
|
|
import * as handlebars from "handlebars";
|
|
|
|
import * as fs from "fs";
|
2020-08-18 05:17:27 +00:00
|
|
|
|
|
|
|
export var command = "list";
|
2023-01-27 04:19:45 +00:00
|
|
|
export var describe = "Extracts a YAML field into an ordered list";
|
2020-08-18 05:17:27 +00:00
|
|
|
|
2021-02-04 05:15:00 +00:00
|
|
|
export function builder(yargs: yargs.Arguments) {
|
2020-08-18 05:17:27 +00:00
|
|
|
return yargs
|
|
|
|
.help("help")
|
|
|
|
|
2023-01-27 04:19:45 +00:00
|
|
|
.alias("t", "template")
|
|
|
|
.default("template", "{{_number}}. {{{title}}}: {{{summary}}}")
|
2020-08-18 05:17:27 +00:00
|
|
|
|
2023-01-27 04:19:45 +00:00
|
|
|
.alias("s", "trim-whitespace")
|
|
|
|
.boolean("trim-whitspace")
|
2020-08-18 05:17:27 +00:00
|
|
|
|
|
|
|
.alias("o", "output")
|
|
|
|
.default("output", "-")
|
|
|
|
|
|
|
|
.demand(1);
|
|
|
|
}
|
|
|
|
|
2021-02-04 05:15:00 +00:00
|
|
|
export function handler(argv: any) {
|
2020-08-18 05:17:27 +00:00
|
|
|
var files = argv._.splice(1);
|
|
|
|
var data = scanner.scanFiles(argv, files);
|
|
|
|
render(argv, data);
|
|
|
|
}
|
|
|
|
|
2023-01-27 04:19:45 +00:00
|
|
|
export function render(argv, files) {
|
|
|
|
// Compile the handlebars template.
|
|
|
|
handlebars.registerHelper("eq", function (a, b) {
|
|
|
|
return a === b;
|
|
|
|
});
|
|
|
|
handlebars.registerHelper("gt", function (a, b) {
|
|
|
|
return a > b;
|
|
|
|
});
|
|
|
|
handlebars.registerHelper("gte", function (a, b) {
|
|
|
|
return a >= b;
|
|
|
|
});
|
|
|
|
handlebars.registerHelper("lt", function (a, b) {
|
|
|
|
return a < b;
|
|
|
|
});
|
|
|
|
handlebars.registerHelper("lte", function (a, b) {
|
|
|
|
return a <= b;
|
|
|
|
});
|
|
|
|
handlebars.registerHelper("ne", function (a, b) {
|
|
|
|
return a !== b;
|
|
|
|
});
|
2020-08-18 05:17:27 +00:00
|
|
|
|
2023-01-27 04:19:45 +00:00
|
|
|
const template = handlebars.compile(argv.template);
|
2020-08-18 05:17:27 +00:00
|
|
|
|
2023-01-27 04:19:45 +00:00
|
|
|
// Go through each of the files and create a row from each one.
|
|
|
|
const trim = /\s+/g;
|
|
|
|
let output: string[] = [];
|
|
|
|
|
|
|
|
for (var key in files) {
|
|
|
|
// Combine everything into a single parameters object including `_number`
|
|
|
|
// to represent the one-based index from the beginning.
|
|
|
|
const number = 1 + parseInt(key.toString());
|
|
|
|
|
|
|
|
let params = { ...files[key], _number: number };
|
|
|
|
|
|
|
|
// Render the results and write it out.
|
|
|
|
let result = template(params);
|
|
|
|
|
|
|
|
if (argv.trimWhitespace) {
|
|
|
|
result = result.replace(trim, " ").trim();
|
2020-08-18 05:17:27 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:19:45 +00:00
|
|
|
output.push(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the results as requested.
|
|
|
|
if (argv.output === "-") {
|
|
|
|
console.log(output.join("\n"));
|
|
|
|
} else {
|
|
|
|
fs.writeFileSync(argv.output, Buffer.from(output.join("\n"), "utf-8"));
|
2020-08-18 05:17:27 +00:00
|
|
|
}
|
|
|
|
}
|