selfping 1.0.0 → 1.1.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 (4) hide show
  1. package/README.md +15 -2
  2. package/bin.mjs +4 -0
  3. package/cli.mjs +220 -0
  4. package/package.json +9 -3
package/README.md CHANGED
@@ -18,6 +18,19 @@ That's it. The key is read from the `SELFPING_API_KEY` environment variable, or
18
18
  await sendPing("New signup: jane@example.com", { apiKey: "sp_..." });
19
19
  ```
20
20
 
21
+ ## From the terminal
22
+
23
+ ```bash
24
+ npx selfping login # paste your key once
25
+ npx selfping "Deploy finished" # send a text
26
+ some-job | npx selfping # send what you pipe in
27
+ npx selfping run -- npm test # text me when this finishes
28
+ ```
29
+
30
+ `run` texts you "npm test finished in 4m12s" or "npm test FAILED in 38s (exit 1)" and exits with the command's own exit code, so it drops into scripts and CI unchanged. Add `--label deploy` to name it.
31
+
32
+ `selfping login` saves the key to `~/.config/selfping/credentials`, readable only by you, and `selfping logout` removes it. When `SELFPING_API_KEY` is set it takes priority, which is the better fit for CI and agents. There is deliberately no `--key` flag, so your key never lands in shell history.
33
+
21
34
  ## Getting a key
22
35
 
23
36
  Sign up at [selfping.com/login](https://www.selfping.com/login) with your phone number, prove you own it with a one-time passcode, and generate a key at [selfping.com/dashboard](https://www.selfping.com/dashboard). Free accounts get 10 texts a month, no credit card. That is the only step that needs a human; everything else can be wired up by your tools.
@@ -43,9 +56,9 @@ try {
43
56
  }
44
57
  ```
45
58
 
46
- ## Keep it to one text
59
+ ## One send, one credit
47
60
 
48
- Messages under 160 characters stay a single SMS segment and cost one credit. Lead with the outcome, include the numbers that matter, and say whether anything needs you.
61
+ Every send costs exactly one credit, long or short. Keep messages under 160 characters anyway: short texts read best on a lock screen. Lead with the outcome, include the numbers that matter, and say whether anything needs you.
49
62
 
50
63
  ## For AI agents
51
64
 
package/bin.mjs ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { main } from "./cli.mjs";
3
+
4
+ process.exitCode = await main(process.argv.slice(2));
package/cli.mjs ADDED
@@ -0,0 +1,220 @@
1
+ import { spawn } from "node:child_process";
2
+ import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { createInterface } from "node:readline";
6
+ import { SelfPingError, sendPing } from "./index.mjs";
7
+
8
+ export const HELP = `selfping: text yourself from the terminal.
9
+
10
+ selfping "Deploy finished" send a text
11
+ some-job | selfping send what you pipe in
12
+ selfping run -- npm test run a command, get a text when it ends
13
+ selfping login save your API key on this computer
14
+ selfping logout remove the saved key
15
+
16
+ Options for run:
17
+ --label <name> name the command in the text
18
+
19
+ The key comes from SELFPING_API_KEY when it is set, otherwise from
20
+ selfping login. Get a key at https://www.selfping.com/dashboard
21
+ `;
22
+
23
+ const MAX_LENGTH = 1600;
24
+ const KEY_PATTERN = /^[A-Za-z0-9_-]{16,64}$/;
25
+
26
+ export function credentialsPath(env = process.env) {
27
+ const base = env.XDG_CONFIG_HOME || join(homedir(), ".config");
28
+ return join(base, "selfping", "credentials");
29
+ }
30
+
31
+ export async function loadKey(env = process.env) {
32
+ const fromEnv = env.SELFPING_API_KEY?.trim();
33
+ if (fromEnv) return fromEnv;
34
+ try {
35
+ const saved = (await readFile(credentialsPath(env), "utf8")).trim();
36
+ return saved || null;
37
+ } catch {
38
+ return null;
39
+ }
40
+ }
41
+
42
+ export function formatDuration(ms) {
43
+ const total = Math.max(0, Math.round(ms / 1000));
44
+ const hours = Math.floor(total / 3600);
45
+ const minutes = Math.floor((total % 3600) / 60);
46
+ const seconds = total % 60;
47
+ if (hours) return `${hours}h${minutes}m`;
48
+ if (minutes) return `${minutes}m${seconds}s`;
49
+ return `${seconds}s`;
50
+ }
51
+
52
+ export function runMessage({ label, code, signal, ms }) {
53
+ const took = formatDuration(ms);
54
+ if (signal) return `${label} stopped by ${signal} after ${took}`;
55
+ if (code === 0) return `${label} finished in ${took}`;
56
+ return `${label} FAILED in ${took} (exit ${code})`;
57
+ }
58
+
59
+ export function clip(text) {
60
+ const trimmed = text.trim();
61
+ return trimmed.length > MAX_LENGTH ? trimmed.slice(-MAX_LENGTH) : trimmed;
62
+ }
63
+
64
+ async function readAll(stream) {
65
+ let data = "";
66
+ for await (const chunk of stream) data += chunk;
67
+ return data;
68
+ }
69
+
70
+ function promptHidden(question, input, output) {
71
+ return new Promise((resolve) => {
72
+ const rl = createInterface({ input, output, terminal: true });
73
+ let muted = false;
74
+ rl._writeToOutput = (text) => {
75
+ if (!muted) output.write(text);
76
+ };
77
+ rl.question(question, (answer) => {
78
+ rl.close();
79
+ output.write("\n");
80
+ resolve(answer);
81
+ });
82
+ muted = true;
83
+ });
84
+ }
85
+
86
+ export function describeError(err) {
87
+ const detail = err instanceof SelfPingError ? err.message : String(err);
88
+ try {
89
+ const parsed = JSON.parse(detail)?.error;
90
+ if (parsed?.message) return [parsed.message, parsed.hint].filter(Boolean).join(" ");
91
+ } catch {}
92
+ return detail;
93
+ }
94
+
95
+ async function send(message, io) {
96
+ const apiKey = await loadKey(io.env);
97
+ if (!apiKey) {
98
+ io.stderr.write(
99
+ "No API key. Run `selfping login` or set SELFPING_API_KEY. Get a key at https://www.selfping.com/dashboard\n"
100
+ );
101
+ return 1;
102
+ }
103
+ try {
104
+ await io.sendPing(clip(message), { apiKey });
105
+ return 0;
106
+ } catch (err) {
107
+ io.stderr.write(`selfping: text not sent. ${describeError(err)}\n`);
108
+ return 1;
109
+ }
110
+ }
111
+
112
+ const DASHBOARD_URL = "https://www.selfping.com/dashboard";
113
+
114
+ function openInBrowser(url, spawnProcess) {
115
+ const command =
116
+ process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
117
+ try {
118
+ const child = spawnProcess(command, [url], { stdio: "ignore", detached: true });
119
+ child.on("error", () => {});
120
+ child.unref?.();
121
+ } catch {}
122
+ }
123
+
124
+ async function login(io) {
125
+ if (io.stdin.isTTY) {
126
+ io.stdout.write(`Opening ${DASHBOARD_URL}. Copy your API key there, then paste it here.\n`);
127
+ openInBrowser(DASHBOARD_URL, io.spawn);
128
+ }
129
+ const raw = io.stdin.isTTY
130
+ ? await promptHidden("Paste your SelfPing API key: ", io.stdin, io.stdout)
131
+ : await readAll(io.stdin);
132
+ const key = raw.trim();
133
+ if (!KEY_PATTERN.test(key)) {
134
+ io.stderr.write("That does not look like a SelfPing API key. Copy it from https://www.selfping.com/dashboard\n");
135
+ return 1;
136
+ }
137
+ const path = credentialsPath(io.env);
138
+ await mkdir(join(path, ".."), { recursive: true, mode: 0o700 });
139
+ await writeFile(path, `${key}\n`, { mode: 0o600 });
140
+ io.stdout.write(`Saved to ${path}. Try it: selfping "Hello from my terminal"\n`);
141
+ if (io.env.SELFPING_API_KEY) {
142
+ io.stdout.write("Note: SELFPING_API_KEY is set in this shell and takes priority over the saved key.\n");
143
+ }
144
+ return 0;
145
+ }
146
+
147
+ async function logout(io) {
148
+ const path = credentialsPath(io.env);
149
+ await rm(path, { force: true });
150
+ io.stdout.write(`Removed ${path}.\n`);
151
+ return 0;
152
+ }
153
+
154
+ async function run(args, io) {
155
+ let label = null;
156
+ let rest = args;
157
+ if (rest[0] === "--label") {
158
+ label = rest[1] ?? null;
159
+ rest = rest.slice(2);
160
+ }
161
+ if (rest[0] === "--") rest = rest.slice(1);
162
+ if (rest.length === 0) {
163
+ io.stderr.write("Usage: selfping run [--label name] -- <command> [args]\n");
164
+ return 2;
165
+ }
166
+ const name = label || rest.join(" ").slice(0, 60);
167
+ const started = Date.now();
168
+ const outcome = await new Promise((resolve) => {
169
+ const child = io.spawn(rest[0], rest.slice(1), { stdio: "inherit" });
170
+ const forward = (signal) => child.kill(signal);
171
+ process.on("SIGINT", forward);
172
+ process.on("SIGTERM", forward);
173
+ child.on("error", (err) => {
174
+ process.off("SIGINT", forward);
175
+ process.off("SIGTERM", forward);
176
+ resolve({ code: 127, signal: null, error: err });
177
+ });
178
+ child.on("close", (code, signal) => {
179
+ process.off("SIGINT", forward);
180
+ process.off("SIGTERM", forward);
181
+ resolve({ code, signal, error: null });
182
+ });
183
+ });
184
+ if (outcome.error) {
185
+ io.stderr.write(`selfping: could not start ${rest[0]}: ${outcome.error.message}\n`);
186
+ }
187
+ await send(
188
+ runMessage({ label: name, code: outcome.code, signal: outcome.signal, ms: Date.now() - started }),
189
+ io
190
+ );
191
+ return outcome.code ?? 1;
192
+ }
193
+
194
+ export async function main(argv, overrides = {}) {
195
+ const io = {
196
+ env: process.env,
197
+ stdin: process.stdin,
198
+ stdout: process.stdout,
199
+ stderr: process.stderr,
200
+ sendPing,
201
+ spawn,
202
+ ...overrides,
203
+ };
204
+ const [first, ...rest] = argv;
205
+
206
+ if (first === "-h" || first === "--help" || first === "help") {
207
+ io.stdout.write(HELP);
208
+ return 0;
209
+ }
210
+ if (first === "login") return login(io);
211
+ if (first === "logout") return logout(io);
212
+ if (first === "run") return run(rest, io);
213
+
214
+ const message = argv.length > 0 ? argv.join(" ") : io.stdin.isTTY ? "" : await readAll(io.stdin);
215
+ if (!message.trim()) {
216
+ io.stdout.write(HELP);
217
+ return argv.length === 0 && io.stdin.isTTY ? 0 : 2;
218
+ }
219
+ return send(message, io);
220
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "selfping",
3
- "version": "1.0.0",
4
- "description": "Text yourself from your code. One function, one key, texts go only to your own verified phone number.",
3
+ "version": "1.1.0",
4
+ "description": "Text yourself from your code or your terminal. One function, one key, texts go only to your own verified phone number.",
5
5
  "keywords": [
6
6
  "sms",
7
7
  "text",
@@ -14,7 +14,8 @@
14
14
  "agent",
15
15
  "mcp",
16
16
  "webhook",
17
- "cron"
17
+ "cron",
18
+ "cli"
18
19
  ],
19
20
  "homepage": "https://www.selfping.com",
20
21
  "bugs": {
@@ -26,6 +27,9 @@
26
27
  "main": "./index.cjs",
27
28
  "module": "./index.mjs",
28
29
  "types": "./index.d.ts",
30
+ "bin": {
31
+ "selfping": "./bin.mjs"
32
+ },
29
33
  "exports": {
30
34
  ".": {
31
35
  "types": "./index.d.ts",
@@ -37,6 +41,8 @@
37
41
  "index.mjs",
38
42
  "index.cjs",
39
43
  "index.d.ts",
44
+ "cli.mjs",
45
+ "bin.mjs",
40
46
  "README.md",
41
47
  "LICENSE"
42
48
  ],