Skip to content

Commit 59f682c

Browse files
authored
Merge pull request #83 from pkgxdev/useConfig-pkgx-2-rules
Use config pkgx 2 rules
2 parents b59b1a0 + 54836c6 commit 59f682c

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

src/hooks/useConfig.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Deno.test("useConfig", () => {
1212
assertEquals(config.UserAgent, "libpkgx")
1313
}
1414

15-
1615
const PKGX_PANTRY_PATH = Deno.build.os == 'windows' ? "C:\\foo;D:\\bar" : "/foo:/bar"
1716

1817
config = ConfigDefault({ PKGX_PANTRY_PATH, CI: "true" })
@@ -45,3 +44,26 @@ Deno.test("useConfig empty PKGX_PANTRY_PATH is ignored", () => {
4544
assertEquals(ConfigDefault({ PKGX_PANTRY_PATH: "" }).pantries, [])
4645
assertEquals(ConfigDefault({ PKGX_PANTRY_PATH: ` ${SEP} ${SEP}` }).pantries, [])
4746
})
47+
48+
Deno.test("pkgx^2 rules", () => {
49+
switch (Deno.build.os) {
50+
case 'windows':
51+
assertEquals(ConfigDefault({ XDG_DATA_HOME: "C:\\foo" }).data, Path.home().join("AppData/Local"));
52+
assertEquals(ConfigDefault().data, Path.home().join("AppData/Local"));
53+
54+
assertEquals(ConfigDefault({ XDG_CACHE_HOME: "C:\\foo" }).cache, Path.home().join("AppData/Local"));
55+
assertEquals(ConfigDefault().cache, Path.home().join("AppData/Local"));
56+
break;
57+
case 'darwin':
58+
assertEquals(ConfigDefault({ XDG_DATA_HOME: "/foo" }).data, Path.home().join("Library/Application Support/pkgx"));
59+
assertEquals(ConfigDefault().data, Path.home().join("Library/Application Support/pkgx"));
60+
61+
assertEquals(ConfigDefault({ XDG_CACHE_HOME: "/foo" }).cache, Path.home().join("Library/Caches/pkgx"));
62+
assertEquals(ConfigDefault().cache, Path.home().join("Library/Caches/pkgx"));
63+
break;
64+
case 'linux':
65+
assertEquals(ConfigDefault({ XDG_DATA_HOME: "/foo" }).data, new Path("/foo/pkgx"));
66+
assertEquals(ConfigDefault().data, Path.home().join(".local/share/pkgx"));
67+
break;
68+
}
69+
})

src/hooks/useConfig.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ const SEP = Deno.build.os == 'windows' ? ';' : ':'
5151

5252
export function ConfigDefault(env = Deno.env.toObject()): Config {
5353
const home = flatmap(env['PKGX_HOME'], x => new Path(x)) ?? Path.home()
54-
const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ?? home.join('.pkgx')
54+
const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ??
55+
flatmap(env['XDG_DATA_HOME'], x => new Path(x).join("pkgx")) ??
56+
home.join('.pkgx')
5557
const pantries = env['PKGX_PANTRY_PATH']?.split(SEP).compact(x => flatmap(x.trim(), x => Path.abs(x) ?? Path.cwd().join(x))) ?? []
56-
const cache = (flatmap(env["XDG_CACHE_HOME"], Path.abs) ?? platform_cache_default(home, env)).join("pkgx")
57-
const data = (flatmap(env["XDG_DATA_HOME"], Path.abs) ?? platform_data_home_default(home, env)).join("pkgx")
58+
const cache = (
59+
(Deno.build.os == 'linux' ? flatmap(env["XDG_CACHE_HOME"], Path.abs) : undefined)
60+
?? platform_cache_default(home, env)
61+
).join("pkgx")
62+
const data = (
63+
(Deno.build.os == 'linux' ? flatmap(env["XDG_DATA_HOME"], Path.abs) : undefined)
64+
?? platform_data_home_default(home, env)
65+
).join("pkgx")
5866
const dist = env['PKGX_DIST_URL']?.trim() ?? 'https://dist.pkgx.dev'
5967
const isCI = boolize(env['CI']) ?? false
6068
const UserAgent = flatmap(getv(), v => `libpkgx/${v}`) ?? 'libpkgx'

src/utils/semver.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//HEY YOU! DO NOT CHANGE THIS TO USE deps.ts since it breaks pkgx/gui and scripts ’n’ shit
2-
import { isArray, isString } from "https://deno.land/x/is_what@v4.1.15/src/index.ts"
3-
41
/**
52
* we have our own implementation because open source is full of weird
63
* but *almost* valid semver schemes, eg:
@@ -129,7 +126,7 @@ export class Range {
129126
constructor(input: string | ([SemVer, SemVer] | SemVer)[]) {
130127
if (input === "*") {
131128
this.set = '*'
132-
} else if (!isString(input)) {
129+
} else if (typeof input !== 'string') {
133130
this.set = input
134131
} else {
135132
input = input.trim()
@@ -193,7 +190,7 @@ export class Range {
193190
if (this.set.length == 0) throw err()
194191

195192
for (const i of this.set) {
196-
if (isArray(i) && !i[0].lt(i[1])) throw err()
193+
if (Array.isArray(i) && !i[0].lt(i[1])) throw err()
197194
}
198195
}
199196
}
@@ -203,7 +200,7 @@ export class Range {
203200
return '*'
204201
} else {
205202
return this.set.map(v => {
206-
if (!isArray(v)) return `=${v.toString()}`
203+
if (!Array.isArray(v)) return `=${v.toString()}`
207204
const [v1, v2] = v
208205
if (v2.major == v1.major + 1 && v2.minor == 0 && v2.patch == 0) {
209206
const v = chomp(v1)
@@ -294,7 +291,7 @@ export class Range {
294291
return true
295292
} else {
296293
return this.set.some(v => {
297-
if (isArray(v)) {
294+
if (Array.isArray(v)) {
298295
const [v1, v2] = v
299296
return version.compare(v1) >= 0 && version.compare(v2) < 0
300297
} else {
@@ -311,7 +308,7 @@ export class Range {
311308
single(): SemVer | undefined {
312309
if (this.set === '*') return
313310
if (this.set.length > 1) return
314-
return isArray(this.set[0]) ? undefined : this.set[0]
311+
return Array.isArray(this.set[0]) ? undefined : this.set[0]
315312
}
316313

317314
[Symbol.for("Deno.customInspect")]() {
@@ -359,12 +356,12 @@ export function intersect(a: Range, b: Range): Range {
359356

360357
for (const aa of a.set) {
361358
for (const bb of b.set) {
362-
if (!isArray(aa) && !isArray(bb)) {
359+
if (!Array.isArray(aa) && !Array.isArray(bb)) {
363360
if (aa.eq(bb)) set.push(aa)
364-
} else if (!isArray(aa)) {
361+
} else if (!Array.isArray(aa)) {
365362
const bbb = bb as [SemVer, SemVer]
366363
if (aa.compare(bbb[0]) >= 0 && aa.lt(bbb[1])) set.push(aa)
367-
} else if (!isArray(bb)) {
364+
} else if (!Array.isArray(bb)) {
368365
const aaa = aa as [SemVer, SemVer]
369366
if (bb.compare(aaa[0]) >= 0 && bb.lt(aaa[1])) set.push(bb)
370367
} else {

0 commit comments

Comments
 (0)