embark/lib/modules/ipfs/upload.js

88 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-12-05 23:14:46 +00:00
require('colors');
2017-03-29 17:50:05 +00:00
let async = require('async');
let shelljs = require('shelljs');
2018-09-21 20:52:28 +00:00
const path = require('path');
2017-03-30 11:12:39 +00:00
2018-09-21 20:52:28 +00:00
class IPFS {
2017-03-30 11:12:39 +00:00
constructor(options) {
this.options = options;
this.buildDir = options.buildDir || 'dist/';
this.storageConfig = options.storageConfig;
this.configIpfsBin = this.storageConfig.ipfs_bin || "ipfs";
this.env = options.env;
2017-03-30 11:12:39 +00:00
}
2018-07-07 21:02:46 +00:00
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";
2017-03-30 11:12:39 +00:00
}
2018-07-07 21:02:46 +00:00
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) {
2018-09-21 20:52:28 +00:00
const pattern = `added ([a-zA-Z1-9]{46}) ${path.basename(self.buildDir)}\n`;
const regex = RegExp(pattern, 'm');
const dirHash = result.match(regex)[1];
2018-07-07 21:02:46 +00:00
2018-09-21 20:52:28 +00:00
callback(null, dirHash);
2018-07-07 21:02:46 +00:00
},
function printUrls(dir_hash, callback) {
console.log(("=== " + __("DApp available at") + " http://localhost:8080/ipfs/" + dir_hash + "/").green);
2018-08-28 20:44:50 +00:00
console.log(("=== " + __("DApp available at") + " https://ipfs.infura.io/ipfs/" + dir_hash + "/").green);
if(self.env === 'development') {
console.log(("\n=== " +
"Blockchain must be running".bold +
" ===").yellow);
console.log((
"embark run".italic +
__(" or ") +
"embark blockchain".italic +
__(" must be running when the site is loaded to interact with the blockchain.\n")
).yellow);
console.log(("=== " +
"Usage with the public gateway".bold +
" ===").yellow);
console.log((
__("If you wish to load your development site from the public gateway (ipfs.infura.io), you will need to first update your CORS settings (") +
"config/blockchain.js > wsOrigins".italic +
__(" and ") +
"config/blockchain.js > rpcCorsDomain".italic +
__(") to allow ") +
"ipfs.infura.io".underline +
__(". If these were set to 'auto', they would now need to be set to ") +
"https://ipfs.infura.io,http://localhost:8000,http://localhost:8500,embark".underline +
".\n").yellow);
}
2018-07-07 21:02:46 +00:00
2018-07-30 15:57:00 +00:00
callback(null, dir_hash);
2018-07-07 21:02:46 +00:00
}
2018-07-30 15:57:00 +00:00
], function (err, dir_hash) {
2018-07-07 21:02:46 +00:00
if (err) {
console.log(__("error uploading to ipfs").red);
console.log(err);
cb(err);
}
2018-07-30 15:57:00 +00:00
else cb(null, dir_hash);
2017-03-30 11:12:39 +00:00
});
}
}
2016-10-02 21:04:22 +00:00
module.exports = IPFS;