From e44ce799464de05a5a7443a09d592fceab560350 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sat, 29 Mar 2025 08:20:47 -0400 Subject: [PATCH 1/3] semver.ts needs no deps --- src/utils/semver.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/utils/semver.ts b/src/utils/semver.ts index cf0a2a6..ec365e9 100644 --- a/src/utils/semver.ts +++ b/src/utils/semver.ts @@ -1,6 +1,3 @@ -//HEY YOU! DO NOT CHANGE THIS TO USE deps.ts since it breaks pkgx/gui and scripts ’n’ shit -import { isArray, isString } from "https://deno.land/x/is_what@v4.1.15/src/index.ts" - /** * we have our own implementation because open source is full of weird * but *almost* valid semver schemes, eg: @@ -129,7 +126,7 @@ export class Range { constructor(input: string | ([SemVer, SemVer] | SemVer)[]) { if (input === "*") { this.set = '*' - } else if (!isString(input)) { + } else if (typeof input !== 'string') { this.set = input } else { input = input.trim() @@ -193,7 +190,7 @@ export class Range { if (this.set.length == 0) throw err() for (const i of this.set) { - if (isArray(i) && !i[0].lt(i[1])) throw err() + if (Array.isArray(i) && !i[0].lt(i[1])) throw err() } } } @@ -203,7 +200,7 @@ export class Range { return '*' } else { return this.set.map(v => { - if (!isArray(v)) return `=${v.toString()}` + if (!Array.isArray(v)) return `=${v.toString()}` const [v1, v2] = v if (v2.major == v1.major + 1 && v2.minor == 0 && v2.patch == 0) { const v = chomp(v1) @@ -294,7 +291,7 @@ export class Range { return true } else { return this.set.some(v => { - if (isArray(v)) { + if (Array.isArray(v)) { const [v1, v2] = v return version.compare(v1) >= 0 && version.compare(v2) < 0 } else { @@ -311,7 +308,7 @@ export class Range { single(): SemVer | undefined { if (this.set === '*') return if (this.set.length > 1) return - return isArray(this.set[0]) ? undefined : this.set[0] + return Array.isArray(this.set[0]) ? undefined : this.set[0] } [Symbol.for("Deno.customInspect")]() { @@ -359,12 +356,12 @@ export function intersect(a: Range, b: Range): Range { for (const aa of a.set) { for (const bb of b.set) { - if (!isArray(aa) && !isArray(bb)) { + if (!Array.isArray(aa) && !Array.isArray(bb)) { if (aa.eq(bb)) set.push(aa) - } else if (!isArray(aa)) { + } else if (!Array.isArray(aa)) { const bbb = bb as [SemVer, SemVer] if (aa.compare(bbb[0]) >= 0 && aa.lt(bbb[1])) set.push(aa) - } else if (!isArray(bb)) { + } else if (!Array.isArray(bb)) { const aaa = aa as [SemVer, SemVer] if (bb.compare(aaa[0]) >= 0 && bb.lt(aaa[1])) set.push(bb) } else { From 0b0e647182fd029ab2dd9e507ccb7692f41c1ffe Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sat, 29 Mar 2025 08:20:58 -0400 Subject: [PATCH 2/3] Update useConfig to pkgx^2 rules --- src/hooks/useConfig.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/hooks/useConfig.ts b/src/hooks/useConfig.ts index 1953579..cc5c325 100644 --- a/src/hooks/useConfig.ts +++ b/src/hooks/useConfig.ts @@ -51,10 +51,18 @@ const SEP = Deno.build.os == 'windows' ? ';' : ':' export function ConfigDefault(env = Deno.env.toObject()): Config { const home = flatmap(env['PKGX_HOME'], x => new Path(x)) ?? Path.home() - const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ?? home.join('.pkgx') + const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ?? + flatmap(env['XDG_DATA_HOME'], x => new Path(x).join("pkgx")) ?? + home.join('.pkgx') const pantries = env['PKGX_PANTRY_PATH']?.split(SEP).compact(x => flatmap(x.trim(), x => Path.abs(x) ?? Path.cwd().join(x))) ?? [] - const cache = (flatmap(env["XDG_CACHE_HOME"], Path.abs) ?? platform_cache_default(home, env)).join("pkgx") - const data = (flatmap(env["XDG_DATA_HOME"], Path.abs) ?? platform_data_home_default(home, env)).join("pkgx") + const cache = ( + (Deno.build.os == 'linux' ? flatmap(env["XDG_CACHE_HOME"], Path.abs) : undefined) + ?? platform_cache_default(home, env) + ).join("pkgx") + const data = ( + (Deno.build.os == 'linux' ? flatmap(env["XDG_DATA_HOME"], Path.abs) : undefined) + ?? platform_data_home_default(home, env) + ).join("pkgx") const dist = env['PKGX_DIST_URL']?.trim() ?? 'https://dist.pkgx.dev' const isCI = boolize(env['CI']) ?? false const UserAgent = flatmap(getv(), v => `libpkgx/${v}`) ?? 'libpkgx' From 54836c6e62bf15ecd8a29597d651832a6a023b79 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sat, 29 Mar 2025 08:26:56 -0400 Subject: [PATCH 3/3] Add tests --- src/hooks/useConfig.test.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/hooks/useConfig.test.ts b/src/hooks/useConfig.test.ts index dd0ccba..b0c1fd5 100644 --- a/src/hooks/useConfig.test.ts +++ b/src/hooks/useConfig.test.ts @@ -12,7 +12,6 @@ Deno.test("useConfig", () => { assertEquals(config.UserAgent, "libpkgx") } - const PKGX_PANTRY_PATH = Deno.build.os == 'windows' ? "C:\\foo;D:\\bar" : "/foo:/bar" config = ConfigDefault({ PKGX_PANTRY_PATH, CI: "true" }) @@ -45,3 +44,26 @@ Deno.test("useConfig empty PKGX_PANTRY_PATH is ignored", () => { assertEquals(ConfigDefault({ PKGX_PANTRY_PATH: "" }).pantries, []) assertEquals(ConfigDefault({ PKGX_PANTRY_PATH: ` ${SEP} ${SEP}` }).pantries, []) }) + +Deno.test("pkgx^2 rules", () => { + switch (Deno.build.os) { + case 'windows': + assertEquals(ConfigDefault({ XDG_DATA_HOME: "C:\\foo" }).data, Path.home().join("AppData/Local")); + assertEquals(ConfigDefault().data, Path.home().join("AppData/Local")); + + assertEquals(ConfigDefault({ XDG_CACHE_HOME: "C:\\foo" }).cache, Path.home().join("AppData/Local")); + assertEquals(ConfigDefault().cache, Path.home().join("AppData/Local")); + break; + case 'darwin': + assertEquals(ConfigDefault({ XDG_DATA_HOME: "/foo" }).data, Path.home().join("Library/Application Support/pkgx")); + assertEquals(ConfigDefault().data, Path.home().join("Library/Application Support/pkgx")); + + assertEquals(ConfigDefault({ XDG_CACHE_HOME: "/foo" }).cache, Path.home().join("Library/Caches/pkgx")); + assertEquals(ConfigDefault().cache, Path.home().join("Library/Caches/pkgx")); + break; + case 'linux': + assertEquals(ConfigDefault({ XDG_DATA_HOME: "/foo" }).data, new Path("/foo/pkgx")); + assertEquals(ConfigDefault().data, Path.home().join(".local/share/pkgx")); + break; + } +}) \ No newline at end of file