@kud/cli-shot 0.3.0 → 0.3.2

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,34 +1,39 @@
1
- import { mkdirSync, readFileSync, writeFileSync, mkdtempSync, rmSync, copyFileSync, readdirSync, existsSync, accessSync, constants } from 'fs';
1
+ import { mkdirSync, readFileSync, writeFileSync, mkdtempSync, rmSync, copyFileSync, readdirSync, chmodSync, existsSync, accessSync, constants } from 'fs';
2
2
  import { tmpdir, cpus } from 'os';
3
3
  import { dirname, join, relative, sep, extname, basename } from 'path';
4
- import { createRequire } from 'module';
5
4
  import { spawn } from 'node-pty';
6
5
  import headless from '@xterm/headless';
7
6
  import { SerializeAddon } from '@xterm/addon-serialize';
7
+ import { createRequire } from 'module';
8
8
  import { execFileSync, spawnSync } from 'child_process';
9
9
  import { fileURLToPath } from 'url';
10
10
 
11
11
  // src/index.ts
12
- var { Terminal } = headless;
13
- var TICK_MS = 60;
14
- var HELPER_FAILURE = /execvp\(\d\) failed|posix_spawnp failed/;
15
- var explainSpawnFailure = (error, command) => {
16
- const message = error instanceof Error ? error.message : String(error);
17
- const helper = helperPath();
18
- const notExecutable = message.includes("posix_spawnp") && helper !== void 0 && !isExecutable(helper);
19
- return new Error(
20
- `Could not open a pty for ${command}: ${message}
21
- ` + (notExecutable ? ` node-pty's helper is not executable. Fix with:
22
- chmod +x ${helper}
23
- It ships without the mode bit when install scripts are blocked;
24
- npm approve-scripts node-pty makes it survive a reinstall.` : ` Check that the command exists and is executable.`),
25
- { cause: error }
26
- );
12
+ var nodePtyDir = () => {
13
+ try {
14
+ return dirname(
15
+ createRequire(import.meta.url).resolve("node-pty/package.json")
16
+ );
17
+ } catch {
18
+ return void 0;
19
+ }
27
20
  };
28
- var helperPath = () => {
29
- const candidate = createRequire(import.meta.url).resolve("node-pty").replace(
30
- /node-pty\/.*$/,
31
- `node-pty/prebuilds/${process.platform}-${process.arch}/spawn-helper`
21
+ var currentContext = () => ({
22
+ platform: process.platform,
23
+ arch: process.arch,
24
+ packageDir: nodePtyDir()
25
+ });
26
+ var helperPath = ({
27
+ platform,
28
+ arch,
29
+ packageDir
30
+ }) => {
31
+ if (!packageDir) return void 0;
32
+ const candidate = join(
33
+ packageDir,
34
+ "prebuilds",
35
+ `${platform}-${arch}`,
36
+ "spawn-helper"
32
37
  );
33
38
  return existsSync(candidate) ? candidate : void 0;
34
39
  };
@@ -40,6 +45,94 @@ var isExecutable = (path) => {
40
45
  return false;
41
46
  }
42
47
  };
48
+ var isHelperFailure = (message) => message.includes("posix_spawnp");
49
+ var repairHelper = (message, context) => {
50
+ if (context.platform !== "darwin" || !isHelperFailure(message)) {
51
+ return { kind: "not-applicable" };
52
+ }
53
+ const helper = helperPath(context);
54
+ if (helper === void 0 || isExecutable(helper)) {
55
+ return { kind: "not-applicable" };
56
+ }
57
+ try {
58
+ (context.chmod ?? chmodSync)(helper, 493);
59
+ } catch (error) {
60
+ return {
61
+ kind: "unfixable",
62
+ helper,
63
+ reason: error instanceof Error ? error.message : String(error)
64
+ };
65
+ }
66
+ return { kind: "repaired", helper };
67
+ };
68
+ var spawnWithRepair = (spawn2, context = currentContext()) => {
69
+ try {
70
+ return spawn2();
71
+ } catch (error) {
72
+ const message = error instanceof Error ? error.message : String(error);
73
+ const repair = repairHelper(message, context);
74
+ if (repair.kind !== "repaired") throw new HelperError(error, repair);
75
+ try {
76
+ return spawn2();
77
+ } catch (retryError) {
78
+ throw new HelperError(retryError, repair);
79
+ }
80
+ }
81
+ };
82
+ var HelperError = class extends Error {
83
+ constructor(original, repair) {
84
+ super(
85
+ original instanceof Error ? original.message : String(original),
86
+ { cause: original }
87
+ );
88
+ this.original = original;
89
+ this.repair = repair;
90
+ }
91
+ original;
92
+ repair;
93
+ };
94
+
95
+ // src/capture.ts
96
+ var { Terminal } = headless;
97
+ var TICK_MS = 60;
98
+ var HELPER_FAILURE = /execvp\(\d\) failed|posix_spawnp failed/;
99
+ var explainSpawnFailure = (error, command, repair = { kind: "not-applicable" }, context = currentContext()) => {
100
+ const message = error instanceof Error ? error.message : String(error);
101
+ const fix = (helper) => ` chmod +x ${helper}
102
+ `;
103
+ let advice;
104
+ if (repair.kind === "unfixable") {
105
+ advice = ` node-pty's helper is not executable and could not be fixed (${repair.reason}):
106
+ ${repair.helper}
107
+ Fix with:
108
+ ` + fix(repair.helper);
109
+ } else if (repair.kind === "repaired") {
110
+ advice = ` node-pty's helper was made executable but the spawn still failed:
111
+ ${repair.helper}
112
+ If it lost the bit again, fix with:
113
+ ` + fix(repair.helper);
114
+ } else {
115
+ const helper = helperPath(context);
116
+ advice = isHelperFailure(message) && helper !== void 0 && !isExecutable(helper) ? ` node-pty's helper is not executable. Fix with:
117
+ ` + fix(helper) + ` It ships without the mode bit when install scripts are blocked;
118
+ npm approve-scripts node-pty makes it survive a reinstall.` : ` Check that the command exists and is executable.`;
119
+ }
120
+ return new Error(
121
+ `Could not open a pty for ${command}: ${message}
122
+ ${advice.trimEnd()}`,
123
+ { cause: error }
124
+ );
125
+ };
126
+ var CI_MARKERS = ["CI", "CONTINUOUS_INTEGRATION"];
127
+ var childEnv = (base, extra = {}) => {
128
+ const merged = { ...base };
129
+ for (const key of CI_MARKERS) delete merged[key];
130
+ return Object.fromEntries(
131
+ Object.entries({ ...merged, ...extra }).filter(
132
+ (entry) => entry[1] !== void 0
133
+ )
134
+ );
135
+ };
43
136
  var DEFAULT_COLS = 110;
44
137
  var DEFAULT_ROWS = 32;
45
138
  var capture = (command, args = [], {
@@ -56,14 +149,18 @@ var capture = (command, args = [], {
56
149
  term.loadAddon(serializer);
57
150
  let pty;
58
151
  try {
59
- pty = spawn(command, [...args], {
60
- name: "xterm-256color",
61
- cols,
62
- rows,
63
- env: { ...process.env, ...env }
64
- });
152
+ pty = spawnWithRepair(
153
+ () => spawn(command, [...args], {
154
+ name: "xterm-256color",
155
+ cols,
156
+ rows,
157
+ env: childEnv(process.env, env)
158
+ })
159
+ );
65
160
  } catch (error) {
66
- reject(explainSpawnFailure(error, command));
161
+ reject(
162
+ error instanceof HelperError ? explainSpawnFailure(error.original, command, error.repair) : explainSpawnFailure(error, command)
163
+ );
67
164
  return;
68
165
  }
69
166
  let raw = 0;
@@ -124,7 +221,9 @@ var capture = (command, args = [], {
124
221
  clearTimeout(hard);
125
222
  reject(
126
223
  explainSpawnFailure(
127
- new Error(`the command exited ${exitCode} without drawing a screen`),
224
+ new Error(
225
+ `the command exited ${exitCode} without drawing a screen`
226
+ ),
128
227
  command
129
228
  )
130
229
  );
package/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { FORMATS, DEFAULT_FONT, listScreens, shootScreens, shootAll } from './chunk-ISLF4ISU.js';
2
+ import { FORMATS, DEFAULT_FONT, listScreens, shootScreens, shootAll } from './chunk-FLZX3UOL.js';
3
3
  import { Command } from 'commander';
4
4
 
5
5
  // src/flags.ts
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { DEFAULT_FONT, DEFAULT_FPS, DEFAULT_HOLD, FORMATS, MOVIE_FORMATS, VERCEL_CONFIG, buildPlayer, capture, castEvent, castHeader, drivenScreen, hasFfmpeg, launchBrowser, listScreens, movieFrames, parseCast, shootAll, shootFormats, shootScreen, shootScreens, toCast, toImage, toMovie } from './chunk-ISLF4ISU.js';
1
+ export { DEFAULT_FONT, DEFAULT_FPS, DEFAULT_HOLD, FORMATS, MOVIE_FORMATS, VERCEL_CONFIG, buildPlayer, capture, castEvent, castHeader, drivenScreen, hasFfmpeg, launchBrowser, listScreens, movieFrames, parseCast, shootAll, shootFormats, shootScreen, shootScreens, toCast, toImage, toMovie } from './chunk-FLZX3UOL.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kud/cli-shot",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "Automatic screenshots and recordings of interactive CLIs — pty capture, terminal emulation, PNG, asciicast, a web player, GIF or MP4 out",
6
6
  "license": "MIT",