Permalink
Browse files

Adding control flow on multi-package installs to prevent race conditions

  • Loading branch information...
1 parent c0951bf commit 10a1e92ddaf9d5df0ff91d0508238f4a9153172c @iamdustan committed Feb 29, 2012
Showing with 34 additions and 21 deletions.
  1. +18 −20 lib/ender.js
  2. +16 −1 lib/ender.npm.js
View
38 lib/ender.js
@@ -38,32 +38,31 @@ var colors = require('colors')
console.log("-----------------------------------------")
}
- , install: function (file) {
- var location = path.join(process.cwd(), file[0], file[0].match(/\.json/) ? '' : '/package.json')
- console.log(location);
+ , install: function (file, options, callback) {
+ var json = file[0].match(/\.json/) ? '' : '/package.json'
+ , location = path.join(process.cwd(), file[0], json)
+
+ function build(_data, callback){
+ _data = (_data instanceof Array) ? _data : [_data]
+
+ // doing in series prevents NPM race conditions
+ async.forEachSeries(_data, function(data, callback) {
+ API.build(data[0], data[1], callback)
+ }, callback)
+ }
+
path.exists(location, function(exists) {
if (exists) {
- fs.readFile(location, 'utf-8', function (err, data) {
- if (err) return console.log('something went wrong trying to read file at ' + location)
- var packageJSON = JSON.parse(data)
- , ender = packageJSON.ender, args
- if (ender instanceof Array) {
- ender.forEach(function (item, ind, arr) {
- args = ENDER.npm.extractArgs(item)
- API.build.apply(this, args)
- })
- }
- else {
- args = ENDER.npm.extractArgs(ender)
- API.build.apply(this, args)
- }
- });
+ async.waterfall([
+ async.apply(fs.readFile, location, 'utf-8')
+ , async.apply(ENDER.npm.extractFromPackage)
+ , async.apply(build)
+ ], callback)
}
});
}
, build: function (packages, options, callback) {
- console.log(packages, options)
packages = options.sans ? packages : ENDER.get.special(options).concat(packages)
packages = ENDER.util.unique(packages)
@@ -271,7 +270,6 @@ module.exports.exec = function (cmd, callback) {
context = ENDER.cmd.getContext(type, args, options.context)
if (API[type]) {
- console.log(type, args, options, callback)
API[type](args, options, callback)
} else {
console.log('sorry, but the method ' + type.yellow + ' doesn\'t exist ' + ':('.cyan)
View
17 lib/ender.npm.js
@@ -6,7 +6,22 @@ var path = require('path')
, ENDER = { file: require('./ender.file') }
ENDER.npm = module.exports = {
- extractArgs: function(pack) {
+
+ extractFromPackage: function(_data, callback) {
+ var packageJSON = JSON.parse(_data)
+ , ender = packageJSON.ender, data = []
+ if (ender instanceof Array) {
+ ender.forEach(function (item, ind, arr) {
+ data.push(ENDER.npm.buildPackageArgs(item))
+ })
+ }
+ else {
+ data.push(ENDER.npm.buildPackageArgs(ender))
+ }
+ return callback(null, data)
+ }
+
+ , buildPackageArgs: function(pack) {
var deps = pack.dependencies || {}
, options = {}, packages = [], i

0 comments on commit 10a1e92

Please sign in to comment.