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

pkgxdev / libpkgx / 10737309581

06 Sep 2024 11:09AM UTC coverage: 82.996% (+0.02%) from 82.978%
10737309581

push

github

mxcl
all configurable PKGX_DIST_URL

closes #71

556 of 733 branches covered (75.85%)

Branch coverage included in aggregate %.

5 of 6 new or added lines in 3 files covered. (83.33%)

2441 of 2878 relevant lines covered (84.82%)

2626.93 hits per line

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

73.46
/src/hooks/useConfig.ts
1
import { flatmap } from "../utils/misc.ts"
36✔
2
import { deno } from "../deps.ts"
36✔
3
import host from "../utils/host.ts"
36✔
4
import Path from "../utils/Path.ts"
36✔
5

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

12
  dist: string
13

14
  options: {
15
    /// prefer xz or gz for bottle downloads
16
    compression: 'xz' | 'gz'
17
  }
18

19
  UserAgent?: string
20

21
  git?: Path
22
}
23

24
function platform_cache_default(home: Path, { LOCALAPPDATA }: { LOCALAPPDATA?: string }) {
110✔
25
  switch (Deno.build.os) {
110✔
26
  case 'darwin':
55!
27
    return home.join('Library/Caches')
55✔
28
  case 'windows':
×
29
    return flatmap(LOCALAPPDATA, Path.abs) ?? home.join('AppData/Local')
×
30
  default:
55!
31
    return home.join('.cache')
55✔
32
  }
110✔
33
}
110✔
34

35
function platform_data_home_default(home: Path, { LOCALAPPDATA }: { LOCALAPPDATA?: string }) {
110✔
36
  switch (host().platform) {
110✔
37
  case 'darwin':
55!
38
    return home.join("Library/Application Support")
55✔
39
  case 'windows': {
×
40
    if (LOCALAPPDATA) {
×
41
      return new Path(LOCALAPPDATA)
×
42
    } else {
×
43
      return home.join("AppData/Local")
×
44
    }}
×
45
  default:
55!
46
    return home.join(".local/share")
55✔
47
  }
110✔
48
}
110✔
49

50
const SEP = Deno.build.os == 'windows' ? ';' : ':'
×
51

52
export function ConfigDefault(env = Deno.env.toObject()): Config {
36✔
53
  const home = flatmap(env['PKGX_HOME'], x => new Path(x)) ?? Path.home()
114✔
54
  const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ?? home.join('.pkgx')
114✔
55
  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")
114✔
57
  const data = (flatmap(env["XDG_DATA_HOME"], Path.abs) ?? platform_data_home_default(home, env)).join("pkgx")
114✔
NEW
58
  const dist = env['PKGX_DIST_URL']?.trim() ?? 'https://dist.pkgx.dev'
×
59
  const isCI = boolize(env['CI']) ?? false
114✔
60
  const UserAgent = flatmap(getv(), v => `libpkgx/${v}`) ?? 'libpkgx'
✔
61
  //TODO prefer 'xz' on Linux (as well) if supported
114✔
62
  const compression = !isCI && host().platform == 'darwin' ? 'xz' : 'gz'
57✔
63

64
  return {
114✔
65
    prefix,
114✔
66
    pantries,
114✔
67
    cache,
114✔
68
    data,
114✔
69
    dist,
114✔
70
    UserAgent,
114✔
71
    options: {
114✔
72
      compression,
114✔
73
    },
114✔
74
    git: git(prefix, env.PATH)
114✔
75
  }
114✔
76
}
114✔
77

78
function getv(): string | undefined {
110✔
79
  if (typeof Deno === 'undefined') {
×
80
    const path = new Path(deno.fromFileUrl(import.meta.url)).parent().parent().parent().join("package.json")
×
81
    const blob = Deno.readFileSync(path.string)
×
82
    const txt = new TextDecoder().decode(blob)
×
83
    const { version } = JSON.parse(txt)
×
84
    return typeof version == 'string' ? version : undefined
×
85
  }
×
86
}
110✔
87

88
const gt = globalThis as unknown as {sh_pkgx_config?: Config}
36✔
89

90
export default function useConfig(input?: Config): Config {
36✔
91
  // storing on globalThis so our config is shared across
4,034✔
92
  // potentially multiple versions of libpkgx being loaded in the same process
4,034✔
93
  if (!gt.sh_pkgx_config || input) {
4,034✔
94
    gt.sh_pkgx_config = input ?? ConfigDefault()
4,096✔
95
  }
4,096✔
96
  return {...gt.sh_pkgx_config}  // copy to prevent mutation
12,102✔
97
}
4,034✔
98

99
function boolize(input: string | undefined): boolean | undefined {
120✔
100
  switch (input?.trim()?.toLowerCase()) {
120✔
101
  case '0':
120✔
102
  case 'false':
120✔
103
  case 'no':
120✔
104
    return false
126✔
105
  case '1':
120✔
106
  case 'true':
120✔
107
  case 'yes':
120✔
108
    return true
132✔
109
  }
120✔
110
}
120✔
111

112
function initialized() {
38✔
113
  return gt.sh_pkgx_config !== undefined
38✔
114
}
38✔
115

116
export const _internals = { initialized, boolize }
144✔
117

118

119
/// we support a pkgx installed or system installed git, nothing else
2✔
120
/// eg. `git` could be a symlink in `PATH` to pkgx, which would cause a fork bomb
2✔
121
/// on darwin if xcode or xcode/clt is not installed this will fail to our http fallback above
2✔
122
//TODO be able to use our own git if installed
2✔
123
//NOTE however we don’t want to have to fully hydrate its env when libpkgx is initialized only when needed so…
2✔
124
function git(_prefix: Path, PATH?: string): Path | undefined {
110✔
125
  return usr()
110✔
126

127
  function usr() {
110✔
128
    // only return /usr/bin if in the PATH so user can explicitly override this
184✔
129
    const rv = PATH?.split(":")?.includes("/usr/bin") ? new Path("/usr") : undefined
184✔
130

131
    return (() => {
184✔
132
      /// don’t cause macOS to abort and then prompt the user to install the XcodeCLT
258✔
133
      //FIXME test! but this is hard to test without docker images or something!
258✔
134
      switch (host().platform) {
258✔
135
      case 'darwin':
129!
136
        if (new Path("/Library/Developer/CommandLineTools/usr/bin/git").isExecutableFile()) return rv
129!
137
        if (new Path("/Applications/Xcode.app").isDirectory()) return rv
×
138
        return  // probably won’t work without prompting the user to install the XcodeCLT
×
139
      case "linux":
129!
140
        return rv
129✔
141
      case "windows":
×
142
        if (PATH) {
×
143
          //FIXME this is GitHub Actions specific
×
144
          return new Path('C:\Program Files\Git\cmd\git.exe')
×
145
        }
×
146
      }
258✔
147
    })()?.join("bin/git")
184✔
148
  }
184✔
149
}
110✔
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