Skip to content

Commit 39c7e3c

Browse files
authored
Merge pull request #364 from embark-framework/bug_fix/build-before-upload-fixed
Bug fix/build before upload fixed
2 parents f9b7f5c + 8bdf79b commit 39c7e3c

File tree

5 files changed

+168
-107
lines changed

5 files changed

+168
-107
lines changed

lib/cmd.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,15 @@ class Cmd {
170170
upload() {
171171
program
172172
.command('upload [platform] [environment]')
173+
.option('--logfile [logfile]', 'filename to output logs (default: none)')
173174
.description('upload your dapp to a decentralized storage (e.g embark upload ipfs)')
174175
.action(function (platform, env, _options) {
175-
// TODO: get env in cmd line as well
176-
embark.initConfig(env || 'development', {
176+
let environment = env || 'development';
177+
embark.initConfig(environment, {
177178
embarkConfig: 'embark.json', interceptLogs: false
178179
});
179-
embark.upload(platform);
180+
_options.env = environment;
181+
embark.upload(platform, _options);
180182
});
181183
}
182184

lib/index.js

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,16 @@ class Embark {
147147
});
148148
}
149149

150-
build(options) {
151-
152-
let engine = new Engine({
153-
env: options.env,
154-
version: this.version,
155-
embarkConfig: 'embark.json',
156-
interceptLogs: false
157-
});
158-
engine.init();
150+
build(options, engine, continueProcessing) {
151+
if(!engine){
152+
engine = new Engine({
153+
env: options.env,
154+
version: this.version,
155+
embarkConfig: 'embark.json',
156+
interceptLogs: false
157+
});
158+
engine.init();
159+
}
159160

160161
async.waterfall([
161162
function startServices(callback) {
@@ -185,7 +186,9 @@ class Embark {
185186
engine.logger.info("finished building".underline);
186187
}
187188
// needed due to child processes
188-
process.exit();
189+
if(err || !continueProcessing){
190+
process.exit();
191+
}
189192
});
190193
}
191194

@@ -247,29 +250,68 @@ class Embark {
247250
}
248251

249252
// TODO: should deploy if it hasn't already
250-
upload(platform) {
251-
let options = {
252-
buildDir: 'dist/',
253-
storageConfig: this.config.storageConfig
254-
};
253+
upload(platform, options) {
254+
255+
options.buildDir = 'dist/';
256+
options.storageConfig = this.config.storageConfig;
257+
258+
// initialise embark engine
259+
let engine = new Engine({
260+
env: options.env,
261+
version: this.version,
262+
embarkConfig: options.embarkConfig || 'embark.json',
263+
logfile: options.logfile
264+
});
265+
engine.init();
255266

267+
// load plugins
256268
this.plugins.loadInternalPlugin('ipfs', options);
257269
this.plugins.loadInternalPlugin('swarm', options);
258270

259-
let cmdPlugins = this.plugins.getPluginsFor('uploadCmds');
271+
let plugins = this.plugins;
260272
let cmdPlugin;
261-
if (cmdPlugins.length > 0) {
262-
cmdPlugin = cmdPlugins.find((pluginCmd) => {
263-
return pluginCmd.name == platform;
264-
});
265-
}
273+
let self = this;
274+
async.waterfall([
275+
function setupStoragePlugin(callback){
276+
// check use has input existing storage plugin
277+
let cmdPlugins = plugins.getPluginsFor('uploadCmds');
278+
279+
if (cmdPlugins.length > 0) {
280+
cmdPlugin = cmdPlugins.find((pluginCmd) => {
281+
return pluginCmd.name == platform;
282+
});
283+
}
284+
if (!cmdPlugin) {
285+
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
286+
callback({message: 'unknown platform: ' + platform});
287+
} else {
288+
callback();
289+
}
290+
},
291+
function buildAndDeployContracts(callback){
292+
// 2. upload to storage (outputDone event triggered after webpack finished)
293+
engine.events.on('outputDone', function () {
294+
engine.logger.info('deploying to ' + platform + '...');
295+
cmdPlugin.uploadCmds[0].cb()
296+
.then((success) => {
297+
callback(null, success);
298+
})
299+
.catch(callback);
300+
});
301+
// 1. build the contracts and dapp webpack
302+
self.build(options, engine, true);
303+
}
304+
], function (err, _result) {
305+
if (err) {
306+
engine.logger.error(err.message);
307+
engine.logger.debug(err.stack);
308+
} else {
309+
engine.logger.info("finished building dapp and deploying to " + platform.underline);
310+
}
266311

267-
if (cmdPlugin) {
268-
cmdPlugin.uploadCmds[0].cb();
269-
} else {
270-
console.log(("unknown platform: " + platform).red);
271-
console.log('try "embark upload ipfs" or "embark upload swarm"'.green);
272-
}
312+
// needed due to child processes
313+
process.exit();
314+
});
273315
}
274316

275317
runTests(file) {

lib/modules/ipfs/upload.js

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,51 @@ class IPFS {
1212
}
1313

1414
deploy() {
15-
console.log("deploying!");
16-
let self = this;
17-
async.waterfall([
18-
function findBinary(callback) {
19-
let ipfs_bin = shelljs.which(self.configIpfsBin);
20-
21-
if (ipfs_bin === 'ipfs not found' || !ipfs_bin) {
22-
console.log(('=== WARNING: ' + self.configIpfsBin + ' not found or not in the path. Guessing ~/go/bin/ipfs for path').yellow);
23-
ipfs_bin = "~/go/bin/ipfs";
15+
return new Promise((resolve, reject) => {
16+
console.log("deploying!");
17+
let self = this;
18+
async.waterfall([
19+
function findBinary(callback) {
20+
let ipfs_bin = shelljs.which(self.configIpfsBin);
21+
22+
if (ipfs_bin === 'ipfs not found' || !ipfs_bin) {
23+
console.log(('=== WARNING: ' + self.configIpfsBin + ' not found or not in the path. Guessing ~/go/bin/ipfs for path').yellow);
24+
ipfs_bin = "~/go/bin/ipfs";
25+
}
26+
27+
callback(null, ipfs_bin);
28+
},
29+
function runCommand(ipfs_bin, callback) {
30+
let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`;
31+
console.log(("=== adding " + self.buildDir + " to ipfs").green);
32+
console.log(cmd.green);
33+
shelljs.exec(cmd, function(code, stdout, stderr){
34+
callback(stderr, stdout);
35+
});
36+
},
37+
function getHashFromOutput(result, callback) {
38+
let rows = result.split("\n");
39+
let dir_row = rows[rows.length - 2];
40+
let dir_hash = dir_row.split(" ")[1];
41+
42+
callback(null, dir_hash);
43+
},
44+
function printUrls(dir_hash, callback) {
45+
console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green);
46+
console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green);
47+
48+
callback();
2449
}
25-
26-
return callback(null, ipfs_bin);
27-
},
28-
function runCommand(ipfs_bin, callback) {
29-
let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`;
30-
console.log(("=== adding " + self.buildDir + " to ipfs").green);
31-
console.log(cmd.green);
32-
let result = shelljs.exec(cmd);
33-
34-
return callback(null, result);
35-
},
36-
function getHashFromOutput(result, callback) {
37-
let rows = result.output.split("\n");
38-
let dir_row = rows[rows.length - 2];
39-
let dir_hash = dir_row.split(" ")[1];
40-
41-
return callback(null, dir_hash);
42-
},
43-
function printUrls(dir_hash, callback) {
44-
console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green);
45-
console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green);
46-
47-
return callback();
48-
}
49-
], function (err, _result) {
50-
if (err) {
51-
console.log("error uploading to ipfs".red);
52-
console.log(err);
53-
}
50+
], function (err, _result) {
51+
if (err) {
52+
console.log("error uploading to ipfs".red);
53+
console.log(err);
54+
reject(err);
55+
}
56+
else resolve('successfully uploaded to ipfs');
57+
});
5458
});
5559
}
56-
5760
}
5861

5962
module.exports = IPFS;

lib/modules/swarm/upload.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,51 @@ class Swarm {
99
}
1010

1111
deploy() {
12-
let self = this;
13-
async.waterfall([
14-
function findBinary(callback) {
15-
let swarm_bin = shelljs.which('swarm');
16-
17-
if (swarm_bin === 'swarm not found' || !swarm_bin) {
18-
console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow);
19-
swarm_bin = "~/go/bin/swarm";
12+
return new Promise((resolve, reject) => {
13+
let self = this;
14+
async.waterfall([
15+
function findBinary(callback) {
16+
let swarm_bin = shelljs.which('swarm');
17+
18+
if (swarm_bin === 'swarm not found' || !swarm_bin) {
19+
console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow);
20+
swarm_bin = "~/go/bin/swarm";
21+
}
22+
23+
callback(null, swarm_bin);
24+
},
25+
function runCommand(swarm_bin, callback) {
26+
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
27+
console.log(("=== adding " + self.buildDir + " to swarm").green);
28+
console.log(cmd.green);
29+
shelljs.exec(cmd, function(code, stdout, stderr){
30+
callback(stderr, {code: code, output: stdout});
31+
});
32+
},
33+
function getHashFromOutput(result, callback) {
34+
if (result.code !== 0) {
35+
callback("couldn't upload, is the swarm daemon running?");
36+
}
37+
else{
38+
let rows = result.output.split("\n");
39+
let dir_hash = rows.reverse()[1];
40+
41+
callback(null, dir_hash);
42+
}
43+
},
44+
function printUrls(dir_hash, callback) {
45+
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
46+
47+
callback();
2048
}
21-
22-
return callback(null, swarm_bin);
23-
},
24-
function runCommand(swarm_bin, callback) {
25-
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
26-
console.log(("=== adding " + self.buildDir + " to swarm").green);
27-
console.log(cmd.green);
28-
let result = shelljs.exec(cmd);
29-
30-
return callback(null, result);
31-
},
32-
function getHashFromOutput(result, callback) {
33-
if (result.code !== 0) {
34-
return callback("couldn't upload, is the swarm daemon running?");
49+
], function (err, _result) {
50+
if (err) {
51+
console.log("error uploading to swarm".red);
52+
console.log(err);
53+
reject(err);
3554
}
36-
37-
let rows = result.output.split("\n");
38-
let dir_hash = rows.reverse()[1];
39-
40-
return callback(null, dir_hash);
41-
},
42-
function printUrls(dir_hash, callback) {
43-
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
44-
45-
return callback();
46-
}
47-
], function (err, _result) {
48-
if (err) {
49-
console.log("error uploading to swarm".red);
50-
console.log(err);
51-
}
55+
else resolve('successfully uploaded to swarm');
56+
});
5257
});
5358
}
5459
}

test_apps/test_app/app/js/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import $ from 'jquery';
2+
import AlreadyDeployedToken from 'Embark/contracts/AlreadyDeployedToken';
3+
import AnotherStorage from 'Embark/contracts/AnotherStorage';
4+
import async from 'async';
5+
import MyToken from 'Embark/contracts/MyToken';
6+
import MyToken2 from 'Embark/contracts/MyToken2';
7+
import SimpleStorage from 'Embark/contracts/SimpleStorage';
8+
import Token from 'Embark/contracts/Token';
9+
110

211
$(document).ready(function() {
312

0 commit comments

Comments
 (0)