@americanumberoneaokgoodtimes/p-plugin 0.0.0-stage → 1.0.1

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/scripts/p.mjs ADDED
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/env node
2
+ import fs from "node:fs";
3
+ import { fileURLToPath } from "node:url";
4
+ import {
5
+ deletePrompt,
6
+ getPrompt,
7
+ isDirectEntrypoint,
8
+ listPrompts,
9
+ resolveStorePath,
10
+ setPrompt,
11
+ } from "./store.mjs";
12
+
13
+ function printHelp() {
14
+ process.stdout.write(
15
+ [
16
+ "Usage:",
17
+ " node scripts/p.mjs list [--json]",
18
+ " node scripts/p.mjs get <id> [--json]",
19
+ " node scripts/p.mjs set <id> <text> (or pipe stdin / pass --stdin)",
20
+ " node scripts/p.mjs delete <id>",
21
+ " node scripts/p.mjs path",
22
+ "",
23
+ `Store: ${resolveStorePath()}`,
24
+ "",
25
+ ].join("\n"),
26
+ );
27
+ }
28
+
29
+ function readStdinSafe() {
30
+ try {
31
+ return fs.readFileSync(0, "utf8");
32
+ } catch {
33
+ return "";
34
+ }
35
+ }
36
+
37
+ function runCli(argv = process.argv) {
38
+ const [, , subcommand, ...args] = argv;
39
+ if (!subcommand || subcommand === "-h" || subcommand === "--help" || subcommand === "help") {
40
+ printHelp();
41
+ return;
42
+ }
43
+
44
+ if (subcommand === "path") {
45
+ process.stdout.write(resolveStorePath() + "\n");
46
+ return;
47
+ }
48
+
49
+ if (subcommand === "list") {
50
+ const asJson = args.includes("--json");
51
+ const storePath = resolveStorePath();
52
+ const entries = listPrompts(storePath);
53
+ if (asJson) {
54
+ process.stdout.write(JSON.stringify({ storePath, prompts: entries }, null, 2) + "\n");
55
+ return;
56
+ }
57
+ if (entries.length === 0) {
58
+ process.stdout.write("No saved prompts.\n");
59
+ return;
60
+ }
61
+ for (const entry of entries) {
62
+ process.stdout.write(`${entry.id}\t${entry.text.replace(/\s+/g, " ")}\n`);
63
+ }
64
+ return;
65
+ }
66
+
67
+ if (subcommand === "get") {
68
+ const asJson = args.includes("--json");
69
+ const id = args.find((a) => a !== "--json");
70
+ if (!id) {
71
+ process.stderr.write("Missing prompt ID.\n");
72
+ process.exitCode = 1;
73
+ return;
74
+ }
75
+ const entry = getPrompt(id);
76
+ if (!entry) {
77
+ process.stderr.write(`Prompt '${id}' not found.\n`);
78
+ process.exitCode = 1;
79
+ return;
80
+ }
81
+ if (asJson) {
82
+ process.stdout.write(JSON.stringify(entry, null, 2) + "\n");
83
+ return;
84
+ }
85
+ process.stdout.write(entry.text + "\n");
86
+ return;
87
+ }
88
+
89
+ if (subcommand === "set") {
90
+ const [id, ...rest] = args;
91
+ let text = "";
92
+ if (rest.length === 1 && (rest[0] === "--stdin" || rest[0] === "-")) {
93
+ text = readStdinSafe().replace(/\r?\n$/, "");
94
+ } else if (rest.length === 0 && !process.stdin.isTTY) {
95
+ text = readStdinSafe().replace(/\r?\n$/, "");
96
+ } else {
97
+ text = rest.join(" ");
98
+ }
99
+
100
+ if (!id || !text.trim()) {
101
+ process.stderr.write("Usage: node scripts/p.mjs set <id> <text>\n");
102
+ process.exitCode = 1;
103
+ return;
104
+ }
105
+ const saved = setPrompt(id, text);
106
+ process.stdout.write(
107
+ `${saved.created ? "Saved" : "Updated"} prompt '${saved.id}' (${saved.text.length} chars).\n`,
108
+ );
109
+ return;
110
+ }
111
+
112
+ if (subcommand === "delete") {
113
+ const [id] = args;
114
+ if (!id) {
115
+ process.stderr.write("Missing prompt ID.\n");
116
+ process.exitCode = 1;
117
+ return;
118
+ }
119
+ const removed = deletePrompt(id);
120
+ if (!removed) {
121
+ process.stderr.write(`Prompt '${id}' not found.\n`);
122
+ process.exitCode = 1;
123
+ return;
124
+ }
125
+ process.stdout.write(`Deleted prompt '${id}'.\n`);
126
+ return;
127
+ }
128
+
129
+ process.stderr.write(`Unknown subcommand '${subcommand}'.\n`);
130
+ printHelp();
131
+ process.exitCode = 1;
132
+ }
133
+
134
+ if (isDirectEntrypoint(import.meta.url, process.argv[1], fileURLToPath)) {
135
+ try {
136
+ runCli(process.argv);
137
+ } catch (err) {
138
+ process.stderr.write(`Error: ${err?.message || err}\n`);
139
+ process.exitCode = 1;
140
+ }
141
+ }