Skip to content
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
6 changes: 1 addition & 5 deletions lib/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ class Cmd {
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.description('Upload your dapp to a decentralized storage (e.g embark upload ipfs).')
.action(function (platform, env, _options) {
let environment = env || 'development';
embark.initConfig(environment, {
embarkConfig: 'embark.json', interceptLogs: false
});
_options.env = environment;
_options.env = env || 'development';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove the initConfig? in fact I don't quite understand how this.config.storageConfig would work later without this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instantiations of Events/Logger/Config objects are done in both Embark.initConfig() and in Engine.init(). In this case, Embark.upload method added an Engine.init() and therefore no longer need to run Embark.initConfig() as it was duplicating effort.

You'll also notice in the Embark.upload after the Engine is instantiated, the Engine's events/logger/config properties are used instead of those local to Embark as initConfig is no longer called.

_options.logFile = _options.logfile; // fix casing
_options.logLevel = _options.loglevel; // fix casing
embark.upload(platform, _options);
Expand Down
11 changes: 10 additions & 1 deletion lib/core/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class Engine {
"webServer": this.webServerService,
"ipfs": this.ipfsService,
"web3": this.web3Service,
"libraryManager": this.libraryManagerService
"libraryManager": this.libraryManagerService,
"swarm": this.swarmService
};

let service = services[serviceName];
Expand Down Expand Up @@ -195,6 +196,14 @@ class Engine {
});
}

swarmService(_options) {
this.registerModule('swarm', {
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
storageConfig: this.config.storageConfig,
web3: _options.web3
});
}

web3Service(options) {
let self = this;
this.web3 = options.web3;
Expand Down
73 changes: 47 additions & 26 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ class Embark {
});
}

build(options, continueProcessing) {
build(options) {
this.context = options.context || [constants.contexts.build];

let engine = new Engine({
env: options.env,
version: this.version,
Expand Down Expand Up @@ -202,9 +203,7 @@ class Embark {
engine.logger.info("finished building".underline);
}
// needed due to child processes
if(err || !continueProcessing){
process.exit();
}
process.exit();
});
}

Expand Down Expand Up @@ -271,59 +270,81 @@ class Embark {
}

upload(platform, options) {
this.context = options.context || [constants.contexts.upload, constants.contexts.build];

// populate options that were instantiated with initConfig to pass around
options.buildDir = 'dist/';
options.storageConfig = this.config.storageConfig;
options.events = this.events;
options.logger = this.logger;
options.config = this.config;

// load plugins
this.plugins.loadInternalPlugin('ipfs', options);
this.plugins.loadInternalPlugin('swarm', options);
this.context = options.context || [constants.contexts.upload, constants.contexts.build];

// upddate our options with loaded plugins
options.plugins = this.plugins;
let engine = new Engine({
env: options.env,
version: this.version,
embarkConfig: 'embark.json',
interceptLogs: false,
logFile: options.logFile,
logLevel: options.logLevel,
events: options.events,
logger: options.logger,
config: options.config,
plugins: options.plugins
});
engine.init();

let cmdPlugin;
let self = this;
async.waterfall([

function startServices(callback) {

engine.startService("libraryManager");
engine.startService("web3");
engine.startService("pipeline");
engine.startService("codeGenerator");
engine.startService("deployment");
engine.startService("ipfs");
engine.startService("swarm", {buildDir:'dist/',web3: engine.web3});
callback();
},
function setupStoragePlugin(callback){
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
engine.logger.info("loaded plugins: " + pluginList.join(", "));
}

// check use has input existing storage plugin
let cmdPlugins = self.plugins.getPluginsFor('uploadCmds');
let cmdPlugins = engine.plugins.getPluginsFor('uploadCmds');

if (cmdPlugins.length > 0) {
cmdPlugin = cmdPlugins.find((pluginCmd) => {
return pluginCmd.name == platform;
});
}
if (!cmdPlugin) {
self.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
callback({message: 'unknown platform: ' + platform});
} else {
callback();
}
},
function buildAndDeployContracts(callback){
function deploy(callback) {
// 2. upload to storage (outputDone event triggered after webpack finished)
self.events.on('outputDone', function () {
engine.events.on('outputDone', function () {
cmdPlugin.uploadCmds[0].cb()
.then((success) => {
callback(null, success);
})
.catch(callback);
});
// 1. build the contracts and dapp webpack
self.build(options, true);
engine.deployManager.deployContracts(function (err) {
engine.logger.info("finished deploying".underline);
if(err){
callback(err);
}
});
}
], function (err, _result) {
if (err) {
self.logger.error(err.message);
self.logger.debug(err.stack);
engine.logger.error(err.message);
engine.logger.debug(err.stack);
} else {
self.logger.info("finished building dapp and deploying to " + platform.underline);
engine.logger.info(`finished building DApp and deploying to ${platform}`.underline);
}

// needed due to child processes
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/swarm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Swarm {

this.upload_swarm = new UploadSwarm({
buildDir: options.buildDir || 'dist/',
storageConfig: options.storageConfig
storageConfig: options.storageConfig,
web3: options.web3
});

embark.registerUploadCommand('swarm', this.upload_swarm.deploy.bind(this.upload_swarm));
Expand Down
46 changes: 17 additions & 29 deletions lib/modules/swarm/upload.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
require('colors');
let async = require('async');
let shelljs = require('shelljs');

class Swarm {
constructor(options) {
this.options = options;
this.buildDir = options.buildDir || 'dist/';
Copy link
Collaborator

@iurimatias iurimatias Apr 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. add this.web3 and this.storageConfig

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correction/addition, it should actually be in swarm/index.js (which is the actual module initialization), then the module can pass the params to upload

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

this.web3 = options.web3;
this.storageConfig = options.storageConfig;
}

deploy() {
return new Promise((resolve, reject) => {
console.log("deploying to swarm!");
let self = this;
let web3 = this.web3;
async.waterfall([
function findBinary(callback) {
let swarm_bin = shelljs.which('swarm');

if (swarm_bin === 'swarm not found' || !swarm_bin) {
console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow);
swarm_bin = "~/go/bin/swarm";
}

callback(null, swarm_bin);
function setProvider(callback){
web3.bzz.setProvider(`http://${self.storageConfig.host}:${self.storageConfig.port}`);
callback();
},
function runCommand(swarm_bin, callback) {
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
function runCommand(callback) {
console.log(("=== adding " + self.buildDir + " to swarm").green);
console.trace(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, {code: code, output: stdout});
});
},
function getHashFromOutput(result, callback) {
if (result.code !== 0) {
callback("couldn't upload, is the swarm daemon running?");
}
else{
let rows = result.output.split("\n");
let dir_hash = rows.reverse()[1];

callback(null, dir_hash);
}
web3.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) {
console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green);
console.log((`=== DApp available at ${self.storageConfig.getUrl}${dir_hash}/`).green);

callback();
}
Expand Down