Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Refactor exitOnError
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Nov 5, 2019
1 parent 4ba6929 commit 5244d9c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
34 changes: 13 additions & 21 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const yargs = require('yargs');
const main = require('../');

const exitOnError = async function(promise) {
try {
await promise;
} catch (e) {
console.log('Error: ', e);
process.exit(1);
}
};
const exitOnError = require('../utils/exit-on-error')

// For account:
const createAccount = {
Expand All @@ -35,14 +27,14 @@ const createAccount = {
type: 'string',
default: '1000000000000000000'
}),
handler: (argv) => exitOnError(main.createAccount(argv))
handler: exitOnError(main.createAccount)
};

const login = {
command: 'login',
desc: 'create a developer account',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.login(argv))
handler: exitOnError(main.login)
};

const viewAccount = {
Expand All @@ -54,7 +46,7 @@ const viewAccount = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.viewAccount(argv))
handler: exitOnError(main.viewAccount)
};

const deleteAccount = {
Expand All @@ -71,7 +63,7 @@ const deleteAccount = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.deleteAccount(argv))
handler: exitOnError(main.deleteAccount)
};

const keys = {
Expand All @@ -83,14 +75,14 @@ const keys = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.keys(argv))
handler: exitOnError(main.keys)
};

const sendMoney = {
command: 'send <sender> <receiver> <amount>',
desc: 'send tokens to given receiver',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.sendMoney(argv))
handler: exitOnError(main.sendMoney)
};

const stake = {
Expand All @@ -112,7 +104,7 @@ const stake = {
type: 'string',
required: true,
}),
handler: (argv) => exitOnError(main.stake(argv))
handler: exitOnError(main.stake)
};

// For contract:
Expand All @@ -128,7 +120,7 @@ const deploy = {
.alias({
'accountId': ['account_id', 'contractName', 'contract_name'],
}),
handler: (argv) => exitOnError(main.deploy(argv))
handler: exitOnError(main.deploy)
};

const scheduleFunctionCall = {
Expand All @@ -140,14 +132,14 @@ const scheduleFunctionCall = {
type: 'string',
default: '1000000000'
}),
handler: (argv) => exitOnError(main.scheduleFunctionCall(argv))
handler: exitOnError(main.scheduleFunctionCall)
};

const callViewFunction = {
command: 'view <contractName> <methodName> [args]',
desc: 'make smart contract call which can view state',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.callViewFunction(argv))
handler: exitOnError(main.callViewFunction)
};

const { spawn } = require('child_process');
Expand Down Expand Up @@ -178,7 +170,7 @@ const txStatus = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.txStatus(argv))
handler: exitOnError(main.txStatus)
};

const clean = {
Expand All @@ -190,7 +182,7 @@ const clean = {
type: 'string',
default: './out'
}),
handler: (argv) => exitOnError(main.clean(argv))
handler: exitOnError(main.clean)
};

let config = require('../get-config')();
Expand Down
9 changes: 9 additions & 0 deletions utils/exit-on-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = (promiseFn) => async (...args) => {
const promise = promiseFn.apply(null, args);
try {
await promise;
} catch (e) {
console.log('Error: ', e);
process.exit(1);
}
};

0 comments on commit 5244d9c

Please sign in to comment.