Changesets · Branches · Tags · Compare
| Changeset | 2b37ea847073f71f10ea713184a955af66f5057db108d94ffec9382f6f5fd57c |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-18 15:34 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-18 15:34 -07:00 |
| Parents | 71e131496249 |
2 files changed
modified packages/config-yaml/src/interfaces/slugs.test.ts +58 −0
| @@ -158,4 +158,62 @@ | ||
| 158 | 158 | "Unknown URI type: invalid", |
| 159 | 159 | ); |
| 160 | 160 | }); |
| 161 | + | |
| 162 | + it("should expand leading tilde when HOME is available", () => { | |
| 163 | + const originalHome = process.env.HOME; | |
| 164 | + const originalUserProfile = process.env.USERPROFILE; | |
| 165 | + | |
| 166 | + try { | |
| 167 | + process.env.HOME = "/tmp/test-home"; | |
| 168 | + delete process.env.USERPROFILE; | |
| 169 | + | |
| 170 | + const decoded = decodePackageIdentifier("~/package.yaml"); | |
| 171 | + | |
| 172 | + expect(decoded).toEqual({ | |
| 173 | + uriType: "file" as const, | |
| 174 | + fileUri: "/tmp/test-home/package.yaml", | |
| 175 | + }); | |
| 176 | + } finally { | |
| 177 | + if (originalHome === undefined) { | |
| 178 | + delete process.env.HOME; | |
| 179 | + } else { | |
| 180 | + process.env.HOME = originalHome; | |
| 181 | + } | |
| 182 | + | |
| 183 | + if (originalUserProfile === undefined) { | |
| 184 | + delete process.env.USERPROFILE; | |
| 185 | + } else { | |
| 186 | + process.env.USERPROFILE = originalUserProfile; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + }); | |
| 190 | + | |
| 191 | + it("should leave leading tilde when no home directory is available", () => { | |
| 192 | + const originalHome = process.env.HOME; | |
| 193 | + const originalUserProfile = process.env.USERPROFILE; | |
| 194 | + | |
| 195 | + try { | |
| 196 | + delete process.env.HOME; | |
| 197 | + delete process.env.USERPROFILE; | |
| 198 | + | |
| 199 | + const decoded = decodePackageIdentifier("~/package.yaml"); | |
| 200 | + | |
| 201 | + expect(decoded).toEqual({ | |
| 202 | + uriType: "file" as const, | |
| 203 | + fileUri: "~/package.yaml", | |
| 204 | + }); | |
| 205 | + } finally { | |
| 206 | + if (originalHome === undefined) { | |
| 207 | + delete process.env.HOME; | |
| 208 | + } else { | |
| 209 | + process.env.HOME = originalHome; | |
| 210 | + } | |
| 211 | + | |
| 212 | + if (originalUserProfile === undefined) { | |
| 213 | + delete process.env.USERPROFILE; | |
| 214 | + } else { | |
| 215 | + process.env.USERPROFILE = originalUserProfile; | |
| 216 | + } | |
| 217 | + } | |
| 218 | + }); | |
| 161 | 219 | }); |
modified packages/config-yaml/src/interfaces/slugs.ts +47 −2
| @@ -1,4 +1,49 @@ | ||
| 1 | -import os from "node:os"; | |
| 1 | +type ProcessWithEnv = { | |
| 2 | + env?: Record<string, string | undefined>; | |
| 3 | +}; | |
| 4 | + | |
| 5 | +function getProcessEnv(): Record<string, string | undefined> | undefined { | |
| 6 | + if ( | |
| 7 | + typeof process !== "undefined" && | |
| 8 | + process && | |
| 9 | + typeof process === "object" | |
| 10 | + ) { | |
| 11 | + return (process as ProcessWithEnv).env; | |
| 12 | + } | |
| 13 | + | |
| 14 | + if (typeof globalThis !== "undefined") { | |
| 15 | + const maybeProcess = (globalThis as { process?: ProcessWithEnv }).process; | |
| 16 | + return maybeProcess?.env; | |
| 17 | + } | |
| 18 | + | |
| 19 | + return undefined; | |
| 20 | +} | |
| 21 | + | |
| 22 | +function getHomeDirectory(): string | undefined { | |
| 23 | + const env = getProcessEnv(); | |
| 24 | + | |
| 25 | + const fromHome = env?.HOME?.trim(); | |
| 26 | + if (fromHome) { | |
| 27 | + return fromHome; | |
| 28 | + } | |
| 29 | + | |
| 30 | + const fromUserProfile = env?.USERPROFILE?.trim(); | |
| 31 | + if (fromUserProfile) { | |
| 32 | + return fromUserProfile; | |
| 33 | + } | |
| 34 | + | |
| 35 | + return undefined; | |
| 36 | +} | |
| 37 | + | |
| 38 | +function expandLeadingTilde(identifier: string): string { | |
| 39 | + const homeDirectory = getHomeDirectory(); | |
| 40 | + if (!homeDirectory) { | |
| 41 | + return identifier; | |
| 42 | + } | |
| 43 | + | |
| 44 | + // Only replace a leading ~ so relative paths like ../~file stay untouched | |
| 45 | + return homeDirectory + identifier.slice(1); | |
| 46 | +} | |
| 2 | 47 | |
| 3 | 48 | export interface PackageSlug { |
| 4 | 49 | ownerSlug: string; |
| @@ -67,7 +112,7 @@ | ||
| 67 | 112 | else if (identifier.startsWith("~")) { |
| 68 | 113 | return { |
| 69 | 114 | uriType: "file", |
| 70 | - fileUri: identifier.replace("~", os.homedir()), | |
| 115 | + fileUri: expandLeadingTilde(identifier), | |
| 71 | 116 | }; |
| 72 | 117 | } |
| 73 | 118 | // Otherwise, it's a slug |