Skip to content

Commit

Permalink
[WIP] Allows imports with spaces
Browse files Browse the repository at this point in the history
fixes #176
  • Loading branch information
ritave committed Jul 3, 2019
1 parent 3ed2ef9 commit 2d60dce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/imports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"debug": "^3.1.0",
"hosted-git-info": "^2.6.0",
"path-browserify": "^1.0.0",
"url": "^0.11.0"
"url": "^0.11.0",
"is-url": "^1.2.4"
}
}
26 changes: 23 additions & 3 deletions packages/imports/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ResolverEngine } from "@resolver-engine/core";
import isUrl from "is-url";
import pathSys from "path";
import urlSys from "url";
import { ImportFile } from "./parsers/importparser";
Expand All @@ -25,6 +26,25 @@ export function findImports(data: ImportFile): string[] {
return result;
}

function resolvePath(workingDir: string, relativePath: string): string {
// If the working dir is an URL (a file imported from an URL location)
// or the relative path is an URL (an URL imported from a local file)
// use url.resolve, which will work in both cases.
// > url.resolve('/local/folder/', 'http://example.com/myfile.sol')
// 'http://example.com/myfile.sol'
// > url.resolve('http://example.com/', 'myfile.sol')
// 'http://example.com/myfile.sol'
//
// If not, use path.resolve, since using url.resolve will escape certain
// charaters (e.g. a space as an %20), breaking the path
// > url.resolve('/local/path/with spaces/', 'myfile.sol')
// '/local/path/with%20spaces/myfile.sol'

return isUrl(workingDir) || isUrl(relativePath)
? urlSys.resolve(workingDir, relativePath)
: pathSys.resolve(pathSys.dirname(workingDir), relativePath);
}

interface ImportTreeNode extends ImportFile {
uri: string;
// uri and url the same as in rest of resolver-engine
Expand Down Expand Up @@ -105,7 +125,7 @@ export async function gatherSources(
if (workingDir !== "") {
workingDir += "/";
}
const absoluteRoots = roots.map(what => urlSys.resolve(workingDir, what));
const absoluteRoots = roots.map(what => resolvePath(workingDir, what));
for (const absWhat of absoluteRoots) {
queue.push({ cwd: workingDir, file: absWhat, relativeTo: workingDir });
alreadyImported.add(absWhat);
Expand All @@ -120,7 +140,7 @@ export async function gatherSources(
// if not - return the same name it was imported with
let relativePath: string;
if (fileData.file[0] === ".") {
relativePath = urlSys.resolve(fileData.relativeTo, fileData.file);
relativePath = resolvePath(fileData.relativeTo, fileData.file);
result.push({ url: relativePath, source: resolvedFile.source, provider: resolvedFile.provider });
} else {
relativePath = fileData.file;
Expand All @@ -131,7 +151,7 @@ export async function gatherSources(
for (const foundImport of foundImports) {
let importName: string;
if (foundImport[0] === ".") {
importName = urlSys.resolve(relativePath, foundImport);
importName = resolvePath(relativePath, foundImport);
} else {
importName = foundImport;
}
Expand Down

0 comments on commit 2d60dce

Please sign in to comment.