Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moved logic from src/node.js

The logic for saving and loading repl history, and for loading .noder.js
files into the repl context have been moved.

This also patches some problems with finding the home directory on
windows. (Shout out to my mom for still using windows.)
  • Loading branch information...
commit d01da283886eee9e4b7290c0f50fe02114b526f3 1 parent 8ffe356
@regality regality authored
Showing with 54 additions and 65 deletions.
  1. +33 −0 lib/repl.js
  2. +21 −65 src/node.js
View
33 lib/repl.js
@@ -352,6 +352,19 @@ REPLServer.prototype.resetContext = function(force) {
this.context = context;
};
+REPLServer.prototype.loadContext = function(file) {
+ var Module = require('module');
+ var context;
+ context = Module._load(file);
+ if (context && typeof context === 'object') {
+ for (var key in context) {
+ if (context.hasOwnProperty(key)) {
+ this.context[key] = context[key];
+ }
+ }
+ }
+};
+
REPLServer.prototype.displayPrompt = function(preserveCursor) {
var prompt = this.prompt;
if (this.bufferedCommand.length) {
@@ -371,6 +384,26 @@ REPLServer.prototype.getHistory = function() {
return this.rli.getHistory();
};
+REPLServer.prototype.loadHistory = function(file, callback) {
+ var self = this;
+ callback = callback || function() {};
+ if (!file) return callback();
+ fs.readFile(file, function(err, data) {
+ if (err) return callback(err);
+ var history = data.toString().split('\n');
+ self.setHistory(history);
+ callback(null);
+ });
+};
+
+REPLServer.prototype.saveHistory = function(file, callback) {
+ if (!file) return callback();
+ var history = this.getHistory().join('\n');
+ fs.writeFile(file, history, function(err) {
+ callback(err);
+ });
+};
+
// A stream to push an array into a REPL
// used in REPLServer.complete
function ArrayStream() {
View
86 src/node.js
@@ -125,36 +125,35 @@
opts.useColors = false;
}
- var home = process.env.HOME;
- var pwd = path.resolve('.');
+ var repl = Module.requireRepl().start(opts);
+
+ var home = (process.platform === 'win32' ?
+ process.env.USERDATA :
+ process.env.HOME);
if (home) {
- var noderc = path.join(home, ".noderc.js");
- var homeContext = loadContext(noderc);
- }
- if (pwd) {
- var pwdrc = path.join(pwd, ".noderc.js");
- var pwdContext = loadContext(pwdrc);
+ var noderc = path.join(home, '.noderc.js');
+ var nodeHistory = path.join(home, '.node_history');
}
+ var pwd = path.resolve('.');
+ var pwdrc = path.join(pwd, '.noderc.js');
+ [noderc, pwdrc].forEach(function(file) {
+ try {
+ repl.loadContext(file);
+ } catch (e) {
+ if (e.code !== 'MODULE_NOT_FOUND') {
+ // rethrow any errors generated from noderc
+ throw e;
+ }
+ }
+ });
- var repl = Module.requireRepl().start(opts);
- loadReplHistory(repl);
+ repl.loadHistory(nodeHistory);
repl.on('exit', function() {
- saveReplHistory(repl, function() {
+ repl.saveHistory(nodeHistory, function() {
process.exit();
});
});
- for (var i in homeContext) {
- if (homeContext.hasOwnProperty(i)) {
- repl.context[i] = homeContext[i];
- }
- }
- for (var i in pwdContext) {
- if (pwdContext.hasOwnProperty(i)) {
- repl.context[i] = pwdContext[i];
- }
- }
-
} else {
// Read all of stdin - execute it.
process.stdin.resume();
@@ -173,49 +172,6 @@
}
}
- function loadContext(file) {
- var Module = NativeModule.require('module');
- var context;
- try {
- context = Module._load(file);
- } catch (e) {
- context = {};
- }
- if (!context || typeof context !== 'object') {
- context = {};
- }
- return context;
- }
-
- var nodeHistory;
-
- function loadReplHistory(repl) {
- var fs = NativeModule.require('fs');
- var path = NativeModule.require('path');
- var home = process.env.HOME;
-
- if (!home) return;
- nodeHistory = path.join(home, ".node_history");
-
- fs.readFile(nodeHistory, function(err, data) {
- if (err) return;
- try {
- var history = JSON.parse(data);
- repl.setHistory(history);
- } catch (e) { }
- });
- }
-
- function saveReplHistory(repl, callback) {
- var fs = NativeModule.require('fs');
- var history = JSON.stringify(repl.getHistory(), false, 1);
- if (!nodeHistory) return callback();
-
- fs.writeFile(nodeHistory, history, function(err) {
- callback();
- });
- }
-
startup.globalVariables = function() {
global.process = process;
global.global = global;
Please sign in to comment.
Something went wrong with that request. Please try again.