Skip to content

Commit

Permalink
fix: resolve relative bundle file path correctly [ROAD-329] (#112)
Browse files Browse the repository at this point in the history
* fix: resolve relative bundle file path correctly [ROAD-329]

* test: getBundleFilePath
  • Loading branch information
michelkaporin authored Oct 15, 2021
1 parent e0a3a1d commit 5f8c641
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
composeFilePayloads,
parseFileIgnores,
getFileInfo,
getBundleFilePath,
} from '../src/files';

import { sampleProjectPath, supportedFiles, bundleFiles, bundleFilesFull, bundleFileIgnores } from './constants/sample';
Expand Down Expand Up @@ -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));
})
});

0 comments on commit 5f8c641

Please sign in to comment.