Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/lib/file.js
diff options
context:
space:
mode:
Diffstat (limited to 'utils/lib/file.js')
-rwxr-xr-xutils/lib/file.js122
1 files changed, 21 insertions, 101 deletions
diff --git a/utils/lib/file.js b/utils/lib/file.js
index e7c2336..8c354ba 100755
--- a/utils/lib/file.js
+++ b/utils/lib/file.js
@@ -1,12 +1,8 @@
-var io = require('io');
-var implementation = require('file-platform');
+// NOTE: portions of the "file" module are implemented in "file-bootstrap" and "file-platform",
+// which are loaded at the bottom of this file to allow for overriding default implementations
-for (var name in implementation) {
- if (Object.prototype.hasOwnProperty.call(implementation, name)) {
- exports[name] = implementation[name];
- }
-}
+var io = require('io');
/* streams */
@@ -296,79 +292,20 @@ 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;
-};
-
exports.relative = function (source, target) {
+ if (!target) {
+ target = source;
+ source = exports.cwd() + '/';
+ }
source = exports.absolute(source);
target = exports.absolute(target);
- source = source.split(SEPARATOR);
- target = target.split(SEPARATOR);
- if (source[source.length - 1] == "")
- source.pop();
- while (source.length && target.length && target[0] == source[0]) {
+ source = source.split(exports.SEPARATORS_RE());
+ target = target.split(exports.SEPARATORS_RE());
+ source.pop();
+ while (
+ source.length &&
+ target.length &&
+ target[0] == source[0]) {
source.shift();
target.shift();
}
@@ -376,35 +313,13 @@ exports.relative = function (source, target) {
source.shift();
target.unshift("..");
}
- return target.join(SEPARATOR);
-};
-
-exports.normal = function (path) {
- return exports.resolve(path);
+ return target.join(exports.SEPARATORS_RE());
};
exports.absolute = function (path) {
return exports.resolve(exports.join(exports.cwd(), ''), path);
};
-exports.dirname = function (path) {
- var parts = exports.split(path);
- parts.pop();
- return exports.join.apply(null, parts);
-};
-
-exports.basename = function (path) {
- return path.split(SEPARATOR).pop();
-};
-
-exports.extension =
-exports.extname = function(path) {
- path = exports.basename(path);
- path = path.replace(/^\.*/, '');
- var index = path.lastIndexOf(".");
- return index <= 0 ? "" : path.substring(index);
-};
-
/* path wrapper, for chaining */
exports.path = function (/*path*/) {
@@ -463,7 +378,8 @@ var pathed = [
'basename',
'canonical',
'dirname',
- 'normal'
+ 'normal',
+ 'relative'
];
for (var i = 0; i < pathed.length; i++) {
@@ -525,3 +441,7 @@ for (var i = 0; i < trivia.length; i++) {
})(name);
}
+// load "file-bootstrap" and "file-platform", which in turn load "file" and modify it's module object.
+// "platform" gets priority so it's loaded last.
+require("file-bootstrap");
+require("file-platform");