Skip to content

Commit

Permalink
Refactored path manipulation functions from file.js into file-bootstr…
Browse files Browse the repository at this point in the history
…ap.js and removed duplicated methods from sandbox.js. Modified narwhal.js to load file-bootstrap.js into system.fs.
  • Loading branch information
Tom Robinson committed Jul 24, 2009
1 parent c25f36a commit 53d6159
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 181 deletions.
108 changes: 108 additions & 0 deletions lib/file-bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* path manipulation */

// defaults to be overrided by file module or bootstrapper
exports.ROOT = "/";
exports.SEPARATOR = "/";
exports.ALT_SEPARATOR = undefined;

exports.join = function () {
return exports.normal(Array.prototype.join.call(arguments, exports.SEPARATOR));
};

exports.split = function (path) {
var SEPARATORS_RE = new RegExp(
"[" +
RegExp.escape(exports.SEPARATOR || '') +
RegExp.escape(exports.ALT_SEPARATOR || '') +
"]"
);

try {
return String(path).split(SEPARATORS_RE);
} catch (exception) {
throw new Error("Cannot split " + (typeof path) + ', "' + path + '"');
}
};

exports.resolve = function () {
var root = "";
var parents = [];
var children = [];
var leaf = "";
for (var i = 0; i < arguments.length; i++) {
var path = String(arguments[i]);
if (path == "")
continue;
if (path.charAt(0) == exports.SEPARATOR) {
path = path.substring(1, path.length);
root = exports.ROOT;
parents = [];
children = [];
}
var parts = path.split(exports.SEPARATOR);
leaf = parts.pop();
if (leaf == "." || leaf == "..") {
parts.push(leaf);
leaf = "";
}
for (var j = 0; j < parts.length; j++) {
var part = parts[j];
if (part == "." || part == '') {
} else if (part == "..") {
if (children.length) {
children.pop();
} else {
if (root) {
} else {
parents.push("..");
}
}
} else {
children.push(part);
}
};
}
path = parents.concat(children).join(exports.SEPARATOR);
if (path) leaf = exports.SEPARATOR + leaf;
return root + path + leaf;
};

exports.normal = function (path) {
return exports.resolve(path);
};


// XXX not standard
exports.isAbsolute = function (path) {
// XXX not windows compatible
return /^\//.test(path);
};

// XXX not standard
exports.isRelative = function (path) {
return !exports.isAbsolute(path);
};

exports.dirname = function (path) {
var parts = exports.split(path);
// XXX needs to be sensitive to the root for
// Windows compatibility
parts.pop();
return exports.join.apply(null, parts) || ".";
};

exports.basename = function (path) {
return path.split(exports.SEPARATOR).pop();
};

exports.extension = function (path) {
path = exports.basename(path);
path = path.replace(/^\.*/, '');
var index = path.lastIndexOf(".");
return index <= 0 ? "" : path.substring(index);
};

exports.extname = function (path) {
system.log.warn('extname is deprecated in favor of extension');
return exports.extension(path);
};
115 changes: 11 additions & 104 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@

var io = require('io');
var implementation = require('file-platform');

var bootstrap = require('file-bootstrap');
for (var name in bootstrap) {
if (Object.prototype.hasOwnProperty.call(bootstrap, name)) {
exports[name] = bootstrap[name];
}
}

var implementation = require('file-platform');
for (var name in implementation) {
if (Object.prototype.hasOwnProperty.call(implementation, name)) {
exports[name] = implementation[name];
Expand Down Expand Up @@ -296,70 +303,9 @@ exports.rmtree = function(path) {

/* path manipulation */

var ROOT = implementation.ROOT;
var SEPARATOR = implementation.SEPARATOR;
var ALT_SEPARATOR = implementation.ALT_SEPARATOR;
var SEPARATORS_RE = new RegExp(
"[" +
RegExp.escape(SEPARATOR || '') +
RegExp.escape(ALT_SEPARATOR || '') +
"]"
);

exports.join = function () {
return exports.normal(Array.prototype.join.call(arguments, SEPARATOR));
};

exports.split = function (path) {
try {
return String(path).split(SEPARATORS_RE);
} catch (exception) {
throw new Error("Cannot split " + (typeof path) + ', "' + path + '"');
}
};

exports.resolve = function () {
var root = "";
var parents = [];
var children = [];
var leaf = "";
for (var i = 0; i < arguments.length; i++) {
var path = String(arguments[i]);
if (path == "")
continue;
if (path.charAt(0) == SEPARATOR) {
path = path.substring(1, path.length);
root = ROOT;
parents = [];
children = [];
}
var parts = path.split(SEPARATOR);
leaf = parts.pop();
if (leaf == "." || leaf == "..") {
parts.push(leaf);
leaf = "";
}
for (var j = 0; j < parts.length; j++) {
var part = parts[j];
if (part == "." || part == '') {
} else if (part == "..") {
if (children.length) {
children.pop();
} else {
if (root) {
} else {
parents.push("..");
}
}
} else {
children.push(part);
}
};
}
path = parents.concat(children).join(SEPARATOR);
if (path) leaf = SEPARATOR + leaf;
return root + path + leaf;
};
var ROOT = bootstrap.ROOT = implementation.ROOT;
var SEPARATOR = bootstrap.SEPARATOR = implementation.SEPARATOR;
var ALT_SEPARATOR = bootstrap.ALT_SEPARATOR = implementation.ALT_SEPARATOR;

exports.relative = function (source, target) {
if (!target) {
Expand All @@ -385,49 +331,10 @@ exports.relative = function (source, target) {
return target.join(SEPARATOR);
};

exports.normal = function (path) {
return exports.resolve(path);
};

exports.absolute = function (path) {
return exports.resolve(exports.join(exports.cwd(), ''), path);
};

// XXX not standard
exports.isAbsolute = function (path) {
// XXX not windows compatible
return /^\//.test(path);
};

// XXX not standard
exports.isRelative = function (path) {
return !exports.isAbsolute(path);
};

exports.dirname = function (path) {
var parts = exports.split(path);
// XXX needs to be sensitive to the root for
// Windows compatibility
parts.pop();
return exports.join.apply(null, parts);
};

exports.basename = function (path) {
return path.split(SEPARATOR).pop();
};

exports.extension = function (path) {
path = exports.basename(path);
path = path.replace(/^\.*/, '');
var index = path.lastIndexOf(".");
return index <= 0 ? "" : path.substring(index);
};

exports.extname = function (path) {
system.log.warn('extname is deprecated in favor of extension');
return exports.extension(path);
};

/* path wrapper, for chaining */

exports.path = function (/*path*/) {
Expand Down
77 changes: 25 additions & 52 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ exports.Loader = function (options) {

loader.find = function (topId) {
for (var j = 0; j < extensions.length; j++) {
var ext = extensions[j];
for (var i = 0; i < paths.length; i++) {
var fileName = join(paths[i], topId + ext);
if (system.fs.isFile(fileName))
return fileName;
var extension = extensions[j];
if (system.fs.isAbsolute(topId)) {
var path = topId + extension;
if (system.fs.isFile(path)) {
return path;
}
}
else {
for (var i = 0; i < paths.length; i++) {
var path = system.fs.join(paths[i], topId + extension);
if (system.fs.isFile(path))
return path;
}
}
}
throw new Error("require error: couldn't find \"" + topId + '"');
Expand Down Expand Up @@ -89,12 +97,20 @@ exports.MultiLoader = function (options) {
var pair = self.loaders[j];
var extension = pair[0];
var loader = pair[1];
for (var i = 0; i < self.paths.length; i++) {
var path = join(self.paths[i], topId + extension);
if (system.fs.isAbsolute(topId)) {
var path = topId + extension;
if (system.fs.isFile(path)) {
return [loader, path];
}
}
else {
for (var i = 0; i < self.paths.length; i++) {
var path = system.fs.join(self.paths[i], topId + extension);
if (system.fs.isFile(path)) {
return [loader, path];
}
}
}
}
throw "require error: couldn't find \"" + topId + '"';
};
Expand Down Expand Up @@ -367,50 +383,7 @@ exports.resolve = function (id, baseId) {
if (typeof id != "string")
throw new Error("module id '" + id + "' is not a String");
if (id.charAt(0) == ".") {
id = dirname(baseId) + "/" + id;
}
return normal(id);
};


////////////////////////////////////////////////
// Ugh, these are duplicated from the File object, since they're required for
// require, which is required for loading the File object.
var dirname = function(path) {
var raw = String(path),
match = raw.match(/^(.*)\/[^\/]+\/?$/);
if (match && match[1])
return match[1]
else if (raw.charAt(0) == "/")
return "/"
else
return "."
};

var normal = function(path) {
var original;

do {
original = path;
path = path
.replace(/[^\/]+\/\.\.\//g, "")
.replace(/([^\.])\.\//g, "$1")
.replace(/^\.\//g, "")
.replace(/\/\/+/g, "/");
} while (path !== original);

return path;
};

var join = function (base) {
for (var i = 1; i < arguments.length; i++) {
var rel = arguments[i];
if (rel.match(/^\//)) {
base = rel;
} else {
base = base + '/' + rel;
}
id = system.fs.dirname(baseId) + "/" + id;
}
return normal(base);
return system.fs.normal(id);
};
////////////////////////////////////////////////
Loading

0 comments on commit 53d6159

Please sign in to comment.