[Pkg-javascript-commits] [node-parents] 16/29: windows: fix for normalize creating `C:.`, and thus `C:path` paths

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 20 13:47:04 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-parents.

commit 5e893159a7dff0293794344195cab02907f530aa
Author: Chris Dickinson <christopher.s.dickinson at gmail.com>
Date:   Mon Jul 7 18:13:36 2014 -0700

    windows: fix for normalize creating `C:.`, and thus `C:path` paths
---
 index.js      | 22 ++++++++++++++++++----
 package.json  |  4 +++-
 test/win32.js | 16 ++++++++--------
 3 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/index.js b/index.js
index 2bbfe4d..84a9639 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,4 @@
-var path = require('path');
+var pathPlatform = require('path-platform');
 
 module.exports = function (cwd, opts) {
     if (cwd === undefined) cwd = process.cwd();
@@ -6,6 +6,10 @@ module.exports = function (cwd, opts) {
     var platform = opts.platform || process.platform;
     
     var isWindows = /^win/.test(platform);
+    var path = isWindows ? pathPlatform.win32 : pathPlatform;
+    var normalize = !isWindows ? path.normalize :
+        path.normalize('c:') === 'c:.' ? fixNormalize(path.normalize) :
+        path.normalize;
     var sep = isWindows ? /[\\\/]/ : '/';
     var init = isWindows ? '' : '/';
     
@@ -13,10 +17,11 @@ module.exports = function (cwd, opts) {
         var ps = [ x, y ].filter(function (p) {
             return p && typeof p === 'string'
         });
-        return path.normalize(ps.join(isWindows ? '\\' : '/'));
+
+        return normalize(ps.join(isWindows ? '\\' : '/'));
     };
     
-    var res = path.normalize(cwd)
+    var res = normalize(cwd)
         .split(sep)
         .reduce(function (acc,dir,ix) {
             return acc.concat(join(acc[ix], dir))
@@ -27,8 +32,17 @@ module.exports = function (cwd, opts) {
     if (res[0] === res[1]) return [ res[0] ];
     if (isWindows && /^\\/.test(cwd)) {
         return res.slice(0,-1).map(function (d) {
-            return d.replace(/^\./, '');
+            var ch = d.charAt(0)
+            return ch === '\\' ? d :
+              ch === '.' ? '\\' + d.slice(1) :
+              '\\' + d
         });
     }
     return res;
+
+    function fixNormalize(fn) {
+      return function(p) {
+        return fn(p).replace(/:\.$/, ':')
+      }
+    }
 }
diff --git a/package.json b/package.json
index 9d27fc9..784a911 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,9 @@
         "example" : "example",
         "test" : "test"
     },
-    "dependencies" : {},
+    "dependencies" : {
+        "path-platform": "^0.0.1"
+    },
     "devDependencies" : {
         "tap" : "~0.2.5"
     },
diff --git a/test/win32.js b/test/win32.js
index 7492088..c6f2ece 100644
--- a/test/win32.js
+++ b/test/win32.js
@@ -2,21 +2,21 @@ var test = require('tap').test;
 var parents = require('../');
 
 test('win32', function (t) {
-    var dir = 'C:\\Program Files\\Maxis\\Sim City 2000\\cities';
+    var dir = 'c:\\Program Files\\Maxis\\Sim City 2000\\cities';
     var dirs = parents(dir, { platform : 'win32' });
     t.same(dirs, [
-        'C:\\Program Files\\Maxis\\Sim City 2000\\cities',
-        'C:\\Program Files\\Maxis\\Sim City 2000',
-        'C:\\Program Files\\Maxis',
-        'C:\\Program Files',
-        'C:',
+        'c:\\Program Files\\Maxis\\Sim City 2000\\cities',
+        'c:\\Program Files\\Maxis\\Sim City 2000',
+        'c:\\Program Files\\Maxis',
+        'c:\\Program Files',
+        'c:',
     ]);
     t.end();
 });
 
 test('win32 c:', function (t) {
-    var dirs = parents('C:\\', { platform : 'win32' });
-    t.same(dirs, [ 'C:' ]);
+    var dirs = parents('c:\\', { platform : 'win32' });
+    t.same(dirs, [ 'c:' ]);
     t.end();
 });
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-parents.git



More information about the Pkg-javascript-commits mailing list