Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
db.json

.DS_Store
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2
}
32 changes: 19 additions & 13 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
#!/usr/bin/env node
global.vorpal = require('vorpal')();
vorpal.isCommandArgKeyPairNormalized = false;
const shell = require('shelljs');
global.db = require('./lib/db')

import vorpal from 'vorpal';
import shell from 'shelljs';
import * as db from './lib/db.js';

globalThis.vorpal = new vorpal();
globalThis.db = db;

globalThis.vorpal.isCommandArgKeyPairNormalized = false;

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

vorpal
.delimiter('paystack $')
.show();

globalThis.vorpal.delimiter('paystack $').show();

require('./commands/webhook')();
require('./commands/api')();
require('./commands/auth')();
require('./commands/samples')();
// module.exports = {vorpal, db, shell};
import webhook from './commands/webhook.js';
import api from './commands/api.js';
import auth from './commands/auth.js';
import samples from './commands/samples.js';

webhook();
api();
auth();
samples();
92 changes: 49 additions & 43 deletions commands/api.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
const APIs = require('../lib/paystack/apis')
const helpers = require('../lib/helpers')

let commands = Object.keys(APIs)
const init = () => {
commands.forEach((command) => {
let section = APIs[command];
let vorp = vorpal.command(command + ' <command>', helpers.getDescription(section, command))
.validate(function (args) {
let selected_integration = db.read('selected_integration.id');
let user = db.read('user.id')
if (!selected_integration || !user) {
helpers.errorLog("You're not signed in, please run the `login` command before you begin");
return false;
}
})
.action(async function (args, callback) {
let schema = JSON.parse(JSON.stringify(helpers.findSchema(command, args)))
let [err, result] = await helpers.promiseWrapper(helpers.executeSchema(schema, args))
if (err) {
import APIs from '../lib/paystack/apis.js';
import * as helpers from '../lib/helpers.js';

if (err.response) {
helpers.errorLog(err.response.data.message)
return;
}
helpers.errorLog(err)
return;
}
helpers.successLog(result.message)
helpers.jsonLog(result.data)
})
vorp.option('--domain <value>', ' ');
let added_options = ['domain'];
section.forEach((f) => {
f.params.forEach((o) => {
if (added_options.indexOf(o.parameter) < 0) {
vorp.option('--' + o.parameter + ' <value> ', ' ')
added_options.push(o.parameter)
}
let commands = Object.keys(APIs);

})
})
})
}
const init = () => {
commands.forEach((command) => {
let section = APIs[command];
let vorp = vorpal
.command(command + ' <command>', helpers.getDescription(section, command))
.validate(function (args) {
let selected_integration = db.read('selected_integration')['id'];
let user = db.read('user')['id'];
if (!selected_integration || !user) {
helpers.errorLog(
"You're not signed in, please run the `login` command before you begin"
);
return false;
}
})
.action(async function (args, callback) {
let schema = JSON.parse(
JSON.stringify(helpers.findSchema(command, args))
);
let [err, result] = await helpers.promiseWrapper(
helpers.executeSchema(schema, args)
);
if (err) {
if (err.response) {
helpers.errorLog(err.response.data.message);
return;
}
helpers.errorLog(err);
return;
}
helpers.successLog(result.message);
helpers.jsonLog(result.data);
});
vorp.option('--domain <value>', ' ');
let added_options = ['domain'];
section.forEach((f) => {
f.params.forEach((o) => {
if (added_options.indexOf(o.parameter) < 0) {
vorp.option('--' + o.parameter + ' <value> ', ' ');
added_options.push(o.parameter);
}
});
});
});
};

module.exports = init
export default init;
116 changes: 60 additions & 56 deletions commands/auth.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
const helpers = require('../lib/helpers')
const Paystack = require('../lib/Paystack')
const init = function () {
vorpal.command('login', 'Sign in with your Paystack username and password').action(async function (args, callback) {
let token = ''
let expiry = parseInt(db.read('token_expiry')) * 1000;
let now = parseFloat(Date.now().toString())
let user;
if (expiry > now) {
token = db.read('token');
user = db.read('user');
helpers.successLog("You're already logged in")
}
else {
var email = helpers.prompt('Email address\n')
let password = helpers.prompt('Password\n', true)
var [e, response] = await helpers.promiseWrapper(Paystack.signIn(email, password))
import * as helpers from '../lib/helpers.js';
import * as Paystack from '../lib/Paystack.js';

const init = () => {
vorpal
.command('login', 'Sign in with your Paystack username and password')
.action(async (_args, _callback) => {
let token, user;
let expiry = parseInt(db.read('token_expiry')) * 1000;
let now = parseFloat(Date.now().toString());
if (expiry > now) {
token = db.read('token');
user = db.read('user');
helpers.successLog("You're already logged in");
return;
} else {
const email = helpers.prompt('Email address\n');
const password = helpers.prompt('Password\n', true);

if (response && !response.mfa_required) {
token = response.token;
user = response.user;
db.write('token', token)
db.write('user', user)
helpers.successLog('Login successful')
} else if (response && response.mfa_required) {
var totp = helpers.prompt('*MFA required* Enter 6-digit verification code\n')
var [e, response] = await helpers.promiseWrapper(Paystack.verifyMfa(totp, response.token))
if (response) {
token = response.token
user = response.user
db.write('token', token)
db.write('user', user)
helpers.successLog('Login successful')
}
} else{
helpers.errorLog('Login failed')
return;
}
const [e, response] = await helpers.promiseWrapper(
Paystack.signIn(email, password),
);

if (response && response.data) {
Paystack.storeLoginDetails(response);
token = response.data.token;
user = response.data.user;
}
if (response || (token && user)) {
var [err, integration] = await helpers.promiseWrapper(Paystack.selectIntegration(user.integrations, token))
if (err) {
helpers.errorLog(err);
}
db.write('selected_integration', integration);
let user_role = db.read('selected_integration.logged_in_user_role');
var [err, integrationData] = await helpers.promiseWrapper(Paystack.getIntegration(integration.id, token));
if (err) {
helpers.errorLog(err);
return
}
integrationData.logged_in_user_role = user_role;
db.write('selected_integration', integrationData);
helpers.infoLog('Logged in as ' + user.email + ' - ' + integration.business_name + ' (' + integration.id + ')');
}

if (token && user) {
const [err, integration] = await helpers.promiseWrapper(
Paystack.selectIntegration(user.integrations, token),
);
if (err) {
helpers.errorLog(err);
} else {
helpers.errorLog(' - - - - - - ')
db.write('selected_integration', integration);
let user_role = db.read('selected_integration').logged_in_user_role;
const [err, integrationData] = await helpers.promiseWrapper(
Paystack.getIntegration(integration.id, token),
);
if (err) {
helpers.errorLog(err);
return;
}
integrationData.logged_in_user_role = user_role;
db.write('selected_integration', integrationData);
helpers.infoLog(
'Logged in as ' +
user.email +
' - ' +
integration.business_name +
' (' +
integration.id +
')',
);
}
})
}
} else {
helpers.errorLog(' - - - - - - ');
}
});
};

module.exports = init
export default init;
65 changes: 36 additions & 29 deletions commands/samples.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@

const helpers = require('../lib/helpers')
const shell = require('shelljs');
import * as helpers from '../lib/helpers.js';
import shell from 'shelljs';

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

let samples = require('../lib/samples')
import samples from '../lib/samples.js';
const keys = Object.keys(samples);

const init = () => {
let keys = Object.keys(samples)
vorpal.command('sample <sample_name> <outputDir>', 'Get started quickly with a Paystack sample project. Available samples are '+ keys.toString() )
vorpal
.command(
'sample <sample_name> <outputDir>',
'Get started quickly with a Paystack sample project. Available samples are ' +
keys.toString(),
)
.validate(function (args) {
let selected_integration = db.read('selected_integration.id');
let user = db.read('user.id')
if (!selected_integration || !user) {
helpers.errorLog("You're not signed in, please run the `login` command before you begin");
return false;
}
}).action(async (args, callback) => {

if(keys.indexOf(args.sample) < 0){
helpers.errorLog("No sample app available with the name "+ args.sample);
helpers.infoLog("Available samples are " + keys.toString())
callback()
return;
}
let sample = samples[args.sample];

shell.cd(args.filepath)
shell.exec('git clone '+ sample.git)
shell.cd(sample.name)
shell.exec('npm install');
shell.exec('npm start')
let selected_integration = db.read('selected_integration').id;
let user = db.read('user').id;
if (!selected_integration || !user) {
helpers.errorLog(
"You're not signed in, please run the `login` command before you begin",
);
return false;
}
})
}
.action(async (args, callback) => {
if (keys.indexOf(args.sample) < 0) {
helpers.errorLog(
'No sample app available with the name ' + args.sample,
);
helpers.infoLog('Available samples are ' + keys.toString());
callback();
return;
}
let sample = samples[args.sample];

shell.cd(args.filepath);
shell.exec('git clone ' + sample.git);
shell.cd(sample.name);
shell.exec('npm install');
shell.exec('npm start');
});
};

module.exports = init
export default init;
Loading