Repository: fraserxu/electron-pdf (js)

Issue:
Support a fileSuffixFn option
```
[options.fileSuffixFn] If provided, this function is invoked
with a numeric index (1 ... options.input.length) allowing the caller to manipulate
the file suffix in a custom way. (e.g. instead output_1.pdf, you could generate output_00001.pdf) 
```

Pull request:
Fixes #283 Added options.fileSuffixFn for ExportJob
Will be cherry-picked onto 4.x branch

Patch:
diff --git a/lib/exportJob.js b/lib/exportJob.js
index 922e223..e584eb9 100644
--- a/lib/exportJob.js
+++ b/lib/exportJob.js
@@ -98,6 +98,10 @@ class ExportJob extends EventEmitter {
    * @param {Boolean} [options.reportRendererStats=false] Pass true to collect and
    * emit a `renderer-process-stats` event at key lifecycle stages (window open, page loaded, pdf generated)
    *
+   * @param {Function} [options.fileSuffixFn] If provided, this function is invoked
+   * with a numeric index (1 ... options.input.length) allowing the caller to manipulate
+   * the file suffix in a custom way. (e.g. instead output_1.pdf, you could generate output_00001.pdf)
+   *
    * @fires ExportJob#window.capture.end after each resource is captured (use
    *   this with inMemory)
    * @fires ExportJob#export-complete after each resource is available in
@@ -611,7 +615,7 @@ class ExportJob extends EventEmitter {
       // this.debug(`ready event received. this.jobId:${this.jobId}, event job id:${jobId}`)
       if (this.jobId === jobId) {
         this._triggerProcessStats('window.event.wait.end').then(() => {
-          this.emit('window.event.wait.end', {eventName})
+          this.emit('window.event.wait.end', { eventName })
           if (this.readyEventObserver) {
             this._triggerReadyEventObserver(customEventDetail, generateFunction)
           } else {
@@ -839,7 +843,11 @@ class ExportJob extends EventEmitter {
     const extIndex = this.output.lastIndexOf(ext)
     const actualExt = context.type === 'png' ? '.png' : ext
     if (_.size(this.input) > 1) {
-      return this.output.substring(0, extIndex) + `_${zeroBasedIndex + 1}${actualExt}`
+      let suffix = `_${zeroBasedIndex + 1}`
+      if (this.options.fileSuffixFn) {
+        suffix = this.options.fileSuffixFn(zeroBasedIndex + 1)
+      }
+      return this.output.substring(0, extIndex) + `${suffix}${actualExt}`
     }
     return this.output.substring(0, extIndex) + actualExt
   }
diff --git a/package.json b/package.json
index caa110c..669cfaf 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "electron-pdf",
-  "version": "10.0.1",
+  "version": "10.0.2",
   "description": "A command line tool to generate PDF from URL, HTML or Markdown files",
   "main": "lib/index.js",
   "scripts": {


Test patch:
diff --git a/test/exportJob-test.js b/test/exportJob-test.js
index a285577..1f554c1 100644
--- a/test/exportJob-test.js
+++ b/test/exportJob-test.js
@@ -70,19 +70,27 @@ test('getTargetFile', t => {
 })
 
 test('getTargetFile_multiple_inputs', t => {
-  job = new ExportJob(['input1', 'input2'], 'output.pdf', options)
+  job = new ExportJob(['input1', 'input2'], 'output.pdf', args, options)
   const fileName = job._getTargetFile({ inputIndex: 1 })
   t.is(fileName, 'output_2.pdf')
 })
 
 test('getTargetFile_png_override', t => {
-  job = new ExportJob(['input1', 'input2'], 'output.pdf', options)
+  job = new ExportJob(['input1', 'input2'], 'output.pdf', args, options)
   const fileName = job._getTargetFile({ inputIndex: 1, type: 'png' })
   t.is(fileName, 'output_2.png')
 })
 
+test('getTargetFile_fileSuffixFn', t => {
+  job = new ExportJob(['input1', 'input2'], 'output.pdf', args, _.extend({
+    fileSuffixFn: index => `_0000${index}`
+  }, options))
+  const fileName = job._getTargetFile({ inputIndex: 1 })
+  t.is(fileName, 'output_00002.pdf')
+})
+
 test('getTargetFile_png_override_single', t => {
-  job = new ExportJob(['input1'], 'output.pdf', options)
+  job = new ExportJob(['input1'], 'output.pdf', args, options)
   const fileName = job._getTargetFile({ inputIndex: 1, type: 'png' })
   t.is(fileName, 'output.png')
 })