activitysmith-cli 0.1.5 → 0.1.6

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 +35 -3
  2. package/package.json +2 -2
  3. package/src/cli.js +141 -8
package/README.md CHANGED
@@ -40,9 +40,8 @@ activitysmith --help
40
40
 
41
41
  ```bash
42
42
  activitysmith push \
43
- --title "Build Failed" \
44
- --message "CI pipeline failed on main branch" \
45
- --channels "devs,ops"
43
+ --title "Build Failed 🚨" \
44
+ --message "CI pipeline failed on main branch"
46
45
  ```
47
46
 
48
47
  ### Start Live Activity
@@ -103,6 +102,39 @@ activitysmith activity end \
103
102
  --auto-dismiss-minutes 3
104
103
  ```
105
104
 
105
+ ### Channels
106
+
107
+ Channels are used to target specific team members or devices. Can be used for both push notifications and live activities.
108
+
109
+ ```bash
110
+ activitysmith push \
111
+ --title "Build Failed 🚨" \
112
+ --message "CI pipeline failed on main branch" \
113
+ --channels "devs,ops"
114
+ ```
115
+
116
+ ### Push Notification Redirection and Actions
117
+
118
+ Push notification redirection and actions are optional and can be used to redirect the user to a specific URL when they tap the notification or to trigger a specific action when they long-press the notification.
119
+ Webhooks are executed by ActivitySmith backend.
120
+
121
+ ```bash
122
+ activitysmith push \
123
+ --title "Build Failed 🚨" \
124
+ --message "CI pipeline failed on main branch" \
125
+ --redirection "https://github.com/org/repo/actions/runs/123456789" \
126
+ --actions '[{"title":"Open Failing Run","type":"open_url","url":"https://github.com/org/repo/actions/runs/123456789"},{"title":"Create Incident","type":"webhook","url":"https://hooks.example.com/incidents/create","method":"POST","body":{"service":"payments-api","severity":"high","source":"activitysmith-cli"}}]'
127
+ ```
128
+
129
+ You can also load actions from a file:
130
+
131
+ ```bash
132
+ activitysmith push \
133
+ --title "Build Failed 🚨" \
134
+ --message "CI pipeline failed on main branch" \
135
+ --actions-file "./actions.json"
136
+ ```
137
+
106
138
  ### JSON Output
107
139
 
108
140
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "activitysmith-cli",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Command-line interface for ActivitySmith. Send push notifications, start, update and end live activities directly from your terminal.",
5
5
  "keywords": [
6
6
  "activitysmith",
@@ -40,7 +40,7 @@
40
40
  "lint": "node --check src/cli.js"
41
41
  },
42
42
  "dependencies": {
43
- "activitysmith": "0.1.1",
43
+ "activitysmith": "^0.1.5",
44
44
  "commander": "^12.1.0"
45
45
  }
46
46
  }
package/src/cli.js CHANGED
@@ -46,6 +46,25 @@ const parseChannelsOption = (value) => {
46
46
  return channels;
47
47
  };
48
48
 
49
+ const normalizeHttpsUrl = (value, label) => {
50
+ if (typeof value !== "string" || value.trim().length === 0) {
51
+ throw new Error(`${label} must be a non-empty string`);
52
+ }
53
+
54
+ let parsed;
55
+ try {
56
+ parsed = new URL(value.trim());
57
+ } catch {
58
+ throw new Error(`${label} must be a valid URL`);
59
+ }
60
+
61
+ if (parsed.protocol !== "https:") {
62
+ throw new Error(`${label} must use https`);
63
+ }
64
+
65
+ return parsed.toString();
66
+ };
67
+
49
68
  const addContentStateOptions = (command, { includeAutoDismiss } = {}) => {
50
69
  command
51
70
  .option("--content-state <json>", "Content state as JSON string")
@@ -129,6 +148,109 @@ const readJsonFile = async (filePath, label) => {
129
148
  }
130
149
  };
131
150
 
151
+ const parseJsonArrayString = (value, label) => {
152
+ let parsed;
153
+ try {
154
+ parsed = JSON.parse(value);
155
+ } catch {
156
+ throw new Error(`${label} must be valid JSON`);
157
+ }
158
+
159
+ if (!Array.isArray(parsed)) {
160
+ throw new Error(`${label} must be a JSON array`);
161
+ }
162
+
163
+ return parsed;
164
+ };
165
+
166
+ const readJsonArrayFile = async (filePath, label) => {
167
+ const resolvedPath = resolve(process.cwd(), filePath);
168
+ let text = "";
169
+
170
+ try {
171
+ text = await readFile(resolvedPath, "utf8");
172
+ } catch {
173
+ throw new Error(`${label} could not be read at ${resolvedPath}`);
174
+ }
175
+
176
+ return parseJsonArrayString(text, label);
177
+ };
178
+
179
+ const parsePushAction = (value, index) => {
180
+ const label = `actions[${index}]`;
181
+ assertPlainObject(value, label);
182
+
183
+ if (typeof value.title !== "string" || value.title.trim().length === 0) {
184
+ throw new Error(`${label}.title is required`);
185
+ }
186
+
187
+ if (typeof value.type !== "string") {
188
+ throw new Error(`${label}.type is required`);
189
+ }
190
+
191
+ const normalizedType = value.type.trim();
192
+ if (normalizedType !== "open_url" && normalizedType !== "webhook") {
193
+ throw new Error(`${label}.type must be one of: open_url, webhook`);
194
+ }
195
+
196
+ const action = {
197
+ title: value.title.trim(),
198
+ type: normalizedType,
199
+ url: normalizeHttpsUrl(value.url, `${label}.url`),
200
+ };
201
+
202
+ if (value.method !== undefined) {
203
+ if (typeof value.method !== "string") {
204
+ throw new Error(`${label}.method must be a string`);
205
+ }
206
+ const normalizedMethod = value.method.trim().toUpperCase();
207
+ if (normalizedMethod !== "GET" && normalizedMethod !== "POST") {
208
+ throw new Error(`${label}.method must be one of: GET, POST`);
209
+ }
210
+ action.method = normalizedMethod;
211
+ }
212
+
213
+ if (value.body !== undefined) {
214
+ assertPlainObject(value.body, `${label}.body`);
215
+ action.body = value.body;
216
+ }
217
+
218
+ if (normalizedType === "open_url" && (value.method !== undefined || value.body !== undefined)) {
219
+ throw new Error(`${label} with type=open_url cannot include method or body`);
220
+ }
221
+
222
+ return action;
223
+ };
224
+
225
+ const loadPushActions = async (options) => {
226
+ if (options.actions && options.actionsFile) {
227
+ throw new Error("Provide either --actions or --actions-file, not both.");
228
+ }
229
+
230
+ let actions;
231
+ if (options.actionsFile) {
232
+ actions = await readJsonArrayFile(options.actionsFile, "actions-file");
233
+ }
234
+
235
+ if (options.actions) {
236
+ actions = parseJsonArrayString(options.actions, "actions");
237
+ }
238
+
239
+ if (actions === undefined) {
240
+ return undefined;
241
+ }
242
+
243
+ if (actions.length === 0) {
244
+ throw new Error("actions must contain at least one action");
245
+ }
246
+
247
+ if (actions.length > 4) {
248
+ throw new Error("actions can contain at most 4 actions");
249
+ }
250
+
251
+ return actions.map(parsePushAction);
252
+ };
253
+
132
254
  const buildContentStateFromOptions = (options) => {
133
255
  const contentState = {};
134
256
 
@@ -366,6 +488,9 @@ program
366
488
  .requiredOption("--title <title>", "Push title")
367
489
  .option("--message <message>", "Push message")
368
490
  .option("--subtitle <subtitle>", "Push subtitle")
491
+ .option("--redirection <url>", "HTTPS URL opened when notification is tapped")
492
+ .option("--actions <json>", "Actions JSON array (max 4)")
493
+ .option("--actions-file <path>", "Path to actions JSON array file")
369
494
  .option(
370
495
  "--channels <channels>",
371
496
  "Comma-separated channel slugs (optional)",
@@ -377,16 +502,24 @@ program
377
502
  try {
378
503
  const apiKey = requireApiKey(globalOptions);
379
504
  const client = createClient(apiKey);
505
+ const actions = await loadPushActions(options);
506
+
507
+ const pushNotificationRequest = withTargetChannels(
508
+ {
509
+ title: options.title,
510
+ message: options.message,
511
+ subtitle: options.subtitle,
512
+ redirection:
513
+ options.redirection !== undefined
514
+ ? normalizeHttpsUrl(options.redirection, "redirection")
515
+ : undefined,
516
+ actions,
517
+ },
518
+ options.channels
519
+ );
380
520
 
381
521
  const response = await client.notifications.sendPushNotification({
382
- pushNotificationRequest: withTargetChannels(
383
- {
384
- title: options.title,
385
- message: options.message,
386
- subtitle: options.subtitle,
387
- },
388
- options.channels
389
- ),
522
+ pushNotificationRequest,
390
523
  });
391
524
 
392
525
  outputResult(response, globalOptions, [