fix all tests to run with node.js
This commit is contained in:
parent
d6ccdce008
commit
f07745839c
15 changed files with 181 additions and 125 deletions
|
|
@ -47,7 +47,7 @@ var Range = require("ace/range").Range;
|
||||||
var Document = function(text, mode) {
|
var Document = function(text, mode) {
|
||||||
|
|
||||||
this.modified = true;
|
this.modified = true;
|
||||||
this.lines = [""];
|
this.lines = [];
|
||||||
this.selection = new Selection(this);
|
this.selection = new Selection(this);
|
||||||
this.$breakpoints = [];
|
this.$breakpoints = [];
|
||||||
|
|
||||||
|
|
@ -623,7 +623,7 @@ var Document = function(text, mode) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.indentRows = function(range, indentString) {
|
this.indentRows = function(range, indentString) {
|
||||||
indentString.replace("\t", this.getTabString());
|
indentString = indentString.replace("\t", this.getTabString());
|
||||||
for (var row=range.start.row; row<=range.end.row; row++) {
|
for (var row=range.start.row; row<=range.end.row; row++) {
|
||||||
this.$insert({row: row, column:0}, indentString);
|
this.$insert({row: row, column:0}, indentString);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,14 +35,17 @@
|
||||||
*
|
*
|
||||||
* ***** END LICENSE BLOCK ***** */
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
require({
|
require("../../../support/paths");
|
||||||
paths: {
|
|
||||||
"ace": "../src/ace"
|
|
||||||
}},
|
|
||||||
["ace/test/assertions", "ace/test/ChangeDocumentTest"],
|
|
||||||
function(a) {
|
|
||||||
console.log(a)
|
|
||||||
alert("a " + a)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
|
var async = require("async");
|
||||||
|
|
||||||
|
async.concat(
|
||||||
|
require("./change_document_test"),
|
||||||
|
require("./document_test"),
|
||||||
|
require("./event_emitter_test"),
|
||||||
|
require("./navigation_test"),
|
||||||
|
require("./range_test"),
|
||||||
|
require("./search_test"),
|
||||||
|
require("./selection_test"),
|
||||||
|
require("./text_edit_test")
|
||||||
|
).exec();
|
||||||
|
|
|
||||||
|
|
@ -36,20 +36,12 @@
|
||||||
* ***** END LICENSE BLOCK ***** */
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
require("./mockdom");
|
||||||
|
|
||||||
var dom = require('jsdom/level2/html').dom.level2.html;
|
var Document = require("../document").Document,
|
||||||
var browser = require('jsdom/browser/index').windowAugmentation(dom);
|
Editor = require("../editor").Editor,
|
||||||
|
Text = require("../mode/text").Mode,
|
||||||
global.document = browser.document;
|
JavaScriptMode = require("../mode/javascript").Mode,
|
||||||
global.window = browser.window;
|
|
||||||
global.self = browser.self;
|
|
||||||
global.navigator = browser.navigator;
|
|
||||||
global.location = browser.location;
|
|
||||||
|
|
||||||
var Document = require("../document"),
|
|
||||||
Editor = require("../editor"),
|
|
||||||
Text = require("../mode/text"),
|
|
||||||
JavaScriptMode = require("../mode/javascript"),
|
|
||||||
MockRenderer = require("./mockrenderer"),
|
MockRenderer = require("./mockrenderer"),
|
||||||
assert = require("./assertions");
|
assert = require("./assertions");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,12 +36,12 @@
|
||||||
* ***** END LICENSE BLOCK ***** */
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
require("./mockdom");
|
||||||
|
|
||||||
var Document = require("../document"),
|
var Document = require("../document").Document,
|
||||||
UndoManager = require("../undomanager"),
|
UndoManager = require("../undomanager").UndoManager,
|
||||||
//Editor = require("../editor"),
|
|
||||||
MockRenderer = require("./mockrenderer"),
|
MockRenderer = require("./mockrenderer"),
|
||||||
Range = require("../range"),
|
Range = require("../range").Range,
|
||||||
assert = require("./assertions"),
|
assert = require("./assertions"),
|
||||||
async = require("async");
|
async = require("async");
|
||||||
|
|
||||||
|
|
@ -83,9 +83,7 @@ var Test = {
|
||||||
"test: move lines down" : function() {
|
"test: move lines down" : function() {
|
||||||
var doc = new Document(["1", "2", "3", "4"]);
|
var doc = new Document(["1", "2", "3", "4"]);
|
||||||
|
|
||||||
console.log(doc.toString().replace(/\n/g, "\\n"));
|
|
||||||
doc.moveLinesDown(0, 1);
|
doc.moveLinesDown(0, 1);
|
||||||
console.log(doc.toString().replace(/\n/g, "\\n"));
|
|
||||||
assert.equal(doc.toString(), ["3", "1", "2", "4"].join("\n"));
|
assert.equal(doc.toString(), ["3", "1", "2", "4"].join("\n"));
|
||||||
|
|
||||||
doc.moveLinesDown(1, 2);
|
doc.moveLinesDown(1, 2);
|
||||||
|
|
@ -98,12 +96,10 @@ var Test = {
|
||||||
assert.equal(doc.toString(), ["3", "4", "2", "1"].join("\n"));
|
assert.equal(doc.toString(), ["3", "4", "2", "1"].join("\n"));
|
||||||
},
|
},
|
||||||
|
|
||||||
"__test: move lines up" : function() {
|
"test: move lines up" : function() {
|
||||||
var doc = new Document(["1", "2", "3", "4"]);
|
var doc = new Document(["1", "2", "3", "4"]);
|
||||||
|
|
||||||
console.log(doc.toString().replace(/\n/g, "\\n"));
|
|
||||||
doc.moveLinesUp(2, 3);
|
doc.moveLinesUp(2, 3);
|
||||||
console.log(doc.toString().replace(/\n/g, "\\n"));
|
|
||||||
assert.equal(doc.toString(), ["1", "3", "4", "2"].join("\n"));
|
assert.equal(doc.toString(), ["1", "3", "4", "2"].join("\n"));
|
||||||
|
|
||||||
doc.moveLinesUp(1, 2);
|
doc.moveLinesUp(1, 2);
|
||||||
|
|
@ -178,49 +174,6 @@ var Test = {
|
||||||
assert.equal(["4", "5", "6"].join("\n"), doc.toString());
|
assert.equal(["4", "5", "6"].join("\n"), doc.toString());
|
||||||
},
|
},
|
||||||
|
|
||||||
"__test: undo/redo for delete line" : function() {
|
|
||||||
var doc = new Document(["111", "222", "333"]);
|
|
||||||
var undoManager = new UndoManager();
|
|
||||||
doc.setUndoManager(undoManager);
|
|
||||||
|
|
||||||
var initialText = doc.toString();
|
|
||||||
|
|
||||||
|
|
||||||
var editor = new Editor(new MockRenderer(), doc);
|
|
||||||
|
|
||||||
editor.removeLines();
|
|
||||||
var step1 = doc.toString();
|
|
||||||
assert.equal(step1, "222\n333");
|
|
||||||
doc.$informUndoManager.call();
|
|
||||||
|
|
||||||
editor.removeLines();
|
|
||||||
var step2 = doc.toString();
|
|
||||||
assert.equal(step2, "333");
|
|
||||||
doc.$informUndoManager.call();
|
|
||||||
|
|
||||||
editor.removeLines();
|
|
||||||
var step3 = doc.toString();
|
|
||||||
assert.equal(step3, "");
|
|
||||||
doc.$informUndoManager.call();
|
|
||||||
|
|
||||||
|
|
||||||
undoManager.undo();
|
|
||||||
doc.$informUndoManager.call();
|
|
||||||
assert.equal(doc.toString(), step2);
|
|
||||||
|
|
||||||
undoManager.undo();
|
|
||||||
doc.$informUndoManager.call();
|
|
||||||
assert.equal(doc.toString(), step1);
|
|
||||||
|
|
||||||
undoManager.undo();
|
|
||||||
doc.$informUndoManager.call();
|
|
||||||
assert.equal(doc.toString(), initialText);
|
|
||||||
|
|
||||||
undoManager.undo();
|
|
||||||
doc.$informUndoManager.call();
|
|
||||||
assert.equal(doc.toString(), initialText);
|
|
||||||
},
|
|
||||||
|
|
||||||
"test: convert document to screen coordinates" : function() {
|
"test: convert document to screen coordinates" : function() {
|
||||||
var doc = new Document("01234\t567890\t1234");
|
var doc = new Document("01234\t567890\t1234");
|
||||||
doc.setTabSize(4);
|
doc.setTabSize(4);
|
||||||
|
|
|
||||||
|
|
@ -37,17 +37,17 @@
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
|
||||||
var oop = require("../lib/oop");
|
var oop = require("pilot/oop").oop;
|
||||||
MEventEmitter = require("../event_emitter"),
|
EventEmitter = require("pilot/event_emitter").EventEmitter,
|
||||||
assert = require("./assertions");
|
assert = require("./assertions");
|
||||||
|
|
||||||
var EventEmitter = function() {};
|
var Emitter = function() {};
|
||||||
|
|
||||||
oop.implement(EventEmitter.prototype, EventEmitter);
|
oop.implement(Emitter.prototype, EventEmitter);
|
||||||
|
|
||||||
var Test = {
|
var Test = {
|
||||||
"test: dispatch event with no data" : function() {
|
"test: dispatch event with no data" : function() {
|
||||||
var emitter = new EventEmitter();
|
var emitter = new Emitter();
|
||||||
|
|
||||||
var called = false;
|
var called = false;
|
||||||
emitter.addEventListener("juhu", function(e) {
|
emitter.addEventListener("juhu", function(e) {
|
||||||
|
|
@ -55,7 +55,7 @@ var Test = {
|
||||||
assert.equal(e.type, "juhu");
|
assert.equal(e.type, "juhu");
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.$dispatchEvent("juhu");
|
emitter._dispatchEvent("juhu");
|
||||||
assert.true(called);
|
assert.true(called);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
8
lib/ace/test/mockdom.js
Normal file
8
lib/ace/test/mockdom.js
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
var dom = require('jsdom/level2/html').dom.level2.html;
|
||||||
|
var browser = require('jsdom/browser/index').windowAugmentation(dom);
|
||||||
|
|
||||||
|
global.document = browser.document;
|
||||||
|
global.window = browser.window;
|
||||||
|
global.self = browser.self;
|
||||||
|
global.navigator = browser.navigator;
|
||||||
|
global.location = browser.location;
|
||||||
|
|
@ -116,6 +116,9 @@ MockRenderer.prototype.setBreakpoints = function() {
|
||||||
MockRenderer.prototype.updateFull = function() {
|
MockRenderer.prototype.updateFull = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
MockRenderer.prototype.updateText = function() {
|
||||||
|
};
|
||||||
|
|
||||||
MockRenderer.prototype.showCursor = function() {
|
MockRenderer.prototype.showCursor = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,21 +36,12 @@
|
||||||
* ***** END LICENSE BLOCK ***** */
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
require("./mockdom");
|
||||||
|
|
||||||
var dom = require('jsdom/level2/html').dom.level2.html;
|
var Document = require("../Document").Document,
|
||||||
var browser = require('jsdom/browser/index').windowAugmentation(dom);
|
Editor = require("../Editor").Editor,
|
||||||
|
|
||||||
global.document = browser.document;
|
|
||||||
global.window = browser.window;
|
|
||||||
global.self = browser.self;
|
|
||||||
global.navigator = browser.navigator;
|
|
||||||
global.location = browser.location;
|
|
||||||
|
|
||||||
var Document = require("../Document"),
|
|
||||||
Editor = require("../Editor"),
|
|
||||||
MockRenderer = require("./mockrenderer"),
|
MockRenderer = require("./mockrenderer"),
|
||||||
assert = require("./assertions");
|
assert = require("./assertions");
|
||||||
|
|
||||||
|
|
||||||
var Test = {
|
var Test = {
|
||||||
createTextDocument : function(rows, cols) {
|
createTextDocument : function(rows, cols) {
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
|
||||||
var Range = require("../range"),
|
var Range = require("../range").Range,
|
||||||
assert = require("./assertions");
|
assert = require("./assertions");
|
||||||
|
|
||||||
var Test = {
|
var Test = {
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,8 @@
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
|
||||||
var Document = require("../document"),
|
var Document = require("../document").Document,
|
||||||
Search = require("../search"),
|
Search = require("../search").Search,
|
||||||
assert = require("./assertions");
|
assert = require("./assertions");
|
||||||
|
|
||||||
var Test = {
|
var Test = {
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
|
||||||
var Document = require("../document"),
|
var Document = require("../document").Document,
|
||||||
assert = require("./assertions");
|
assert = require("./assertions");
|
||||||
|
|
||||||
var Test = {
|
var Test = {
|
||||||
|
|
|
||||||
|
|
@ -36,19 +36,12 @@
|
||||||
* ***** END LICENSE BLOCK ***** */
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
require("./mockdom");
|
||||||
|
|
||||||
var dom = require('jsdom/level2/html').dom.level2.html;
|
var Document = require("../document").Document,
|
||||||
var browser = require('jsdom/browser/index').windowAugmentation(dom);
|
Editor = require("../editor").Editor,
|
||||||
|
|
||||||
global.document = browser.document;
|
|
||||||
global.window = browser.window;
|
|
||||||
global.self = browser.self;
|
|
||||||
global.navigator = browser.navigator;
|
|
||||||
global.location = browser.location;
|
|
||||||
|
|
||||||
var Document = require("../document"),
|
|
||||||
Editor = require("../editor"),
|
|
||||||
JavaScriptMode = require("../mode/javascript").Mode,
|
JavaScriptMode = require("../mode/javascript").Mode,
|
||||||
|
UndoManager = require("../undomanager").UndoManager,
|
||||||
MockRenderer = require("./mockrenderer"),
|
MockRenderer = require("./mockrenderer"),
|
||||||
assert = require("./assertions");
|
assert = require("./assertions");
|
||||||
|
|
||||||
|
|
@ -119,7 +112,7 @@ var Test = {
|
||||||
editor.moveCursorTo(1, 3);
|
editor.moveCursorTo(1, 3);
|
||||||
editor.getSelection().selectDown();
|
editor.getSelection().selectDown();
|
||||||
|
|
||||||
editor.blockIndent(" ");
|
editor.indent();
|
||||||
|
|
||||||
assert.equal(["a12345", " b12345", " c12345"].join("\n"),
|
assert.equal(["a12345", " b12345", " c12345"].join("\n"),
|
||||||
doc.toString());
|
doc.toString());
|
||||||
|
|
@ -135,26 +128,25 @@ var Test = {
|
||||||
var doc = new Document([" a12345", " b12345", " c12345"].join("\n"));
|
var doc = new Document([" a12345", " b12345", " c12345"].join("\n"));
|
||||||
var editor = new Editor(new MockRenderer(), doc);
|
var editor = new Editor(new MockRenderer(), doc);
|
||||||
|
|
||||||
editor.moveCursorTo(0, 3);
|
editor.moveCursorTo(0, 5);
|
||||||
editor.getSelection().selectDown();
|
editor.getSelection().selectDown();
|
||||||
editor.getSelection().selectDown();
|
editor.getSelection().selectDown();
|
||||||
|
|
||||||
editor.blockOutdent(" ");
|
editor.blockOutdent();
|
||||||
assert.equal(doc.toString(), [" a12345", "b12345", " c12345"].join("\n"));
|
assert.equal(doc.toString(), [" a12345", "b12345", " c12345"].join("\n"));
|
||||||
|
|
||||||
assert.position(editor.getCursorPosition(), 2, 0);
|
assert.position(editor.getCursorPosition(), 2, 1);
|
||||||
|
|
||||||
var range = editor.getSelectionRange();
|
var range = editor.getSelectionRange();
|
||||||
assert.position(range.start, 0, 1);
|
assert.position(range.start, 0, 1);
|
||||||
assert.position(range.end, 2, 1);
|
assert.position(range.end, 2, 1);
|
||||||
|
|
||||||
|
editor.blockOutdent();
|
||||||
editor.blockOutdent(" ");
|
|
||||||
assert.equal(doc.toString(), ["a12345", "b12345", "c12345"].join("\n"));
|
assert.equal(doc.toString(), ["a12345", "b12345", "c12345"].join("\n"));
|
||||||
|
|
||||||
var range = editor.getSelectionRange();
|
var range = editor.getSelectionRange();
|
||||||
assert.position(range.start, 0, 1);
|
assert.position(range.start, 0, 0);
|
||||||
assert.position(range.end, 2, 1);
|
assert.position(range.end, 2, 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
"test: outent without a selection should update cursor" : function() {
|
"test: outent without a selection should update cursor" : function() {
|
||||||
|
|
@ -340,6 +332,49 @@ var Test = {
|
||||||
|
|
||||||
editor.onTextInput("\t");
|
editor.onTextInput("\t");
|
||||||
assert.equal(doc.toString(), "\t");
|
assert.equal(doc.toString(), "\t");
|
||||||
|
},
|
||||||
|
|
||||||
|
"test: undo/redo for delete line" : function() {
|
||||||
|
var doc = new Document(["111", "222", "333"]);
|
||||||
|
var undoManager = new UndoManager();
|
||||||
|
doc.setUndoManager(undoManager);
|
||||||
|
|
||||||
|
var initialText = doc.toString();
|
||||||
|
|
||||||
|
|
||||||
|
var editor = new Editor(new MockRenderer(), doc);
|
||||||
|
|
||||||
|
editor.removeLines();
|
||||||
|
var step1 = doc.toString();
|
||||||
|
assert.equal(step1, "222\n333");
|
||||||
|
doc.$informUndoManager.call();
|
||||||
|
|
||||||
|
editor.removeLines();
|
||||||
|
var step2 = doc.toString();
|
||||||
|
assert.equal(step2, "333");
|
||||||
|
doc.$informUndoManager.call();
|
||||||
|
|
||||||
|
editor.removeLines();
|
||||||
|
var step3 = doc.toString();
|
||||||
|
assert.equal(step3, "");
|
||||||
|
doc.$informUndoManager.call();
|
||||||
|
|
||||||
|
|
||||||
|
undoManager.undo();
|
||||||
|
doc.$informUndoManager.call();
|
||||||
|
assert.equal(doc.toString(), step2);
|
||||||
|
|
||||||
|
undoManager.undo();
|
||||||
|
doc.$informUndoManager.call();
|
||||||
|
assert.equal(doc.toString(), step1);
|
||||||
|
|
||||||
|
undoManager.undo();
|
||||||
|
doc.$informUndoManager.call();
|
||||||
|
assert.equal(doc.toString(), initialText);
|
||||||
|
|
||||||
|
undoManager.undo();
|
||||||
|
doc.$informUndoManager.call();
|
||||||
|
assert.equal(doc.toString(), initialText);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,11 @@
|
||||||
* ***** END LICENSE BLOCK ***** */
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
require("../../../support/paths");
|
require("../../../support/paths");
|
||||||
|
require("./mockdom");
|
||||||
|
|
||||||
var Document = "../document",
|
var Document = require("../document").Document,
|
||||||
VirtualRenderer = "../virtual_renderer",
|
VirtualRenderer = require("../virtual_renderer").VirtualRenderer,
|
||||||
assert = "../assertions";
|
assert = require("./assertions");
|
||||||
|
|
||||||
var Test = {
|
var Test = {
|
||||||
"test: screen2text the column should be rounded to the next character edge" : function() {
|
"test: screen2text the column should be rounded to the next character edge" : function() {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
require("./requireJS-node");
|
require("./requireJS-node");
|
||||||
|
require.paths.unshift(__dirname + "/../lib");
|
||||||
|
require.paths.unshift(__dirname + "/../plugins");
|
||||||
require.paths.unshift(__dirname + "/async/lib");
|
require.paths.unshift(__dirname + "/async/lib");
|
||||||
require.paths.unshift(__dirname + "/node-htmlparser/lib");
|
require.paths.unshift(__dirname + "/node-htmlparser/lib");
|
||||||
require.paths.unshift(__dirname + "/jsdom/lib");
|
require.paths.unshift(__dirname + "/jsdom/lib");
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
|
var fs = require("fs");
|
||||||
var currentModule, defaultCompile = module.constructor.prototype._compile;
|
var currentModule, defaultCompile = module.constructor.prototype._compile;
|
||||||
//console.log(module.id);
|
|
||||||
|
|
||||||
module.constructor.prototype._compile = function(content, filename){
|
module.constructor.prototype._compile = function(content, filename){
|
||||||
currentModule = this;
|
currentModule = this;
|
||||||
|
|
@ -21,15 +21,29 @@ global.define = function (id, injects, factory) {
|
||||||
|
|
||||||
var module = currentModule;
|
var module = currentModule;
|
||||||
|
|
||||||
var req = function(relativeId) {
|
var req = function(relativeId, callback) {
|
||||||
|
if (Array.isArray(relativeId)) {
|
||||||
|
// async require
|
||||||
|
return callback.apply(this, relativeId.map(req))
|
||||||
|
}
|
||||||
|
|
||||||
|
var chunks = relativeId.split("!");
|
||||||
|
if (chunks.length >= 2) {
|
||||||
|
var prefix = chunks[0];
|
||||||
|
relativeId = chunks.slice(1).join("!")
|
||||||
|
}
|
||||||
|
|
||||||
if (relativeId.charAt(0) === '.') {
|
if (relativeId.charAt(0) === '.') {
|
||||||
var rootPath = path.dirname(path.dirname(requireModule.filename)) + "/",
|
var rootPath = path.dirname(path.dirname(requireModule.filename)) + "/",
|
||||||
absolutePath = path.dirname(module.filename) + "/" + relativeId;
|
absolutePath = path.dirname(module.filename) + "/" + relativeId;
|
||||||
|
|
||||||
relativeId = "../" + absolutePath.match(new RegExp(rootPath + "(.*)"))[1];
|
relativeId = "../" + absolutePath.match(new RegExp(rootPath + "(.*)"))[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return require(relativeId);
|
if (prefix == "text") {
|
||||||
|
return fs.readFileSync(findModulePath(relativeId))
|
||||||
|
} else
|
||||||
|
return require(relativeId);
|
||||||
};
|
};
|
||||||
if (!factory) {
|
if (!factory) {
|
||||||
// two or less arguments
|
// two or less arguments
|
||||||
|
|
@ -77,3 +91,57 @@ global.define = function (id, injects, factory) {
|
||||||
module.exports = returned;
|
module.exports = returned;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// slighly modified version of
|
||||||
|
// https://github.com/ry/node/blob/1dad95a3a960c645ffec28f9ec023dad6a17c0d4/src/node.js#L159
|
||||||
|
//
|
||||||
|
// given a module name, and a list of paths to test, returns the first
|
||||||
|
// matching file in the following precedence.
|
||||||
|
//
|
||||||
|
// require("a.<ext>")
|
||||||
|
// -> a.<ext>
|
||||||
|
//
|
||||||
|
// require("a")
|
||||||
|
// -> a
|
||||||
|
// -> a.<ext>
|
||||||
|
// -> a/index.<ext>
|
||||||
|
function findModulePath(request) {
|
||||||
|
var fs = require('fs'),
|
||||||
|
exts = ["js"],
|
||||||
|
paths = ['.'].concat(require.paths)
|
||||||
|
|
||||||
|
paths = request.charAt(0) === '/' ? [''] : paths;
|
||||||
|
|
||||||
|
// check if the file exists and is not a directory
|
||||||
|
var tryFile = function(requestPath) {
|
||||||
|
try {
|
||||||
|
var stats = fs.statSync(requestPath);
|
||||||
|
if (stats && !stats.isDirectory()) {
|
||||||
|
return requestPath;
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// given a path check a the file exists with any of the set extensions
|
||||||
|
var tryExtensions = function(p, extension) {
|
||||||
|
for (var i = 0, EL = exts.length; i < EL; i++) {
|
||||||
|
f = tryFile(p + exts[i]);
|
||||||
|
if (f) { return f; }
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// For each path
|
||||||
|
for (var i = 0, PL = paths.length; i < PL; i++) {
|
||||||
|
var p = paths[i],
|
||||||
|
// try to join the request to the path
|
||||||
|
f = tryFile(path.join(p, request)) ||
|
||||||
|
// try it with each of the extensions
|
||||||
|
tryExtensions(path.join(p, request)) ||
|
||||||
|
// try it with each of the extensions at "index"
|
||||||
|
tryExtensions(path.join(p, request, 'index'));
|
||||||
|
if (f) { return f; }
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue