Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions test/spec/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,32 @@ test('utils.buildUrlObj', t => {

test('utils.buildLocalPath', t => {
let localPath;
localPath = utils.buildLocalPath('/path/to/file', '', '/path/to/inputDir', '/path/to/outputDir');
localPath = utils.buildLocalPath('/path/to/file?foo=bar#hash', '', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/path/to/file');
localPath = utils.buildLocalPath('http://example.com/path/to/file?foo=bar#hash', '', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/example.com/path/to/file');
localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir/file', '', '/path/to/inputDir', '/path/to/outputDir');
localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir/file?foo=bar#hash', '', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/subdir/file');
localPath = utils.buildLocalPath('http://example2.com/path/to/file?foo=bar#hash', 'http://example.com/path/to/file', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/example2.com/path/to/file');
localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir2/file', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir');
localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir2/file?foo=bar#hash', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/subdir2/file');
localPath = utils.buildLocalPath('../../path2/to/file?foo=bar#hash', 'http://example.com/path/to/file', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/example.com/path2/to/file');
localPath = utils.buildLocalPath('../subdir2/file', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir');
localPath = utils.buildLocalPath('../subdir2/file?foo=bar#hash', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/subdir2/file');
localPath = utils.buildLocalPath('../subdir2/file', '/path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir');
localPath = utils.buildLocalPath('../subdir2/file?foo=bar#hash', '/path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir');
t.is(localPath, '/path/to/outputDir/subdir2/file');
});

test('utils.removeQueryString', t => {
let result;
result = utils.removeQueryString('/path/to/file?foo=bar');
t.is(result, '/path/to/file');
result = utils.removeQueryString('/path/to/file?foo=bar&abc=def');
t.is(result, '/path/to/file');
result = utils.removeQueryString('/path/to/file#hash');
t.is(result, '/path/to/file');
result = utils.removeQueryString('/path/to/file?foo=bar&abc=def#hash');
t.is(result, '/path/to/file');
});
10 changes: 8 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,19 @@ function buildAbsolutePath(relativePath, basePath, inputDir, outputDir) {
return path.join(outputDir, fullPath.startsWith(inputDir) ? path.relative(inputDir, fullPath) : fullPath);
}

function removeQueryString(absolutePath) {
const obj = buildUrlObj(`file://${absolutePath}`);
return obj.pathname;
}

function buildLocalPath(uri, parentUri, inputDir, outputDir) {
print(`buildLocalPath: uri=${uri}, parentUri=${parentUri}, inputDir=${inputDir}, outputDir=${outputDir}`);
let localPath;
let obj;
if (path.isAbsolute(uri)) {
localPath = path.join(outputDir, uri);
print(`\tFrom absolute path to localPath: ${localPath}`);
return localPath;
return removeQueryString(localPath);
}
obj = buildUrlObj(uri);
if (obj) {
Expand All @@ -131,7 +136,7 @@ function buildLocalPath(uri, parentUri, inputDir, outputDir) {
}
localPath = buildAbsolutePath(uri, parentUri, inputDir, outputDir);
print(`\tFrom relative path to localPath: ${localPath}`);
return localPath;
return removeQueryString(localPath);
}

module.exports = {
Expand All @@ -144,5 +149,6 @@ module.exports = {
getDateTimeString,
mkdirP,
buildUrlObj,
removeQueryString,
buildLocalPath
};