Skip to content

Commit 3db59c7

Browse files
committed
0.0.1
1 parent d39540a commit 3db59c7

5 files changed

Lines changed: 85 additions & 26 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11

22
module.exports = function (grunt) {
33

4-
grunt.initConfig({
5-
shell: {
6-
exec: {
7-
options: {
8-
stdout: true,
9-
stderr: true
10-
}
11-
}
12-
},
4+
grunt.initConfig({
135
jshint: {
146
options: {
157
jshintrc: true,
@@ -21,7 +13,6 @@ module.exports = function (grunt) {
2113
]
2214
},
2315
mochaTest: {
24-
config: config,
2516
test: {
2617
options: {
2718
reporter: 'spec',

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ mongo.command('db.isMaster()').then(function (data) {
3535
});
3636

3737

38+
//data = {
39+
// command: 'mongo --host localhost --port 27017 --eval "db.isMaster()"',
40+
// raw: '["MongoDB shell version: 3.2.0\\nconnecting to: localhost:27017/test\\n{\\n\\t\\"ismaster\\" : false,\\n\\t\\"secondary\\" : false,\\n\\t\\"info\\" : \\"Does not have a valid replica set config\\",\\n\\t\\"isreplicaset\\" : true,\\n\\t\\"maxBsonObjectSize\\" : 16777216,\\n\\t\\"maxMessageSizeBytes\\" : 48000000,\\n\\t\\"maxWriteBatchSize\\" : 1000,\\n\\t\\"localTime\\" : ISODate(\\"2015-12-24T22:19:22.939Z\\"),\\n\\t\\"maxWireVersion\\" : 4,\\n\\t\\"minWireVersion\\" : 0,\\n\\t\\"ok\\" : 1\\n}\\n",""]',
41+
// lines:
42+
// ['MongoDB shell version: 3.2.0',
43+
// 'connecting to: localhost:27017/test'],
44+
// object:
45+
// {
46+
// ismaster: false,
47+
// secondary: false,
48+
// info: 'Does not have a valid replica set config',
49+
// isreplicaset: true,
50+
// maxBsonObjectSize: 16777216,
51+
// maxMessageSizeBytes: 48000000,
52+
// maxWriteBatchSize: 1000,
53+
// localTime: { '$date': '2015-12-24T22:19:22.939Z' },
54+
// maxWireVersion: 4,
55+
// minWireVersion: 0,
56+
// ok: 1
57+
// }
58+
//}
59+
3860
```
3961

4062
With callback:

lib/index.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ var Mongo = function (opts) {
3131

3232
Mongo.prototype.command = function (command, callback) {
3333
var self = this;
34-
var exec_command = 'mongo ';
34+
var exec_command = 'mongo';
3535

3636
return Promise.resolve().then(function () {
37-
var params = _.reduce(self.driver, function (result, value, key) {
37+
var params = _.reduce(self, function (result, value, key) {
3838
result += util.format('--%s %s ', key, value);
3939
return result;
4040
}, '');
4141

42-
exec_command += util.format(' %s --eval"%s"', params, command);
43-
console.log('exec_command =', exec_command);
42+
exec_command += util.format(' %s --eval "%s"', params, command);
43+
//console.log('exec_command =', exec_command);
4444

4545
var exec_options = {
4646
env: {
@@ -49,7 +49,6 @@ Mongo.prototype.command = function (command, callback) {
4949
EDITOR: process.env.EDITOR,
5050
HOMEDRIVE: process.env.HOMEDRIVE,
5151
HOMEPATH: process.env.HOMEPATH,
52-
PATH: process.env.PATH,
5352
DEBUG: ''
5453
}
5554
};
@@ -83,6 +82,24 @@ var extractResult = function (result) {
8382
var lines = obj[0].split(os.EOL);
8483
resultp.lines = lines;
8584

85+
try{
86+
var tempArray = lines.splice(2);
87+
//console.log('tempArray', tempArray);
88+
89+
var jsonString = tempArray.reduce(function (previousValue, currentValue) {
90+
var cleaned = cleanJsonDate(currentValue.trim());
91+
return previousValue + cleaned;
92+
}, '');
93+
//console.log('jsonString', jsonString);
94+
resultp.object = JSON.parse(jsonString);
95+
96+
97+
} catch (e) {
98+
resultp.error = e;
99+
}
100+
101+
102+
86103
return resultp;
87104
}
88105
}
@@ -106,3 +123,30 @@ var extractResult = function (result) {
106123

107124
return result;
108125
};
126+
127+
128+
var cleanJsonDate = function (line) {
129+
if (line.indexOf('ISODate') > -1) {
130+
var parts = line.split(':');
131+
132+
var re = /ISODate\("(.*)"/g;
133+
var str = line;
134+
var m, value;
135+
136+
while ((m = re.exec(str)) !== null) {
137+
if (m.index === re.lastIndex) {
138+
re.lastIndex++;
139+
}
140+
// View your result using the m-variable.
141+
// eg m[0] etc.
142+
//console.log('m', m);
143+
value = m && m[1] ? m[1] : 'error';
144+
}
145+
146+
return util.format('%s : { "$date" : "%s" },', parts[0], value);
147+
} else {
148+
return line;
149+
}
150+
151+
152+
};

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongo-cli-js",
33
"version": "0.0.1",
4-
"description": "A node.js wrapper for the docker-machine CLI",
4+
"description": "A node.js wrapper for the mongo CLI",
55
"main": "lib/index.js",
66
"scripts": {
77
"test": "grunt"
@@ -20,14 +20,12 @@
2020
},
2121
"homepage": "https://github.com/Quobject/mongo-cli-js",
2222
"devDependencies": {
23-
"grunt-shell": "~1.1.2",
2423
"grunt": "~0.4.5",
2524
"grunt-contrib-jshint": "~0.11.3",
2625
"mocha": "~2.3.3",
2726
"grunt-mocha-test": "~0.12.7",
2827
"should": "~7.1.0",
29-
"chai": "~3.3.0",
30-
"dockermachine": "0.0.5"
28+
"chai": "~3.3.0"
3129
},
3230
"dependencies": {
3331
"lodash": "~3.10.1",

test/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ var assert = require('chai').assert;
2424

2525
describe('Mongo', function () {
2626

27-
it('should merge opts', function () {
28-
var mongo = new Mongo({ a: 'a' });
29-
assert.isNotNull(mongo);
30-
assert.equal(mongo.a, 'a');
31-
console.log('mongo', mongo);
32-
});
27+
//it('should merge opts', function () {
28+
// var mongo = new Mongo({ a: 'a' });
29+
// assert.isNotNull(mongo);
30+
// assert.equal(mongo.a, 'a');
31+
// console.log('mongo', mongo);
32+
//});
3333

3434

3535
it('command db.isMaster() should pass', function (done) {
36-
var mongo = new Mongo();
36+
var mongo = new Mongo({
37+
host: 'localhost',
38+
port: 27017
39+
});
40+
3741

3842
assert.isNotNull(mongo);
3943
var failed = false;

0 commit comments

Comments
 (0)