nwinkler
nwinkler

Reputation: 54467

Storing state in a Grunt task

I'm facing the following interesting problem:

Based on the above I would need to find a way to track the changed files from the first invocation, so I can revert the changes in the second step. I can't use a pattern for the second step to avoid false positives.

Example:

  1. Start a release - many steps as part of this, compile, uglify/minify, bump version number, create a git release branch.
  2. Replace the version numbers of some dependencies, e.g. in bower.json - change them from #develop to #1.2.3
  3. Some more steps, including Git commits
  4. Replace the version numbers changed above from #1.2.3 back to #develop
  5. Additional steps to clean up the release

The last step is difficult since I don't want to change any occurrences of #1.2.3 that I did not update in the previous step. For this I would need to store the list of changed files somewhere, either in memory, or in a temporary file in the project root, e.g. something like .grunt-replace. After reading the file in the last step, it could be deleted by the plugin.

Is anybody using a Grunt plugin like this? How would you solve this? Does Grunt have functionality that would support something like the above, or should I just use the Node.js file API?

Are there other patterns I should consider for keeping some kind of state between Grunt invocations?

Upvotes: 2

Views: 67

Answers (1)

LifeQuery
LifeQuery

Reputation: 3282

You can use a file watcher to keep the grunt process alive and then keep track of state using events.

For instance:

var files = [];


module.exports = function(grunt) {

  grunt.initConfig({
    watch: {
      files: ['orig.js'],
      tasks: ['my-task']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-watch');


  grunt.registerTask('my-task', function(){
    // Execute some task
  });

  grunt.event.on('watch', function(action, filepath, project) {
    //add file
    files.push(filepath);

  });

  grunt.registerTask('default', ['watch']);

};

You can then keep track of which files have been changed and act accordingly.

I have to add though, that this is a bit of a hack. Your build flow shouldn't be stateful, you need to be able to build from scratch.

Upvotes: 1

Related Questions