import { createQuestLayout, createGangLayout, QuestLayoutOptions, GangLayoutType } from '../src'; import { convertSvgsToPdf } from './svg-to-pdf'; import yargs from 'yargs'; import csvToJson from 'csvtojson'; import { promises } from 'fs'; import Path from 'path'; import combinePdfs from 'combine-pdfs'; const { readFile, writeFile, unlink } = promises; // @ts-expect-error yargs.usage does exist, TS is wrong const argv = yargs.usage('Usage: yarn generate:pdfs ').demandCommand(1).parse(); async function main() { const csvPath = argv._[0]; const json = await csvToJson().fromFile(csvPath); console.log('Generating SVG labels...'); const svgs = json.map(toQuestLayoutOptions).map(createQuestLayout); console.log('Generating SVG pages...'); const layouts = createGangLayout({ layout: GangLayoutType.AveryPresta61520, svgs }); // for debugging // await writeSvgLayouts(layouts); console.log('Generating PDF...'); const files = await convertSvgsToPdf({ svgs: layouts }); // combine PDfs into a single output const pdfs = await Promise.all(files.map((file) => readFile(file))); const combined = await combinePdfs(pdfs); const parsedFile = Path.parse(csvPath); const filename = `${parsedFile.name}.pdf`; await writeFile(Path.resolve(__dirname, '../pdfs', filename), combined); // delete individual files await Promise.all(files.map(unlink)); console.log(`Generated ${files.length} pages.`); } export async function writeSvgLayouts(layouts: string[]): Promise { await Promise.all( layouts.map(async (layout, i) => { await writeFile(Path.resolve(__dirname, `../pdfs/svg-layout-${i}.svg`), layout); }) ); } // CSV data is expected to have these headers interface CsvRecord { Manufacturer: string; Model: string; 'Serial Number': string; 'Coast ID': string; 'Acquired Year': string; 'Asset Details URL': string; 'Service Request URL': string; } function toQuestLayoutOptions(item: CsvRecord): QuestLayoutOptions { const coastId = item['Coast ID'] ?? ''; return { width: 375, assetUrl: item['Asset Details URL'], serviceRequestUrl: item['Service Request URL'], manufacturer: item['Manufacturer'], model: item['Model'], serialNumber: item['Serial Number'], coastId: coastId.replace(/,/g, ''), yearAcquired: item['Acquired Year'], }; } main();