2018-05-28 15:50:01 +00:00
|
|
|
const shelljs = require('shelljs');
|
|
|
|
const ProcessWrapper = require('../../process/processWrapper');
|
|
|
|
const constants = require('../../constants');
|
|
|
|
|
|
|
|
let swarmProcess;
|
|
|
|
|
|
|
|
class SwarmProcess extends ProcessWrapper {
|
|
|
|
constructor(options) {
|
|
|
|
super();
|
|
|
|
this.storageConfig = options.storageConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
startSwarmDaemon() {
|
2018-05-28 15:52:49 +00:00
|
|
|
const self = this;
|
2018-05-28 15:50:39 +00:00
|
|
|
if (!this.storageConfig.account || !this.storageConfig.account.address || !this.storageConfig.account.password) {
|
2018-05-28 15:50:01 +00:00
|
|
|
return 'Account address and password are needed in the storage config to start the Swarm process';
|
|
|
|
}
|
2018-05-24 14:25:32 +00:00
|
|
|
const child = shelljs.exec(
|
2018-05-28 15:50:39 +00:00
|
|
|
`${this.storageConfig.swarmPath || 'swarm'} --bzzaccount ${this.storageConfig.account.address} --password ${this.storageConfig.account.password} --corsdomain http://localhost:8000 --ens-api ''`,
|
|
|
|
{silent: true}, (err, _stdout, _stderr) =>
|
|
|
|
{
|
2018-05-28 15:50:01 +00:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
2018-05-28 15:51:38 +00:00
|
|
|
process.exit(1);
|
2018-05-28 15:50:01 +00:00
|
|
|
}
|
2018-05-28 15:51:38 +00:00
|
|
|
process.exit();
|
2018-05-28 15:50:01 +00:00
|
|
|
});
|
2018-05-24 14:25:32 +00:00
|
|
|
let lastMessage;
|
2018-05-28 15:52:49 +00:00
|
|
|
child.stderr.on('data', (data) => {
|
2018-05-24 14:25:32 +00:00
|
|
|
if (!self.readyCalled && data.indexOf('Swarm http proxy started') > -1) {
|
|
|
|
self.readyCalled = true;
|
|
|
|
self.send({result: constants.storage.initiated});
|
|
|
|
}
|
|
|
|
lastMessage = data;
|
|
|
|
console.log('Swarm: ' + data);
|
|
|
|
});
|
|
|
|
child.on('exit', (code) => {
|
|
|
|
if (code) {
|
|
|
|
console.error('Swarm exited with error code ' + code);
|
|
|
|
console.error(lastMessage);
|
|
|
|
}
|
|
|
|
});
|
2018-05-28 15:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on('message', (msg) => {
|
|
|
|
if (msg.action === constants.storage.init) {
|
|
|
|
swarmProcess = new SwarmProcess(msg.options);
|
|
|
|
const error = swarmProcess.startSwarmDaemon();
|
|
|
|
|
2018-05-24 14:25:32 +00:00
|
|
|
if (error) {
|
|
|
|
swarmProcess.send({result: constants.storage.initiated, error});
|
|
|
|
}
|
2018-05-28 15:50:01 +00:00
|
|
|
}
|
|
|
|
});
|