Skip to content

EDP34: Attempt to use mongocli #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
189 changes: 93 additions & 96 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as _ from 'lodash';
import * as child_process from 'child_process';
import * as os from 'os';
import * as util from 'util';
import nodeify from 'nodeify-ts';
import * as _ from "lodash";
import * as child_process from "child_process";
import * as os from "os";
import * as util from "util";
import nodeify from "nodeify-ts";
const exec = child_process.exec;

const cleanJson = function (line) {
const cleanJson = function(line) {
let parts, re, str, m, value, value2, comma;

if (line.indexOf('ISODate') > -1) {
parts = line.split(':');
if (line.indexOf("ISODate") > -1) {
parts = line.split(":");

re = /ISODate\("(.*)"/g;
str = line;
Expand All @@ -21,12 +21,12 @@ const cleanJson = function (line) {
// View your result using the m-constiable.
// eg m[0] etc.
//console.log('m', m);
value = m && m[1] ? m[1] : 'error';
value = m && m[1] ? m[1] : "error";
}

return util.format('%s : { "$date" : "%s" },', parts[0], value);
} else if (line.indexOf('ObjectId') > -1) {
parts = line.split(':');
} else if (line.indexOf("ObjectId") > -1) {
parts = line.split(":");

re = /ObjectId\("(.*)"/g;
str = line;
Expand All @@ -38,12 +38,12 @@ const cleanJson = function (line) {
// View your result using the m-constiable.
// eg m[0] etc.
//console.log('m', m);
value = m && m[1] ? m[1] : 'error';
value = m && m[1] ? m[1] : "error";
}

return util.format('%s : { "$oid" : "%s" },', parts[0], value);
} else if (line.indexOf('NumberLong') > -1) {
parts = line.split(':');
} else if (line.indexOf("NumberLong") > -1) {
parts = line.split(":");

re = /NumberLong\((.*)\)(,*)/g;
str = line;
Expand All @@ -55,14 +55,18 @@ const cleanJson = function (line) {
// View your result using the m-constiable.
// eg m[0] etc.
//console.log('m', m);
value = m && m[1] ? m[1] : 'error';
comma = m && m[2] ? m[2] : '';
value = m && m[1] ? m[1] : "error";
comma = m && m[2] ? m[2] : "";
}

return util.format('%s : { "$numberLong" : "%s" }%s', parts[0], value, comma);

} else if (line.indexOf('Timestamp') > -1) {
parts = line.split(':');
return util.format(
'%s : { "$numberLong" : "%s" }%s',
parts[0],
value,
comma
);
} else if (line.indexOf("Timestamp") > -1) {
parts = line.split(":");

re = /Timestamp\((\d*),\s*(\d*)\)/g;
str = line;
Expand All @@ -74,95 +78,89 @@ const cleanJson = function (line) {
// View your result using the m-constiable.
// eg m[0] etc.
//console.log('m', m);
value = m && m[1] ? m[1] : 'error';
value2 = m && m[2] ? m[2] : 'error';
value = m && m[1] ? m[1] : "error";
value2 = m && m[2] ? m[2] : "error";
}
//return '';
return util.format('%s : { "$timestamp" : { "t" : %s, "i" : %s } },', parts[0], value, value2);


return util.format(
'%s : { "$timestamp" : { "t" : %s, "i" : %s } },',
parts[0],
value,
value2
);
} else {

return line;
}


};


const objectStartingRow = function (resultp) {
const objectStartingRow = function(resultp) {
const lines = resultp.raw.split(os.EOL);
resultp.lines = _.clone(lines);

let jsonStartIndex = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
//console.log('line', line);
if (line === '{') {
if (line === "{") {
jsonStartIndex = i;
break;
}
}

//console.log('jsonStartIndex', jsonStartIndex);


try {
const tempArray = lines.splice(jsonStartIndex);
//console.log('tempArray', tempArray);

const jsonString = tempArray.reduce(function (previousValue, currentValue) {
const jsonString = tempArray.reduce(function(previousValue, currentValue) {
const cleaned = cleanJson(currentValue.trim());
return previousValue + cleaned;
}, '');
}, "");
//console.log('jsonString', jsonString);
resultp.object = JSON.parse(jsonString);

} catch (e) {
resultp.error = e;
}

return resultp;

};


const extractResult = function (result) {
const extractResult = function(result) {
const extracterArray = [
{
re: /"db.isMaster/,
run: function (resultp) {
run: function(resultp) {
return objectStartingRow(resultp);
},
},
{
re: /"rs.conf/,
run: function (resultp) {
run: function(resultp) {
return objectStartingRow(resultp);
},
},
{
re: /"rs.initiate/,
run: function (resultp) {
run: function(resultp) {
return objectStartingRow(resultp);
},
},
{
re: /"rs.add/,
run: function (resultp) {
run: function(resultp) {
return objectStartingRow(resultp);
},
},
{
re: /"rs.status/,
run: function (resultp) {
run: function(resultp) {
return objectStartingRow(resultp);
},
},
];


extracterArray.forEach(function (extracter) {
extracterArray.forEach(function(extracter) {
const re = extracter.re;
const str = result.command;
let m;
Expand All @@ -181,57 +179,56 @@ const extractResult = function (result) {
};

export class Mongo {

constructor(private options: IOptions = new Options()) { }
constructor(private options: IOptions = new Options()) {}

public command(command: string, callback?: (err, data) => void) {
let mongo = this;
let execCommand = 'mongo ';

const promise = Promise.resolve().then(function () {
//console.log('execCommand =', execCommand);

let params = mongo.options.toParams();
//console.log('params = ', params);

execCommand += ` ${params} --eval "${command}" `;

let execOptions = {
cwd: mongo.options.currentWorkingDirectory,
env: {
DEBUG: '',
EDITOR: process.env.EDITOR,
HOME: process.env.HOME,
HOMEDRIVE: process.env.HOMEDRIVE,
HOMEPATH: process.env.HOMEPATH,
PATH: process.env.PATH,
},
maxBuffer: 200 * 1024 * 1024,
};

//console.log('exec options =', execOptions);

return new Promise(function (resolve, reject) {
exec(execCommand, execOptions, (error, stdout, stderr) => {
if (error) {
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
console.error(message);
reject(message);
}
//console.log(`stdout: ${stdout}`);
resolve({ stderr: stderr, stdout: stdout });
let execCommand = "mongocli ";

const promise = Promise.resolve()
.then(function() {
//console.log('execCommand =', execCommand);

let params = mongo.options.toParams();
//console.log('params = ', params);

execCommand += ` ${params} --eval "${command}" `;

let execOptions = {
cwd: mongo.options.currentWorkingDirectory,
env: {
DEBUG: "",
EDITOR: process.env.EDITOR,
HOME: process.env.HOME,
HOMEDRIVE: process.env.HOMEDRIVE,
HOMEPATH: process.env.HOMEPATH,
PATH: process.env.PATH,
},
maxBuffer: 200 * 1024 * 1024,
};

//console.log('exec options =', execOptions);

return new Promise(function(resolve, reject) {
exec(execCommand, execOptions, (error, stdout, stderr) => {
if (error) {
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
console.error(message);
reject(message);
}
//console.log(`stdout: ${stdout}`);
resolve({ stderr: stderr, stdout: stdout });
});
});
})
.then(function(data: { stderr: string; stdout: string }) {
let result = {
command: execCommand,
error: data.stderr,
raw: data.stdout,
};
return extractResult(result);
});
}).then(function (data: { stderr: string, stdout: string }) {

let result = {
command: execCommand,
error: data.stderr,
raw: data.stdout,
};
return extractResult(result);

});

return nodeify(promise, callback);
}
Expand All @@ -247,19 +244,19 @@ export interface IOptions {
export class Options implements IOptions {
public constructor(
public currentWorkingDirectory?: string,
public host = 'localhost',
public port = '27017',
) { }
public host = "localhost",
public port = "27017"
) {}

public toParams(): string {
const params = Object.keys(this).reduce((previousValue, key) => {
if (key === 'currentWorkingDirectory') {
if (key === "currentWorkingDirectory") {
return previousValue;
}
const value = this[key];
//const key2 = _.snakeCase(key).replace('_', '-');
return `${previousValue} --${key} ${value}`;
}, '');
}, "");

return params;
}
Expand Down