Skip to content

Commit 8e5f04a

Browse files
authored
fix: should handle ./ relative path (#109)
We have a tgz its entry path is `./foo/bar`, and when invoke `stripFileName(1, './foo/bar')`, it always return `foo/bar`, actually, we expect it to return `bar` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved handling of file paths to ensure consistent formatting across different operating systems. - **Tests** - Added new test cases to verify correct behavior when normalizing and stripping relative file paths. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: jiacheng.li <jiacheng.li@bytedance.com>
1 parent d173d2d commit 8e5f04a

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/utils.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ function safePipe(streams) {
168168

169169
exports.safePipe = safePipe;
170170

171+
function normalizePath(fileName) {
172+
fileName = path.normalize(fileName);
173+
// https://nodejs.org/api/path.html#path_path_normalize_path
174+
if (process.platform === 'win32') fileName = fileName.replace(/\\+/g, '/');
175+
return fileName;
176+
}
177+
171178
exports.stripFileName = (strip, fileName, type) => {
172179
// before
173180
// node/package.json
@@ -189,15 +196,18 @@ exports.stripFileName = (strip, fileName, type) => {
189196
// /foo => foo
190197
if (fileName[0] === '/') fileName = fileName.replace(/^\/+/, '');
191198

199+
// fix case
200+
// ./foo/bar => foo/bar
201+
if (fileName) {
202+
fileName = normalizePath(fileName);
203+
}
204+
192205
let s = fileName.split('/');
193206

194207
// fix relative path
195208
// foo/../bar/../../asdf/
196209
// => asdf/
197210
if (s.indexOf('..') !== -1) {
198-
fileName = path.normalize(fileName);
199-
// https://npm.taobao.org/mirrors/node/latest/docs/api/path.html#path_path_normalize_path
200-
if (process.platform === 'win32') fileName = fileName.replace(/\\+/g, '/');
201211
// replace '../' on ../../foo/bar
202212
fileName = fileName.replace(/(\.\.\/)+/, '');
203213
if (type === 'directory' && fileName && fileName[fileName.length - 1] !== '/') {

test/utils.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ describe('test/utils.test.js', () => {
5252
assert(utils.stripFileName(0, '/home/../../../foo.txt', 'file') === 'foo.txt');
5353
assert(utils.stripFileName(0, '///../../../../foo', 'file') === 'foo');
5454
assert(utils.stripFileName(0, '../../../../etc/hosts', 'file') === 'etc/hosts');
55+
56+
assert(utils.stripFileName(0, './etc/hosts', 'file') === 'etc/hosts');
57+
assert(utils.stripFileName(0, './././etc/hosts', 'file') === 'etc/hosts');
58+
assert(utils.stripFileName(1, './././etc/hosts', 'file') === 'hosts');
5559
});
5660

5761
it('should replace \\', () => {

0 commit comments

Comments
 (0)