/ lib / modules / ipfs / upload.js
upload.js
 1  require('colors');
 2  let async = require('async');
 3  let shelljs = require('shelljs');
 4  
 5  const path = require('path');
 6  
 7  class IPFS {
 8    constructor(options) {
 9      this.options = options;
10      this.buildDir = options.buildDir || 'dist/';
11      this.storageConfig = options.storageConfig;
12      this.configIpfsBin = this.storageConfig.ipfs_bin || "ipfs";
13      this.env = options.env;
14    }
15  
16    deploy(cb) {
17      console.log("deploying to ipfs!");
18      let self = this;
19      async.waterfall([
20        function findBinary(callback) {
21          let ipfs_bin = shelljs.which(self.configIpfsBin);
22  
23          if (ipfs_bin === 'ipfs not found' || !ipfs_bin) {
24            console.log(('=== WARNING: ' + self.configIpfsBin + ' ' + __('not found or not in the path. Guessing %s for path', '~/go/bin/ipfs')).yellow);
25            ipfs_bin = "~/go/bin/ipfs";
26          }
27  
28          callback(null, ipfs_bin);
29        },
30        function runCommand(ipfs_bin, callback) {
31          let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`;
32          console.log(("=== " + __("adding %s to ipfs", self.buildDir)).green);
33          console.debug(cmd);
34          shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
35            console.log(stdout.green);
36            callback(stderr, stdout);
37          });
38        },
39        function getHashFromOutput(result, callback) {
40          const pattern = `added ([a-zA-Z1-9]{46}) ${path.basename(self.buildDir)}\n`;
41          const regex = RegExp(pattern, 'm');
42          const dirHash = result.match(regex)[1];
43  
44          callback(null, dirHash);
45        },
46        function printUrls(dir_hash, callback) {
47          console.log(("=== " + __("DApp available at") + " http://localhost:8080/ipfs/" + dir_hash + "/").green);
48          console.log(("=== " + __("DApp available at") + " https://ipfs.infura.io/ipfs/" + dir_hash + "/").green);
49          if(self.env === 'development') {
50            console.log(("\n=== " +
51              "Blockchain must be running".bold + 
52              " ===").yellow);
53            console.log((
54              "embark run".italic + 
55              __(" or ") + 
56              "embark blockchain".italic + 
57              __(" must be running when the site is loaded to interact with the blockchain.\n")
58            ).yellow);
59            console.log(("=== " + 
60              "Usage with the public gateway".bold + 
61              " ===").yellow);
62            console.log((
63              __("If you wish to load your development site from the public gateway (ipfs.infura.io), you will need to first update your CORS settings (") +
64              "config/blockchain.js > wsOrigins".italic + 
65              __(" and ") +
66              "config/blockchain.js > rpcCorsDomain".italic +
67              __(") to allow ") + 
68              "ipfs.infura.io".underline + 
69              __(". If these were set to 'auto', they would now need to be set to ") + 
70              "https://ipfs.infura.io,http://localhost:8000,http://localhost:8500,embark".underline + 
71              ".\n").yellow);
72          }
73  
74          callback(null, dir_hash);
75        }
76      ], function (err, dir_hash) {
77        if (err) {
78          console.log(__("error uploading to ipfs").red);
79          console.log(err);
80          cb(err);
81        }
82        else cb(null, dir_hash);
83      });
84    }
85  }
86  
87  module.exports = IPFS;