Skip to content

Commit

Permalink
Introduced output template
Browse files Browse the repository at this point in the history
  • Loading branch information
daanlenaerts committed Dec 16, 2024
1 parent e40d0be commit ae4c519
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Options:
-f, --print-format <format> format to export
-d, --date-field <field> date field to use (default: "posting_date")
-ds, --docstatus <status> docstatus to filter on
-ot, --output-template output filename template (e.g. "{customer_name} - {name}")
--include-json also export JSON file
-h, --help display help for command
```
Expand Down
15 changes: 13 additions & 2 deletions controllers/export-to-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export interface ExportToPdfOptions {
targetDir: string;
printFormat?: string;
includeJson?: boolean;
outputTemplate?: string;
}

function generateFilename(template: string | undefined, document: any, name: string): string {
if (!template) return name;

return template.replace(/{(\w+)}/g, (match, key) => {
return document[key] || match;
});
}

export async function exportToPdf(opts: ExportToPdfOptions) {
Expand Down Expand Up @@ -50,8 +59,10 @@ export async function exportToPdf(opts: ExportToPdfOptions) {
await mkdir(targetDir, { recursive: true });
}

const baseFilename = generateFilename(opts.outputTemplate, document, opts.name);

// Save the PDF file if it doesn't exist
const pdfFilename = path.join(targetDir, `${opts.name}.pdf`);
const pdfFilename = path.join(targetDir, `${baseFilename}.pdf`);

if (await exists(pdfFilename)) {
// Stop processing if the file already exists
Expand All @@ -63,7 +74,7 @@ export async function exportToPdf(opts: ExportToPdfOptions) {

if (opts.includeJson) {
// Save the json file
const jsonFilename = path.join(targetDir, `${opts.name}.json`);
const jsonFilename = path.join(targetDir, `${baseFilename}.json`);
console.log(`Saving ${opts.doctype} ${opts.name} to ${jsonFilename}`);
await Bun.write(jsonFilename, JSON.stringify(document));
}
Expand Down
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ program.command('watch')
.option('-f, --print-format <format>', 'format to export')
.option('-d, --date-field <field>', 'date field to use', 'posting_date')
.option('-ds, --docstatus <status>', 'docstatus to filter on')
.option('-ot, --output-template <template>', 'output filename template e.g. "{customer_name} - {name}"', '{name}')
.option('--include-json', 'also export JSON file')
.action(async (doctype: string, options: any) => {
while (true) {
Expand All @@ -45,7 +46,8 @@ program.command('watch')
dateField: options.dateField,
targetDir: options.targetDir,
printFormat: options.printFormat,
includeJson: options.includeJson
outputTemplate: options.outputTemplate,
includeJson: options.includeJson,
});

if (exportedDocument !== null && (newLastTimestamp === null || exportedDocument.modified > newLastTimestamp)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "erpnext-documents-export",
"module": "index.ts",
"type": "module",
"version": "1.2.0",
"version": "1.3.0",
"devDependencies": {
"@types/bun": "latest"
},
Expand Down

0 comments on commit ae4c519

Please sign in to comment.