@statocysts/cli 0.2.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.
- package/README.md +80 -0
- package/dist/cli.mjs +102 -0
- package/package.json +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# @statocysts/cli
|
|
2
|
+
|
|
3
|
+
Command-line interface for [statocysts](https://github.com/octoplorer/statocysts) notification library.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @statocysts/cli
|
|
9
|
+
# or
|
|
10
|
+
pnpm add -g @statocysts/cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
statocysts -u <url> [-m <message> | -f <file>]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Options
|
|
20
|
+
|
|
21
|
+
| Option | Alias | Description |
|
|
22
|
+
| ----------- | ----- | ------------------------------------------------------------- |
|
|
23
|
+
| `--url` | `-u` | Notification service URL(s). Can be specified multiple times. |
|
|
24
|
+
| `--message` | `-m` | Message content to send directly. |
|
|
25
|
+
| `--file` | `-f` | Read message content from a file. |
|
|
26
|
+
| `--help` | | Show help information. |
|
|
27
|
+
| `--version` | | Show version number. |
|
|
28
|
+
|
|
29
|
+
### Message Priority
|
|
30
|
+
|
|
31
|
+
The message content is determined in the following order:
|
|
32
|
+
|
|
33
|
+
1. `--message` argument (highest priority)
|
|
34
|
+
2. `--file` argument
|
|
35
|
+
3. Standard input (stdin)
|
|
36
|
+
|
|
37
|
+
## Examples
|
|
38
|
+
|
|
39
|
+
### Send to a single URL
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
statocysts -u "slack://webhook/xxx/yyy/zzz" -m "Hello World"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Send to multiple URLs
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
statocysts -u "slack://webhook/xxx/yyy/zzz" -u "json://example.com/api/notify" -m "Hello World"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Read message from file
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
statocysts -u "slack://webhook/xxx/yyy/zzz" -f message.txt
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Read message from stdin
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
echo "Hello World" | statocysts -u "slack://webhook/xxx/yyy/zzz"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Use with pipeline
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
cat deployment.log | statocysts -u "slack://webhook/xxx/yyy/zzz"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Supported Services
|
|
70
|
+
|
|
71
|
+
This CLI supports all notification services provided by statocysts:
|
|
72
|
+
|
|
73
|
+
- **Slack**: `slack://webhook/xxx/yyy/zzz` or `slack://bot@channel:token`
|
|
74
|
+
- **JSON**: `json://example.com/api/endpoint`
|
|
75
|
+
|
|
76
|
+
For more details on URL formats, please refer to the [statocysts documentation](https://github.com/octoplorer/statocysts).
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import * as readline from "node:readline";
|
|
5
|
+
import { send } from "statocysts";
|
|
6
|
+
import yargs from "yargs";
|
|
7
|
+
import { hideBin } from "yargs/helpers";
|
|
8
|
+
|
|
9
|
+
//#region src/cli.ts
|
|
10
|
+
/**
|
|
11
|
+
* Read message content from stdin
|
|
12
|
+
*/
|
|
13
|
+
async function readFromStdin() {
|
|
14
|
+
if (process.stdin.isTTY) return "";
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const rl = readline.createInterface({
|
|
17
|
+
input: process.stdin,
|
|
18
|
+
terminal: false
|
|
19
|
+
});
|
|
20
|
+
const lines = [];
|
|
21
|
+
rl.on("line", (line) => {
|
|
22
|
+
lines.push(line);
|
|
23
|
+
});
|
|
24
|
+
rl.on("close", () => {
|
|
25
|
+
resolve(lines.join("\n"));
|
|
26
|
+
});
|
|
27
|
+
rl.on("error", reject);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get message content from various sources
|
|
32
|
+
* Priority: --message > --file > stdin
|
|
33
|
+
*/
|
|
34
|
+
async function getMessage(args) {
|
|
35
|
+
if (args.message) return args.message;
|
|
36
|
+
if (args.file) try {
|
|
37
|
+
return fs.readFileSync(args.file, "utf-8").trim();
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw new Error(`Failed to read file "${args.file}": ${error.message}`);
|
|
40
|
+
}
|
|
41
|
+
const stdinContent = await readFromStdin();
|
|
42
|
+
if (stdinContent) return stdinContent.trim();
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Send notifications to all specified URLs
|
|
47
|
+
*/
|
|
48
|
+
async function sendNotifications(urls, message) {
|
|
49
|
+
const results = await Promise.allSettled(urls.map((url) => send(url, message)));
|
|
50
|
+
const failures = [];
|
|
51
|
+
results.forEach((result, index) => {
|
|
52
|
+
if (result.status === "rejected") failures.push({
|
|
53
|
+
url: urls[index],
|
|
54
|
+
error: result.reason?.message || String(result.reason)
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
if (failures.length > 0) {
|
|
58
|
+
console.error("\nFailed to send to some URLs:");
|
|
59
|
+
failures.forEach(({ url, error }) => {
|
|
60
|
+
console.error(` - ${url}: ${error}`);
|
|
61
|
+
});
|
|
62
|
+
if (failures.length === urls.length) process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function main() {
|
|
66
|
+
const argv = await yargs(hideBin(process.argv)).scriptName("statocysts").usage("$0 -u <url> [-m <message> | -f <file>]").option("url", {
|
|
67
|
+
alias: "u",
|
|
68
|
+
type: "string",
|
|
69
|
+
array: true,
|
|
70
|
+
demandOption: true,
|
|
71
|
+
description: "Notification service URL(s)"
|
|
72
|
+
}).option("message", {
|
|
73
|
+
alias: "m",
|
|
74
|
+
type: "string",
|
|
75
|
+
description: "Message content to send"
|
|
76
|
+
}).option("file", {
|
|
77
|
+
alias: "f",
|
|
78
|
+
type: "string",
|
|
79
|
+
description: "Read message content from file"
|
|
80
|
+
}).example([
|
|
81
|
+
["$0 -u \"slack://webhook/xxx/yyy/zzz\" -m \"Hello World\"", "Send message to Slack webhook"],
|
|
82
|
+
["$0 -u \"slack://webhook/a/b/c\" -u \"json://example.com/api\" -m \"Hello\"", "Send to multiple URLs"],
|
|
83
|
+
["$0 -u \"slack://webhook/xxx/yyy/zzz\" -f message.txt", "Send message from file"],
|
|
84
|
+
["echo \"Hello\" | $0 -u \"slack://webhook/xxx/yyy/zzz\"", "Send message from stdin"]
|
|
85
|
+
]).help().version().strict().parse();
|
|
86
|
+
try {
|
|
87
|
+
const message = await getMessage(argv);
|
|
88
|
+
if (!message) {
|
|
89
|
+
console.error("Error: No message provided. Use -m, -f, or pipe content via stdin.");
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
await sendNotifications(argv.url, message);
|
|
93
|
+
console.log("Notification sent successfully!");
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error(`Error: ${error.message}`);
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
main();
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
export { };
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@statocysts/cli",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.2.2",
|
|
5
|
+
"description": "CLI for statocysts notification library",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/octoplorer/statocysts",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/octoplorer/statocysts.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/octoplorer/statocysts/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"notification",
|
|
17
|
+
"cli",
|
|
18
|
+
"statocysts"
|
|
19
|
+
],
|
|
20
|
+
"bin": {
|
|
21
|
+
"stato": "./dist/cli.mjs"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": "^20.19.0 || ^22.12.0 || >=23"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"yargs": "^18.0.0",
|
|
34
|
+
"statocysts": "0.2.2"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^22.0.0",
|
|
38
|
+
"@types/yargs": "^17.0.35",
|
|
39
|
+
"tsdown": "^0.18.0",
|
|
40
|
+
"typescript": "^5.9.3"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsdown",
|
|
44
|
+
"dev": "tsdown --watch",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
}
|
|
47
|
+
}
|