@putdotio/rokit 1.3.0 → 1.4.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.
Files changed (3) hide show
  1. package/README.md +11 -0
  2. package/dist/rokit.mjs +173 -52
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -42,6 +42,7 @@ pnpm exec rokit press --delay-ms 250 Right Select
42
42
  pnpm exec rokit query /query/active-app
43
43
  pnpm exec rokit wait-node videoPlayerScreen visible
44
44
  pnpm exec rokit screenshot artifacts/live/player.png
45
+ pnpm exec rokit --json active-app
45
46
  ```
46
47
 
47
48
  App-specific scenario scripts can also import the generic helpers:
@@ -91,6 +92,16 @@ rokit install <zip-path>
91
92
  rokit --version
92
93
  ```
93
94
 
95
+ Global options:
96
+
97
+ ```bash
98
+ rokit --json <command>
99
+ rokit --output json <command>
100
+ ```
101
+
102
+ JSON mode wraps command output as `{ "status": "ok", "command": "...", ... }`
103
+ and reports failures as `{ "status": "failed", "error": { "message": "..." } }`.
104
+
94
105
  - `check` confirms the Roku ECP endpoint responds and the developer installer
95
106
  is reachable.
96
107
  - `device-info` prints enhanced Roku device metadata as JSON.
package/dist/rokit.mjs CHANGED
@@ -23,9 +23,9 @@ const requirePassword = (env) => {
23
23
  if (!password) return fail("ROKIT_PASSWORD is not set");
24
24
  return password;
25
25
  };
26
+ var RokitCliError = class extends Error {};
26
27
  const fail = (message) => {
27
- console.error(message);
28
- process.exit(1);
28
+ throw new RokitCliError(message);
29
29
  };
30
30
  const formatErrorMessage = (error) => {
31
31
  if (error instanceof Error) return error.message;
@@ -42,100 +42,212 @@ const parseTimeout = (value) => {
42
42
  //#region src/cli.ts
43
43
  const packageJson = createRequire(import.meta.url)("../package.json");
44
44
  const main = async (argv = process.argv.slice(2)) => {
45
- const firstArg = argv[0];
46
- if (!firstArg || firstArg === "--help" || firstArg === "-h") {
47
- printHelp();
48
- return;
49
- }
50
- if (firstArg === "--version" || firstArg === "-v") {
51
- console.log(packageJson.version);
52
- return;
53
- }
54
- loadLocalEnv();
55
- const env = loadEnv();
56
- const target = requireTarget(env);
57
- const context = {
58
- password: env.password,
59
- target,
60
- timeoutMs: env.timeoutMs,
61
- username: env.username
62
- };
63
- const command = parseCommand(argv);
45
+ let outputMode = inferOutputMode(argv);
64
46
  try {
65
- await runCommand(context, command);
47
+ const options = parseGlobalOptions(argv);
48
+ outputMode = options.outputMode;
49
+ const firstArg = options.args[0];
50
+ if (!firstArg || firstArg === "--help" || firstArg === "-h") {
51
+ printHelp();
52
+ return;
53
+ }
54
+ if (firstArg === "--version" || firstArg === "-v") {
55
+ console.log(packageJson.version);
56
+ return;
57
+ }
58
+ loadLocalEnv();
59
+ const env = loadEnv();
60
+ const target = requireTarget(env);
61
+ const result = await runCommand({
62
+ password: env.password,
63
+ target,
64
+ timeoutMs: env.timeoutMs,
65
+ username: env.username
66
+ }, parseCommand(options.args));
67
+ printResult(options.outputMode, result);
66
68
  } catch (error) {
67
- fail(formatErrorMessage(error));
69
+ printError(outputMode, formatErrorMessage(error));
70
+ process.exitCode = 1;
68
71
  }
69
72
  };
70
73
  const runCommand = async (context, command) => {
71
74
  if (command.name === "check") {
72
75
  const summary = await checkDevice(context);
73
- console.log(`device: ${summary.name} (${summary.model})`);
74
- console.log(`ecp: ${summary.ecp}`);
75
- console.log(`developer installer HTTP status: ${summary.installerStatus}`);
76
- return;
77
- }
78
- if (command.name === "device-info") {
79
- console.log(JSON.stringify(await getDeviceInfo(context), null, 2));
80
- return;
76
+ return {
77
+ command: command.name,
78
+ data: summary,
79
+ message: [
80
+ `device: ${summary.name} (${summary.model})`,
81
+ `ecp: ${summary.ecp}`,
82
+ `developer installer HTTP status: ${summary.installerStatus}`
83
+ ].join("\n"),
84
+ status: "ok"
85
+ };
81
86
  }
87
+ if (command.name === "device-info") return {
88
+ command: command.name,
89
+ data: await getDeviceInfo(context),
90
+ status: "ok"
91
+ };
82
92
  if (command.name === "active-app") {
83
93
  const app = await queryActiveApp(context);
84
- console.log(`active app: ${app.id} ${app.name} ${app.version}`.trim());
85
- return;
94
+ return {
95
+ command: command.name,
96
+ data: app,
97
+ message: `active app: ${app.id} ${app.name} ${app.version}`.trim(),
98
+ status: "ok"
99
+ };
86
100
  }
87
101
  if (command.name === "wait-active") {
88
102
  const app = await waitForActiveApp(context, command.appId, command.timeoutMs);
89
- console.log(`active app: ${app.id} ${app.name} ${app.version}`.trim());
90
- return;
103
+ return {
104
+ command: command.name,
105
+ data: app,
106
+ message: `active app: ${app.id} ${app.name} ${app.version}`.trim(),
107
+ status: "ok"
108
+ };
91
109
  }
92
110
  if (command.name === "launch") {
93
111
  const app = await launchApp(context, command.args.appId, command.args.params);
94
- console.log(`launched: ${app.id} ${app.name} ${app.version}`.trim());
95
- return;
112
+ return {
113
+ command: command.name,
114
+ data: app,
115
+ message: `launched: ${app.id} ${app.name} ${app.version}`.trim(),
116
+ status: "ok"
117
+ };
96
118
  }
97
119
  if (command.name === "press") {
120
+ const pressed = [];
98
121
  for (const [index, key] of command.args.keys.entries()) {
99
122
  if (index > 0 && command.args.delayMs > 0) await sleep(command.args.delayMs);
100
123
  await pressKey(context, key);
101
- console.log(`pressed: ${key}`);
124
+ pressed.push(key);
102
125
  }
103
- return;
126
+ return {
127
+ command: command.name,
128
+ data: {
129
+ delayMs: command.args.delayMs,
130
+ keys: pressed
131
+ },
132
+ message: pressed.map((key) => `pressed: ${key}`).join("\n"),
133
+ status: "ok"
134
+ };
104
135
  }
105
136
  if (command.name === "query") {
106
- console.log(await queryEcp(context, command.path));
107
- return;
137
+ const body = await queryEcp(context, command.path);
138
+ return {
139
+ command: command.name,
140
+ data: {
141
+ body,
142
+ path: command.path
143
+ },
144
+ message: body,
145
+ status: "ok"
146
+ };
108
147
  }
109
148
  if (command.name === "sgnodes") {
110
- console.log(await querySceneGraph(context));
111
- return;
149
+ const body = await querySceneGraph(context);
150
+ return {
151
+ command: command.name,
152
+ data: { body },
153
+ message: body,
154
+ status: "ok"
155
+ };
112
156
  }
113
157
  if (command.name === "assert-node") {
114
158
  await assertSceneGraphNode(context, command.args.nodeName, command.args.expectation);
115
- console.log(`asserted node: ${formatNodeCondition(command.args)}`);
116
- return;
159
+ return {
160
+ command: command.name,
161
+ data: formatNodeData(command.args),
162
+ message: `asserted node: ${formatNodeCondition(command.args)}`,
163
+ status: "ok"
164
+ };
117
165
  }
118
166
  if (command.name === "wait-node") {
119
167
  await waitForSceneGraphNode(context, command.args.nodeName, command.args.expectation, command.args.timeoutMs);
120
- console.log(`matched node: ${formatNodeCondition(command.args)}`);
121
- return;
168
+ return {
169
+ command: command.name,
170
+ data: formatNodeData(command.args),
171
+ message: `matched node: ${formatNodeCondition(command.args)}`,
172
+ status: "ok"
173
+ };
122
174
  }
123
175
  if (command.name === "screenshot") {
124
176
  const password = requirePassword(context);
125
177
  mkdirSync(dirname(command.outputPath), { recursive: true });
126
- console.log(`screenshot: ${await takeScreenshot({
178
+ const path = await takeScreenshot({
127
179
  ...context,
128
180
  password
129
- }, command.outputPath)}`);
130
- return;
181
+ }, command.outputPath);
182
+ return {
183
+ command: command.name,
184
+ data: { path },
185
+ message: `screenshot: ${path}`,
186
+ status: "ok"
187
+ };
131
188
  }
132
189
  if (command.name === "install") {
133
190
  const password = requirePassword(context);
134
- console.log(await installPackage({
191
+ const message = await installPackage({
135
192
  ...context,
136
193
  password
137
- }, command.zipPath));
194
+ }, command.zipPath);
195
+ return {
196
+ command: command.name,
197
+ data: { message },
198
+ message,
199
+ status: "ok"
200
+ };
138
201
  }
202
+ throw new Error(`unsupported command: ${command.name}`);
203
+ };
204
+ const parseGlobalOptions = (argv) => {
205
+ const args = [];
206
+ let outputMode = "text";
207
+ for (let index = 0; index < argv.length; index += 1) {
208
+ const arg = argv[index];
209
+ if (arg === "--json") {
210
+ outputMode = "json";
211
+ continue;
212
+ }
213
+ if (arg === "--output") {
214
+ const value = argv[index + 1];
215
+ if (value !== "json" && value !== "text") fail("usage: rokit [--json|--output json|--output text] <command>");
216
+ outputMode = value === "json" ? "json" : "text";
217
+ index += 1;
218
+ continue;
219
+ }
220
+ args.push(arg);
221
+ }
222
+ return {
223
+ args,
224
+ outputMode
225
+ };
226
+ };
227
+ const inferOutputMode = (argv) => {
228
+ if (argv.includes("--json")) return "json";
229
+ return argv[argv.indexOf("--output") + 1] === "json" ? "json" : "text";
230
+ };
231
+ const printResult = (outputMode, result) => {
232
+ if (outputMode === "json") {
233
+ console.log(JSON.stringify(result, null, 2));
234
+ return;
235
+ }
236
+ if (result.message !== void 0) {
237
+ console.log(result.message);
238
+ return;
239
+ }
240
+ console.log(JSON.stringify(result.data, null, 2));
241
+ };
242
+ const printError = (outputMode, message) => {
243
+ if (outputMode === "json") {
244
+ console.error(JSON.stringify({
245
+ error: { message },
246
+ status: "failed"
247
+ }, null, 2));
248
+ return;
249
+ }
250
+ console.error(message);
139
251
  };
140
252
  const parseCommand = (argv) => {
141
253
  const [name, ...args] = argv;
@@ -265,6 +377,11 @@ const formatNodeCondition = ({ expectation, nodeName }) => {
265
377
  const suffix = expectation.text === void 0 ? "" : ` text=${expectation.text}`;
266
378
  return `${nodeName} ${expectation.state}${suffix}`;
267
379
  };
380
+ const formatNodeData = ({ expectation, nodeName, timeoutMs }) => ({
381
+ expectation,
382
+ nodeName,
383
+ timeoutMs
384
+ });
268
385
  const sleep = (ms) => new Promise((resolve) => {
269
386
  setTimeout(resolve, ms);
270
387
  });
@@ -305,6 +422,10 @@ usage:
305
422
  rokit install <zip-path>
306
423
  rokit --version
307
424
 
425
+ global options:
426
+ --json
427
+ --output json | text
428
+
308
429
  environment:
309
430
  ROKIT_TARGET=<roku-ip>
310
431
  ROKIT_PASSWORD=<developer-mode-password>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putdotio/rokit",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "A tiny CLI companion for Roku device harness work.",
5
5
  "keywords": [
6
6
  "cli",