Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt-newer with Grunt-uglify and Bower

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.

like image 516
Jason Avatar asked Oct 31 '22 17:10

Jason


1 Answers

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());
    });
});
like image 152
Dror Bereznitsky Avatar answered Nov 16 '22 15:11

Dror Bereznitsky