• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkgxdev / libpkgx / 6509968279

13 Oct 2023 03:11PM UTC coverage: 80.838%. First build
6509968279

Pull #48

github

web-flow
Merge 2db7a0eb4 into 99c9c22af
Pull Request #48: v1 wip

475 of 667 branches covered (0.0%)

Branch coverage included in aggregate %.

141 of 163 new or added lines in 12 files covered. (86.5%)

2149 of 2579 relevant lines covered (83.33%)

1029.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.94
/src/hooks/useConfig.ts
1
import { flatmap } from "../utils/misc.ts"
48✔
2
import host from "../utils/host.ts"
48✔
3
import Path from "../utils/Path.ts"
48✔
4

5
export interface Config {
6
  prefix: Path
7
  pantries: Path[]
8
  cache: Path
9
  data: Path
10

11
  options: {
12
    /// prefer xz or gz for bottle downloads
13
    compression: 'xz' | 'gz'
14
  }
15

16
  UserAgent?: string
17

18
  git?: Path
19
}
20

21
function platform_cache_default() {
66✔
22
  if (host().platform == 'darwin') {
22!
23
    return Path.home().join('Library/Caches')
22✔
24
  } else {
×
25
    return Path.home().join('.cache')
44✔
26
  }
44✔
27
}
66✔
28

NEW
29
const SEP = Deno.build.os == 'windows' ? ';' : ':'
×
30

31
export function ConfigDefault(env = Deno.env.toObject()): Config {
48✔
32
  const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ?? Path.home().join('.pkgx')
154✔
NEW
33
  const pantries = env['PKGX_PANTRY_PATH']?.split(SEP).compact(x => flatmap(x.trim(), x => Path.abs(x) ?? Path.cwd().join(x))) ?? []
×
34
  const cache = (flatmap(env["XDG_CACHE_HOME"], x => new Path(x)) ?? platform_cache_default()).join("pkgx")
154✔
35
  const isCI = boolize(env['CI']) ?? false
154✔
36
  const UserAgent = flatmap(getv(), v => `libpkgx/${v}`) ?? 'libpkgx'
✔
37

38
  const data = (() => {
154✔
39
    const xdg = env["XDG_DATA_HOME"]
254✔
40
    if (xdg) {
254✔
41
      return new Path(xdg)
336✔
42
    } else if (host().platform == "darwin") {
86!
43
      return Path.home().join("Library/Application Support")
92✔
44
    } else {
×
45
      return Path.home().join(".local/share")
180✔
46
    }
180✔
47
  })().join("pkgx")
154✔
48

49
  //TODO prefer 'xz' on Linux (as well) if supported
154✔
50
  const compression = !isCI && host().platform == 'darwin' ? 'xz' : 'gz'
52✔
51

52
  return {
154✔
53
    prefix,
154✔
54
    pantries,
154✔
55
    cache,
154✔
56
    data,
154✔
57
    UserAgent,
154✔
58
    options: {
154✔
59
      compression,
154✔
60
    },
154✔
61
    git: git(prefix, env.PATH)
154✔
62
  }
154✔
63
}
154✔
64

65
function getv(): string | undefined {
148✔
66
  if (typeof Deno === 'undefined') {
×
67
    const url = new URL(import.meta.url)
×
68
    const path = new Path(url.pathname).parent().parent().parent().join("package.json")
×
69
    const blob = Deno.readFileSync(path.string)
×
70
    const txt = new TextDecoder().decode(blob)
×
71
    const { version } = JSON.parse(txt)
×
72
    return typeof version == 'string' ? version : undefined
×
73
  }
×
74
}
148✔
75

76
const gt = globalThis as unknown as {sh_pkgx_config?: Config}
48✔
77

78
export default function useConfig(input?: Config): Config {
48✔
79
  // storing on globalThis so our config is shared across
781✔
80
  // potentially multiple versions of libpkgx being loaded in the same process
781✔
81
  if (!gt.sh_pkgx_config || input) {
781✔
82
    gt.sh_pkgx_config = input ?? ConfigDefault()
×
83
  }
863✔
84
  return {...gt.sh_pkgx_config}  // copy to prevent mutation
2,343✔
85
}
781✔
86

87
function boolize(input: string | undefined): boolean | undefined {
163✔
88
  switch (input?.trim()?.toLowerCase()) {
163✔
89
    case '0':
163✔
90
    case 'false':
163✔
91
    case 'no':
163✔
92
      return false
172✔
93
    case '1':
163✔
94
    case 'true':
163✔
95
    case 'yes':
163✔
96
      return true
178✔
97
  }
163✔
98
}
163✔
99

100
function reset() {
×
101
  return delete gt.sh_pkgx_config
×
102
}
×
103

104
function initialized() {
51✔
105
  return gt.sh_pkgx_config !== undefined
51✔
106
}
51✔
107

108
export const _internals = { reset, initialized, boolize }
240✔
109

110

111
/// we support a pkgx installed or system installed git, nothing else
3✔
112
/// eg. `git` could be a symlink in `PATH` to pkgx, which would cause a fork bomb
3✔
113
/// on darwin if xcode or xcode/clt is not installed this will fail to our http fallback above
3✔
114
//TODO be able to use our own git if installed
3✔
115
//NOTE however we don’t want to have to fully hydrate its env when libpkgx is initialized only when needed so…
3✔
116
function git(_prefix: Path, PATH?: string): Path | undefined {
148✔
117
  return usr()
148✔
118

119
  function usr() {
148✔
120
    // only return /usr/bin if in the PATH so user can explicitly override this
248✔
121
    const rv = PATH?.split(":")?.includes("/usr/bin") ? new Path("/usr") : undefined
248✔
122

123
    /// don’t cause macOS to abort and then prompt the user to install the XcodeCLT
248✔
124
    //FIXME test! but this is hard to test without docker images or something!
248✔
125
    if (host().platform == 'darwin') {
84!
126
      if (new Path("/Library/Developer/CommandLineTools/usr/bin/git").isExecutableFile()) return rv
84!
127
      if (new Path("/Applications/Xcode.app").isDirectory()) return rv
×
128
      return  // don’t use `git`
×
129
    }
×
130

131
    return rv?.join("bin/git")
164✔
132
  }
248✔
133
}
148✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc