Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Now basedir returns the lowermost directory in a path not the uppermost.

  • Loading branch information...
commit 5d4e692a2838fcc80a515104bfd4dd9f30e86c8d 1 parent a63ebb4
mde authored
Showing with 46 additions and 15 deletions.
  1. +43 −12 lib/file.js
  2. +3 −3 test/file.js
View
55 lib/file.js
@@ -376,22 +376,53 @@ var fileUtils = new (function () {
}
};
- this.basedir = function (p) {
- var str = p || ''
- , abs = this.isAbsolute(p);
- if (abs) {
- return abs;
- }
- // Split into segments
- str = str.split(/\\|\//)[0];
+ /**
+ Given a patern, return the base directory of it (ie. the folder
+ that will contain all the files matching the path).
+ eg. file.basedir('/test/**') => '/test/'
+ Path ending by '/' are considerd as folder while other are considerd
+ as files, eg.:
+ file.basedir('/test/a/') => '/test/a'
+ file.basedir('/test/a') => '/test'
+ The returned path always end with a '/' so we have:
+ file.basedir(file.basedir(x)) == file.basedir(x)
+ */
+ this.basedir = function (pathParam) {
+ var basedir = ''
+ , parts
+ , part
+ , pos = 0
+ , p = pathParam || '';
+
// If the path has a leading asterisk, basedir is the current dir
- if (str.indexOf('*') > -1) {
+ if (p.indexOf('*') == 0 || p.indexOf('**') == 0) {
return '.';
}
- // Otherwise it's the first segment in the path
- else {
- return str;
+
+ // always consider .. at the end as a folder and not a filename
+ if (/(?:^|\/|\\)\.\.$/.test(p.slice(-3))) {
+ p += '/';
}
+
+ parts = p.split(/\\|\//);
+ for (var i = 0, l = parts.length - 1; i < l; i++) {
+ part = parts[i];
+ if (part.indexOf('*') > -1 || part.indexOf('**') > -1) {
+ break;
+ }
+ pos += part.length + 1;
+ basedir += part + p[pos - 1];
+ }
+ if (!basedir) {
+ basedir = '.';
+ }
+ console.log(basedir);
+ // Strip trailing slashes
+ if (!(basedir == '\\' || basedir == '/')) {
+ basedir = basedir.replace(/\\$|\/$/, '');
+ }
+ return basedir;
+
};
/**
View
6 test/file.js
@@ -152,12 +152,12 @@ tests = {
, 'test basedir with Unix absolute path': function () {
var p = '/foo/bar/baz';
- assert.equal('/', file.basedir(p));
+ assert.equal('/foo/bar', file.basedir(p));
}
, 'test basedir with Win absolute path': function () {
var p = 'C:\\foo\\bar\\baz';
- assert.equal('C:\\', file.basedir(p));
+ assert.equal('C:\\foo\\bar', file.basedir(p));
}
, 'test basedir with Unix absolute path and double-asterisk': function () {
@@ -187,7 +187,7 @@ tests = {
, 'test basedir with leading dot-dot-slash and double-asterisk': function () {
var p = '../test/**/*.js';
- assert.equal('..', file.basedir(p));
+ assert.equal('../test', file.basedir(p));
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.