Skip to content

Commit

Permalink
improve 「kaizen ipfs upload」command
Browse files Browse the repository at this point in the history
  • Loading branch information
ahlumin committed Nov 20, 2018
1 parent 2915e13 commit 267d0e8
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 55 deletions.
109 changes: 78 additions & 31 deletions build/components/IPFSUpload/index.js
Expand Up @@ -10,26 +10,20 @@ var path = require('path');

var fs = require('fs');

var fsx = require('fs-extra');

var prompt = require('prompt');

var Log = require('../../lib/Log');

var Spinner = require('../../lib/Spinner');

function builder(yargs) {
return yargs.option('host', {
type: 'string',
describe: 'IPFS daemon API server host',
default: 'ipfs.infura.io'
}).option('port', {
type: 'string',
describe: 'IPFS daemon API server port',
default: '5001'
}).option('protocol', {
return yargs.positional('path', {
type: 'string',
describe: 'IPFS daemon API server protocol',
default: 'https'
});
describe: 'the path of the file or the folder which you want to upload to IPFS',
default: '.'
}).example('kaizen ipfs upload . => to upload the current folder').example('kaizen ipfs upload ./build => to upload the build folder in the current folder');
}

function handler(_x) {
Expand All @@ -40,69 +34,122 @@ function _handler() {
_handler = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(argv) {
var result, protocol, host, port, ipfs, targetPath, files, hashes;
var targetPath, result, configPath, kaizenConfig, _kaizenConfig$ipfs, host, port, protocol, ipfs, files, hashes, _hashes;

return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return confirmUploadDialog();
targetPath = path.resolve('./', argv.path);
_context.next = 4;
return confirmUploadDialog(targetPath);

case 3:
case 4:
result = _context.sent;

if (!(/^yes|y$/i.test(result.confirm) === false)) {
_context.next = 7;
_context.next = 8;
break;
}

Log.SuccessLog("==== Cancel Upload ====");
return _context.abrupt("return");

case 7:
case 8:
Spinner.start();
protocol = argv.protocol, host = argv.host, port = argv.port;
configPath = path.resolve('./', 'kaizen.json');

if (!(fsx.existsSync(configPath) === false)) {
_context.next = 14;
break;
}

Spinner.stop();
Log.ErrorLog('Missing kaizen.json, you should use「kaizen set-ipfs」command to setting IPFS configuration');
return _context.abrupt("return");

case 14:
kaizenConfig = fsx.readJsonSync(configPath);

if (kaizenConfig.ipfs) {
_context.next = 19;
break;
}

Spinner.stop();
Log.ErrorLog('Missing kaizen.json, you should use「kaizen set-ipfs」command to setting IPFS configuration');
return _context.abrupt("return");

case 19:
if (fs.existsSync(targetPath)) {
_context.next = 23;
break;
}

Spinner.stop();
Log.ErrorLog('The path that you specify is not exist');
return _context.abrupt("return");

case 23:
_kaizenConfig$ipfs = kaizenConfig.ipfs, host = _kaizenConfig$ipfs.host, port = _kaizenConfig$ipfs.port, protocol = _kaizenConfig$ipfs.protocol;
ipfs = IPFS_API(host, port, {
protocol: protocol
});
targetPath = path.resolve('./');

if (!fs.lstatSync(targetPath).isDirectory()) {
_context.next = 33;
break;
}

files = recursiveFetchFilePath(targetPath).map(function (file) {
return getIPFSContentObject(file, targetPath);
});
_context.next = 14;
_context.next = 29;
return ipfs.files.add(files);

case 14:
case 29:
hashes = _context.sent;
fs.writeFileSync(path.resolve('./', 'ipfs.json'), JSON.stringify(hashes));
_context.next = 37;
break;

case 33:
_context.next = 35;
return ipfs.files.add(fs.readFileSync(targetPath));

case 35:
_hashes = _context.sent;
fs.writeFileSync(path.resolve('./', 'ipfs.json'), JSON.stringify(_hashes));

case 37:
Spinner.stop();
Log.SuccessLog("==== Upload your files to IPFS Successfully ====");
_context.next = 25;
_context.next = 46;
break;

case 20:
_context.prev = 20;
case 41:
_context.prev = 41;
_context.t0 = _context["catch"](0);
Spinner.stop();
Log.ErrorLog('something went wrong!');
console.error(_context.t0);

case 25:
case 46:
case "end":
return _context.stop();
}
}
}, _callee, this, [[0, 20]]);
}, _callee, this, [[0, 41]]);
}));
return _handler.apply(this, arguments);
}

function confirmUploadDialog() {
function confirmUploadDialog(targetPath) {
var promptSchema = {
properties: {
confirm: {
message: 'Please ensure you will upload files in current folder to the IPFS (yes/no)',
message: "Please ensure you will upload \u300C".concat(targetPath, "\u300D to the IPFS (yes/no)"),
required: true
}
}
Expand Down Expand Up @@ -146,7 +193,7 @@ function getIPFSContentObject(filePath, targetPath) {
}

module.exports = function (yargs) {
var command = 'ipfs upload';
var commandDescription = 'To upload files in folder where the terminal currently in to IPFS';
var command = 'ipfs upload [path]';
var commandDescription = 'To upload file or folder to IPFS';
yargs.command(command, commandDescription, builder, handler);
};
75 changes: 51 additions & 24 deletions src/components/IPFSUpload/index.js
@@ -1,44 +1,71 @@
const IPFS_API = require('ipfs-api');
const path = require('path');
const fs = require('fs');
const fsx = require('fs-extra');
const prompt = require('prompt');
const Log = require('../../lib/Log');
const Spinner = require('../../lib/Spinner');

function builder(yargs) {
return yargs
.option('host', {
.positional('path', {
type: 'string',
describe: 'IPFS daemon API server host',
default: 'ipfs.infura.io',
describe: 'the path of the file or the folder which you want to upload to IPFS',
default: '.'
})
.option('port', {
type: 'string',
describe: 'IPFS daemon API server port',
default: '5001',
})
.option('protocol', {
type: 'string',
describe: 'IPFS daemon API server protocol',
default: 'https',
});
.example('kaizen ipfs upload . => to upload the current folder')
.example('kaizen ipfs upload ./build => to upload the build folder in the current folder')
}

async function handler(argv) {
try {
const result = await confirmUploadDialog();
const targetPath = path.resolve('./', argv.path);
const result = await confirmUploadDialog(targetPath);
if (/^yes|y$/i.test(result.confirm) === false) {
Log.SuccessLog(`==== Cancel Upload ====`);
return;
}

Spinner.start();
const { protocol, host, port, } = argv;
const ipfs = IPFS_API(host, port, { protocol, });
const targetPath = path.resolve('./');
const files = recursiveFetchFilePath(targetPath).map(file => getIPFSContentObject(file, targetPath));
const hashes = await ipfs.files.add(files);
fs.writeFileSync(path.resolve('./', 'ipfs.json'), JSON.stringify(hashes));

const configPath = path.resolve('./', 'kaizen.json');
if (fsx.existsSync(configPath) === false) {
Spinner.stop();
Log.ErrorLog('Missing kaizen.json, you should use「kaizen set-ipfs」command to setting IPFS configuration');
return;
}


const kaizenConfig = fsx.readJsonSync(configPath);
if (!kaizenConfig.ipfs) {
Spinner.stop();
Log.ErrorLog('Missing kaizen.json, you should use「kaizen set-ipfs」command to setting IPFS configuration');
return;
}

if(!fs.existsSync(targetPath)) {
Spinner.stop();
Log.ErrorLog('The path that you specify is not exist');
return;
}

const {
host,
port,
protocol
} = kaizenConfig.ipfs;

const ipfs = IPFS_API(host, port, { protocol });

if (fs.lstatSync(targetPath).isDirectory()) {
const files = recursiveFetchFilePath(targetPath).map(file => getIPFSContentObject(file, targetPath));
const hashes = await ipfs.files.add(files);
fs.writeFileSync(path.resolve('./', 'ipfs.json'), JSON.stringify(hashes));
} else {
const hashes = await ipfs.files.add(fs.readFileSync(targetPath));
fs.writeFileSync(path.resolve('./', 'ipfs.json'), JSON.stringify(hashes));
}

Spinner.stop();
Log.SuccessLog(`==== Upload your files to IPFS Successfully ====`);
} catch (error) {
Expand All @@ -48,11 +75,11 @@ async function handler(argv) {
}
}

function confirmUploadDialog() {
function confirmUploadDialog(targetPath) {
const promptSchema = {
properties: {
confirm: {
message: 'Please ensure you will upload files in current folder to the IPFS (yes/no)',
message: `Please ensure you will upload ${targetPath}to the IPFS (yes/no)`,
required: true
},
}
Expand Down Expand Up @@ -95,7 +122,7 @@ function getIPFSContentObject(filePath, targetPath) {


module.exports = function (yargs) {
const command = 'ipfs upload';
const commandDescription = 'To upload files in folder where the terminal currently in to IPFS';
const command = 'ipfs upload [path]';
const commandDescription = 'To upload file or folder to IPFS';
yargs.command(command, commandDescription, builder, handler);
}

0 comments on commit 267d0e8

Please sign in to comment.