From 2780b4eb2fb24f209ff1d62084e8c179e1c23001 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 8 Jul 2018 00:02:46 +0300 Subject: [PATCH] fix upload callback --- lib/index.js | 2 +- lib/modules/ipfs/upload.js | 84 ++++++++++++++++++------------------ lib/modules/storage/index.js | 2 + lib/modules/swarm/upload.js | 58 ++++++++++++------------- 4 files changed, 72 insertions(+), 74 deletions(-) diff --git a/lib/index.js b/lib/index.js index acd1b2aa5..2166c5183 100644 --- a/lib/index.js +++ b/lib/index.js @@ -330,7 +330,7 @@ class Embark { }, function deploy(callback) { engine.events.on('outputDone', function () { - embark.events.request("storage:upload", callback); + engine.events.request("storage:upload", callback); }); engine.events.on('check:backOnline:Ethereum', function () { engine.logger.info(__('Ethereum node detected') + '..'); diff --git a/lib/modules/ipfs/upload.js b/lib/modules/ipfs/upload.js index 8837ffde5..53d53e61f 100644 --- a/lib/modules/ipfs/upload.js +++ b/lib/modules/ipfs/upload.js @@ -11,51 +11,49 @@ class IPFS { this.configIpfsBin = this.storageConfig.ipfs_bin || "ipfs"; } - deploy() { - return new Promise((resolve, reject) => { - console.log("deploying to ipfs!"); - let self = this; - async.waterfall([ - function findBinary(callback) { - let ipfs_bin = shelljs.which(self.configIpfsBin); + deploy(cb) { + console.log("deploying to ipfs!"); + let self = this; + async.waterfall([ + function findBinary(callback) { + let ipfs_bin = shelljs.which(self.configIpfsBin); - if (ipfs_bin === 'ipfs not found' || !ipfs_bin) { - console.log(('=== WARNING: ' + self.configIpfsBin + ' ' + __('not found or not in the path. Guessing %s for path', '~/go/bin/ipfs')).yellow); - ipfs_bin = "~/go/bin/ipfs"; - } - - callback(null, ipfs_bin); - }, - function runCommand(ipfs_bin, callback) { - let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`; - console.log(("=== " + __("adding %s to ipfs", self.buildDir)).green); - console.debug(cmd); - shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel - console.log(stdout.green); - callback(stderr, stdout); - }); - }, - function getHashFromOutput(result, callback) { - let rows = result.split("\n"); - let dir_row = rows[rows.length - 2]; - let dir_hash = dir_row.split(" ")[1]; - - callback(null, dir_hash); - }, - function printUrls(dir_hash, callback) { - console.log(("=== " + __("DApp available at") + " http://localhost:8080/ipfs/" + dir_hash + "/").green); - console.log(("=== " + __("DApp available at") + " http://ipfs.infura.io/ipfs/" + dir_hash + "/").green); - - callback(); + if (ipfs_bin === 'ipfs not found' || !ipfs_bin) { + console.log(('=== WARNING: ' + self.configIpfsBin + ' ' + __('not found or not in the path. Guessing %s for path', '~/go/bin/ipfs')).yellow); + ipfs_bin = "~/go/bin/ipfs"; } - ], function (err, _result) { - if (err) { - console.log(__("error uploading to ipfs").red); - console.log(err); - reject(err); - } - else resolve(__('successfully uploaded to ipfs')); - }); + + callback(null, ipfs_bin); + }, + function runCommand(ipfs_bin, callback) { + let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`; + console.log(("=== " + __("adding %s to ipfs", self.buildDir)).green); + console.debug(cmd); + shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel + console.log(stdout.green); + callback(stderr, stdout); + }); + }, + function getHashFromOutput(result, callback) { + let rows = result.split("\n"); + let dir_row = rows[rows.length - 2]; + let dir_hash = dir_row.split(" ")[1]; + + callback(null, dir_hash); + }, + function printUrls(dir_hash, callback) { + console.log(("=== " + __("DApp available at") + " http://localhost:8080/ipfs/" + dir_hash + "/").green); + console.log(("=== " + __("DApp available at") + " http://ipfs.infura.io/ipfs/" + dir_hash + "/").green); + + callback(); + } + ], function (err, _result) { + if (err) { + console.log(__("error uploading to ipfs").red); + console.log(err); + cb(err); + } + else cb(__('successfully uploaded to ipfs')); }); } } diff --git a/lib/modules/storage/index.js b/lib/modules/storage/index.js index 9d654c715..a3b77b99c 100644 --- a/lib/modules/storage/index.js +++ b/lib/modules/storage/index.js @@ -22,6 +22,8 @@ class Storage { if (['swarm', 'ipfs'].indexOf(platform) === -1) { return cb({message: __('platform "{{platform}}" is specified as the upload provider, however no plugins have registered an upload command for "{{platform}}".', {platform: platform})}); } + + embark.events.request("storage:upload:" + platform, cb); }); } diff --git a/lib/modules/swarm/upload.js b/lib/modules/swarm/upload.js index 995cafb90..a64657fdf 100644 --- a/lib/modules/swarm/upload.js +++ b/lib/modules/swarm/upload.js @@ -10,41 +10,39 @@ class Swarm { this.getUrl = options.getUrl; } - deploy() { - return new Promise((resolve, reject) => { - console.log(__("deploying to swarm!")); - let self = this; - let bzz = this.bzz; - async.waterfall([ - function runCommand(callback) { - console.log(("=== " + __("adding %s to swarm", self.buildDir)).green); - bzz.upload({ - path: self.buildDir, // path to data / file / directory - kind: "directory", // could also be "file" or "data" - defaultFile: "index.html" // optional, and only for kind === "directory" - }) + deploy(cb) { + console.log(__("deploying to swarm!")); + let self = this; + let bzz = this.bzz; + async.waterfall([ + function runCommand(callback) { + console.log(("=== " + __("adding %s to swarm", self.buildDir)).green); + bzz.upload({ + path: self.buildDir, // path to data / file / directory + kind: "directory", // could also be "file" or "data" + defaultFile: "index.html" // optional, and only for kind === "directory" + }) .then((success) => { callback(null, success); }) .catch(callback); - }, - function printUrls(dir_hash, callback) { - if (!dir_hash) { - return callback('No directory hash was returned'); - } - console.log(("=== " + __("DApp available at") + ` ${self.getUrl}${dir_hash}/`).green); - console.log(("=== " + __("DApp available at") + ` http://swarm-gateways.net/bzz:/${dir_hash}`).green); + }, + function printUrls(dir_hash, callback) { + if (!dir_hash) { + return callback('No directory hash was returned'); + } + console.log(("=== " + __("DApp available at") + ` ${self.getUrl}${dir_hash}/`).green); + console.log(("=== " + __("DApp available at") + ` http://swarm-gateways.net/bzz:/${dir_hash}`).green); - callback(); - } - ], function (err, _result) { - if (err) { - console.log(__("error uploading to swarm").red); - console.log(err); - return reject(err); - } - resolve(__('successfully uploaded to swarm')); - }); + callback(); + } + ], function (err, _result) { + if (err) { + console.log(__("error uploading to swarm").red); + console.log(err); + return cb(err); + } + cb(null, __('successfully uploaded to swarm')); }); } }