holidays-jp-gen
Version:
A CLI tool to generate lightweight TypeScript holiday data modules for Japan based on official government CSV data.
27 lines (26 loc) • 1 kB
JavaScript
import fs from "node:fs";
import { Command, Option } from "commander";
import { downloadCsv, generateTs, parseCsv } from "./core.js";
const DEFAULT_START_YEAR = 1955;
export function defineCommand() {
const program = new Command();
program
.addOption(new Option("-y, --year <year>", "Start year")
.default(DEFAULT_START_YEAR)
.argParser(Number))
.option("-o, --output <path>", "Output file path");
return program;
}
export async function main(options) {
const { year, output } = options;
if (Number.isNaN(year)) {
console.error("error: option '-y, --year <year>' argument is not a number");
process.exit(1);
}
const outputPath = output || `holidays-jp-from-${year}.ts`;
console.log(`Generating a file for ${year} and beyond: ${outputPath}`);
const buffer = await downloadCsv();
const holidays = parseCsv(buffer);
fs.writeFileSync(outputPath, generateTs(holidays, year), "utf-8");
console.log("Done!");
}