fix: make config-yaml compatible with browser (#7842)
This commit is contained in:
parent
95ba13e70b
commit
02465b6528
2 changed files with 105 additions and 2 deletions
|
|
@ -158,4 +158,62 @@ describe("PackageIdentifier", () => {
|
|||
"Unknown URI type: invalid",
|
||||
);
|
||||
});
|
||||
|
||||
it("should expand leading tilde when HOME is available", () => {
|
||||
const originalHome = process.env.HOME;
|
||||
const originalUserProfile = process.env.USERPROFILE;
|
||||
|
||||
try {
|
||||
process.env.HOME = "/tmp/test-home";
|
||||
delete process.env.USERPROFILE;
|
||||
|
||||
const decoded = decodePackageIdentifier("~/package.yaml");
|
||||
|
||||
expect(decoded).toEqual({
|
||||
uriType: "file" as const,
|
||||
fileUri: "/tmp/test-home/package.yaml",
|
||||
});
|
||||
} finally {
|
||||
if (originalHome === undefined) {
|
||||
delete process.env.HOME;
|
||||
} else {
|
||||
process.env.HOME = originalHome;
|
||||
}
|
||||
|
||||
if (originalUserProfile === undefined) {
|
||||
delete process.env.USERPROFILE;
|
||||
} else {
|
||||
process.env.USERPROFILE = originalUserProfile;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("should leave leading tilde when no home directory is available", () => {
|
||||
const originalHome = process.env.HOME;
|
||||
const originalUserProfile = process.env.USERPROFILE;
|
||||
|
||||
try {
|
||||
delete process.env.HOME;
|
||||
delete process.env.USERPROFILE;
|
||||
|
||||
const decoded = decodePackageIdentifier("~/package.yaml");
|
||||
|
||||
expect(decoded).toEqual({
|
||||
uriType: "file" as const,
|
||||
fileUri: "~/package.yaml",
|
||||
});
|
||||
} finally {
|
||||
if (originalHome === undefined) {
|
||||
delete process.env.HOME;
|
||||
} else {
|
||||
process.env.HOME = originalHome;
|
||||
}
|
||||
|
||||
if (originalUserProfile === undefined) {
|
||||
delete process.env.USERPROFILE;
|
||||
} else {
|
||||
process.env.USERPROFILE = originalUserProfile;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,49 @@
|
|||
import os from "node:os";
|
||||
type ProcessWithEnv = {
|
||||
env?: Record<string, string | undefined>;
|
||||
};
|
||||
|
||||
function getProcessEnv(): Record<string, string | undefined> | undefined {
|
||||
if (
|
||||
typeof process !== "undefined" &&
|
||||
process &&
|
||||
typeof process === "object"
|
||||
) {
|
||||
return (process as ProcessWithEnv).env;
|
||||
}
|
||||
|
||||
if (typeof globalThis !== "undefined") {
|
||||
const maybeProcess = (globalThis as { process?: ProcessWithEnv }).process;
|
||||
return maybeProcess?.env;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function getHomeDirectory(): string | undefined {
|
||||
const env = getProcessEnv();
|
||||
|
||||
const fromHome = env?.HOME?.trim();
|
||||
if (fromHome) {
|
||||
return fromHome;
|
||||
}
|
||||
|
||||
const fromUserProfile = env?.USERPROFILE?.trim();
|
||||
if (fromUserProfile) {
|
||||
return fromUserProfile;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function expandLeadingTilde(identifier: string): string {
|
||||
const homeDirectory = getHomeDirectory();
|
||||
if (!homeDirectory) {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
// Only replace a leading ~ so relative paths like ../~file stay untouched
|
||||
return homeDirectory + identifier.slice(1);
|
||||
}
|
||||
|
||||
export interface PackageSlug {
|
||||
ownerSlug: string;
|
||||
|
|
@ -67,7 +112,7 @@ export function decodePackageIdentifier(identifier: string): PackageIdentifier {
|
|||
else if (identifier.startsWith("~")) {
|
||||
return {
|
||||
uriType: "file",
|
||||
fileUri: identifier.replace("~", os.homedir()),
|
||||
fileUri: expandLeadingTilde(identifier),
|
||||
};
|
||||
}
|
||||
// Otherwise, it's a slug
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue