From da889cfd5324b006a951ac39e4b28f628322c2f0 Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Wed, 22 Apr 2015 03:11:45 +0100 Subject: [PATCH 1/2] Async refactoring and cleanup --- README.md | 1 + cli/domain/local.js | 17 +++++------ cli/domain/registry.js | 30 +++++-------------- cli/facade/dev.js | 15 +++++----- cli/facade/info.js | 8 +++-- cli/facade/ls.js | 1 - package.json | 3 +- registry/domain/components-cache.js | 45 +++++++++++++++++++---------- registry/domain/s3.js | 9 +++--- utils/request.js | 2 +- 10 files changed, 67 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index f2621efb7..6e0b0611e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/cli/domain/local.js b/cli/domain/local.js index 383884a01..cc24b3353 100644 --- a/cli/domain/local.js +++ b/cli/domain/local.js @@ -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'); @@ -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){ diff --git a/cli/domain/registry.js b/cli/domain/registry.js index 0b05ba6e9..432fdcb39 100644 --- a/cli/domain/registry.js +++ b/cli/domain/registry.js @@ -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'); @@ -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){ diff --git a/cli/facade/dev.js b/cli/facade/dev.js index ec7829d52..b5c94d3f4 100644 --- a/cli/facade/dev.js +++ b/cli/facade/dev.js @@ -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'); @@ -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(); } diff --git a/cli/facade/info.js b/cli/facade/info.js index 18327fb3c..e35c14581 100644 --- a/cli/facade/info.js +++ b/cli/facade/info.js @@ -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'); @@ -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, diff --git a/cli/facade/ls.js b/cli/facade/ls.js index a53080928..75bf6bc89 100644 --- a/cli/facade/ls.js +++ b/cli/facade/ls.js @@ -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'); diff --git a/package.json b/package.json index c4b68d56f..736ea0067 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ }, "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", @@ -56,7 +56,6 @@ "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", diff --git a/registry/domain/components-cache.js b/registry/domain/components-cache.js index 3b7ed3888..7637da13e 100644 --- a/registry/domain/components-cache.js +++ b/registry/domain/components-cache.js @@ -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'); @@ -44,6 +44,19 @@ module.exports = function(conf, cdn){ return cb(err); } + async.map(components, getVersionsForComponent, function(errors, versions){ + if(!!errors){ return cb(errors); } + + _.forEach(components, function(component, i){ + componentsInfo[component] = versions[i]; + }); + + cb(null, { + lastEdit: getUnixUTCTimestamp(), + components: componentsInfo + }); + }); +/* giveMe.all(getVersionsForComponent, _.map(components, function(component){ return [component]; }), function(errors, versions){ @@ -58,7 +71,7 @@ module.exports = function(conf, cdn){ lastEdit: getUnixUTCTimestamp(), components: componentsInfo }); - }); + });*/ }); }; @@ -110,19 +123,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){ diff --git a/registry/domain/s3.js b/registry/domain/s3.js index 4b5c5a893..d3f296fe0 100644 --- a/registry/domain/s3.js +++ b/registry/domain/s3.js @@ -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'); @@ -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)); } diff --git a/utils/request.js b/utils/request.js index a78b5f1f7..f5363d41f 100644 --- a/utils/request.js +++ b/utils/request.js @@ -29,7 +29,7 @@ module.exports = function(urlPath, options, callback){ if(response.statusCode === 404){ callback('not found', null); - } else { + } else { callback(null, body); } } From c9ade758e6f2ad6a894146333241f6496f6bff83 Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Wed, 22 Apr 2015 03:13:56 +0100 Subject: [PATCH 2/2] Cleanup --- registry/domain/components-cache.js | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/registry/domain/components-cache.js b/registry/domain/components-cache.js index 7637da13e..901e85c47 100644 --- a/registry/domain/components-cache.js +++ b/registry/domain/components-cache.js @@ -56,22 +56,6 @@ module.exports = function(conf, cdn){ components: componentsInfo }); }); -/* - giveMe.all(getVersionsForComponent, _.map(components, function(component){ - return [component]; - }), function(errors, versions){ - - if(!!errors){ return cb(errors); } - - _.forEach(components, function(component, i){ - componentsInfo[component] = versions[i]; - }); - - cb(null, { - lastEdit: getUnixUTCTimestamp(), - components: componentsInfo - }); - });*/ }); };