user34537
user34537

Reputation:

Auto compile *.jison file with node

I have two files with a .jison extention in a folder. Everytime I save I like to run the command jison [the_file].jison. How do I do this using node? Nodemon and gulp seem like valid solution but I have no experience with either and like to keep it simple

Upvotes: 0

Views: 178

Answers (2)

Lucas Farias
Lucas Farias

Reputation: 349

If you want to use Grunt, just install npm "grunt" "grunt-shell" "grunt-contrib-watch" Here's a sample for your Gruntfile.js

module.exports = function(grunt) {
  // Define tasks
  grunt.initConfig({
    watch:{
      scripts:{
        files: ['<path-to>/<jison-file>.jison'],
        tasks: ['shell:jison_compile'],
        options: {
          interrupt : true
        }
      }
    },
    shell: {
      jison_compile:{
        command: 'jison <path-to>/<jison-file>.jison'
      }
    },
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-shell');
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Default task(s).
  grunt.registerTask('default', ['shell:jison_compile']);

};

Then you can use either grunt to run it's default task (compile jison file) or use grunt watch to keep it waiting for changes in the specified files

Upvotes: 0

Davin Tryon
Davin Tryon

Reputation: 67296

With gulp this would be pretty straightforward. The key is to set up a watch that will trigger a task every time the file changes:

something like this should get you started:

var exec = require('gulp-exec');

gulp.task('jison', function() {
  return gulp.src(['**/*.jison'])
    .pipe(exec('jison <%= file.path %>.jison'));
});

gulp.task('watch', function() {
  gulp.watch(['**/*.jison'], ['jison']);
});

gulp.task('default', ['watch', 'jison']);

So, above we define a task named jison, watch any changes of .jison files, and set up the default task. gulp-exec is brought in to run bash commands.

Upvotes: 1

Related Questions