Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 5d4e692a2838fcc80a515104bfd4dd9f30e86c8d 1 parent a63ebb4
Matthew Eernisse authored December 06, 2012

Showing 2 changed files with 46 additions and 15 deletions. Show diff stats Hide diff stats

  1. 55  lib/file.js
  2. 6  test/file.js
55  lib/file.js
@@ -376,22 +376,53 @@ var fileUtils = new (function () {
376 376
     }
377 377
   };
378 378
 
379  
-  this.basedir = function (p) {
380  
-    var str = p || ''
381  
-      , abs = this.isAbsolute(p);
382  
-    if (abs) {
383  
-      return abs;
384  
-    }
385  
-    // Split into segments
386  
-    str = str.split(/\\|\//)[0];
  379
+  /**
  380
+    Given a patern, return the base directory of it (ie. the folder
  381
+    that will contain all the files matching the path).
  382
+    eg. file.basedir('/test/**') => '/test/'
  383
+    Path ending by '/' are considerd as folder while other are considerd
  384
+    as files, eg.:
  385
+        file.basedir('/test/a/') => '/test/a'
  386
+        file.basedir('/test/a') => '/test'
  387
+    The returned path always end with a '/' so we have:
  388
+        file.basedir(file.basedir(x)) == file.basedir(x)
  389
+  */
  390
+  this.basedir = function (pathParam) {
  391
+    var basedir = ''
  392
+      , parts
  393
+      , part
  394
+      , pos = 0
  395
+      , p = pathParam || '';
  396
+
387 397
     // If the path has a leading asterisk, basedir is the current dir
388  
-    if (str.indexOf('*') > -1) {
  398
+    if (p.indexOf('*') == 0 || p.indexOf('**') == 0) {
389 399
       return '.';
390 400
     }
391  
-    // Otherwise it's the first segment in the path
392  
-    else {
393  
-      return str;
  401
+
  402
+    // always consider .. at the end as a folder and not a filename
  403
+    if (/(?:^|\/|\\)\.\.$/.test(p.slice(-3))) {
  404
+      p += '/';
394 405
     }
  406
+
  407
+    parts = p.split(/\\|\//);
  408
+    for (var i = 0, l = parts.length - 1; i < l; i++) {
  409
+      part = parts[i];
  410
+      if (part.indexOf('*') > -1 || part.indexOf('**') > -1) {
  411
+        break;
  412
+      }
  413
+      pos += part.length + 1;
  414
+      basedir += part + p[pos - 1];
  415
+    }
  416
+    if (!basedir) {
  417
+      basedir = '.';
  418
+    }
  419
+    console.log(basedir);
  420
+    // Strip trailing slashes
  421
+    if (!(basedir == '\\' || basedir == '/')) {
  422
+      basedir = basedir.replace(/\\$|\/$/, '');
  423
+    }
  424
+    return basedir;
  425
+
395 426
   };
396 427
 
397 428
   /**
6  test/file.js
@@ -152,12 +152,12 @@ tests = {
152 152
 
153 153
 , 'test basedir with Unix absolute path': function () {
154 154
     var p = '/foo/bar/baz';
155  
-    assert.equal('/', file.basedir(p));
  155
+    assert.equal('/foo/bar', file.basedir(p));
156 156
   }
157 157
 
158 158
 , 'test basedir with Win absolute path': function () {
159 159
     var p = 'C:\\foo\\bar\\baz';
160  
-    assert.equal('C:\\', file.basedir(p));
  160
+    assert.equal('C:\\foo\\bar', file.basedir(p));
161 161
   }
162 162
 
163 163
 , 'test basedir with Unix absolute path and double-asterisk': function () {
@@ -187,7 +187,7 @@ tests = {
187 187
 
188 188
 , 'test basedir with leading dot-dot-slash and double-asterisk': function () {
189 189
     var p = '../test/**/*.js';
190  
-    assert.equal('..', file.basedir(p));
  190
+    assert.equal('../test', file.basedir(p));
191 191
   }
192 192
 
193 193
 };

0 notes on commit 5d4e692

Please sign in to comment.
Something went wrong with that request. Please try again.