Browse Source

Fix #202

Add support for conf.remoteRoot (similar to VSCode).

* server/helpers/workspace.js (resolveUrl): Replace remoteRoot when defined.
* server/spec/helpers/workspace-spec.js: Add tests.
pull/9/head
Nicolas Petton 2 years ago
parent
commit
f91215f656
No known key found for this signature in database GPG Key ID: E8BCD7866AFCF978
2 changed files with 72 additions and 3 deletions
  1. +14
    -3
      server/helpers/workspace.js
  2. +58
    -0
      server/spec/helpers/workspace-spec.js

+ 14
- 3
server/helpers/workspace.js View File

@ -70,22 +70,33 @@ const resolveRoot = conf => {
return resolve(dir);
};
const convertRemoteRoot = (path, conf) => {
if (!conf.remoteRoot) {
return path;
}
return join(
resolveRoot(conf),
path.replace(new RegExp(`^${conf.remoteRoot}`), "")
);
};
const resolveUrl = (url, conf) => {
// In Node, script urls can be file paths. The path doesn't
// always exist either, so also check for a protocol when parsed
// as a URL.
if (isAbsolute(url) || !parse(url).protocol) {
return url;
return convertRemoteRoot(url, conf);
}
// Always treat URLs using the file: protocol to have absolute pathnames.
let { protocol, pathname } = new URL(url);
if (protocol === "file:") {
return pathname;
return convertRemoteRoot(pathname, conf);
}
let root = resolveRoot(conf);
return resolve(`${root}/${pathname}`);
return convertRemoteRoot(resolve(`${root}/${pathname}`), conf);
};
const expandRoot = (path, root = "") => {


+ 58
- 0
server/spec/helpers/workspace-spec.js View File

@ -219,4 +219,62 @@ describe("URL resolution", () => {
expect(resolveUrl(url, conf)).toEqual(url);
});
describe("remote root replacement", () => {
it("Does not replace remote root when no remote root defined", () => {
let conf = {
projectFile: "/home/user/projects/foo/.indium.json"
};
let path = "/home/user/projects/foo/bar.js";
expect(resolveUrl(path, conf)).toEqual(path);
});
it("Replaces remote root when defined", () => {
let conf = {
projectFile: "/home/user/projects/foo/.indium.json",
remoteRoot: "/var/task/"
};
let path = "/var/task/bar.js";
expect(resolveUrl(path, conf)).toEqual("/home/user/projects/foo/bar.js");
});
it("Replaces local root with remote root when defined", () => {
let conf = {
projectFile: "/home/user/projects/foo/.indium.json",
root: "bar",
remoteRoot: "/var/task/"
};
let path = "/var/task/baz.js";
expect(resolveUrl(path, conf)).toEqual("/home/user/projects/foo/bar/baz.js");
});
it("Replaces remote root with no trailing slash", () => {
let conf = {
projectFile: "/home/user/projects/foo/.indium.json",
remoteRoot: "/var/task"
};
let path = "/var/task/bar/baz.js";
expect(resolveUrl(path, conf)).toEqual("/home/user/projects/foo/bar/baz.js");
});
it("Replaces remote root with file:// protocol", () => {
let conf = {
projectFile: "/home/user/projects/foo/.indium.json",
remoteRoot: "/var/task/"
};
let path = "/var/task/bar.js";
expect(resolveUrl(path, conf)).toEqual("/home/user/projects/foo/bar.js");
});
});
});

Loading…
Cancel
Save