Skip to content

Commit ea78735

Browse files
Merge pull request #21 from TobaniEG/master
General patches and code clean-up
2 parents d74f224 + d0d6c0b commit ea78735

25 files changed

Lines changed: 11347 additions & 11093 deletions

.DS_Store

-6 KB
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
22
db.json
3+
4+
.DS_Store

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"printWidth": 80,
5+
"tabWidth": 2
6+
}

cli.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
#!/usr/bin/env node
2-
global.vorpal = require('vorpal')();
3-
vorpal.isCommandArgKeyPairNormalized = false;
4-
const shell = require('shelljs');
5-
global.db = require('./lib/db')
2+
3+
import vorpal from 'vorpal';
4+
import shell from 'shelljs';
5+
import * as db from './lib/db.js';
6+
7+
globalThis.vorpal = new vorpal();
8+
globalThis.db = db;
9+
10+
globalThis.vorpal.isCommandArgKeyPairNormalized = false;
11+
612
if (!shell.which('git')) {
713
shell.echo('Sorry, this script requires git');
814
shell.exit(1);
915
}
1016

11-
vorpal
12-
.delimiter('paystack $')
13-
.show();
14-
17+
globalThis.vorpal.delimiter('paystack $').show();
1518

16-
require('./commands/webhook')();
17-
require('./commands/api')();
18-
require('./commands/auth')();
19-
require('./commands/samples')();
20-
// module.exports = {vorpal, db, shell};
19+
import webhook from './commands/webhook.js';
20+
import api from './commands/api.js';
21+
import auth from './commands/auth.js';
22+
import samples from './commands/samples.js';
2123

24+
webhook();
25+
api();
26+
auth();
27+
samples();

commands/api.js

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
1-
const APIs = require('../lib/paystack/apis')
2-
const helpers = require('../lib/helpers')
3-
4-
let commands = Object.keys(APIs)
5-
const init = () => {
6-
commands.forEach((command) => {
7-
let section = APIs[command];
8-
let vorp = vorpal.command(command + ' <command>', helpers.getDescription(section, command))
9-
.validate(function (args) {
10-
let selected_integration = db.read('selected_integration.id');
11-
let user = db.read('user.id')
12-
if (!selected_integration || !user) {
13-
helpers.errorLog("You're not signed in, please run the `login` command before you begin");
14-
return false;
15-
}
16-
})
17-
.action(async function (args, callback) {
18-
let schema = JSON.parse(JSON.stringify(helpers.findSchema(command, args)))
19-
let [err, result] = await helpers.promiseWrapper(helpers.executeSchema(schema, args))
20-
if (err) {
1+
import APIs from '../lib/paystack/apis.js';
2+
import * as helpers from '../lib/helpers.js';
213

22-
if (err.response) {
23-
helpers.errorLog(err.response.data.message)
24-
return;
25-
}
26-
helpers.errorLog(err)
27-
return;
28-
}
29-
helpers.successLog(result.message)
30-
helpers.jsonLog(result.data)
31-
})
32-
vorp.option('--domain <value>', ' ');
33-
let added_options = ['domain'];
34-
section.forEach((f) => {
35-
f.params.forEach((o) => {
36-
if (added_options.indexOf(o.parameter) < 0) {
37-
vorp.option('--' + o.parameter + ' <value> ', ' ')
38-
added_options.push(o.parameter)
39-
}
4+
let commands = Object.keys(APIs);
405

41-
})
42-
})
43-
})
44-
}
6+
const init = () => {
7+
commands.forEach((command) => {
8+
let section = APIs[command];
9+
let vorp = vorpal
10+
.command(command + ' <command>', helpers.getDescription(section, command))
11+
.validate(function (args) {
12+
let selected_integration = db.read('selected_integration')['id'];
13+
let user = db.read('user')['id'];
14+
if (!selected_integration || !user) {
15+
helpers.errorLog(
16+
"You're not signed in, please run the `login` command before you begin"
17+
);
18+
return false;
19+
}
20+
})
21+
.action(async function (args, callback) {
22+
let schema = JSON.parse(
23+
JSON.stringify(helpers.findSchema(command, args))
24+
);
25+
let [err, result] = await helpers.promiseWrapper(
26+
helpers.executeSchema(schema, args)
27+
);
28+
if (err) {
29+
if (err.response) {
30+
helpers.errorLog(err.response.data.message);
31+
return;
32+
}
33+
helpers.errorLog(err);
34+
return;
35+
}
36+
helpers.successLog(result.message);
37+
helpers.jsonLog(result.data);
38+
});
39+
vorp.option('--domain <value>', ' ');
40+
let added_options = ['domain'];
41+
section.forEach((f) => {
42+
f.params.forEach((o) => {
43+
if (added_options.indexOf(o.parameter) < 0) {
44+
vorp.option('--' + o.parameter + ' <value> ', ' ');
45+
added_options.push(o.parameter);
46+
}
47+
});
48+
});
49+
});
50+
};
4551

46-
module.exports = init
52+
export default init;

commands/auth.js

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
1-
const helpers = require('../lib/helpers')
2-
const Paystack = require('../lib/Paystack')
3-
const init = function () {
4-
vorpal.command('login', 'Sign in with your Paystack username and password').action(async function (args, callback) {
5-
let token = ''
6-
let expiry = parseInt(db.read('token_expiry')) * 1000;
7-
let now = parseFloat(Date.now().toString())
8-
let user;
9-
if (expiry > now) {
10-
token = db.read('token');
11-
user = db.read('user');
12-
helpers.successLog("You're already logged in")
13-
}
14-
else {
15-
var email = helpers.prompt('Email address\n')
16-
let password = helpers.prompt('Password\n', true)
17-
var [e, response] = await helpers.promiseWrapper(Paystack.signIn(email, password))
1+
import * as helpers from '../lib/helpers.js';
2+
import * as Paystack from '../lib/Paystack.js';
3+
4+
const init = () => {
5+
vorpal
6+
.command('login', 'Sign in with your Paystack username and password')
7+
.action(async (_args, _callback) => {
8+
let token, user;
9+
let expiry = parseInt(db.read('token_expiry')) * 1000;
10+
let now = parseFloat(Date.now().toString());
11+
if (expiry > now) {
12+
token = db.read('token');
13+
user = db.read('user');
14+
helpers.successLog("You're already logged in");
15+
return;
16+
} else {
17+
const email = helpers.prompt('Email address\n');
18+
const password = helpers.prompt('Password\n', true);
1819

19-
if (response && !response.mfa_required) {
20-
token = response.token;
21-
user = response.user;
22-
db.write('token', token)
23-
db.write('user', user)
24-
helpers.successLog('Login successful')
25-
} else if (response && response.mfa_required) {
26-
var totp = helpers.prompt('*MFA required* Enter 6-digit verification code\n')
27-
var [e, response] = await helpers.promiseWrapper(Paystack.verifyMfa(totp, response.token))
28-
if (response) {
29-
token = response.token
30-
user = response.user
31-
db.write('token', token)
32-
db.write('user', user)
33-
helpers.successLog('Login successful')
34-
}
35-
} else{
36-
helpers.errorLog('Login failed')
37-
return;
38-
}
20+
const [e, response] = await helpers.promiseWrapper(
21+
Paystack.signIn(email, password),
22+
);
23+
24+
if (response && response.data) {
25+
Paystack.storeLoginDetails(response);
26+
token = response.data.token;
27+
user = response.data.user;
3928
}
40-
if (response || (token && user)) {
41-
var [err, integration] = await helpers.promiseWrapper(Paystack.selectIntegration(user.integrations, token))
42-
if (err) {
43-
helpers.errorLog(err);
44-
}
45-
db.write('selected_integration', integration);
46-
let user_role = db.read('selected_integration.logged_in_user_role');
47-
var [err, integrationData] = await helpers.promiseWrapper(Paystack.getIntegration(integration.id, token));
48-
if (err) {
49-
helpers.errorLog(err);
50-
return
51-
}
52-
integrationData.logged_in_user_role = user_role;
53-
db.write('selected_integration', integrationData);
54-
helpers.infoLog('Logged in as ' + user.email + ' - ' + integration.business_name + ' (' + integration.id + ')');
29+
}
30+
31+
if (token && user) {
32+
const [err, integration] = await helpers.promiseWrapper(
33+
Paystack.selectIntegration(user.integrations, token),
34+
);
35+
if (err) {
36+
helpers.errorLog(err);
5537
} else {
56-
helpers.errorLog(' - - - - - - ')
38+
db.write('selected_integration', integration);
39+
let user_role = db.read('selected_integration').logged_in_user_role;
40+
const [err, integrationData] = await helpers.promiseWrapper(
41+
Paystack.getIntegration(integration.id, token),
42+
);
43+
if (err) {
44+
helpers.errorLog(err);
45+
return;
46+
}
47+
integrationData.logged_in_user_role = user_role;
48+
db.write('selected_integration', integrationData);
49+
helpers.infoLog(
50+
'Logged in as ' +
51+
user.email +
52+
' - ' +
53+
integration.business_name +
54+
' (' +
55+
integration.id +
56+
')',
57+
);
5758
}
58-
})
59-
}
59+
} else {
60+
helpers.errorLog(' - - - - - - ');
61+
}
62+
});
63+
};
6064

61-
module.exports = init
65+
export default init;

commands/samples.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
2-
const helpers = require('../lib/helpers')
3-
const shell = require('shelljs');
1+
import * as helpers from '../lib/helpers.js';
2+
import shell from 'shelljs';
43

54
if (!shell.which('git')) {
65
shell.echo('Sorry, this script requires git');
76
shell.exit(1);
87
}
98

10-
let samples = require('../lib/samples')
9+
import samples from '../lib/samples.js';
10+
const keys = Object.keys(samples);
1111

1212
const init = () => {
13-
let keys = Object.keys(samples)
14-
vorpal.command('sample <sample_name> <outputDir>', 'Get started quickly with a Paystack sample project. Available samples are '+ keys.toString() )
13+
vorpal
14+
.command(
15+
'sample <sample_name> <outputDir>',
16+
'Get started quickly with a Paystack sample project. Available samples are ' +
17+
keys.toString(),
18+
)
1519
.validate(function (args) {
16-
let selected_integration = db.read('selected_integration.id');
17-
let user = db.read('user.id')
18-
if (!selected_integration || !user) {
19-
helpers.errorLog("You're not signed in, please run the `login` command before you begin");
20-
return false;
21-
}
22-
}).action(async (args, callback) => {
23-
24-
if(keys.indexOf(args.sample) < 0){
25-
helpers.errorLog("No sample app available with the name "+ args.sample);
26-
helpers.infoLog("Available samples are " + keys.toString())
27-
callback()
28-
return;
29-
}
30-
let sample = samples[args.sample];
31-
32-
shell.cd(args.filepath)
33-
shell.exec('git clone '+ sample.git)
34-
shell.cd(sample.name)
35-
shell.exec('npm install');
36-
shell.exec('npm start')
20+
let selected_integration = db.read('selected_integration').id;
21+
let user = db.read('user').id;
22+
if (!selected_integration || !user) {
23+
helpers.errorLog(
24+
"You're not signed in, please run the `login` command before you begin",
25+
);
26+
return false;
27+
}
3728
})
38-
}
29+
.action(async (args, callback) => {
30+
if (keys.indexOf(args.sample) < 0) {
31+
helpers.errorLog(
32+
'No sample app available with the name ' + args.sample,
33+
);
34+
helpers.infoLog('Available samples are ' + keys.toString());
35+
callback();
36+
return;
37+
}
38+
let sample = samples[args.sample];
3939

40+
shell.cd(args.filepath);
41+
shell.exec('git clone ' + sample.git);
42+
shell.cd(sample.name);
43+
shell.exec('npm install');
44+
shell.exec('npm start');
45+
});
46+
};
4047

41-
module.exports = init
48+
export default init;

0 commit comments

Comments
 (0)