zames_pro 2.7.0 → 2.8.0

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.
@@ -1,67 +1,94 @@
1
- // Automatically install the Chromium build and system dependencies
2
- // that Playwright needs.
3
- //
4
- // Runs when the zames package is installed so the user does not have to run
5
- // "npx playwright install chromium" / "install-deps chromium" by hand.
6
- //
7
- // On Linux (including WSL) the browser binary also needs system libraries.
8
- // They are installed via "install-deps", which requires root (sudo).
9
- // If we are not root and sudo is not passwordless, we skip it and tell the
10
- // user the exact command to run.
11
- //
12
- // Install failure is NOT fatal: without network access (or in CI) the
13
- // package install must still succeed.
14
-
15
1
  import { spawnSync } from 'node:child_process'
16
2
 
3
+ const NL = String.fromCharCode(10)
17
4
  const isWin = process.platform === 'win32'
5
+ const isMac = process.platform === 'darwin'
6
+ const isLinux = process.platform === 'linux'
7
+
8
+ function run(cmd, args, opts) {
9
+ return spawnSync(cmd, args, Object.assign({ stdio: 'inherit', shell: isWin }, opts || {}))
10
+ }
18
11
 
19
- // npm puts node_modules/.bin on PATH for lifecycle scripts, so the local
20
- // playwright CLI is resolvable. On Windows npm runs scripts through cmd.exe,
21
- // so shell: true is needed for the .cmd shim to be found.
22
- function run(cmd, args) {
23
- return spawnSync(cmd, args, {
24
- stdio: 'inherit',
25
- shell: isWin,
26
- })
12
+ function has(cmd) {
13
+ if (isWin) {
14
+ const r = spawnSync('where', [cmd], { stdio: 'ignore' })
15
+ return !r.error && r.status === 0
16
+ }
17
+ const r = spawnSync('sh', ['-c', 'command -v "$1"', 'sh', cmd], { stdio: 'ignore' })
18
+ return !r.error && r.status === 0
19
+ }
20
+
21
+ function isRoot() {
22
+ return typeof process.getuid === 'function' && process.getuid() === 0
27
23
  }
28
24
 
29
25
  function canSudo() {
30
- // Already root? No sudo needed.
31
- if (typeof process.getuid === 'function' && process.getuid() === 0) return true
32
- // Passwordless sudo (common in WSL / CI images).
33
- const r = run('sudo', ['-n', 'true'])
34
- return !r.error && r.status === 0
26
+ if (isRoot()) return true
27
+ const r = spawnSync('sudo', ['-n', 'true'], { stdio: 'ignore' })
28
+ return !r.error && r.status === 0
29
+ }
30
+
31
+ const MANAGERS = [
32
+ { bin: 'apt-get', args: (p) => ['install', '-y', p], update: ['update'] },
33
+ { bin: 'dnf', args: (p) => ['install', '-y', p] },
34
+ { bin: 'yum', args: (p) => ['install', '-y', p] },
35
+ { bin: 'pacman', args: (p) => ['-S', '--noconfirm', p] },
36
+ { bin: 'zypper', args: (p) => ['install', '-y', p] },
37
+ { bin: 'apk', args: (p) => ['add', p] },
38
+ ]
39
+
40
+ function asRoot(cmd, args) {
41
+ if (isRoot()) return run(cmd, args)
42
+ return run('sudo', [cmd].concat(args))
43
+ }
44
+
45
+ function installPackages(pkgs) {
46
+ const mgr = MANAGERS.find((m) => has(m.bin))
47
+ if (!mgr) return false
48
+ if (mgr.update) asRoot(mgr.bin, mgr.update)
49
+ let any = false
50
+ for (const p of pkgs) {
51
+ const r = asRoot(mgr.bin, mgr.args(p))
52
+ if (!r.error && r.status === 0) any = true
53
+ }
54
+ return any
35
55
  }
36
56
 
37
- // 1) Browser binary for this Playwright version.
38
57
  const browser = run('playwright', ['install', 'chromium'])
39
58
  if (browser.error || browser.status !== 0) {
40
- console.warn(
41
- '\nzames: could not install Chromium for Playwright automatically.\n' +
42
- ' Run it manually: npx playwright install chromium\n',
43
- )
44
- process.exit(0)
59
+ console.warn(NL + 'zames: could not install Chromium for Playwright automatically.' + NL +
60
+ ' Run it manually: npx playwright install chromium' + NL)
61
+ process.exit(0)
62
+ }
63
+
64
+ if (isLinux) {
65
+ if (canSudo()) {
66
+ const deps = run('playwright', ['install-deps', 'chromium'])
67
+ if (deps.error || deps.status !== 0) {
68
+ console.warn(NL + 'zames: could not install Chromium system dependencies automatically.' + NL +
69
+ ' Run it manually: sudo npx playwright install-deps chromium' + NL)
70
+ }
71
+ } else {
72
+ console.warn(NL + 'zames: on Linux/WSL Chromium needs system libraries (sudo required).' + NL +
73
+ ' Run it once manually: sudo npx playwright install-deps chromium' + NL)
74
+ }
45
75
  }
46
76
 
47
- // 2) System libraries (Linux / WSL only). Without them Chromium fails
48
- // to launch with "error while loading shared libraries".
49
- if (!isWin) {
50
- if (canSudo()) {
51
- const deps = run('playwright', ['install-deps', 'chromium'])
52
- if (deps.error || deps.status !== 0) {
53
- console.warn(
54
- '\nzames: could not install Chromium system dependencies automatically.\n' +
55
- ' Run it manually: sudo npx playwright install-deps chromium\n',
56
- )
57
- }
58
- } else {
59
- console.warn(
60
- '\nzames: on Linux/WSL Chromium needs system libraries (sudo required).\n' +
61
- ' Run it once manually: sudo npx playwright install-deps chromium\n',
62
- )
63
- }
77
+ if (isLinux) {
78
+ const haveXclip = has('xclip') || has('xsel')
79
+ const haveWayland = has('wl-paste')
80
+ if (!haveXclip || !haveWayland) {
81
+ if (!canSudo()) {
82
+ console.warn(NL + 'zames: pasting images from the clipboard needs a clipboard tool.' + NL +
83
+ ' Install one (root required), e.g.:' + NL +
84
+ ' sudo apt install xclip # X11 (Debian/Ubuntu)' + NL +
85
+ ' sudo apt install wl-clipboard # Wayland' + NL +
86
+ ' Without it, paste a file path or use the attach button in the browser.' + NL)
87
+ } else if (!installPackages(['xclip', 'wl-clipboard'])) {
88
+ console.warn(NL + 'zames: could not install a clipboard tool automatically.' + NL +
89
+ ' Install xclip (X11) or wl-clipboard (Wayland) manually.' + NL)
90
+ }
91
+ }
64
92
  }
65
93
 
66
- // Never fail the package install because of a browser download.
67
94
  process.exit(0)