Created
June 3, 2013 10:42
-
-
Save justlaputa/5697381 to your computer and use it in GitHub Desktop.
js library to check component version before running `bower install`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global module:false,process:false*/ | |
/** | |
* helper script for Grunt bower task | |
* this script is used to compare javascript libraries versions | |
* installed by bower, and run `bower install` only when necessary | |
*/ | |
var fs = require('fs'); | |
var bower = require('bower'); | |
var components = require('./bower.json'); | |
var _ = require('lodash'); | |
/** compare the version in `component.json` and real version | |
* `components/` directory. | |
* return true only if they are the same | |
*/ | |
function checkVersion(name, version) { | |
var match = false, | |
existVer = '', | |
dir = './components/' + name + '/'; | |
if (fs.existsSync(dir + 'component.json')) { | |
existVer = require(dir + 'component.json').version; | |
} else if (fs.existsSync(dir + 'bower.json')) { | |
existVer = require(dir + 'bower.json').version; | |
} | |
if (version === existVer ) { | |
match = true; | |
} | |
return match; | |
} | |
function filterPaths(paths) { | |
var name, | |
version, | |
newPaths = {}, | |
hashIndex; //index of the '#' in version string, like `jquery#1.9.3` | |
//if components directory does not exist, we need to run bower install | |
if (!fs.existsSync('./components') || | |
!fs.statSync('./components').isDirectory()) { | |
console.log('no bower library directory, install all'); | |
return paths; | |
} | |
for (name in paths) { | |
version = paths[name]; | |
hashIndex = version.lastIndexOf('#'); | |
if (hashIndex !== -1) { | |
version = version.substring(hashIndex + 1); | |
} | |
//install the library only if the version does not match | |
if (!checkVersion(name, version)) { | |
console.log('bower keep %s#%s', name, version); | |
newPaths[name] = paths[name]; | |
} else { | |
console.log('bower skip %s#%s', name, version); | |
} | |
} | |
return newPaths; | |
} | |
function getEndpointsFromPaths(paths) { | |
var endpoints = [], | |
name, version; | |
for (name in paths) { | |
version = paths[name]; | |
if (version.indexOf('#') !== -1) { | |
endpoints.push(version); | |
} else { | |
endpoints.push(name + '#' + version); | |
} | |
} | |
return endpoints; | |
} | |
function install(callback) { | |
var paths = _.extend({}, components.dependencies, components.devDependencies), | |
endpoints; | |
paths = filterPaths(paths); | |
if (!_.isEmpty(paths)) { | |
//see bower's api implementation of 'lib/core/manager.js' | |
endpoints = getEndpointsFromPaths(paths); | |
bower.commands.install(endpoints) | |
.on('data', function(data) { | |
if (data) { | |
process.stdout.write(data); | |
} | |
}) | |
.on('package', function(data) { | |
if (data) { | |
process.stdout.write(data); | |
} | |
}) | |
.on('warn', function(data) { | |
if (data) { | |
process.stdout.write(data + '\n'); | |
} | |
}) | |
.on('end', function(data) { | |
if (data) { | |
process.stdout.write(data); | |
} | |
if (callback) { | |
callback(); | |
} | |
}) | |
.on('error', function(err) { | |
if (err) { | |
throw err; | |
} | |
process.exit(1); | |
}); | |
} else { | |
if (callback) { | |
callback(); | |
} | |
} | |
} | |
module.exports = { | |
install: install | |
}; | |
// run install directly when executed from command line | |
if (require.main === module) { | |
install(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment