Reputation: 54467
I'm facing the following interesting problem:
#develop
in some files.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:
bower.json
- change them from #develop
to #1.2.3
#1.2.3
back to #develop
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
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