Skip to content

Commit

Permalink
Synchronous downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed May 5, 2016
1 parent 652cad6 commit 4464b2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
51 changes: 35 additions & 16 deletions app/index.js
Expand Up @@ -51,6 +51,25 @@ module.exports = yo.generators.Base.extend({
initializing: function() {
console.log(chalk.yellow.bold('[ Initializing ]'));

/* -- Process data in an array synchronously, moving onto the n+1 item only after the nth item callback */

this.synchronousLoop = function(data, processData, loopDone) {
if (data.length > 0) {
var loop = function(data, i, processData, loopDone) {
processData(data[i], i, function() {
if (++i < data.length) {
loop(data, i, processData, loopDone);
} else {
loopDone();
}
});
};
loop(data, 0, processData, loopDone);
} else {
loopDone();
}
};

/* -- Set up the download command */

this.download = function(url, cb) {
Expand Down Expand Up @@ -139,22 +158,22 @@ module.exports = yo.generators.Base.extend({
/* -- Download files */

console.log(chalk.green('> Downloading files'));
var downloadCount = this.install.DOWNLOAD_FILES.length;
var downloadProgress = [];
for (var i = 0; i < downloadCount; i++) {
var done = this.async();
var download = this.install.DOWNLOAD_FILES[i];
console.log('+ ' + chalk.green(download.name) + ' downloading');
downloadProgress[i] = new pleasant();
downloadProgress[i].start('Working');
this.download(download.url, function() {
downloadProgress[(downloadCount - 1)].stop();
downloadCount--;
if (downloadCount === 0) {
done();
}
});
}
var done = this.async();
var _this = this;
this.synchronousLoop(this.install.DOWNLOAD_FILES,
function(element, i, callback){
console.log('+ ' + chalk.green(element.name) + ' downloading');
var progress = new pleasant();
progress.start('Working');
_this.download(element.url, function() {
progress.stop();
callback();
});
},
function(){
done();
});

},

/* -- writing -- Where you write the generator specific files (routes, controllers, etc) */
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "generator-craftinstall",
"version": "1.1.2",
"version": "1.1.3",
"description": "generator-craftinstall is a Yeoman generator for Craft CMS installs",
"main": "app/index.js",
"files": [
Expand Down

0 comments on commit 4464b2a

Please sign in to comment.