Skip to content

Commit 2ad8526

Browse files
committed
add early returns and lint
1 parent 8770e1e commit 2ad8526

File tree

6 files changed

+108
-74
lines changed

6 files changed

+108
-74
lines changed

lib/index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,15 @@ class Embark {
323323
return true;
324324
}
325325
});
326-
if(!checkFn || typeof checkFn.fn !== 'function') callback();
327-
else{
328-
checkFn.fn(function(serviceCheckResult){
329-
if(!serviceCheckResult.status || serviceCheckResult.status === 'off'){
330-
callback({message:`Cannot upload: ${platform} node is not running on http://${engine.config.storageConfig.host}:${engine.config.storageConfig.port}.`});
331-
} else {
332-
callback(null);
333-
}
334-
});
326+
if (!checkFn || typeof checkFn.fn !== 'function') {
327+
return callback();
335328
}
329+
checkFn.fn(function (serviceCheckResult) {
330+
if (!serviceCheckResult.status || serviceCheckResult.status === 'off') {
331+
return callback({message: `Cannot upload: ${platform} node is not running on http://${engine.config.storageConfig.host}:${engine.config.storageConfig.port}.`});
332+
}
333+
callback();
334+
});
336335
},
337336
function setupStoragePlugin(callback){
338337
let pluginList = engine.plugins.listPlugins();
@@ -352,9 +351,9 @@ class Embark {
352351
}
353352
if (!cmdPlugin) {
354353
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
355-
callback({message: 'unknown platform: ' + platform});
356-
}
357-
else callback(null);
354+
return callback({message: 'unknown platform: ' + platform});
355+
}
356+
callback();
358357
},
359358
function deploy(callback) {
360359
// 2. upload to storage (outputDone event triggered after webpack finished)

lib/modules/ipfs/embarkjs.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import IpfsApi from 'ipfs-api';
22

33
let __embarkIPFS = {};
44

5-
__embarkIPFS.setProvider = function(options) {
5+
__embarkIPFS.setProvider = function (options) {
66
var self = this;
7-
var promise = new Promise(function(resolve, reject) {
7+
var promise = new Promise(function (resolve, reject) {
88
try {
99
if (options === undefined) {
1010
self.ipfsConnection = IpfsApi('localhost', '5001');
@@ -30,14 +30,14 @@ __embarkIPFS.setProvider = function(options) {
3030
return promise;
3131
};
3232

33-
__embarkIPFS.saveText = function(text) {
33+
__embarkIPFS.saveText = function (text) {
3434
const self = this;
35-
var promise = new Promise(function(resolve, reject) {
35+
var promise = new Promise(function (resolve, reject) {
3636
if (!self.ipfsConnection) {
3737
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
3838
reject(connectionError);
3939
}
40-
self.ipfsConnection.add(self.ipfsConnection.Buffer.from(text), function(err, result) {
40+
self.ipfsConnection.add(self.ipfsConnection.Buffer.from(text), function (err, result) {
4141
if (err) {
4242
reject(err);
4343
} else {
@@ -49,11 +49,11 @@ __embarkIPFS.saveText = function(text) {
4949
return promise;
5050
};
5151

52-
__embarkIPFS.get = function(hash) {
52+
__embarkIPFS.get = function (hash) {
5353
const self = this;
5454
// TODO: detect type, then convert if needed
5555
//var ipfsHash = web3.toAscii(hash);
56-
var promise = new Promise(function(resolve, reject) {
56+
var promise = new Promise(function (resolve, reject) {
5757
if (!self.ipfsConnection) {
5858
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
5959
reject(connectionError);
@@ -69,24 +69,24 @@ __embarkIPFS.get = function(hash) {
6969
return promise;
7070
};
7171

72-
__embarkIPFS.uploadFile = function(inputSelector) {
72+
__embarkIPFS.uploadFile = function (inputSelector) {
7373
const self = this;
7474
var file = inputSelector[0].files[0];
7575

7676
if (file === undefined) {
7777
throw new Error('no file found');
7878
}
7979

80-
var promise = new Promise(function(resolve, reject) {
80+
var promise = new Promise(function (resolve, reject) {
8181
if (!self.ipfsConnection) {
8282
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
8383
reject(connectionError);
8484
}
8585
var reader = new FileReader();
86-
reader.onloadend = function() {
86+
reader.onloadend = function () {
8787
var fileContent = reader.result;
8888
var buffer = self.ipfsConnection.Buffer.from(fileContent);
89-
self.ipfsConnection.add(buffer, function(err, result) {
89+
self.ipfsConnection.add(buffer, function (err, result) {
9090
if (err) {
9191
reject(err);
9292
} else {
@@ -100,16 +100,22 @@ __embarkIPFS.uploadFile = function(inputSelector) {
100100
return promise;
101101
};
102102

103-
__embarkIPFS.isAvailable = function(){
103+
__embarkIPFS.isAvailable = function () {
104104
return new Promise((resolve) => {
105-
if(!this.ipfsConnection) resolve(false);
105+
if (!this.ipfsConnection) {
106+
return resolve(false);
107+
}
106108
this.ipfsConnection.id()
107-
.then((id) => { resolve(Boolean(id)); })
108-
.catch(() => { resolve(false); });
109+
.then((id) => {
110+
resolve(Boolean(id));
111+
})
112+
.catch(() => {
113+
resolve(false);
114+
});
109115
});
110116
};
111117

112-
__embarkIPFS.getUrl = function(hash) {
118+
__embarkIPFS.getUrl = function (hash) {
113119
return (this._getUrl || "http://localhost:8080/ipfs/") + hash;
114120
};
115121

lib/modules/swarm/embarkjs.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
let __embarkSwarm = {};
33
const bytes = require("eth-lib/lib/bytes");
44

5-
__embarkSwarm.setProvider = function(options) {
5+
__embarkSwarm.setProvider = function (options) {
66
this.bzz = web3.bzz;
77
this.protocol = options.protocol;
88
this.host = options.host;
@@ -13,7 +13,7 @@ __embarkSwarm.setProvider = function(options) {
1313

1414
var promise = new Promise((resolve, reject) => {
1515
try {
16-
if(!this.bzz.currentProvider) {
16+
if (!this.bzz.currentProvider) {
1717
this.bzz.setProvider(`${options.protocol}://${options.host}:${options.port}`);
1818
}
1919
resolve(this);
@@ -25,38 +25,46 @@ __embarkSwarm.setProvider = function(options) {
2525
return promise;
2626
};
2727

28-
__embarkSwarm.isAvailable = function(){
28+
__embarkSwarm.isAvailable = function () {
2929
return new Promise((resolve, reject) => {
30-
if(!this.bzz) resolve(false);
30+
if (!this.bzz) {
31+
return resolve(false);
32+
}
3133
this.bzz.isAvailable()
3234
.then(resolve)
33-
.catch(() => { reject(this.connectError); });
35+
.catch(() => {
36+
reject(this.connectError);
37+
});
3438
});
3539
};
3640

37-
__embarkSwarm.saveText = function(text) {
41+
__embarkSwarm.saveText = function (text) {
3842
return new Promise((resolve, reject) => {
3943
this.isAvailable().then((isAvailable) => {
40-
if(!isAvailable) reject(this.connectError);
44+
if (!isAvailable) {
45+
return reject(this.connectError);
46+
}
4147
this.bzz.upload(text)
4248
.then(resolve)
4349
.catch(reject);
4450
}).catch(reject);
4551
});
4652
};
4753

48-
__embarkSwarm.get = function(hash) {
54+
__embarkSwarm.get = function (hash) {
4955
return new Promise((resolve, reject) => {
5056
this.isAvailable().then((isAvailable) => {
51-
if(!isAvailable) reject(this.connectError);
57+
if (!isAvailable) {
58+
return reject(this.connectError);
59+
}
5260
this.bzz.download(hash)
5361
.then((uint8Array) => resolve(bytes.toString(bytes.fromUint8Array(uint8Array))))
5462
.catch(reject);
5563
}).catch(reject);
5664
});
5765
};
5866

59-
__embarkSwarm.uploadFile = function(inputSelector) {
67+
__embarkSwarm.uploadFile = function (inputSelector) {
6068
let file = inputSelector[0].files[0];
6169

6270
if (file === undefined) {
@@ -68,18 +76,20 @@ __embarkSwarm.uploadFile = function(inputSelector) {
6876
reader.onloadend = (event) => {
6977
var fileContent = new Uint8Array(event.target.result);
7078
this.isAvailable().then((isAvailable) => {
71-
if(!isAvailable) reject(this.connectError);
79+
if (!isAvailable) {
80+
return reject(this.connectError);
81+
}
7282
this.bzz.upload(fileContent)
73-
.then(resolve)
74-
.catch(reject);
83+
.then(resolve)
84+
.catch(reject);
7585
}).catch(reject);
7686
};
7787
reader.onerror = reject;
7888
reader.readAsArrayBuffer(file);
7989
});
8090
};
8191

82-
__embarkSwarm.getUrl = function(hash) {
92+
__embarkSwarm.getUrl = function (hash) {
8393
return this._getUrl + hash;
8494
};
8595

lib/modules/whisper/js/embarkjs.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// for the whisper v5 and web3.js 1.0
44
let __embarkWhisperNewWeb3 = {};
55

6-
__embarkWhisperNewWeb3.setProvider = function(options) {
6+
__embarkWhisperNewWeb3.setProvider = function (options) {
77
const self = this;
88
let provider;
99
if (options === undefined) {
@@ -13,20 +13,24 @@ __embarkWhisperNewWeb3.setProvider = function(options) {
1313
}
1414
// TODO: take into account type
1515
self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider));
16-
self.getWhisperVersion(function(err, version) {
16+
self.getWhisperVersion(function (err, version) {
1717
if (err) {
1818
console.log("whisper not available");
1919
} else if (version >= 5) {
20-
self.web3.shh.newSymKey().then((id) => { self.symKeyID = id; });
21-
self.web3.shh.newKeyPair().then((id) => { self.sig = id; });
20+
self.web3.shh.newSymKey().then((id) => {
21+
self.symKeyID = id;
22+
});
23+
self.web3.shh.newKeyPair().then((id) => {
24+
self.sig = id;
25+
});
2226
} else {
2327
throw new Error("version of whisper not supported");
2428
}
2529
self.whisperVersion = self.web3.version.whisper;
2630
});
2731
};
2832

29-
__embarkWhisperNewWeb3.sendMessage = function(options) {
33+
__embarkWhisperNewWeb3.sendMessage = function (options) {
3034
var topics, data, ttl, payload;
3135
topics = options.topic || options.topics;
3236
data = options.data || options.payload;
@@ -56,10 +60,11 @@ __embarkWhisperNewWeb3.sendMessage = function(options) {
5660
powTarget: powTarget
5761
};
5862

59-
this.web3.shh.post(message, function() { });
63+
this.web3.shh.post(message, function () {
64+
});
6065
};
6166

62-
__embarkWhisperNewWeb3.listenTo = function(options) {
67+
__embarkWhisperNewWeb3.listenTo = function (options) {
6368
var topics = options.topic || options.topics;
6469

6570
let promise = new __MessageEvents();
@@ -73,7 +78,7 @@ __embarkWhisperNewWeb3.listenTo = function(options) {
7378
let filter = this.web3.shh.subscribe("messages", {
7479
symKeyID: this.symKeyID,
7580
topics: topics
76-
}).on('data', function(result) {
81+
}).on('data', function (result) {
7782
var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
7883
var data;
7984
data = {
@@ -91,21 +96,25 @@ __embarkWhisperNewWeb3.listenTo = function(options) {
9196
return promise;
9297
};
9398

94-
__embarkWhisperNewWeb3.getWhisperVersion = function(cb) {
95-
this.web3.shh.getVersion(function(err, version) {
99+
__embarkWhisperNewWeb3.getWhisperVersion = function (cb) {
100+
this.web3.shh.getVersion(function (err, version) {
96101
cb(err, version);
97102
});
98103
};
99104

100-
__embarkWhisperNewWeb3.isAvailable = function(){
105+
__embarkWhisperNewWeb3.isAvailable = function () {
101106
return new Promise((resolve, reject) => {
102-
if(!this.web3.shh) resolve(false);
103-
try{
107+
if (!this.web3.shh) {
108+
return resolve(false);
109+
}
110+
try {
104111
this.getWhisperVersion((err) => {
105112
resolve(Boolean(!err));
106113
});
107114
}
108-
catch(err){ reject(err); }
115+
catch (err) {
116+
reject(err);
117+
}
109118
});
110119
};
111120

0 commit comments

Comments
 (0)