visual-studio-codepostscriptvscode-tasks

How to automatically update a PDF viewed on the right whenever the corresponding EPS is changed on the left?


I am using Visual Studio Code to learn PostScript language.

enter image description here

I use the following task.json to convert EPS to PDF.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "EPS",
            "command": "ps2pdf",
            "args": [
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

And my EPS is

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 10 10 300 300

%%BeginProlog
/inch {72 mul} def
/cm {2.54 div inch} def
%%EndProlog


newpath
    1 cm 1 cm moveto
    9 cm 0 cm rlineto
    0 cm  9 cm rlineto
    -9 cm 0 cm rlineto
    9 cm 9 cm rlineto
closepath
5 setlinewidth
stroke
showpage
%%EOF

Question

How to automatically update a PDF viewed on the right whenever the corresponding EPS is changed on the left?


Solution

  • I'm sure you've figured something out in the last 2 years-- but for anyone else who stumbles on this like I did, here is a snippet of how I did it with TypeScript for my résumé written in LaTeX-- I'm sure one could apply this to PostScript:

    package.json:

    {
      "name": "resume",
      "version": "1.0.0",
      "scripts": {
        "watch": "ts-node watch.ts",
        "generate": "pdflatex -jobname=resume resume.tex"
      },
      "devDependencies": {
        "@types/node": "^20.5.1",
        "chokidar": "^3.5.3",
        "ts-node": "^10.9.1"
      }
    }
    

    watch.ts:

    import { watch } from "chokidar";
    import { exec } from "child_process";
    
    // Files to watch for change
    const watchFiles: string[] = [
      'resume.tex',
      '_header.tex',
      'TLCresume.sty',
      'sections/**/*.tex',
    ]
    
    // Watch for changes in the files specified above
    const watcher = watch(watchFiles)
    
    function buildPDF() {
      // Execute the 'generate' script in package.json
      // This will run the 'pdflatex' command to generate the PDF
      exec('npm run generate', (err, stdout, stderr) => {
        if (err) {
          // node couldn't execute the command
          return;
        }
    
        console.log("PDF generated!")
      });
    }
    
    // Build PDF once at the start
    buildPDF();
    let lastBuildTime: number = Date.now();
    
    watcher.on('all', (event, path) => {
      if (Date.now() - lastBuildTime < 1000) {
        // Don't build PDF if it was just built less than a second ago
        return;
      } else {
        console.log(`${event} @ ${path}`);
      }
    
      buildPDF();
      lastBuildTime = Date.now();
    });
    
    

    Then:

    1. Install the dependencies in package.json using npm with npm install.
    2. Change the "generate" script in package.json to whatever command is appropriate for generating your PostScript PDF.
    3. Change the watchFiles array to include paths/RegEx for your PostScript files.
    4. Finally, run npm run watch to refresh the PDF in VSCode whenever you save your file(s)!