// node index.js --count=5 --format=html --type=paragraph // --count: 1-32 (default; 5) // --format: html, txt (default) // --type: list, paragraph (default) import fs from "fs"; import minimist from "minimist"; const select = (data) => data[Math.floor(Math.random() * data.length)]; const args = minimist(process.argv.slice(2), { "alias": { "c": "count", "f": "format", "t": "type" }, "default": { "count": "5", "format": "txt", "type": "paragraph" }, "string": [ "count", "format", "type" ] }); args.count = parseInt(args.count, 10); if (args.format !== "html" && args.format !== "txt") { throw new Error('--format option must be "html" or "txt".'); } if (args.type !== "list" && args.type !== "paragraph") { throw new Error('--type option must be "paragraph" or "list".'); } if (args.count > 32) { throw new Error("--count option must be <=32.") } const data = JSON.parse(fs.readFileSync("./zzz.json", "utf8")); let output = []; for (var i = 0; i < args.count; i++) { if (args.type === "list") { output.push(select(data)); continue; } const count = Math.floor(Math.random() * 5 + 3); const p = []; for (var j = 0; j < count; j++) { p.push(select(data)); } output.push(p.join("")); } if (args.format === "html" && args.type === "list") { output = output.map((li) => `
  • ${li}
  • `); output.unshift(""); } if (args.format === "html" && args.type === "paragraph") { output = output.map((p) => `

    ${p}

    `); } let delimiter = "\n"; if (args.type === "paragraph") { delimiter = "\n\n"; } console.log(output.join(delimiter));