@vltpkg/cli-sdk 1.5.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/commands/build.d.ts +8 -3
  2. package/dist/commands/build.js +31 -3
  3. package/dist/commands/ci.d.ts +1 -1
  4. package/dist/commands/create.d.ts +2 -1
  5. package/dist/commands/create.js +2 -3
  6. package/dist/commands/exec.d.ts +2 -1
  7. package/dist/commands/exec.js +2 -3
  8. package/dist/commands/init.js +5 -2
  9. package/dist/commands/install/reporter.d.ts +1 -1
  10. package/dist/commands/install/reporter.js +45 -5
  11. package/dist/commands/install.d.ts +21 -3
  12. package/dist/commands/install.js +84 -30
  13. package/dist/commands/list.js +5 -1
  14. package/dist/commands/query.js +5 -1
  15. package/dist/commands/setup.d.ts +28 -2
  16. package/dist/commands/setup.js +185 -3
  17. package/dist/commands/skills.js +2 -3
  18. package/dist/commands/token.js +1 -1
  19. package/dist/commands/uninstall.d.ts +10 -3
  20. package/dist/commands/uninstall.js +28 -2
  21. package/dist/commands/update.d.ts +1 -1
  22. package/dist/config/definition.d.ts +32 -0
  23. package/dist/config/definition.js +60 -0
  24. package/dist/config/index.d.ts +26 -3
  25. package/dist/config/index.js +149 -7
  26. package/dist/global-bin-dir.d.ts +37 -0
  27. package/dist/global-bin-dir.js +138 -0
  28. package/dist/global-project.d.ts +18 -0
  29. package/dist/global-project.js +67 -0
  30. package/dist/global-state.d.ts +24 -0
  31. package/dist/global-state.js +94 -0
  32. package/dist/global.d.ts +78 -0
  33. package/dist/global.js +409 -0
  34. package/dist/index.js +7 -2
  35. package/dist/install-receipt.d.ts +40 -0
  36. package/dist/install-receipt.js +69 -0
  37. package/dist/output.js +13 -0
  38. package/dist/pack-tarball.d.ts +3 -1
  39. package/dist/pack-tarball.js +29 -19
  40. package/dist/parse-add-remove-args.js +2 -14
  41. package/dist/query-auth.d.ts +3 -2
  42. package/dist/query-auth.js +6 -4
  43. package/dist/reload-config.js +2 -1
  44. package/dist/require-registry.d.ts +7 -0
  45. package/dist/require-registry.js +13 -1
  46. package/dist/self-update.d.ts +74 -0
  47. package/dist/self-update.js +401 -0
  48. package/dist/shell-setup/env.d.ts +9 -0
  49. package/dist/shell-setup/env.js +25 -0
  50. package/dist/shell-setup/index.d.ts +69 -0
  51. package/dist/shell-setup/index.js +213 -0
  52. package/dist/shell-setup/posix.d.ts +36 -0
  53. package/dist/shell-setup/posix.js +158 -0
  54. package/dist/shell-setup/windows.d.ts +39 -0
  55. package/dist/shell-setup/windows.js +151 -0
  56. package/dist/shell.d.ts +18 -0
  57. package/dist/shell.js +110 -0
  58. package/dist/telemetry.js +8 -1
  59. package/package.json +30 -28
@@ -0,0 +1,151 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { promiseSpawn } from '@vltpkg/promise-spawn';
3
+ import { asError } from '@vltpkg/types';
4
+ import { mkdtemp, readFile, rm } from 'node:fs/promises';
5
+ import { tmpdir } from 'node:os';
6
+ import { join, win32 } from 'node:path';
7
+ export const userEnvKey = 'HKCU\\Environment';
8
+ /** how the user Path is named in messages */
9
+ export const userEnvLabel = 'your Windows user environment';
10
+ /** where users edit their Path by hand */
11
+ export const editPathHint = 'Start > "Edit environment variables for your account" > Path';
12
+ const refreshVar = 'VLT_PATH_REFRESH';
13
+ const system32 = (env) => win32.join([env.SystemRoot, env.windir].find(d => d && win32.isAbsolute(d)) ?? 'C:\\Windows', 'System32');
14
+ // absolute: never a reg.exe from the cwd
15
+ const run = (env, exe, args) => promiseSpawn(win32.join(system32(env), exe), args, {
16
+ stdio: ['ignore', 'pipe', 'pipe'],
17
+ windowsHide: true,
18
+ });
19
+ // reg's stderr, else the spawn error
20
+ const detail = (er) => {
21
+ const e = asError(er);
22
+ const c = e.cause;
23
+ return (typeof c?.stderr === 'string' && c.stderr ? c.stderr
24
+ : c?.cause ? asError(c.cause).message
25
+ : e.message);
26
+ };
27
+ const fail = (what, key, why, hint, cause) => error(`Could not ${what} your user Path (${key}): ${why}\n ${hint}`, {
28
+ code: 'ECONFIG',
29
+ cause,
30
+ });
31
+ const addIt = (dir) => `Add ${dir} yourself: ${editPathHint}`;
32
+ const norm = (p, env) => win32
33
+ .normalize(expandEnv(p.trim().replace(/^"(.*)"$/, '$1'), env))
34
+ .replace(/[\\/]+$/, '')
35
+ .toLowerCase();
36
+ // .reg strings escape \ and "
37
+ const unesc = (s) => s.replace(/\\([\s\S])/g, '$1');
38
+ /** `name` in the first key of a `reg export` file */
39
+ export const parseRegExport = (text, name) => {
40
+ // hex values wrap as `,\` + newline + indent
41
+ const lines = text.replace(/,\\\r?\n */g, ',').split(/\r?\n/);
42
+ let inKey = false;
43
+ for (const line of lines) {
44
+ if (line.startsWith('[')) {
45
+ // subkeys follow
46
+ if (inKey)
47
+ return undefined;
48
+ inKey = true;
49
+ continue;
50
+ }
51
+ // [\s\S]: data may hold \r, U+2028, U+2029
52
+ const [, n = '', v = ''] = /^"((?:[^"\\]|\\[\s\S])*)"=([\s\S]*)$/.exec(line) ?? [];
53
+ if (!inKey || unesc(n).toLowerCase() !== name.toLowerCase()) {
54
+ continue;
55
+ }
56
+ const [, sz] = /^"((?:[^"\\]|\\[\s\S])*)"$/.exec(v) ?? [];
57
+ if (sz !== undefined)
58
+ return { type: 'REG_SZ', data: unesc(sz) };
59
+ const [, hex] = /^hex\(2\):([\da-f,]*)$/i.exec(v) ?? [];
60
+ if (hex !== undefined) {
61
+ const bytes = hex
62
+ .split(',')
63
+ .filter(Boolean)
64
+ .map(h => parseInt(h, 16));
65
+ return {
66
+ type: 'REG_EXPAND_SZ',
67
+ data: Buffer.from(bytes)
68
+ .toString('utf16le')
69
+ .replace(/\0+$/, ''),
70
+ };
71
+ }
72
+ return { type: v.replace(/:[\s\S]*$/, ''), data: '' };
73
+ }
74
+ return undefined;
75
+ };
76
+ /** `%VAR%` expanded, names case-insensitive */
77
+ export const expandEnv = (s, env = process.env) => s.replace(/%([^%]+)%/g, (m, n) => Object.entries(env).find(([k]) => k.toLowerCase() === n.toLowerCase())?.[1] ?? m);
78
+ /** Path with `dir` first, unless already in it */
79
+ export const addPathEntry = (value, dir, env = process.env) => {
80
+ if (!value)
81
+ return { action: 'create', data: dir };
82
+ const previous = value.data;
83
+ const d = norm(dir, env);
84
+ if (previous.split(';').some(e => e.trim() && norm(e, env) === d)) {
85
+ return { action: 'unchanged', previous, data: previous };
86
+ }
87
+ return {
88
+ action: 'prepend',
89
+ previous,
90
+ data: previous ? `${dir};${previous}` : dir,
91
+ };
92
+ };
93
+ /** the user Path value, undefined when not set */
94
+ export const readUserPath = async ({ env = process.env, key = userEnvKey, } = {}) => {
95
+ const d = await mkdtemp(join(tmpdir(), 'vlt-'));
96
+ const file = join(d, 'env.reg');
97
+ try {
98
+ // utf-16 file: reg query prints in the console code page
99
+ await run(env, 'reg.exe', ['export', key, file, '/y']);
100
+ const v = parseRegExport((await readFile(file)).toString('utf16le'), 'Path');
101
+ // lone surrogate or NUL: would not survive the write
102
+ if (v && /[\p{Cs}\0]/u.test(v.data)) {
103
+ throw new Error('it has characters vlt could not read');
104
+ }
105
+ return v;
106
+ }
107
+ finally {
108
+ // AV may hold the new file
109
+ await rm(d, {
110
+ recursive: true,
111
+ force: true,
112
+ maxRetries: 3,
113
+ }).catch(() => { });
114
+ }
115
+ };
116
+ /** what adding `dir` to the user Path would change */
117
+ export const planWindowsPath = async (dir, { env = process.env, key = userEnvKey } = {}) => {
118
+ // would split or expand in Path
119
+ if (/[;%]/.test(dir)) {
120
+ throw fail('update', key, `the folder name has ";" or "%"`, 'Set --global-bin-dir to a folder without them');
121
+ }
122
+ const value = await readUserPath({ env, key }).catch((er) => {
123
+ throw fail('read', key, detail(er), addIt(dir), er);
124
+ });
125
+ if (value && !/^REG_(EXPAND_)?SZ$/.test(value.type)) {
126
+ throw fail('update', key, `unexpected value type ${value.type}`, addIt(dir));
127
+ }
128
+ return { key, dir, ...addPathEntry(value, dir, env) };
129
+ };
130
+ /** write the planned Path, then tell running programs */
131
+ export const applyWindowsPath = async (c, { env = process.env } = {}) => {
132
+ await run(env, 'reg.exe', [
133
+ 'add',
134
+ c.key,
135
+ '/v',
136
+ 'Path',
137
+ '/t',
138
+ 'REG_EXPAND_SZ',
139
+ '/d',
140
+ c.data,
141
+ '/f',
142
+ ]).catch((er) => {
143
+ throw fail('update', c.key, detail(er), addIt(c.dir), er);
144
+ });
145
+ if (c.key !== userEnvKey)
146
+ return;
147
+ // setx broadcasts WM_SETTINGCHANGE: new terminals see the new Path
148
+ await run(env, 'setx.exe', [refreshVar, '1'])
149
+ .then(() => run(env, 'reg.exe', ['delete', c.key, '/v', refreshVar, '/f']))
150
+ .catch(() => { });
151
+ };
@@ -0,0 +1,18 @@
1
+ export type Shell = 'bash' | 'zsh' | 'fish' | 'sh' | 'nu' | 'pwsh' | 'cmd' | 'unknown';
2
+ export type ShellOptions = {
3
+ env?: NodeJS.ProcessEnv;
4
+ platform?: NodeJS.Platform;
5
+ home?: string;
6
+ };
7
+ /** `~/x` for a path under the home dir */
8
+ export declare const prettyPath: (path: string, home?: string, win32?: boolean) => string;
9
+ /** the user's shell, from `SHELL`, or a guess on win32 */
10
+ export declare const detectShell: (env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform) => Shell;
11
+ /** `dir` double-quoted for a POSIX shell, `$HOME` late-bound */
12
+ export declare const posixPath: (dir: string, o?: ShellOptions) => string;
13
+ /** `dir` double-quoted for fish, `$HOME` late-bound */
14
+ export declare const fishPath: (dir: string, o?: ShellOptions) => string;
15
+ /** the line that adds `dir` to the `PATH` in `shell` */
16
+ export declare const shellPathCommand: (shell: Shell, dir: string, o?: ShellOptions) => string;
17
+ /** rc file to add the `PATH` line to, for display */
18
+ export declare const shellRcFile: (shell: Shell, o?: ShellOptions) => string | undefined;
package/dist/shell.js ADDED
@@ -0,0 +1,110 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ const shells = new Map([
4
+ ['bash', 'bash'],
5
+ ['zsh', 'zsh'],
6
+ ['fish', 'fish'],
7
+ ['nu', 'nu'],
8
+ ['pwsh', 'pwsh'],
9
+ ['powershell', 'pwsh'],
10
+ ['sh', 'sh'],
11
+ ['dash', 'sh'],
12
+ ['ksh', 'sh'],
13
+ ['mksh', 'sh'],
14
+ ['ash', 'sh'],
15
+ ]);
16
+ const isSep = (c, win32) => c === '/' || (win32 && c === '\\');
17
+ // rest of `path` after `home`: '' for home itself, undefined outside
18
+ const homeRest = (path, home, win32) => !home ? undefined
19
+ : path === home ? ''
20
+ : path.startsWith(home) && isSep(path[home.length], win32) ?
21
+ path.slice(home.length)
22
+ : undefined;
23
+ /** `~/x` for a path under the home dir */
24
+ export const prettyPath = (path, home = homedir(), win32 = process.platform === 'win32') => {
25
+ const rest = homeRest(path, home, win32);
26
+ return rest === undefined ? path : `~${rest}`;
27
+ };
28
+ /** the user's shell, from `SHELL`, or a guess on win32 */
29
+ export const detectShell = (env = process.env, platform = process.platform) => {
30
+ const { SHELL } = env;
31
+ if (SHELL) {
32
+ const name = SHELL.replace(/^.*[\\/]/, '')
33
+ .replace(/\.exe$/i, '')
34
+ .toLowerCase();
35
+ return shells.get(name) ?? 'unknown';
36
+ }
37
+ if (platform !== 'win32')
38
+ return 'unknown';
39
+ // set machine-wide, PowerShell adds the user module dir
40
+ const mods = (env.PSModulePath ?? '').split(';').filter(Boolean);
41
+ return mods.length >= 3 ? 'pwsh' : 'cmd';
42
+ };
43
+ const opts = ({ env = process.env, platform = process.platform, home = homedir(), }) => ({
44
+ env,
45
+ platform,
46
+ home,
47
+ win32: platform === 'win32',
48
+ });
49
+ const escape = (s, chars, esc = '\\') => s.replace(chars, c => esc + c);
50
+ // `$HOME/x`, or the path, `/c/x` style on win32
51
+ const posixInner = (dir, o, chars) => {
52
+ const { home, win32 } = opts(o);
53
+ const slash = (p) => (win32 ? p.replace(/\\/g, '/') : p);
54
+ const rest = homeRest(dir, home, win32);
55
+ if (rest !== undefined)
56
+ return '$HOME' + escape(slash(rest), chars);
57
+ const p = win32 ?
58
+ slash(dir).replace(/^([a-z]):/i, (_, d) => `/${d.toLowerCase()}`)
59
+ : dir;
60
+ return escape(p, chars);
61
+ };
62
+ const shChars = /[\\"$`]/g;
63
+ /** `dir` double-quoted for a POSIX shell, `$HOME` late-bound */
64
+ export const posixPath = (dir, o = {}) => `"${posixInner(dir, o, shChars)}"`;
65
+ /** `dir` double-quoted for fish, `$HOME` late-bound */
66
+ export const fishPath = (dir, o = {}) => `"${posixInner(dir, o, /[\\"$]/g)}"`;
67
+ /** the line that adds `dir` to the `PATH` in `shell` */
68
+ export const shellPathCommand = (shell, dir, o = {}) => {
69
+ const { home, win32 } = opts(o);
70
+ const rest = homeRest(dir, home, win32);
71
+ switch (shell) {
72
+ case 'fish':
73
+ return `fish_add_path ${fishPath(dir, o)}`;
74
+ case 'nu':
75
+ return `$env.PATH = ($env.PATH | prepend "${escape(dir, /[\\"]/g)}")`;
76
+ case 'pwsh': {
77
+ const p = rest === undefined ?
78
+ escape(dir, /[`"$]/g, '`')
79
+ : '$HOME' + escape(rest, /[`"$]/g, '`');
80
+ return `$env:PATH += "${win32 ? ';' : ':'}${p}"`;
81
+ }
82
+ case 'cmd':
83
+ return `set "PATH=%PATH%;${rest === undefined ? dir : '%USERPROFILE%' + rest}"`;
84
+ default:
85
+ return `export PATH="${posixInner(dir, o, shChars)}:$PATH"`;
86
+ }
87
+ };
88
+ /** rc file to add the `PATH` line to, for display */
89
+ export const shellRcFile = (shell, o = {}) => {
90
+ const { env, platform, home, win32 } = opts(o);
91
+ switch (shell) {
92
+ case 'zsh':
93
+ return prettyPath(`${env.ZDOTDIR || home}/.zshrc`, home, win32);
94
+ case 'bash': {
95
+ const has = (f) => existsSync(`${home}/${f}`);
96
+ // like `vlt setup --path`: never a new .bashrc
97
+ if (platform !== 'darwin' && has('.bashrc'))
98
+ return '~/.bashrc';
99
+ // a new .bash_profile would shadow .profile
100
+ const f = ['.bash_profile', '.bash_login'].find(has);
101
+ return `~/${f ?? '.profile'}`;
102
+ }
103
+ case 'sh':
104
+ return env.ENV ? prettyPath(env.ENV, home, win32) : '~/.profile';
105
+ case 'pwsh':
106
+ return '$PROFILE';
107
+ default:
108
+ return undefined;
109
+ }
110
+ };
package/dist/telemetry.js CHANGED
@@ -73,9 +73,16 @@ const getClient = () => {
73
73
  // call shutdown at the end so this is just a safety net.
74
74
  flushAt: 20,
75
75
  flushInterval: 10_000,
76
+ // one attempt, capped like flush(); failed sends resolve as 204
77
+ // so offline or erroring runs neither wait nor log
78
+ fetchRetryCount: 0,
79
+ requestTimeout: SHUTDOWN_TIMEOUT_MS,
80
+ fetch: async (url, init) => {
81
+ const res = await fetch(url, init).catch(() => undefined);
82
+ return res?.ok ? res : new Response(null, { status: 204 });
83
+ },
76
84
  });
77
85
  return _posthog;
78
- /* c8 ignore next 4 */
79
86
  }
80
87
  catch {
81
88
  _initFailed = true;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/cli-sdk",
3
3
  "description": "The source for the vlt CLI",
4
- "version": "1.5.0",
4
+ "version": "1.6.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -14,33 +14,35 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@resvg/resvg-wasm": "^2.6.2",
17
- "@vltpkg/config": "1.5.0",
18
- "@vltpkg/dep-id": "1.5.0",
19
- "@vltpkg/dot-prop": "1.5.0",
20
- "@vltpkg/error-cause": "1.5.0",
21
- "@vltpkg/git": "1.5.0",
22
- "@vltpkg/graph": "1.5.0",
23
- "@vltpkg/graph-run": "1.5.0",
24
- "@vltpkg/init": "1.5.0",
25
- "@vltpkg/output": "1.5.0",
26
- "@vltpkg/package-info": "1.5.0",
27
- "@vltpkg/package-json": "1.5.0",
28
- "@vltpkg/pick-manifest": "1.5.0",
29
- "@vltpkg/promise-spawn": "1.5.0",
30
- "@vltpkg/query": "1.5.0",
31
- "@vltpkg/registry-client": "1.5.0",
32
- "@vltpkg/rollback-remove": "1.5.0",
33
- "@vltpkg/run": "1.5.0",
34
- "@vltpkg/security-archive": "1.5.0",
35
- "@vltpkg/semver": "1.5.0",
36
- "@vltpkg/spec": "1.5.0",
37
- "@vltpkg/tar": "1.5.0",
38
- "@vltpkg/types": "1.5.0",
39
- "@vltpkg/url-open": "1.5.0",
40
- "@vltpkg/vlt-json": "1.5.0",
41
- "@vltpkg/vlx": "1.5.0",
42
- "@vltpkg/workspaces": "1.5.0",
43
- "@vltpkg/xdg": "1.5.0",
17
+ "@vltpkg/cmd-shim": "1.6.1",
18
+ "@vltpkg/config": "1.6.1",
19
+ "@vltpkg/dep-id": "1.6.1",
20
+ "@vltpkg/dot-prop": "1.6.1",
21
+ "@vltpkg/error-cause": "1.6.1",
22
+ "@vltpkg/git": "1.6.1",
23
+ "@vltpkg/graph": "1.6.1",
24
+ "@vltpkg/graph-run": "1.6.1",
25
+ "@vltpkg/init": "1.6.1",
26
+ "@vltpkg/output": "1.6.1",
27
+ "@vltpkg/package-info": "1.6.1",
28
+ "@vltpkg/package-json": "1.6.1",
29
+ "@vltpkg/pick-manifest": "1.6.1",
30
+ "@vltpkg/promise-spawn": "1.6.1",
31
+ "@vltpkg/query": "1.6.1",
32
+ "@vltpkg/registry-client": "1.6.1",
33
+ "@vltpkg/rollback-remove": "1.6.1",
34
+ "@vltpkg/run": "1.6.1",
35
+ "@vltpkg/security-archive": "1.6.1",
36
+ "@vltpkg/semver": "1.6.1",
37
+ "@vltpkg/spec": "1.6.1",
38
+ "@vltpkg/tar": "1.6.1",
39
+ "@vltpkg/types": "1.6.1",
40
+ "@vltpkg/url-open": "1.6.1",
41
+ "@vltpkg/vlt-json": "1.6.1",
42
+ "@vltpkg/vlx": "1.6.1",
43
+ "@vltpkg/which": "1.6.1",
44
+ "@vltpkg/workspaces": "1.6.1",
45
+ "@vltpkg/xdg": "1.6.1",
44
46
  "ansi-to-pre": "^1.0.6",
45
47
  "beautiful-mermaid": "^1.1.3",
46
48
  "chalk": "^5.6.2",