I've got a project using Grunt and Bower. Grunt-uglify will concatenate/minify files from the Bower directory to the deploy/scripts.js
folder. I'm using Grunt-newer so it will only update deploy/scripts.js
if new files are added or changed. Everything works great...except...
When I add a new library with Bower, file date reflects when the file was uploaded to the Bower library (or whoever is hosting it), not the date it was create on my computer. Thus, Grunt-newer sees the new Bower libraries are being older than deploy/scripts.js
and does not update the file.
One--cumbersome--solution is to open the new library .js file, and resave it. It modifies the file dates and thus, grunt-newer will create the deploy/script.js
file. However, the usefulness of Bower seems moot with such an awkward solution.
You can use Bower hooks in order to manipulate the files modification time. This is kind of a hack but can achieve what you are looking for.
You will need to register a postinstall hook and pass the list of updated components as an argument. When the script is called, the % will be replaced with a space-separated list of components being installed or uninstalled.
The hooks should be registered in the .bowerrc
file:
{
"scripts": {
"postinstall": "hook.sh %"
}
}
Then you will need a script which iterates over the components and changes the modification time of the files.
For example a shell script:
#!/bin/bash
for var in "$@"
do
find "./bower_components/$var" -exec touch {} \;
done
Here is another example of a node.js script for the same purpose:
var fs = require('fs');
var path = require('path')
var components = process.argv.slice(2)
components.forEach(function (comp) {
var comp_path = path.join(process.cwd(),"bower_components",comp);
var files = fs.readdirSync(comp_path);
files.forEach(function(file) {
fs.utimesSync(path.join(comp_path, file), new Date(), new Date());
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With