0
votes

I use grunt-contrib-pug to compile my .pug files from src/ and distribute the corresponding .html files to dist/. Here is my pug-task config (written in .coffee):

compile:
    options: pretty: false
    files: [ {
        expand: true
        cwd: 'src/'
        src: [ '**/*.pug', '!includes/**' ]
        dest: 'dist/'
        ext: '.html'
    } ]

When I delete a .pug file from src/, is there any way to synchronize delete corresponding html files in dist/? I know you can use grunt-contrib-clean followed by compiling the pug files again, but this is not time efficient when working with a large codebase.

1
grunt-delete-sync looks like it will do what you want: github.com/taylorcode/grunt-delete-sync maybe feed it with a grunt watch if it doesn't run continuously. Otherwise try: stackoverflow.com/a/18699787/3656963 with a grunt watch on src and alter fileToDelete as you need. - Revive
Thank you! The second suggestion worked well after altering the code to suit my project. - Audun Olsen

1 Answers

1
votes

As referenced by I-LOVE-2-REVIVE, I looked further into Grunt's file API, and on that basis, this is the solution I came up with:

grunt.event.on 'watch', (action, filepath, target) ->
  if action == 'deleted' && /pug/.test(filepath)

     file = 'dist' + filepath.slice(3, -3) + 'html'
     grunt.file.delete file
     # Log deleted files
     grunt.log.write '\n' + filepath + ' deleted > ' + file + ' deleted.\n'

It works great!