Skip to content

Commit

Permalink
multiple minor improvements and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shivkanthb committed Mar 13, 2019
1 parent 4ea553f commit 3b0fe2b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 67 deletions.
17 changes: 12 additions & 5 deletions cmds/help.js
Expand Up @@ -6,15 +6,11 @@ const menus = {
history .............. history of requests previously run
collections .......... view all stored request collections
new .................. add a new requestion or collection
delete ............... delete requests or collections
version .............. show package version
help ................. show help menu for a command
If no command => it runs the standard curl command incl all flags
Additional options:
--sb .............. uses sandbox host
--qs .............. pass querystring values as a file path or json string
--headers .............. pass headers as a file path or json string
`,

new: `
Expand All @@ -29,6 +25,17 @@ const menus = {
id ................... runs request {id} present in history
collection_name:id ... runs request {id} present inside {collection_name}
`,

delete: `
cx delete <id | collection_name:id>
id ................... runs request {id} present in history
collection_name:id ... runs request {id} present inside {collection_name}
cx delete <collection_name> --collection
collection_name ...... name of the entire collection you want to delete
`

}
Expand Down
24 changes: 13 additions & 11 deletions cmds/new.js
@@ -1,8 +1,10 @@
const { outputCollectionExists,
outputCollectionNotExists,
outputDefaultNewChoice } = require('./../output');
const { wrapArguments } = require('./../helpers');
const shortid = require('shortid')
const { wrapArguments,
buildExecString } = require('./../helpers');
const { sanitizeCurlArgs } = require('./../helpers/parse-curl');
const shortid = require('shortid');
const { parseCurlCommand } = require('../helpers/parse-curl');
const {
askCollectionName,
Expand Down Expand Up @@ -37,8 +39,7 @@ module.exports = (args, db) => {

async function newRequest(db) {
let collectionName = await askCollectionName();
// check if user provided collection first exists
if (!db.getCollection(collectionName)) {
if (!db.getCollection(collectionName)) { // check if user provided collection first exists
outputCollectionNotExists();
return;
}
Expand All @@ -51,8 +52,7 @@ async function newCollection(db) {
if (db.getCollection(collectionName)) {
outputCollectionExists();
} else {
// adding new collection if doesn't exist
db.addCollection(collectionName);
db.addCollection(collectionName); // adding new collection if doesn't exist
}
proceed = await askAddNewRequest(collectionName);
if (!proceed) {
Expand All @@ -66,8 +66,12 @@ async function addNewRequestToCollection(db, collectionName) {
if (requestDetailsObj.cx_result) {
let shortID = shortid.generate();
let _args = requestDetailsObj.cx_result.split(/\s+/);
let cmd_string = wrapArguments(_args);
let exec_str = 'curl -i ' + cmd_string.join(' ');
if (_args[0].toLowerCase() === 'curl' || _args[0].toLowerCase() === 'cx') {
_args = _args.slice(1);
}
let _cmdArgs = sanitizeCurlArgs(_args)
let cmd_string = wrapArguments(_cmdArgs);
let exec_str = buildExecString(cmd_string);
let curlObject = parseCurlCommand(exec_str);
let cmd = {
id: shortID,
Expand All @@ -76,9 +80,7 @@ async function addNewRequestToCollection(db, collectionName) {
command: exec_str,
url: curlObject.url,
}
console.log(cmd);
// Saving the new request
db.addRequestToCollection(collectionName, cmd);
db.addRequestToCollection(collectionName, cmd); // Saving the new request
}
}

5 changes: 0 additions & 5 deletions fb_config.json

This file was deleted.

3 changes: 0 additions & 3 deletions headers.json

This file was deleted.

4 changes: 4 additions & 0 deletions helpers/index.js
Expand Up @@ -58,5 +58,9 @@ module.exports = {
return "\"" + arg.replace(/'/g, '"') + "\"";
});
return cmd_string;
},

buildExecString: function(cmd_string) {
return 'curl -i ' + cmd_string.join(' ');
}
}
17 changes: 6 additions & 11 deletions index.js
@@ -1,5 +1,7 @@
const minimist = require('minimist')
const { sanitizeCurlArgs } = require('./helpers/parse-curl')
const minimist = require('minimist');
const { sanitizeCurlArgs } = require('./helpers/parse-curl');
const { wrapArguments,
buildExecString } = require('./helpers');
const Storage = require('./storage').Database;
const { outputEmptyArgsError } = require('./output');
let db = new Storage();
Expand All @@ -13,15 +15,7 @@ module.exports = () => {
return;
}

let cmd_string = process.argv.slice(2).map((arg) => {
// if (/\s/g.test(arg)) {
// return "'" + arg.replace(/'/g, "'\\''") + "'";
// }
// return arg;
return "\"" + arg.replace(/'/g, '"') + "\"";
});

let exec_str = 'curl -i ' + cmd_string.join(' ');
let cmd_string = wrapArguments(cmdArgs);
let cmd = args._[0]

if (args.version || args.v) {
Expand Down Expand Up @@ -66,6 +60,7 @@ module.exports = () => {
break

default:
let exec_str = buildExecString(cmd_string);
require('./cmds/curlx')(args, exec_str, db)
break
}
Expand Down
4 changes: 0 additions & 4 deletions model.js

This file was deleted.

21 changes: 3 additions & 18 deletions prompts/index.js
@@ -1,6 +1,6 @@
const prompts = require('prompts');

let onCancel = prompt => {
let onCancel = () => {
console.log('See you next time ✌️');
return true;
}
Expand All @@ -18,31 +18,16 @@ async function askRequestInfo(collectionName) {
let questions = [{
type: 'text',
name: 'cx_result',
message: 'Enter complete request (without cx or curl in front). Eg- -X GET https://httpbin.org/get'
message: 'Enter complete request (without cx or curl in front). Eg: -X GET https://httpbin.org/get'
},
{
type: prev => (prev.length > 0) ? 'text' : null,
name: 'cx_result_name',
initial: `${collectionName} - request `,
initial: `${collectionName} - request${Math.floor(Math.random()*(999-100+1)+100)}`,
message: 'Give a name for your request'
}];
let response = await prompts(questions, { onCancel });
return response;

if (response.cx_result) {
let _args = response.cx_result.split(/\s+/);
let cmd_string = wrapArguments(_args);
let exec_str = 'curl -i ' + cmd_string.join(' ');
let curlObject = parseCurlCommand(exec_str);
let cmd = {
id: shortID,
name: response.cx_result_name,
method: curlObject.method,
command: exec_str,
url: curlObject.url,
}
return cmd;
}
}

async function askAddNewRequest(collectionName) {
Expand Down
11 changes: 1 addition & 10 deletions storage.js
Expand Up @@ -3,12 +3,11 @@ const FileSync = require('lowdb/adapters/FileSync')
const os = require('os');
const path = require('path');
const fs = require('fs');
const model = require('./model');
let dbdir = os.homedir() + '/cxdb';
let dbPath = path.join(os.homedir(), 'gx.json');
let dbHistoryPath = path.join(dbdir, 'history.json');
let dbCollectionPath = path.join(dbdir, 'collection.json');
let adapter, db;
let db;


function createDbFolderIfNotExist() {
Expand Down Expand Up @@ -103,14 +102,6 @@ class Database {
}

getCollections() {
// try {
// let data = fs.readFileSync(dbPath, 'utf-8');
// this.data = JSON.parse(data);
// this.collections = this.data.collections;
// return Object.keys(this.collections);
// } catch (err) {
// console.log(err);
// }
return db.get('collections')
.value()
}
Expand Down

0 comments on commit 3b0fe2b

Please sign in to comment.