Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async refactoring #59

Merged
merged 2 commits into from
Apr 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,6 @@ Maintainer:
* [@matteofigus](https://github.com/matteofigus)

Contributors:
* [@andyroyle](https://github.com/andyroyle)
* [@federicomaffei](https://github.com/federicomaffei)
* [@jankowiakmaria](https://github.com/jankowiakmaria)
17 changes: 8 additions & 9 deletions cli/domain/local.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var async = require('async');
var CleanCss = require('clean-css');
var format = require('stringformat');
var fs = require('fs-extra');
var giveMe = require('give-me');
var handlebars = require('handlebars');
var hashBuilder = require('../../utils/hash-builder');
var jade = require('jade');
Expand Down Expand Up @@ -350,15 +350,14 @@ module.exports = function(){
if(component.oc.files.static.length === 0){
return callback(null, component);
}

giveMe.sequence(copyDir, _.map(component.oc.files.static, function(staticComponent){
return [staticComponent, path.join(componentPath, staticComponent)];
}), function(errors, dirs){
if(errors && errors.length > 0){
return callback(_.compact(errors)[0]);
} else {
callback(null, component);
async.eachSeries(component.oc.files.static, function(staticDir, cb){
copyDir(staticDir, path.join(componentPath, staticDir), cb);
}, function(errors){
if(errors){
return callback(errors);
}

callback(null, component);
});
},
unlink: function(componentName, callback){
Expand Down
30 changes: 8 additions & 22 deletions cli/domain/registry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var async = require('async');
var fs = require('fs-extra');
var giveMe = require('give-me');
var put = require('../../utils/put');
var request = require('../../utils/request');
var settings = require('../../resources/settings');
Expand Down Expand Up @@ -85,27 +85,13 @@ module.exports = function(){
return callback('no components found in oc registry', null);
}

var infoRoutes = _.map(components, function(component){
return [component + settings.registry.componentInfoPath];
});

giveMe.all(request, infoRoutes, function(callbacks){

var errors = _.compact(_.map(callbacks, function(callbackItem){
return callbackItem[0];
}));

if(errors && errors.length === 0){
errors = null;
}

callback(errors, _.map(callbacks, function(callbackItem, i){
return _.extend(JSON.parse(callbackItem[1]), {
href: components[i]
});
}));
});

async.map(components, function(component, cb){
request(component + settings.registry.componentInfoPath, function(err, res){
cb(err, _.extend(JSON.parse(res), {
href: component
}));
});
}, callback);
});
},
putComponent: function(options, callback){
Expand Down
15 changes: 8 additions & 7 deletions cli/facade/dev.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var async = require('async');
var colors = require('colors');
var format = require('stringformat');
var giveMe = require('give-me');
var path = require('path');
var oc = require('../../index');
var strings = require('../../resources/index');
Expand All @@ -25,25 +25,26 @@ module.exports = function(dependencies){
if(!packaging){
packaging = true;
logger.log('Packaging components...'.yellow);
giveMe.all(local.package, _.map(componentsDirs, function(dir){
return [dir, false];
}), function(errors, results){

async.each(componentsDirs, function(dir, cb){
local.package(dir, false, cb);
}, function(errors){
if(!!errors){
_.forEach(errors, function(error, i){
if(!!error){
var errorDescription = (error instanceof SyntaxError) ? error.message : error;
logger.log(format('An error happened when packaging {0}: {1}', componentsDirs[i], errorDescription.red));
logger.log(format('An error happened while packaging {0}: {1}', componentsDirs[i], errorDescription.red));
}
});
logger.log('retrying in 10 seconds...'.yellow);
setTimeout(function() {
setTimeout(function(){
packaging = false;
packageComponents(componentsDirs);
}, 10000);
} else {

logger.log('complete'.green);
if(typeof(callback) === 'function'){
if(_.isFunction(callback)){
callback();
}

Expand Down
8 changes: 6 additions & 2 deletions cli/facade/info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var async = require('async');
var colors = require('colors');
var giveMe = require('give-me');
var strings = require('../../resources/index');
var Table = require('cli-table');
var _ = require('underscore');
Expand Down Expand Up @@ -30,7 +30,11 @@ module.exports = function(dependencies){
return [res.registries[0] + '/' + componentName + '/' + componentVersion];
});

giveMe.all(registry.getApiComponentByHref, componentUrls, function(callbacks){
async.map(componentUrls, function(componentUrl, cb){
registry.getApiComponentByHref(componentUrl, function(errInfo, resInfo){
cb(null, [errInfo, resInfo]);
});
}, function(poo, callbacks){

var rows = [],
margin = 2,
Expand Down
1 change: 0 additions & 1 deletion cli/facade/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var colors = require('colors');
var format = require('stringformat');
var giveMe = require('give-me');
var strings = require('../../resources/index');
var Table = require('cli-table');
var _ = require('underscore');
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@
},
"dependencies": {
"accept-language-parser": "1.0.2",
"async": "^0.9.0",
"async": "0.9.0",
"aws-sdk": "2.1.20",
"clean-css": "3.1.9",
"cli-table": "0.3.1",
"colors": "1.0.3",
"express": "3.18.3",
"form-data": "0.1.4",
"fs-extra": "0.12.0",
"give-me": "0.1.0",
"handlebars": "3.0.1",
"jade": "1.9.1",
"multer": "0.1.4",
Expand Down
35 changes: 17 additions & 18 deletions registry/domain/components-cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var async = require('async');
var getUnixUTCTimestamp = require('../../utils/get-unix-utc-timestamp');
var giveMe = require('give-me');
var semver = require('semver');
var _ = require('underscore');

Expand Down Expand Up @@ -44,10 +44,7 @@ module.exports = function(conf, cdn){
return cb(err);
}

giveMe.all(getVersionsForComponent, _.map(components, function(component){
return [component];
}), function(errors, versions){

async.map(components, getVersionsForComponent, function(errors, versions){
if(!!errors){ return cb(errors); }

_.forEach(components, function(component, i){
Expand Down Expand Up @@ -110,19 +107,21 @@ module.exports = function(conf, cdn){
load: function(eventsHandler, callback){
_eventsHandler = eventsHandler;

giveMe.all([getFromJson, getFromDirectories], function(errors, components){
if(!!errors && !!errors[1]){
return returnError('components_list_get', errors[1], callback);
} else if((!!errors && !!errors[0]) || !_.isEqual(components[0].components, components[1].components)){
saveData(components[1], function(saveErr, saveResult){
if(!!saveErr){
return returnError('components_list_save', saveErr, callback);
}
cacheDataAndStartRefresh(components[1], callback);
});
} else {
cacheDataAndStartRefresh(components[0], callback);
}
getFromJson(function(jsonErr, jsonComponents){
getFromDirectories(function(dirErr, dirComponents){
if(!!dirErr){
return returnError('components_list_get', dirErr, callback);
} else if(jsonErr || !_.isEqual(dirComponents.components, jsonComponents.components)){
saveData(dirComponents, function(saveErr, saveResult){
if(!!saveErr){
return returnError('components_list_save', saveErr, callback);
}
cacheDataAndStartRefresh(dirComponents, callback);
});
} else {
cacheDataAndStartRefresh(jsonComponents, callback);
}
});
});
},
refresh: function(callback){
Expand Down
9 changes: 4 additions & 5 deletions registry/domain/s3.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var async = require('async');
var AWS = require('aws-sdk');
var Cache = require('nice-cache');
var format = require('stringformat');
var fs = require('fs-extra');
var getMimeType = require('../../utils/get-mime-type');
var getNextYear = require('../../utils/get-next-year');
var giveMe = require('give-me');
var nodeDir = require('node-dir');
var path = require('path');
var strings = require('../../resources');
Expand Down Expand Up @@ -104,13 +104,12 @@ module.exports = function(conf){
nodeDir.paths(dirInput, function(err, paths) {
var files = paths.files;

giveMe.all(_.bind(self.putFile, self), _.map(files, function(file){
async.each(files, function(file, cb){
var relativeFile = file.substr(dirInput.length),
url = (dirOutput + relativeFile).replace(/\\/, '/');

return [file, url, relativeFile === '/server.js'];
}), function(errors, callbacks){

self.putFile(file, url, relativeFile === '/server.js', cb);
}, function(errors){
if(errors){
return callback(_.compact(errors));
}
Expand Down
2 changes: 1 addition & 1 deletion utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function(urlPath, options, callback){

if(response.statusCode === 404){
callback('not found', null);
} else {
} else {
callback(null, body);
}
}
Expand Down