From 5f8c6411a73e93e34cf45f6b2eee743d1371ddcd Mon Sep 17 00:00:00 2001 From: Michel Kaporin Date: Fri, 15 Oct 2021 12:11:42 +0200 Subject: [PATCH] fix: resolve relative bundle file path correctly [ROAD-329] (#112) * fix: resolve relative bundle file path correctly [ROAD-329] * test: getBundleFilePath --- src/files.ts | 4 ++-- tests/files.spec.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/files.ts b/src/files.ts index 313524ea..7c040452 100644 --- a/src/files.ts +++ b/src/files.ts @@ -350,8 +350,8 @@ export async function prepareExtendingBundle( }; } -function getBundleFilePath(filePath: string, baseDir: string) { - const relPath = nodePath.relative(baseDir, filePath); +export function getBundleFilePath(filePath: string, baseDir: string) { + const relPath = baseDir ? nodePath.relative(baseDir, filePath) : filePath; // relPath without explicit base makes no sense const posixPath = !isWindows ? relPath : relPath.replace(/\\/g, '/'); return encodeURI(posixPath); } diff --git a/tests/files.spec.ts b/tests/files.spec.ts index f0be83e9..5f029b2b 100644 --- a/tests/files.spec.ts +++ b/tests/files.spec.ts @@ -8,6 +8,7 @@ import { composeFilePayloads, parseFileIgnores, getFileInfo, + getBundleFilePath, } from '../src/files'; import { sampleProjectPath, supportedFiles, bundleFiles, bundleFilesFull, bundleFileIgnores } from './constants/sample'; @@ -113,4 +114,16 @@ describe('files', () => { const fileMeta = await getFileInfo(filePath, sampleProjectPath); expect(fileMeta?.hash).toEqual('3e2979852cc2e97f48f7e7973a8b0837eb73ed0485c868176bc3aa58c499f534'); }); + + it('obtains correct bundle file path if no baseDir specified', () => { + const baseDir = ''; + const darwinPath = '/Users/user/Git/goof/routes/index.js'; + expect(getBundleFilePath(darwinPath, baseDir)).toEqual(darwinPath); + + const linuxPath = '/home/user/Git/goof/routes/index.js'; + expect(getBundleFilePath(linuxPath, baseDir)).toEqual(linuxPath); + + const windowsPath = 'C:\\Users\\user\\Git\\goof\\index.js'; + expect(getBundleFilePath(windowsPath, baseDir)).toEqual(encodeURI(windowsPath)); + }) });