Skip to content
Back to search

vlt

SkillWorks with Claude CodeGitHub

Agent skills for the vlt CLI and vlt.io registries.

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today

Check it deeper on your machine

It has changed since its scan on 11 Nov 2026. Its scripts were only partly scanned.

As a Claude Code plugin, install it and then ask Claude Code:

/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/vltpkg/vltpkg with scanskillsafety

Or from a terminal, clone it and run it:

git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/vltpkg/vltpkg'

It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.

Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/vltpkg--vltpkg/badge.svg)](/listings/vltpkg--vltpkg)

HTML

<a href="/listings/vltpkg--vltpkg"><img src="/api/listings/vltpkg--vltpkg/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

.claude-plugin/plugin.json · Manifest · 14 lines
  1. {
  2. "name": "vlt",
  3. "description": "Agent skills for the vlt CLI and vlt.io registries.",
  4. "author": {
  5. "name": "vlt technology inc.",
  6. "email": "support@vlt.sh",
  7. "url": "https://vlt.sh"
  8. },
  9. "skills": [
  10. "./skills/vlt-cli",
  11. "./skills/vlt-registry-migration",
  12. "./skills/dss-query"
  13. ]
  14. }
README.md · README · 125 lines · 1 finding marked
  1. ![vlt](https://github.com/user-attachments/assets/aec7c817-b83f-4d71-b34a-4e480b97e82c)
  2. # vlt /vōlt/
  3. ![vlt Version](https://img.shields.io/npm/v/vlt?logo=npm&label=Version)
  4. ![Package Downloads](https://img.shields.io/npm/dm/vlt?logo=npm&label=Downloads)
  5. ![GitHub Branch Status](https://img.shields.io/github/checks-status/vltpkg/vltpkg/main?logo=github&label=GitHub)
  6. ![Discord Server Status](https://img.shields.io/discord/1093366081067954178?logo=discord&label=Discord)
  7. [![Socket Security Status](https://socket.dev/api/badge/npm/package/vlt)](https://socket.dev/npm/package/vlt)
  8. **Develop. Run. Distribute.**
  9. This is the source monorepo for the [vlt](https://www.vlt.io) package
  10. manager.
  11. ### Documentation
  12. Full documentation, startup guides & API references can be found at
  13. [docs.vlt.sh](https://docs.vlt.sh).
  14. ## Contributing
  15. Development requires Git, Node.js 22.22.x, and the latest released
  16. `vlt` CLI. The repository's [`.nvmrc`](./.nvmrc) selects the supported
  17. Node.js major when using `nvm`.
  18. ```bash
  19. # Clone the repository
  20. git clone https://github.com/vltpkg/vltpkg.git
  21. cd vltpkg
  22. # Install and select the Node.js version from .nvmrc
  23. nvm install
  24. nvm use
  25. # Install the released CLI used to bootstrap the repository
  26. curl -fsSL https://install.vlt.sh | bash
    • Note: Pipes a downloaded script directly into a shell (curl | sh / wget | bash)
    • Line 37 on GitHub
  27. # Install dependencies and prepare generated workspace files
  28. vlt install
  29. vlr --recursive prepare
  30. # Run the CLI directly from source
  31. ./scripts/bins/vlt --version
  32. ```
  33. `vlt setup` is not required for this repository. That command connects
  34. the CLI to a vlt.io account; this repository's checked-in
  35. [`vlt.json`](./vlt.json) already configures the registry used to
  36. bootstrap its dependencies.
  37. See the [contributing guide](./CONTRIBUTING.md) for development
  38. commands and workspace-specific guidance.
  39. ### Contributors
  40. <a href="https://github.com/vltpkg/vltpkg/graphs/contributors">
  41. <img src="https://contrib.rocks/image?repo=vltpkg/vltpkg" />
  42. </a>
  43. ### Licenses
  44. Below you can find a complete table of each workspace available in
  45. this repository and its corresponding license:
  46. #### Reusable Client Internals (`src/*`)
  47. | Workspace Name | License | Downloads |
  48. | -------------------------------------------------- | ----------------------------------------------------- | --------------------------------------------------------------------------------- |
  49. | [@vltpkg/cache](./src/cache) | [BSD-2-Clause-Patent](./src/cache/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/cache?logo=npm) |
  50. | [@vltpkg/cache-unzip](./src/cache-unzip) | [BSD-2-Clause-Patent](./src/cache-unzip/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/cache-unzip?logo=npm) |
  51. | [@vltpkg/cli-sdk](./src/cli-sdk) | [BSD-2-Clause-Patent](./src/cli-sdk/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/cli-sdk?logo=npm) |
  52. | [@vltpkg/cmd-shim](./src/cmd-shim) | [BSD-2-Clause-Patent](./src/cmd-shim/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/cmd-shim?logo=npm) |
  53. | [@vltpkg/config](./src/config) | [BSD-2-Clause-Patent](./src/config/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/config?logo=npm) |
  54. | [@vltpkg/dep-id](./src/dep-id) | [BSD-2-Clause-Patent](./src/dep-id/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/dep-id?logo=npm) |
  55. | [@vltpkg/dot-prop](./src/dot-prop) | [MIT](./src/dot-prop/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/dot-prop?logo=npm) |
  56. | [@vltpkg/dss-breadcrumb](./src/dss-breadcrumb) | [BSD-2-Clause-Patent](./src/dss-breadcrumb/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/dss-breadcrumb?logo=npm) |
  57. | [@vltpkg/dss-parser](./src/dss-parser) | [BSD-2-Clause-Patent](./src/dss-parser/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/dss-parser?logo=npm) |
  58. | [@vltpkg/error-cause](./src/error-cause) | [BSD-2-Clause-Patent](./src/error-cause/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/error-cause?logo=npm) |
  59. | [@vltpkg/fast-split](./src/fast-split) | [BSD-2-Clause-Patent](./src/fast-split/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/fast-split?logo=npm) |
  60. | [@vltpkg/git](./src/git) | [ISC](./src/git/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/git?logo=npm) |
  61. | [@vltpkg/git-scp-url](./src/git-scp-url) | [BSD-2-Clause-Patent](./src/git-scp-url/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/git-scp-url?logo=npm) |
  62. | [@vltpkg/graph](./src/graph) | [BSD-2-Clause-Patent](./src/graph/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/graph?logo=npm) |
  63. | [@vltpkg/graph-run](./src/graph-run) | [BSD-2-Clause-Patent](./src/graph-run/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/graph-run?logo=npm) |
  64. | [@vltpkg/init](./src/init) | [BSD-2-Clause-Patent](./src/init/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/init?logo=npm) |
  65. | [@vltpkg/keychain](./src/keychain) | [BSD-2-Clause-Patent](./src/keychain/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/keychain?logo=npm) |
  66. | [@vltpkg/output](./src/output) | [BSD-2-Clause-Patent](./src/output/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/output?logo=npm) |
  67. | [@vltpkg/package-info](./src/package-info) | [BSD-2-Clause-Patent](./src/package-info/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/package-info?logo=npm) |
  68. | [@vltpkg/package-json](./src/package-json) | [BSD-2-Clause-Patent](./src/package-json/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/package-json?logo=npm) |
  69. | [@vltpkg/pick-manifest](./src/pick-manifest) | [BSD-2-Clause-Patent](./src/pick-manifest/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/pick-manifest?logo=npm) |
  70. | [@vltpkg/promise-spawn](./src/promise-spawn) | [ISC](./src/promise-spawn/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/promise-spawn?logo=npm) |
  71. | [@vltpkg/project](./src/project) | [BSD-2-Clause-Patent](./src/project/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/project?logo=npm) |
  72. | [@vltpkg/query](./src/query) | [BSD-2-Clause-Patent](./src/query/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/query?logo=npm) |
  73. | [@vltpkg/registry-client](./src/registry-client) | [BSD-2-Clause-Patent](./src/registry-client/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/registry-client?logo=npm) |
  74. | [@vltpkg/rollback-remove](./src/rollback-remove) | [BSD-2-Clause-Patent](./src/rollback-remove/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/rollback-remove?logo=npm) |
  75. | [@vltpkg/run](./src/run) | [BSD-2-Clause-Patent](./src/run/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/run?logo=npm) |
  76. | [@vltpkg/satisfies](./src/satisfies) | [BSD-2-Clause-Patent](./src/satisfies/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/satisfies?logo=npm) |
  77. | [@vltpkg/security-archive](./src/security-archive) | [BSD-2-Clause-Patent](./src/security-archive/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/security-archive?logo=npm) |
  78. | [@vltpkg/semver](./src/semver) | [BSD-2-Clause-Patent](./src/semver/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/semver?logo=npm) |
  79. | [@vltpkg/skills](./src/skills) | [BSD-2-Clause-Patent](./src/skills/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/skills?logo=npm) |
  80. | [@vltpkg/spec](./src/spec) | [BSD-2-Clause-Patent](./src/spec/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/spec?logo=npm) |
  81. | [@vltpkg/tar](./src/tar) | [BSD-2-Clause-Patent](./src/tar/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/tar?logo=npm) |
  82. | [@vltpkg/types](./src/types) | [BSD-2-Clause-Patent](./src/types/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/types?logo=npm) |
  83. | [@vltpkg/url-open](./src/url-open) | [BSD-2-Clause-Patent](./src/url-open/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/url-open?logo=npm) |
  84. | [@vltpkg/vlt-json](./src/vlt-json) | [BSD-2-Clause-Patent](./src/vlt-json/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/vlt-json?logo=npm) |
  85. | [@vltpkg/vlx](./src/vlx) | [BSD-2-Clause-Patent](./src/vlx/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/vlx?logo=npm) |
  86. | [@vltpkg/which](./src/which) | [ISC](./src/which/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/which?logo=npm) |
  87. | [@vltpkg/workspaces](./src/workspaces) | [BSD-2-Clause-Patent](./src/workspaces/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/workspaces?logo=npm) |
  88. | [@vltpkg/xdg](./src/xdg) | [BSD-2-Clause-Patent](./src/xdg/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/xdg?logo=npm) |
  89. #### Infrastructure (`infra/*`)
  90. | Workspace Name | License | Downloads |
  91. | ---------------------------------------- | ------------------------------------------------- | ----------------------------------------------------------------------- |
  92. | [@vltpkg/benchmark](./infra/benchmark) | [BSD-2-Clause-Patent](./infra/benchmark/LICENSE) | N/A |
  93. | [@vltpkg/infra-build](./infra/build) | [BSD-2-Clause-Patent](./infra/build/LICENSE) | N/A |
  94. | [vlt](./infra/cli) | [BSD-2-Clause-Patent](./infra/cli/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/vlt?logo=npm) |
  95. | [@vltpkg/cli-js](./infra/cli-js) | [BSD-2-Clause-Patent](./infra/cli-js/LICENSE) | ![NPM Downloads](https://img.shields.io/npm/dm/@vltpkg/cli-js?logo=npm) |
  96. | [@vltpkg/smoke-test](./infra/smoke-test) | [BSD-2-Clause-Patent](./infra/smoke-test/LICENSE) | N/A |
  97. #### Documentation (`www/*`)
  98. | Workspace Name | License |
  99. | -------------------------- | ----------------------------------------- |
  100. | [@vltpkg/docs](./www/docs) | [BSD-2-Clause-Patent](./www/docs/LICENSE) |
src/cli-sdk/src/self-update.ts · Bundled script · 587 lines · 1 finding marked
  1. import { findCmdShimIfExists } from '@vltpkg/cmd-shim'
  2. import { isDepID } from '@vltpkg/dep-id'
  3. import type { DepID } from '@vltpkg/dep-id'
  4. import { error } from '@vltpkg/error-cause'
  5. import type { Graph } from '@vltpkg/graph'
  6. import { lt, valid } from '@vltpkg/semver'
  7. import { Spec } from '@vltpkg/spec'
  8. import { which } from '@vltpkg/which'
  9. import { spawn } from 'node:child_process'
  10. import { createHash } from 'node:crypto'
  11. import { constants, existsSync } from 'node:fs'
  12. import {
  13. access,
  14. chmod,
  15. lstat,
  16. readFile,
  17. realpath,
  18. rename,
  19. rm,
  20. stat,
  21. } from 'node:fs/promises'
  22. import {
  23. basename,
  24. dirname,
  25. extname,
  26. join,
  27. relative,
  28. resolve,
  29. sep,
  30. } from 'node:path'
  31. import { loadPackageJson } from 'package-json-from-dist'
  32. import type { LoadedConfig } from './config/index.ts'
  33. import { findSelf } from './global-bin-dir.ts'
  34. import type { Self } from './global-bin-dir.ts'
  35. import { globalSpec } from './global.ts'
  36. import type { GlobalBins } from './global.ts'
  37. import {
  38. globalWorkspacePath,
  39. isInside,
  40. samePath,
  41. } from './global-project.ts'
  42. import { readGlobalState, updateGlobalState } from './global-state.ts'
  43. import { writeInstallReceipt } from './install-receipt.ts'
  44. import type { InstallReceipt } from './install-receipt.ts'
  45. import { parseSpecArg } from './require-registry.ts'
  46. import { prettyPath } from './shell.ts'
  47. const { version: running } = loadPackageJson(
  48. import.meta.filename,
  49. process.env.__VLT_INTERNAL_CLI_PACKAGE_JSON,
  50. ) as { version: string }
  51. export type SelfUpdateCase =
  52. 'standalone' | 'npm' | 'global-project' | 'bootstrap'
  53. /** `vlt install -g vlt` that ran, or had nothing to do */
  54. export type SelfUpdated = {
  55. case: SelfUpdateCase
  56. status: 'updated' | 'up-to-date' | 'newer-running' | 'failed'
  57. /** running version */
  58. from: string
  59. /** resolved version */
  60. to: string
  61. /** dir of the updated vlt */
  62. dir: string
  63. /** newer-running: dist-tag of the implicit spec */
  64. tag?: string
  65. /** bootstrap: the dir that is not writable */
  66. notWritable?: string
  67. /** bootstrap: PATH dir whose vlt still runs first */
  68. shadowedBy?: string
  69. }
  70. /** vlt can't update this vlt, `message` says how to */
  71. export type SelfRefused = {
  72. status: 'refused'
  73. /** running version */
  74. from: string
  75. /** dir of the running vlt */
  76. dir: string
  77. message: string
  78. }
  79. /** result of `vlt install -g vlt` */
  80. export type SelfUpdate = SelfUpdated | SelfRefused
  81. export type SelfUpdatePlan =
  82. | { done: SelfUpdate }
  83. /** caller runs the -g install, then `finish` */
  84. | {
  85. selfUnwritable: boolean
  86. finish: (global?: GlobalBins) => Promise<SelfUpdated>
  87. }
  88. /** `checksum` of the file written to `dest`, `sha512-<base64>` */
  89. export type Release = { checksum: string }
  90. /** download one standalone entry point to `dest` */
  91. export type FetchRelease = (o: {
  92. version: string
  93. target: string
  94. channel?: string
  95. bin: string
  96. dest: string
  97. }) => Promise<Release>
  98. type Owner =
  99. | { kind: 'standalone-receipt'; receipt: InstallReceipt }
  100. | {
  101. kind: 'npm'
  102. npmCli: string
  103. prefix: string
  104. receipt?: InstallReceipt
  105. }
  106. | {
  107. kind:
  108. | 'installer'
  109. | 'homebrew'
  110. | 'standalone'
  111. | 'global-project'
  112. | 'unknown'
  113. }
  114. const installerLine =
  115. 'Re-run the installer to update vlt:\n curl -fsSL https://install.vlt.sh | bash'
    • Note: Pipes a downloaded script directly into a shell (curl | sh / wget | bash)
    • Line 125 on GitHub
  116. const writable = (dir: string) =>
  117. access(dir, constants.W_OK).then(
  118. () => true,
  119. () => false,
  120. )
  121. // exit 1 without usage: the usage was fine
  122. const refuse = (self: Self, message: string): SelfRefused => {
  123. process.exitCode = 1
  124. return { status: 'refused', from: running, dir: self.dir, message }
  125. }
  126. const isFile = (f: string) =>
  127. stat(f).then(
  128. s => s.isFile(),
  129. () => false,
  130. )
  131. const cellars = () =>
  132. [
  133. process.env.HOMEBREW_CELLAR,
  134. '/opt/homebrew/Cellar',
  135. '/usr/local/Cellar',
  136. '/home/linuxbrew/.linuxbrew/Cellar',
  137. ].filter((c): c is string => !!c)
  138. const whichNpm = async () => {
  139. try {
  140. const found = await which('npm', { nothrow: true })
  141. if (!found) return
  142. const real = await realpath(
  143. (await findCmdShimIfExists(found))?.[1] ?? found,
  144. )
  145. return basename(real) === 'npm-cli.js' ? real : undefined
  146. } catch {}
  147. }
  148. // npm's -g layout: <prefix>/lib/node_modules, win32 <prefix>/node_modules
  149. const findNpm = async (self: Self) => {
  150. const i = self.target.lastIndexOf(
  151. `${sep}node_modules${sep}vlt${sep}`,
  152. )
  153. if (i === -1) return
  154. const nm = self.target.slice(0, i) + sep + 'node_modules'
  155. const win32 = process.platform === 'win32'
  156. const up = dirname(nm)
  157. const prefix =
  158. win32 ?
  159. // vlt.cmd is in the prefix
  160. samePath(up, self.dir) && up
  161. : basename(up) === 'lib' && dirname(up)
  162. if (!prefix) return
  163. const node = dirname(process.execPath)
  164. const candidates = [
  165. join(nm, 'npm/bin/npm-cli.js'),
  166. win32 ?
  167. join(node, 'node_modules/npm/bin/npm-cli.js')
  168. : join(dirname(node), 'lib/node_modules/npm/bin/npm-cli.js'),
  169. ]
  170. for (const c of candidates) {
  171. if (await isFile(c)) return { npmCli: c, prefix }
  172. }
  173. const npmCli = await whichNpm()
  174. return npmCli && { npmCli, prefix }
  175. }
  176. const classify = async (self: Self, root: string): Promise<Owner> => {
  177. // before the receipt: it can outlive the vlt it was written for
  178. const g = await realpath(root).catch(() => resolve(root))
  179. if (isInside(g, self.target)) return { kind: 'global-project' }
  180. const { receipt } = self
  181. if (receipt?.data.method === 'standalone') {
  182. if (self.standalone)
  183. return { kind: 'standalone-receipt', receipt }
  184. } else if (receipt) {
  185. const npm = receipt.data.method === 'npm' && (await findNpm(self))
  186. return npm ?
  187. { kind: 'npm', ...npm, receipt }
  188. : { kind: 'installer' }
  189. }
  190. // before npm: a node formula has npm's layout
  191. if (cellars().some(c => isInside(c, self.target))) {
  192. return { kind: 'homebrew' }
  193. }
  194. if (self.standalone) return { kind: 'standalone' }
  195. const npm = await findNpm(self)
  196. return npm ? { kind: 'npm', ...npm } : { kind: 'unknown' }
  197. }
  198. const sha512 = async (file: string) =>
  199. `sha512-${createHash('sha512')
  200. .update(await readFile(file))
  201. .digest('base64')}`
  202. const safeBin = (b: string) => /^[a-z0-9][\w.-]*$/i.test(b)
  203. /** a refusal message, or undefined once replaced */
  204. const updateStandalone = async (
  205. self: Self,
  206. receipt: InstallReceipt,
  207. to: string,
  208. fetchRelease?: FetchRelease,
  209. ): Promise<string | undefined> => {
  210. const { target, channel, binaries = [] } = receipt.data
  211. if (!fetchRelease || !target) return installerLine
  212. const win32 = process.platform === 'win32'
  213. const at = prettyPath(self.dir)
  214. // stat().uid is 0 on win32
  215. if (
  216. !win32 &&
  217. (await stat(self.found)).uid === 0 &&
  218. process.getuid?.() !== 0
  219. ) {
  220. return `vlt at ${at} is owned by root. Update it the way you installed it, or run this as root.`
  221. }
  222. if (!(await writable(self.dir))) {
  223. return `vlt at ${at} is not writable. Update it the way you installed it.`
  224. }
  225. const ext =
  226. extname(self.found).toLowerCase() === '.exe' ? '.exe' : ''
  227. const entries: { bin: string; file: string }[] = []
  228. for (const bin of new Set(binaries)) {
  229. if (bin === 'vlt' || !safeBin(bin)) continue
  230. const file = join(self.dir, bin + ext)
  231. const st = await lstat(file).catch(() => undefined)
  232. // links follow vlt
  233. if (st && !st.isSymbolicLink()) entries.push({ bin, file })
  234. }
  235. // last, so a failure part-way leaves a working vlt
  236. entries.push({
  237. bin: 'vlt',
  238. file: join(self.dir, basename(self.found)),
  239. })
  240. const tmps: string[] = []
  241. const cleanup = () =>
  242. Promise.all(tmps.map(f => rm(f, { force: true })))
  243. const ready: { file: string; dest: string }[] = []
  244. try {
  245. for (const { bin, file } of entries) {
  246. // a running vlt.exe.old: the rename below says so
  247. if (win32)
  248. await rm(`${file}.old`, { force: true }).catch(() => {})
  249. // same dir: rename is atomic
  250. const dest = join(self.dir, `.${bin}-${to}.tmp`)
  251. tmps.push(dest)
  252. const { checksum } = await fetchRelease({
  253. version: to,
  254. target,
  255. channel,
  256. bin,
  257. dest,
  258. })
  259. const found = await sha512(dest)
  260. if (found !== checksum) {
  261. throw error(`Checksum mismatch for ${bin} ${to}`, {
  262. code: 'EINTEGRITY',
  263. wanted: checksum,
  264. found,
  265. })
  266. }
  267. await chmod(dest, (await stat(file)).mode & 0o777)
  268. ready.push({ file, dest })
  269. }
  270. } catch (er) {
  271. await cleanup()
  272. throw er
  273. }
  274. // never write into a running file: rename only
  275. for (const { file, dest } of ready) {
  276. try {
  277. if (win32) {
  278. // a running .exe can be renamed, not replaced
  279. await rename(file, `${file}.old`)
  280. await rename(dest, file).catch(async (er: unknown) => {
  281. await rename(`${file}.old`, file).catch(() => {})
  282. throw er
  283. })
  284. } else {
  285. await rename(dest, file)
  286. }
  287. } catch (cause) {
  288. await cleanup()
  289. throw error(
  290. `Could not replace ${file}. Close other vlt processes and editors, and check permissions.`,
  291. { path: file, cause },
  292. )
  293. }
  294. }
  295. }
  296. const runNpm = (npmCli: string, prefix: string, arg: string) =>
  297. new Promise<number>((res, rej) => {
  298. // stdout to stderr: keeps --view=json parseable
  299. const child = spawn(
  300. process.execPath,
  301. [npmCli, 'install', '--global', '--prefix', prefix, arg],
  302. { stdio: ['inherit', 2, 2] },
  303. )
  304. child.on('error', rej)
  305. child.on('close', (code: number | null) => res(code ?? 1))
  306. })
  307. const rewriteReceipt = (
  308. receipt: InstallReceipt | undefined,
  309. to: string,
  310. ) =>
  311. receipt &&
  312. writeInstallReceipt(receipt.file, { version: to }).catch(() => {})
  313. // vlt, vlt@..., a vlt tarball
  314. const vltArg = /^vlt(@|$)|(^|[/\\:])vlt-\d[^/\\]*\.tgz$/
  315. /** `vlt install -g vlt...`, for the human view */
  316. export const isSelfUpdateArgs = (
  317. conf: Pick<LoadedConfig, 'command' | 'globalRoot' | 'positionals'>,
  318. ): boolean =>
  319. conf.command === 'install' &&
  320. !!conf.globalRoot &&
  321. conf.positionals.some(p => vltArg.test(p))
  322. /**
  323. * `vlt install -g vlt`: update the vlt on the PATH the way it was
  324. * installed. Undefined when vlt is not a positional.
  325. */
  326. export const selfUpdate = async (
  327. conf: LoadedConfig,
  328. { fetchRelease }: { fetchRelease?: FetchRelease } = {},
  329. ): Promise<SelfUpdatePlan | undefined> => {
  330. const { globalRoot, positionals } = conf
  331. if (!globalRoot || !positionals.length) return
  332. const specs = await Promise.all(
  333. positionals.map(p => globalSpec(p, conf)),
  334. )
  335. const [spec] = specs
  336. if (!spec || !specs.some(s => s.name === 'vlt')) return
  337. if (specs.length > 1) {
  338. throw error('Update vlt on its own, without other packages', {
  339. code: 'EUSAGE',
  340. found: positionals.join(' '),
  341. })
  342. }
  343. const self = await findSelf()
  344. if (!self) {
  345. throw error(
  346. `Can't tell which vlt to update: run the vlt on your PATH, not ${process.argv[1] ?? process.execPath}`,
  347. { code: 'EUSAGE' },
  348. )
  349. }
  350. const owner = await classify(self, globalRoot)
  351. const at = prettyPath(self.dir)
  352. const foreign = `vlt at ${at} is not managed by vlt or npm. Update it the way you installed it.`
  353. if (owner.kind === 'installer') {
  354. return { done: refuse(self, installerLine) }
  355. }
  356. if (owner.kind === 'homebrew') {
  357. return {
  358. done: refuse(
  359. self,
  360. `vlt at ${at} was installed by Homebrew. Run \`brew upgrade vlt\`.`,
  361. ),
  362. }
  363. }
  364. if (owner.kind === 'standalone') {
  365. return { done: refuse(self, foreign) }
  366. }
  367. // npm's node_modules not writable: npm's EACCES says so
  368. const bootstrap =
  369. (owner.kind === 'npm' || owner.kind === 'unknown') &&
  370. !(await writable(self.dir))
  371. if (owner.kind === 'unknown' && !bootstrap) {
  372. return { done: refuse(self, foreign) }
  373. }
  374. const raw = String(positionals[0])
  375. const implicit = !parseSpecArg(raw, conf.options).bareSpec
  376. const receipt = 'receipt' in owner ? owner.receipt : undefined
  377. const channel = receipt?.data.channel
  378. const tag = channel ?? conf.options.tag
  379. const { name, version: to } =
  380. await conf.options.packageInfo.manifest(
  381. implicit && channel ?
  382. Spec.parse('vlt', tag, conf.options)
  383. : spec,
  384. )
  385. if (name !== 'vlt' || !to) {
  386. throw error(`${raw} is not a version of vlt`, {
  387. code: 'EUSAGE',
  388. found: name,
  389. })
  390. }
  391. const kind: SelfUpdateCase =
  392. bootstrap ? 'bootstrap'
  393. : owner.kind === 'standalone-receipt' ? 'standalone'
  394. : owner.kind === 'npm' ? 'npm'
  395. : 'global-project'
  396. const base = { case: kind, from: running, to, dir: self.dir }
  397. if (to === running)
  398. return { done: { ...base, status: 'up-to-date' } }
  399. // `latest` may lag a new major
  400. if (implicit && lt(to, running)) {
  401. return { done: { ...base, status: 'newer-running', tag } }
  402. }
  403. const registry = spec.final.type === 'registry'
  404. if (owner.kind === 'standalone-receipt') {
  405. // releases are registry versions
  406. if (!registry || !valid(to)) {
  407. throw error(`${raw} is not a vlt release`, {
  408. code: 'EUSAGE',
  409. found: raw,
  410. })
  411. }
  412. const refused = await updateStandalone(
  413. self,
  414. owner.receipt,
  415. to,
  416. fetchRelease,
  417. )
  418. if (refused) return { done: refuse(self, refused) }
  419. await rewriteReceipt(owner.receipt, to)
  420. return { done: { ...base, status: 'updated' } }
  421. }
  422. if (owner.kind === 'npm' && !bootstrap) {
  423. const code = await runNpm(
  424. owner.npmCli,
  425. owner.prefix,
  426. registry ? `vlt@${to}` : `vlt@${spec.bareSpec}`,
  427. )
  428. // npm already said why
  429. if (code !== 0) {
  430. process.exitCode = code
  431. return { done: { ...base, status: 'failed' } }
  432. }
  433. await rewriteReceipt(receipt, to)
  434. return { done: { ...base, status: 'updated' } }
  435. }
  436. // the ws pins what was resolved
  437. if (registry) conf.positionals = [`vlt@${to}`]
  438. return {
  439. selfUnwritable: bootstrap,
  440. finish: async global => {
  441. const u: SelfUpdated = {
  442. ...base,
  443. status: 'updated',
  444. dir: global?.binDir ?? self.dir,
  445. ...(bootstrap ? { notWritable: self.dir } : {}),
  446. }
  447. if (
  448. bootstrap &&
  449. global?.inPath &&
  450. global.bins.includes('vlt')
  451. ) {
  452. const now = await which('vlt', { nothrow: true }).catch(
  453. () => null,
  454. )
  455. const first = now && dirname(now)
  456. if (
  457. first &&
  458. !samePath(
  459. await realpath(first).catch(() => first),
  460. global.binDir,
  461. )
  462. ) {
  463. u.shadowedBy = first
  464. }
  465. }
  466. return u
  467. },
  468. }
  469. }
  470. /** `vlt uninstall -g vlt` of a vlt that is not a global package */
  471. export const refuseSelfUninstall = async (
  472. conf: LoadedConfig,
  473. ): Promise<SelfRefused | undefined> => {
  474. const { globalRoot, positionals } = conf
  475. if (!globalRoot || !positionals.includes('vlt')) return
  476. const ws = resolve(globalRoot, globalWorkspacePath('vlt'))
  477. if (existsSync(resolve(ws, 'package.json'))) return
  478. const self = await findSelf()
  479. if (!self) return
  480. const { kind } = await classify(self, globalRoot)
  481. return refuse(
  482. self,
  483. kind === 'npm' ?
  484. 'vlt was installed by npm. Run `npm uninstall -g vlt` to remove it.'
  485. : kind === 'homebrew' ?
  486. 'vlt was installed by Homebrew. Run `brew uninstall vlt` to remove it.'
  487. : `vlt at ${prettyPath(self.dir)} is not managed by vlt. Remove it the way you installed it.`,
  488. )
  489. }
  490. /** store id of the running vlt, when it runs from the global project */
  491. export const runningStoreDep = async (
  492. root: string,
  493. ): Promise<DepID | undefined> => {
  494. const script = process.argv[1]
  495. if (!script) return
  496. try {
  497. const [real, store] = await Promise.all([
  498. realpath(script),
  499. realpath(resolve(root, 'node_modules/.vlt')),
  500. ])
  501. const [id] = relative(store, real).split(sep)
  502. return isDepID(id) ? id : undefined
  503. } catch {}
  504. }
  505. /**
  506. * Record the running vlt's store dir for removal by a later -g
  507. * command when the graph no longer has it, and forget listed dirs
  508. * the graph has again.
  509. */
  510. export const deferSelfRemove = async (
  511. root: string,
  512. graph: Pick<Graph, 'nodes'>,
  513. keep?: DepID,
  514. ): Promise<void> => {
  515. const store = resolve(await realpath(root), 'node_modules/.vlt')
  516. const add =
  517. keep && !graph.nodes.has(keep) ? [resolve(store, keep)] : []
  518. const { pendingRemove = [] } = await readGlobalState(root)
  519. const kept = pendingRemove.filter(p => {
  520. const id = basename(p)
  521. return !(isDepID(id) && graph.nodes.has(id))
  522. })
  523. if (!add.length && kept.length === pendingRemove.length) return
  524. await updateGlobalState(root, s => ({
  525. ...s,
  526. pendingRemove: [...new Set([...kept, ...add])],
  527. }))
  528. }
  529. /** human lines for a self-update */
  530. export const selfUpdateLines = (s: SelfUpdate): string[] => {
  531. if (s.status === 'refused') return [s.message]
  532. if (s.status === 'up-to-date') {
  533. return [`✅ vlt ${s.to} is already up to date`]
  534. }
  535. if (s.status === 'newer-running') {
  536. return [
  537. `ℹ️ vlt ${s.from} is running but "${s.tag}" is ${s.to}; run \`vlt i -g vlt@${s.tag}\` to downgrade`,
  538. ]
  539. }
  540. if (s.status === 'failed') return []
  541. if (s.case === 'bootstrap') {
  542. return [
  543. `ℹ️ ${prettyPath(s.notWritable ?? s.dir)} is not writable, so vlt ${s.to} was installed as a global package.`,
  544. ...(s.shadowedBy ?
  545. [
  546. `⚠️ ${prettyPath(s.shadowedBy)} is earlier in your PATH, so \`vlt\` still runs vlt ${s.from}.`,
  547. ]
  548. : []),
  549. ]
  550. }
  551. return [
  552. `⬆️ Updated vlt ${s.from} → ${s.to} in ${prettyPath(s.dir)}${s.case === 'npm' ? ' (via npm)' : ''}`,
  553. ]
  554. }

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.