Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
node_modules
npm-debug.log
.idea
25 changes: 17 additions & 8 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ exports.mkdirs = function (_path, mode, callback) {
// @mode: the file mode to create the directory with:
// ex: file.mkdirs("/tmp/dir", 755, function () {})
exports.mkdirsSync = function (_path, mode) {
if (_path[0] !== path.sep) {
_path = path.join(process.cwd(), _path)
}

_path = path.resolve(process.cwd(), _path);

var dirs = _path.split(path.sep);
var walker = [dirs.shift()];
Expand Down Expand Up @@ -182,16 +181,26 @@ exports.walkSync = function (start, callback) {
exports.path = {};

exports.path.abspath = function (to) {
to = path.normalize(to);
var from;
switch (to.charAt(0)) {
case "~": from = process.env.HOME; to = to.substr(1); break
case path.sep: from = ""; break
default : from = process.cwd(); break

if (to.charAt(0) === '~') {
from = "";
to = process.env.HOME + to.substr(1);
}
return path.join(from, to);
else if (to.indexOf(path.sep) === 0) {
from = "";
}
else {
from = process.cwd();
}
return path.resolve(from, to);
}

exports.path.relativePath = function (base, compare) {
base = path.normalize(base);
compare = path.normalize(compare);

base = base.split(path.sep);
compare = compare.split(path.sep);

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
{ "url" : "http://github.com/aconbere/node-file-utils" }
, "main" : "./lib/file"
, "license": "MIT"
, "devDependencies": { "mocha": "1.9.x" }
, "devDependencies": { "mocha": "1.9.x" },
"scripts": {
"test": "mocha tests"
}
}
33 changes: 22 additions & 11 deletions tests/file_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ describe("file#mkdirs", function () {
it("should make all the directories in the tree", function (done) {
file.mkdirs("/test/test/test/test", 0755, function(err) {
if (err) throw new Error(err);
assert.equal(madeDirs[0], "/test");
assert.equal(madeDirs[1], "/test/test");
assert.equal(madeDirs[2], "/test/test/test");
assert.equal(madeDirs[3], "/test/test/test/test");
assert.equal(madeDirs[0], path.resolve("/test"));
assert.equal(madeDirs[1], path.resolve("/test/test"));
assert.equal(madeDirs[2], path.resolve("/test/test/test"));
assert.equal(madeDirs[3], path.resolve("/test/test/test/test"));
done();
});
});

it("should make all the directories in the tree on windows", function (done) {
file.mkdirs(path.resolve("/test/test/test/test"), 0755, function(err) {
if (err) throw new Error(err);
assert.equal(madeDirs[0], path.resolve("/test"));
assert.equal(madeDirs[1], path.resolve("/test/test"));
assert.equal(madeDirs[2], path.resolve("/test/test/test"));
assert.equal(madeDirs[3], path.resolve("/test/test/test/test"));
done();
});
});
Expand All @@ -45,10 +56,10 @@ describe("file#mkdirsSync", function () {
file.mkdirsSync("/test/test/test/test", 0755, function(err) {
if (err) throw new Error(err);
});
assert.equal(madeDirs[0], "/test");
assert.equal(madeDirs[1], "/test/test");
assert.equal(madeDirs[2], "/test/test/test");
assert.equal(madeDirs[3], "/test/test/test/test");
assert.equal(madeDirs[0], path.resolve("/test"));
assert.equal(madeDirs[1], path.resolve("/test/test"));
assert.equal(madeDirs[2], path.resolve("/test/test/test"));
assert.equal(madeDirs[3], path.resolve("/test/test/test/test"));
done();
});
});
Expand Down Expand Up @@ -82,13 +93,13 @@ describe("file.path#abspath", function () {
});

it("should convert ~ to the home directory", function (done) {
assert.equal(file.path.abspath("~"), file.path.join(process.env.HOME, ""));
assert.equal(file.path.abspath("~/test/dir"), file.path.join(process.env.HOME, "test/dir"));
assert.equal(file.path.abspath("~"), path.resolve(process.env.HOME));
assert.equal(file.path.abspath("~/test/dir"), path.resolve(process.env.HOME, "test/dir"));
done();
});

it("should not convert paths begining with /", function (done) {
assert.equal(file.path.abspath("/x/y/z"), "/x/y/z");
assert.equal(file.path.abspath("/x/y/z"), path.resolve("/x/y/z"));
done();
});
});
Expand Down