1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var path = require('path');
|
22 | var util = require('util');
|
23 | var colors = require('colors');
|
24 |
|
25 |
|
26 | var conf = {
|
27 | url: '',
|
28 | types: [ 'email', 'url', 'uuid' ],
|
29 | modelPath: path.join(__dirname, 'models')
|
30 | };
|
31 |
|
32 |
|
33 | if (process.env.DISABLE_COLORS) {
|
34 | colors.mode = 'none';
|
35 | }
|
36 |
|
37 |
|
38 | console.log('Loading REPL...'.grey);
|
39 |
|
40 |
|
41 | global.REPL_PROMPT = process.env.REPL_PROMPT || '> ';
|
42 | global.INSPECT_DEPTH = Number(process.env.INSPECT_DEPTH);
|
43 | if (isNaN(global.INSPECT_DEPTH)) {
|
44 | global.INSPECT_DEPTH = 2;
|
45 | }
|
46 |
|
47 |
|
48 | var models = global.models = require('mongoose-models');
|
49 | models.init(conf);
|
50 |
|
51 |
|
52 | models._require = models.require;
|
53 | models.require = function(model) {
|
54 | global[model] = models._require(model);
|
55 | };
|
56 |
|
57 |
|
58 | var print = global.print = function() {
|
59 | console.log.apply(console, arguments);
|
60 | process.stdout.write(global.REPL_PROMPT);
|
61 | };
|
62 |
|
63 |
|
64 | var store = global.store = function(name) {
|
65 | return function() {
|
66 | var args = arguments;
|
67 | process.nextTick(function() {
|
68 | global[name] = args;
|
69 | print(('\nStored ' + args.length + ' arguments to "' + name + '"').green.italic);
|
70 | process.stdout.write(buffer);
|
71 | });
|
72 | };
|
73 | };
|
74 |
|
75 |
|
76 | process.stdin.resume();
|
77 | process.stdin.setEncoding('utf8');
|
78 |
|
79 |
|
80 |
|
81 | var buffer = '';
|
82 | process.stdin.on('keypress', function(ch) {
|
83 | buffer += ch;
|
84 | });
|
85 |
|
86 |
|
87 | process.stdin.on('data', function (data) {
|
88 | buffer = '';
|
89 | print(util.inspect(eval(data), true, global.INSPECT_DEPTH, ! process.env.DISABLE_COLORS));
|
90 | });
|
91 |
|
92 |
|
93 | process.on('SIGINT', function() {
|
94 | console.log('\nGood Bye.'.grey)
|
95 | process.exit();
|
96 | });
|
97 |
|
98 |
|
99 | process.on('uncaughtException', function(err) {
|
100 | if (typeof err === 'object' && err && err.stack) {
|
101 | err = err.stack;
|
102 | }
|
103 | print(err.red);
|
104 | });
|
105 |
|
106 |
|
107 | process.stdout.write(global.REPL_PROMPT);
|
108 |
|
109 |
|