promptlike 0.7.0 → 0.8.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.
- package/CHANGELOG.md +50 -0
- package/README.md +101 -8
- package/dist/commands/export.js +14 -1
- package/dist/commands/list.js +5 -2
- package/dist/commands/logout.js +9 -4
- package/dist/commands/repo.js +171 -0
- package/dist/commands/show.js +4 -4
- package/dist/help.js +65 -0
- package/dist/index.js +9 -171
- package/dist/lib/api-client.js +109 -5
- package/dist/lib/config.js +53 -30
- package/dist/lib/confirm.js +53 -0
- package/dist/lib/constants.js +47 -2
- package/dist/lib/errors.js +17 -0
- package/dist/lib/format.js +16 -0
- package/dist/lib/fsSafe.js +171 -0
- package/dist/lib/libraryCache.js +6 -7
- package/dist/lib/localHistory.js +3 -2
- package/dist/lib/localStore.js +4 -4
- package/dist/lib/promptResolver.js +11 -1
- package/dist/lib/repoRef.js +118 -0
- package/dist/lib/settings.js +2 -2
- package/dist/program.js +197 -0
- package/package.json +1 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Safe file helpers for everything the CLI keeps on disk (session, settings,
|
|
4
|
+
* cache, local prompts, history, exports).
|
|
5
|
+
*
|
|
6
|
+
* Threats covered (each has a test in fsSafe.test.ts):
|
|
7
|
+
* - SYMLINK: a link planted at the target path (e.g. `config.json -> ~/.bashrc`)
|
|
8
|
+
* must never make the CLI write through it. Writes go to a fresh temp file
|
|
9
|
+
* that is `rename`d over the target; rename replaces the link itself and
|
|
10
|
+
* never follows it.
|
|
11
|
+
* - LOOSE PERMISSIONS: `mkdir`/`writeFile` modes only apply on creation. A
|
|
12
|
+
* directory or file that already exists with group/world access is tightened
|
|
13
|
+
* to 0700 / 0600 on the next write.
|
|
14
|
+
* - TORN WRITES: a crash mid-write must not leave a half token file (the temp
|
|
15
|
+
* file is only renamed once fully written).
|
|
16
|
+
*
|
|
17
|
+
* POSIX permission bits do not exist on Windows; chmod there is a no-op and the
|
|
18
|
+
* checks are skipped (the per-user profile ACLs apply instead).
|
|
19
|
+
*/
|
|
20
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
23
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
24
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
25
|
+
}
|
|
26
|
+
Object.defineProperty(o, k2, desc);
|
|
27
|
+
}) : (function(o, m, k, k2) {
|
|
28
|
+
if (k2 === undefined) k2 = k;
|
|
29
|
+
o[k2] = m[k];
|
|
30
|
+
}));
|
|
31
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
32
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
33
|
+
}) : function(o, v) {
|
|
34
|
+
o["default"] = v;
|
|
35
|
+
});
|
|
36
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
37
|
+
var ownKeys = function(o) {
|
|
38
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
39
|
+
var ar = [];
|
|
40
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
41
|
+
return ar;
|
|
42
|
+
};
|
|
43
|
+
return ownKeys(o);
|
|
44
|
+
};
|
|
45
|
+
return function (mod) {
|
|
46
|
+
if (mod && mod.__esModule) return mod;
|
|
47
|
+
var result = {};
|
|
48
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
49
|
+
__setModuleDefault(result, mod);
|
|
50
|
+
return result;
|
|
51
|
+
};
|
|
52
|
+
})();
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.isSymlink = isSymlink;
|
|
55
|
+
exports.pathExists = pathExists;
|
|
56
|
+
exports.ensurePrivateDir = ensurePrivateDir;
|
|
57
|
+
exports.writePrivateFile = writePrivateFile;
|
|
58
|
+
exports.createPrivateFileExclusive = createPrivateFileExclusive;
|
|
59
|
+
exports.readPrivateFile = readPrivateFile;
|
|
60
|
+
exports.assertNotSymlink = assertNotSymlink;
|
|
61
|
+
const fs = __importStar(require("fs"));
|
|
62
|
+
const path = __importStar(require("path"));
|
|
63
|
+
const IS_WINDOWS = process.platform === 'win32';
|
|
64
|
+
/** True when `p` exists and is itself a symbolic link (does not follow it). */
|
|
65
|
+
function isSymlink(p) {
|
|
66
|
+
try {
|
|
67
|
+
return fs.lstatSync(p).isSymbolicLink();
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** True when anything (file, dir, link — even a dangling link) exists at `p`. */
|
|
74
|
+
function pathExists(p) {
|
|
75
|
+
try {
|
|
76
|
+
fs.lstatSync(p);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create `dir` (and parents) with mode 0700, and tighten it when it already
|
|
85
|
+
* exists with group/world bits. Only the LEAF directory is tightened — parents
|
|
86
|
+
* such as `~/.config` belong to the user and other tools.
|
|
87
|
+
*/
|
|
88
|
+
function ensurePrivateDir(dir) {
|
|
89
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
90
|
+
if (IS_WINDOWS)
|
|
91
|
+
return;
|
|
92
|
+
try {
|
|
93
|
+
const st = fs.statSync(dir);
|
|
94
|
+
if (st.isDirectory() && (st.mode & 0o077) !== 0)
|
|
95
|
+
fs.chmodSync(dir, 0o700);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
/* a directory we cannot stat is reported by the write that follows */
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Write `data` to `file` atomically with mode 0600 (replaces an existing file
|
|
103
|
+
* or symlink; never writes through a link). The parent directory is created
|
|
104
|
+
* 0700 and tightened if needed.
|
|
105
|
+
*/
|
|
106
|
+
function writePrivateFile(file, data) {
|
|
107
|
+
ensurePrivateDir(path.dirname(file));
|
|
108
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
109
|
+
// 'wx' = create exclusively, so an attacker-planted file or link at the temp
|
|
110
|
+
// name makes the write fail instead of being followed.
|
|
111
|
+
fs.writeFileSync(tmp, data, { mode: 0o600, flag: 'wx' });
|
|
112
|
+
try {
|
|
113
|
+
fs.renameSync(tmp, file);
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
try {
|
|
117
|
+
fs.unlinkSync(tmp);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
/* best effort */
|
|
121
|
+
}
|
|
122
|
+
throw err;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create `file` with mode 0600 only if it does not exist; fails with EEXIST
|
|
127
|
+
* otherwise, and never follows a link (O_EXCL). Used for append-only history.
|
|
128
|
+
*/
|
|
129
|
+
function createPrivateFileExclusive(file, data) {
|
|
130
|
+
ensurePrivateDir(path.dirname(file));
|
|
131
|
+
fs.writeFileSync(file, data, { flag: 'wx', mode: 0o600 });
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Read a private file without following a symlink at the final path component.
|
|
135
|
+
* Returns null when the file is missing, is a link, or is not a regular file.
|
|
136
|
+
* A regular file that is group/world accessible is tightened to 0600.
|
|
137
|
+
*/
|
|
138
|
+
function readPrivateFile(file) {
|
|
139
|
+
let st;
|
|
140
|
+
try {
|
|
141
|
+
st = fs.lstatSync(file);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
if (st.isSymbolicLink() || !st.isFile())
|
|
147
|
+
return null;
|
|
148
|
+
if (!IS_WINDOWS && (st.mode & 0o077) !== 0) {
|
|
149
|
+
try {
|
|
150
|
+
fs.chmodSync(file, 0o600);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
/* read-only filesystem: still readable */
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
return fs.readFileSync(file, 'utf-8');
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Refuse to write an export over a symlink. `existsSync` follows links, so a
|
|
165
|
+
* dangling link looked like "nothing there" and the write created its target.
|
|
166
|
+
*/
|
|
167
|
+
function assertNotSymlink(target) {
|
|
168
|
+
if (isSymlink(target)) {
|
|
169
|
+
throw new Error(`${target} is a symbolic link — refusing to write through it. Remove the link or choose another path.`);
|
|
170
|
+
}
|
|
171
|
+
}
|
package/dist/lib/libraryCache.js
CHANGED
|
@@ -69,6 +69,7 @@ const path = __importStar(require("path"));
|
|
|
69
69
|
const format_1 = require("./format");
|
|
70
70
|
const rank_1 = require("./rank");
|
|
71
71
|
const settings_1 = require("./settings");
|
|
72
|
+
const fsSafe_1 = require("./fsSafe");
|
|
72
73
|
exports.CACHE_TTL_MS = 10 * 60 * 1000;
|
|
73
74
|
/** Background refreshes are at most one per minute (lock file mtime). */
|
|
74
75
|
const REFRESH_LOCK_MS = 60 * 1000;
|
|
@@ -83,11 +84,9 @@ function cacheFile() {
|
|
|
83
84
|
return path.join(cacheDir(), 'library.json');
|
|
84
85
|
}
|
|
85
86
|
function writePrivate(file, data) {
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
fs.writeFileSync(tmp, JSON.stringify(data), { mode: 0o600 });
|
|
90
|
-
fs.renameSync(tmp, file);
|
|
87
|
+
// Atomic (temp + rename): a hook reading mid-write never sees half a file,
|
|
88
|
+
// and a symlink planted at `file` is replaced, never written through.
|
|
89
|
+
(0, fsSafe_1.writePrivateFile)(file, JSON.stringify(data));
|
|
91
90
|
}
|
|
92
91
|
/** Cached library for `userId`, or null (missing, corrupt, or another account's). */
|
|
93
92
|
function readCache(userId) {
|
|
@@ -171,8 +170,8 @@ function startBackgroundRefresh(opts = {}) {
|
|
|
171
170
|
}
|
|
172
171
|
}
|
|
173
172
|
try {
|
|
174
|
-
|
|
175
|
-
|
|
173
|
+
(0, fsSafe_1.ensurePrivateDir)(cacheDir());
|
|
174
|
+
(0, fsSafe_1.writePrivateFile)(lock, String(now));
|
|
176
175
|
(opts.spawner ?? defaultSpawner)(process.execPath, [script, 'suggest', '--refresh-cache']);
|
|
177
176
|
return true;
|
|
178
177
|
}
|
package/dist/lib/localHistory.js
CHANGED
|
@@ -59,6 +59,7 @@ exports.findVersion = findVersion;
|
|
|
59
59
|
const fs = __importStar(require("fs"));
|
|
60
60
|
const path = __importStar(require("path"));
|
|
61
61
|
const store = __importStar(require("./localStore"));
|
|
62
|
+
const fsSafe_1 = require("./fsSafe");
|
|
62
63
|
function historyRoot() {
|
|
63
64
|
return path.join(store.localRoot(), 'history');
|
|
64
65
|
}
|
|
@@ -111,7 +112,7 @@ function latestVersion(slug) {
|
|
|
111
112
|
/** Append one version. Never overwrites: on a number clash it takes the next one. */
|
|
112
113
|
function append(slug, snap, note) {
|
|
113
114
|
const dir = historyDir(slug);
|
|
114
|
-
|
|
115
|
+
(0, fsSafe_1.ensurePrivateDir)(dir);
|
|
115
116
|
const saved = new Date().toISOString();
|
|
116
117
|
const base = store.serialize({ slug, ...snap, created: saved, updated: saved });
|
|
117
118
|
const extra = [`version: __N__`, `saved: ${saved}`, ...(note ? [`note: ${oneLine(note)}`] : [])];
|
|
@@ -119,7 +120,7 @@ function append(slug, snap, note) {
|
|
|
119
120
|
for (;;) {
|
|
120
121
|
const text = base.replace(/^---\n/, `---\n${extra.join('\n').replace('__N__', String(n))}\n`);
|
|
121
122
|
try {
|
|
122
|
-
|
|
123
|
+
(0, fsSafe_1.createPrivateFileExclusive)(versionFile(slug, n), text);
|
|
123
124
|
return parseVersion(n, text, slug);
|
|
124
125
|
}
|
|
125
126
|
catch (err) {
|
package/dist/lib/localStore.js
CHANGED
|
@@ -75,6 +75,7 @@ exports.fillVariables = fillVariables;
|
|
|
75
75
|
const fs = __importStar(require("fs"));
|
|
76
76
|
const os = __importStar(require("os"));
|
|
77
77
|
const path = __importStar(require("path"));
|
|
78
|
+
const fsSafe_1 = require("./fsSafe");
|
|
78
79
|
/** Root of the local store. Overridable for tests and for people who relocate it. */
|
|
79
80
|
function localRoot() {
|
|
80
81
|
const override = process.env.PROMPTLIKE_HOME;
|
|
@@ -168,9 +169,7 @@ function parse(slug, raw) {
|
|
|
168
169
|
};
|
|
169
170
|
}
|
|
170
171
|
function ensureDir() {
|
|
171
|
-
|
|
172
|
-
if (!fs.existsSync(dir))
|
|
173
|
-
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
172
|
+
(0, fsSafe_1.ensurePrivateDir)(promptsDir());
|
|
174
173
|
}
|
|
175
174
|
function filePath(slug) {
|
|
176
175
|
return path.join(promptsDir(), `${slug}.md`);
|
|
@@ -197,7 +196,8 @@ function save(p) {
|
|
|
197
196
|
created: p.created || previous?.created || now,
|
|
198
197
|
updated: now,
|
|
199
198
|
};
|
|
200
|
-
|
|
199
|
+
// Atomic + 0600; a symlink at the target is replaced, never written through.
|
|
200
|
+
(0, fsSafe_1.writePrivateFile)(filePath(record.slug), serialize(record));
|
|
201
201
|
return record;
|
|
202
202
|
}
|
|
203
203
|
function read(slug) {
|
|
@@ -28,7 +28,17 @@ async function resolveProjectId(value) {
|
|
|
28
28
|
if (matches.length === 1)
|
|
29
29
|
return matches[0].id;
|
|
30
30
|
if (matches.length === 0) {
|
|
31
|
-
|
|
31
|
+
// Same short id `ple repo list` prints (8+ hex characters).
|
|
32
|
+
if (SHORT_ID_RE.test(trimmed)) {
|
|
33
|
+
const byPrefix = projects.filter((p) => p.id.toLowerCase().startsWith(lower));
|
|
34
|
+
if (byPrefix.length === 1)
|
|
35
|
+
return byPrefix[0].id;
|
|
36
|
+
if (byPrefix.length > 1) {
|
|
37
|
+
const hint = byPrefix.map((p) => `${(0, format_1.shortId)(p.id)} ${p.name}`).join(', ');
|
|
38
|
+
throw new Error(`'${trimmed}' matched multiple repos (${hint}). Use the full repo id.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`No repo matched '${trimmed}'. Run 'ple repo list' and use the repo's exact name, slug, or id.`);
|
|
32
42
|
}
|
|
33
43
|
// Multiple repos share this name — ask for the id.
|
|
34
44
|
const hint = matches.map((p) => `${(0, format_1.shortId)(p.id)} ${p.name}`).join(', ');
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Repo name validation and `<repo>` reference resolution for `ple repo ...`.
|
|
4
|
+
* Pure functions: no network, no disk — everything here is unit-tested.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.REPO_DESCRIPTION_MAX = exports.REPO_NAME_MAX = void 0;
|
|
8
|
+
exports.slugFromName = slugFromName;
|
|
9
|
+
exports.validateRepoName = validateRepoName;
|
|
10
|
+
exports.validateRepoDescription = validateRepoDescription;
|
|
11
|
+
exports.pickVisibility = pickVisibility;
|
|
12
|
+
exports.parseVisibility = parseVisibility;
|
|
13
|
+
exports.resolveRepoRef = resolveRepoRef;
|
|
14
|
+
const format_1 = require("./format");
|
|
15
|
+
exports.REPO_NAME_MAX = 100;
|
|
16
|
+
exports.REPO_DESCRIPTION_MAX = 500;
|
|
17
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
18
|
+
const SHORT_ID_RE = /^[0-9a-f]{8,}$/i;
|
|
19
|
+
const CONTROL_RE = /[\u0000-\u001f\u007f-\u009f]/;
|
|
20
|
+
/** Same transform the server applies to build the URL slug (`/api/projects`). */
|
|
21
|
+
function slugFromName(name) {
|
|
22
|
+
return name
|
|
23
|
+
.trim()
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
.replace(/[^a-z0-9\-._]+/g, '-')
|
|
26
|
+
.replace(/^[-._]+|[-._]+$/g, '')
|
|
27
|
+
.slice(0, 100)
|
|
28
|
+
.replace(/-+$/, '');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Validate a repo name the same way the server will, so a typo fails here with a
|
|
32
|
+
* fix instead of after a round trip. The server stays the authority (it re-checks
|
|
33
|
+
* everything; this is not a security boundary, only a courtesy).
|
|
34
|
+
*
|
|
35
|
+
* Returns the trimmed name, or throws an Error that says what to change.
|
|
36
|
+
*/
|
|
37
|
+
function validateRepoName(raw) {
|
|
38
|
+
const name = raw.trim();
|
|
39
|
+
if (!name)
|
|
40
|
+
throw new Error('Repo name is empty. Example: ple repo create "my-prompts"');
|
|
41
|
+
if (name.length > exports.REPO_NAME_MAX) {
|
|
42
|
+
throw new Error(`Repo name is too long (${name.length} characters, max ${exports.REPO_NAME_MAX}).`);
|
|
43
|
+
}
|
|
44
|
+
if (CONTROL_RE.test(name)) {
|
|
45
|
+
throw new Error('Repo name contains control characters (line breaks, escape codes). Use plain text.');
|
|
46
|
+
}
|
|
47
|
+
if (!/[a-z0-9]/i.test(name)) {
|
|
48
|
+
throw new Error("Repo name needs at least one letter or digit (a-z, 0-9) — it becomes the repo's URL slug.");
|
|
49
|
+
}
|
|
50
|
+
return name;
|
|
51
|
+
}
|
|
52
|
+
function validateRepoDescription(raw) {
|
|
53
|
+
if (raw === undefined)
|
|
54
|
+
return undefined;
|
|
55
|
+
const d = raw.trim();
|
|
56
|
+
if (!d)
|
|
57
|
+
return undefined;
|
|
58
|
+
if (d.length > exports.REPO_DESCRIPTION_MAX) {
|
|
59
|
+
throw new Error(`Description is too long (${d.length} characters, max ${exports.REPO_DESCRIPTION_MAX}).`);
|
|
60
|
+
}
|
|
61
|
+
if (CONTROL_RE.test(d.replace(/\n/g, ' '))) {
|
|
62
|
+
throw new Error('Description contains control characters. Use plain text.');
|
|
63
|
+
}
|
|
64
|
+
return d;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* `--private` / `--public` → visibility. Private is the default: a repo should
|
|
68
|
+
* only become public because someone said so.
|
|
69
|
+
*/
|
|
70
|
+
function pickVisibility(opts) {
|
|
71
|
+
if (opts.private && opts.public) {
|
|
72
|
+
throw new Error('Choose one: --private or --public (not both).');
|
|
73
|
+
}
|
|
74
|
+
return opts.public ? 'public' : 'private';
|
|
75
|
+
}
|
|
76
|
+
function parseVisibility(value) {
|
|
77
|
+
const v = value.trim().toLowerCase();
|
|
78
|
+
if (v === 'private' || v === 'public')
|
|
79
|
+
return v;
|
|
80
|
+
throw new Error(`Visibility must be 'private' or 'public', got '${value}'.`);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Resolve a user-typed `<repo>` against the repos the account can see:
|
|
84
|
+
* full id, then exact name or slug (case-insensitive), then a short id prefix
|
|
85
|
+
* (8+ hex characters). Ambiguity is reported, never guessed.
|
|
86
|
+
*/
|
|
87
|
+
function resolveRepoRef(ref, repos) {
|
|
88
|
+
const value = ref.trim();
|
|
89
|
+
if (!value)
|
|
90
|
+
throw new Error('Give a repo name, slug or id. See: ple repo list');
|
|
91
|
+
if (UUID_RE.test(value)) {
|
|
92
|
+
const byId = repos.find((r) => r.id.toLowerCase() === value.toLowerCase());
|
|
93
|
+
if (byId)
|
|
94
|
+
return byId;
|
|
95
|
+
throw notFound(value);
|
|
96
|
+
}
|
|
97
|
+
const lower = value.toLowerCase();
|
|
98
|
+
const byName = repos.filter((r) => r.name.toLowerCase() === lower || (r.slug ?? '').toLowerCase() === lower);
|
|
99
|
+
if (byName.length === 1)
|
|
100
|
+
return byName[0];
|
|
101
|
+
if (byName.length > 1)
|
|
102
|
+
throw ambiguous(value, byName);
|
|
103
|
+
if (SHORT_ID_RE.test(value)) {
|
|
104
|
+
const byPrefix = repos.filter((r) => r.id.toLowerCase().startsWith(lower));
|
|
105
|
+
if (byPrefix.length === 1)
|
|
106
|
+
return byPrefix[0];
|
|
107
|
+
if (byPrefix.length > 1)
|
|
108
|
+
throw ambiguous(value, byPrefix);
|
|
109
|
+
}
|
|
110
|
+
throw notFound(value);
|
|
111
|
+
}
|
|
112
|
+
function notFound(ref) {
|
|
113
|
+
return new Error(`No repo matched '${ref}'. List yours with: ple repo list`);
|
|
114
|
+
}
|
|
115
|
+
function ambiguous(ref, matches) {
|
|
116
|
+
const hint = matches.map((r) => `${(0, format_1.shortId)(r.id)} ${r.name}`).join(', ');
|
|
117
|
+
return new Error(`'${ref}' matched several repos (${hint}). Use the repo id.`);
|
|
118
|
+
}
|
package/dist/lib/settings.js
CHANGED
|
@@ -52,6 +52,7 @@ exports.suggestEnabled = suggestEnabled;
|
|
|
52
52
|
const fs = __importStar(require("fs"));
|
|
53
53
|
const os = __importStar(require("os"));
|
|
54
54
|
const path = __importStar(require("path"));
|
|
55
|
+
const fsSafe_1 = require("./fsSafe");
|
|
55
56
|
exports.SETTING_KEYS = ['suggest'];
|
|
56
57
|
/** Config dir (same as the session file). `PROMPTLIKE_CONFIG_DIR` overrides it for tests. */
|
|
57
58
|
function configDir() {
|
|
@@ -76,8 +77,7 @@ function readSettings() {
|
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
function writeSettings(next) {
|
|
79
|
-
|
|
80
|
-
fs.writeFileSync(settingsFile(), JSON.stringify(next, null, 2) + '\n', { mode: 0o600 });
|
|
80
|
+
(0, fsSafe_1.writePrivateFile)(settingsFile(), JSON.stringify(next, null, 2) + '\n');
|
|
81
81
|
}
|
|
82
82
|
/** Parse a user-typed on/off value. */
|
|
83
83
|
function parseOnOff(value) {
|
package/dist/program.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Command table for `ple`: registers every command with `commander`. Each
|
|
4
|
+
* command's real logic lives under `src/commands/` — this file only does argument
|
|
5
|
+
* parsing, the grouped `--help`, and the global error wrapper (exit codes).
|
|
6
|
+
* Split from index.ts so tests can build the program without running it.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.runWithErrorHandler = runWithErrorHandler;
|
|
10
|
+
exports.buildProgram = buildProgram;
|
|
11
|
+
const commander_1 = require("commander");
|
|
12
|
+
const constants_1 = require("./lib/constants");
|
|
13
|
+
const format_1 = require("./lib/format");
|
|
14
|
+
const login_1 = require("./commands/login");
|
|
15
|
+
const logout_1 = require("./commands/logout");
|
|
16
|
+
const list_1 = require("./commands/list");
|
|
17
|
+
const copy_1 = require("./commands/copy");
|
|
18
|
+
const show_1 = require("./commands/show");
|
|
19
|
+
const search_1 = require("./commands/search");
|
|
20
|
+
const get_1 = require("./commands/get");
|
|
21
|
+
const improve_1 = require("./commands/improve");
|
|
22
|
+
const loop_1 = require("./commands/loop");
|
|
23
|
+
const save_1 = require("./commands/save");
|
|
24
|
+
const sync_1 = require("./commands/sync");
|
|
25
|
+
const export_1 = require("./commands/export");
|
|
26
|
+
const stats_1 = require("./commands/stats");
|
|
27
|
+
const import_1 = require("./commands/import");
|
|
28
|
+
const log_1 = require("./commands/log");
|
|
29
|
+
const diff_1 = require("./commands/diff");
|
|
30
|
+
const rollback_1 = require("./commands/rollback");
|
|
31
|
+
const suggest_1 = require("./commands/suggest");
|
|
32
|
+
const config_1 = require("./commands/config");
|
|
33
|
+
const repo_1 = require("./commands/repo");
|
|
34
|
+
const errors_1 = require("./lib/errors");
|
|
35
|
+
const help_1 = require("./help");
|
|
36
|
+
/**
|
|
37
|
+
* A single global try/catch — consistent error output and exit codes for every
|
|
38
|
+
* command: 1 = fix your input/login/plan, 2 = network/server problem, retry later.
|
|
39
|
+
* A command that already set `process.exitCode` keeps its own code.
|
|
40
|
+
*/
|
|
41
|
+
function runWithErrorHandler(fn) {
|
|
42
|
+
return async (...args) => {
|
|
43
|
+
try {
|
|
44
|
+
await fn(...args);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
48
|
+
console.error(format_1.c.error(`✗ ${message}`));
|
|
49
|
+
process.exit(process.exitCode || (0, errors_1.exitCodeFor)(err));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function buildProgram() {
|
|
54
|
+
const program = new commander_1.Command();
|
|
55
|
+
program
|
|
56
|
+
.name(constants_1.CLI_NAME)
|
|
57
|
+
.description('Your PromptLikeEngineer prompt library in the terminal — works offline, no account needed to start')
|
|
58
|
+
.version(constants_1.CLI_VERSION)
|
|
59
|
+
.configureHelp({ formatHelp: help_1.formatRootHelp })
|
|
60
|
+
.showSuggestionAfterError();
|
|
61
|
+
const scope = (cmd) => cmd.option('--project <repo>', 'Limit to one repo (name, slug or id) — use when titles collide');
|
|
62
|
+
// ---- prompts ------------------------------------------------------------
|
|
63
|
+
program
|
|
64
|
+
.command('save <title>')
|
|
65
|
+
.description('Save a prompt locally — no account needed')
|
|
66
|
+
.option('-m, --message <text>', 'Prompt body (or pipe it via stdin)')
|
|
67
|
+
.option('-d, --description <text>', 'Short description')
|
|
68
|
+
.option('-t, --tag <name>', 'Tag (repeatable)', save_1.collectTag, [])
|
|
69
|
+
.option('-f, --force', 'Overwrite an existing local prompt')
|
|
70
|
+
.addHelpText('after', '\nExamples:\n ple save "code review" -m "Review this diff: {{diff}}"\n pbpaste | ple save "bug triage" --tag triage')
|
|
71
|
+
.action(runWithErrorHandler(save_1.saveCommand));
|
|
72
|
+
scope(program
|
|
73
|
+
.command('get <name>')
|
|
74
|
+
.description('Print a prompt raw to stdout; fill {{variables}} (agent/pipe friendly)')
|
|
75
|
+
.option('-v, --var <keyval>', 'Fill a variable — key=value (repeatable)', get_1.collectVar, {})
|
|
76
|
+
.option('-c, --copy', 'Also copy the output to the clipboard'))
|
|
77
|
+
.addHelpText('after', '\nExample:\n ple get "code review" --var diff="$(git diff)" | claude')
|
|
78
|
+
.action(runWithErrorHandler(get_1.getCommand));
|
|
79
|
+
scope(program.command('show <name>').description('Show a prompt in the terminal')).action(runWithErrorHandler(show_1.showCommand));
|
|
80
|
+
scope(program.command('copy <name>').description('Copy a prompt to the clipboard (by title or short id)')).action(runWithErrorHandler(copy_1.copyCommand));
|
|
81
|
+
scope(program
|
|
82
|
+
.command('list')
|
|
83
|
+
.description('List your prompts')
|
|
84
|
+
.option('-t, --tag <name>', 'Filter by a specific tag')
|
|
85
|
+
.option('-l, --limit <n>', 'Max number of rows (default 30)', '30'))
|
|
86
|
+
.addHelpText('after', '\nExamples:\n ple list --tag debug\n ple list --project my-prompts')
|
|
87
|
+
.action(runWithErrorHandler(list_1.listCommand));
|
|
88
|
+
program
|
|
89
|
+
.command('search <keyword>')
|
|
90
|
+
.description('Search titles and descriptions (signed out: also local prompt bodies and tags)')
|
|
91
|
+
.action(runWithErrorHandler(search_1.searchCommand));
|
|
92
|
+
scope(program
|
|
93
|
+
.command('log <name>')
|
|
94
|
+
.description('Version history of a prompt (* = current). Signed out: the local history')
|
|
95
|
+
.option('-l, --limit <n>', 'Max versions to show (default 20)', '20')
|
|
96
|
+
.option('--json', 'Print as JSON')).action(runWithErrorHandler(log_1.logCommand));
|
|
97
|
+
scope(program
|
|
98
|
+
.command('diff <name> [a] [b]')
|
|
99
|
+
.description('Diff two versions (default: previous ↔ current). Refs: 3, v3 or 1.0.2')
|
|
100
|
+
.option('--no-color', 'Plain output (automatic when piped)')).action(runWithErrorHandler((name, a, b, opts) => (0, diff_1.diffCommand)(name, a, b, opts)));
|
|
101
|
+
scope(program
|
|
102
|
+
.command('rollback <name> <version>')
|
|
103
|
+
.description('Restore an old version as a new one — nothing is deleted (signed out: local history)')
|
|
104
|
+
.option('-y, --yes', 'Skip the confirmation prompt')).action(runWithErrorHandler((name, version, opts) => (0, rollback_1.rollbackCommand)(name, version, opts)));
|
|
105
|
+
scope(program
|
|
106
|
+
.command('improve <name>')
|
|
107
|
+
.option('-p, --pick <n>', 'Non-interactive: auto-pick variant N (1..3), 0 to cancel — for scripts/agents')
|
|
108
|
+
.description('Generate AI variants of a prompt, pick a winner, save it as a new version (Pro)')
|
|
109
|
+
.option('-f, --focus <hint>', "Steer the variants (e.g. 'shorter', 'more explicit output format')")).action(runWithErrorHandler(improve_1.improveCommand));
|
|
110
|
+
scope(program
|
|
111
|
+
.command('loop <name>')
|
|
112
|
+
.description('Multi-round improve — optionally test each variant with your own agent command (Pro)')
|
|
113
|
+
.option('-e, --exec <cmd>', 'Test each variant: prompt passed via PLE_PROMPT env + stdin; {} placeholder substituted')
|
|
114
|
+
.option('-r, --rounds <n>', 'Number of improvement rounds', '1')
|
|
115
|
+
.option('-f, --focus <hint>', 'Steer the variants')
|
|
116
|
+
.option('-p, --pick <n>', 'Non-interactive: auto-pick variant N each round + auto-continue — for scripts/agents')).action(runWithErrorHandler(loop_1.loopCommand));
|
|
117
|
+
// ---- repos --------------------------------------------------------------
|
|
118
|
+
const repo = program
|
|
119
|
+
.command('repo')
|
|
120
|
+
.description('Create and manage repos (collections of prompts) in your account')
|
|
121
|
+
.addHelpText('after', '\nExamples:\n ple repo create "team-prompts" --description "Shared review prompts"\n ple repo list\n ple repo visibility team-prompts public --yes\n\nRepos are private unless you say --public. Deleting a repo is only possible on the web.');
|
|
122
|
+
repo.command('create <name>')
|
|
123
|
+
.description('Create a repo (private by default)')
|
|
124
|
+
.option('--private', 'Only you and people you invite can see it (default)')
|
|
125
|
+
.option('--public', 'Anyone can read its public prompts')
|
|
126
|
+
.option('-d, --description <text>', 'Short description (max 500 characters)')
|
|
127
|
+
.option('--json', 'Print the result as JSON')
|
|
128
|
+
.action(runWithErrorHandler(repo_1.repoCreateCommand));
|
|
129
|
+
repo.command('list')
|
|
130
|
+
.description('List your repos with visibility, your role and prompt count')
|
|
131
|
+
.option('--json', 'Print as JSON')
|
|
132
|
+
.action(runWithErrorHandler(repo_1.repoListCommand));
|
|
133
|
+
repo.command('show <repo>')
|
|
134
|
+
.description('Show one repo (name, slug or id)')
|
|
135
|
+
.option('--json', 'Print as JSON')
|
|
136
|
+
.action(runWithErrorHandler(repo_1.repoShowCommand));
|
|
137
|
+
repo.command('visibility <repo> <value>')
|
|
138
|
+
.description('Make a repo private or public (owner only): value is private | public')
|
|
139
|
+
.option('-y, --yes', 'Confirm without asking (required when making a repo public in a script)')
|
|
140
|
+
.option('--json', 'Print the result as JSON')
|
|
141
|
+
.action(runWithErrorHandler((ref, value, opts) => (0, repo_1.repoVisibilityCommand)(ref, value, opts)));
|
|
142
|
+
// ---- account ------------------------------------------------------------
|
|
143
|
+
program
|
|
144
|
+
.command('login')
|
|
145
|
+
.description('Sign in with email + password; store the session under ~/.config/promptlike/')
|
|
146
|
+
.action(runWithErrorHandler(login_1.loginCommand));
|
|
147
|
+
program
|
|
148
|
+
.command('logout')
|
|
149
|
+
.description('Delete the local session file')
|
|
150
|
+
.action(runWithErrorHandler(logout_1.logoutCommand));
|
|
151
|
+
// ---- import / export / sync --------------------------------------------
|
|
152
|
+
program
|
|
153
|
+
.command('sync')
|
|
154
|
+
.description('Upload your local prompts to your account (one-way, explicit)')
|
|
155
|
+
.option('--dry-run', 'List what would be uploaded, change nothing')
|
|
156
|
+
.action(runWithErrorHandler(sync_1.syncCommand));
|
|
157
|
+
program
|
|
158
|
+
.command('import')
|
|
159
|
+
.description('Bring in prompts you already have: .claude/commands, SKILL.md, .cursor/commands, ~/.codex/prompts')
|
|
160
|
+
.option('--scan', 'Scan this repo and your home folder for agent prompt files')
|
|
161
|
+
.option('--dry-run', 'Only show what was found, import nothing')
|
|
162
|
+
.option('-a, --all', 'Import everything new without asking')
|
|
163
|
+
.option('--project <repo>', 'Import into this repo (signed in)')
|
|
164
|
+
.action(runWithErrorHandler((opts) => (0, import_1.importCommand)(opts)));
|
|
165
|
+
scope(program
|
|
166
|
+
.command('export [name]')
|
|
167
|
+
.description('Export a prompt (or a whole repo with --project) as an AGENTS.md section or an Agent Skill (SKILL.md)')
|
|
168
|
+
.requiredOption('--format <format>', 'agents-md | skill')
|
|
169
|
+
.option('-o, --out <path>', 'agents-md: file to write (default stdout) · skill: parent folder (default .)')
|
|
170
|
+
.option('--zip', 'skill: write <name>.zip (upload in Claude) instead of a folder')
|
|
171
|
+
.option('-f, --force', 'Overwrite an existing file or folder (never through a symbolic link)')).action(runWithErrorHandler(export_1.exportCommand));
|
|
172
|
+
// ---- insights & settings ------------------------------------------------
|
|
173
|
+
program
|
|
174
|
+
.command('stats')
|
|
175
|
+
.description('How your library is used — uses by surface, most used, gathering dust')
|
|
176
|
+
.option('-r, --range <days>', 'Window: 7, 30 or 365 (Free: 7)', '7')
|
|
177
|
+
.option('--json', 'Print the raw summary as JSON')
|
|
178
|
+
.action(runWithErrorHandler(stats_1.statsCommand));
|
|
179
|
+
program
|
|
180
|
+
.command('suggest [text...]')
|
|
181
|
+
.description('Which saved prompt fits this task? Local match — the text never leaves your machine')
|
|
182
|
+
.option('--json', 'Print matches as JSON')
|
|
183
|
+
.option('-l, --limit <n>', 'Max matches (default 3)', '3')
|
|
184
|
+
.option('--hook', 'Claude Code UserPromptSubmit mode: read hook JSON on stdin, print a short hint or nothing')
|
|
185
|
+
.option('--refresh-cache', 'Refresh the local library cache from your account and exit')
|
|
186
|
+
.action(runWithErrorHandler((text, opts) => (0, suggest_1.suggestCommand)(text ?? [], opts)));
|
|
187
|
+
const configCmd = program.command('config').description('CLI preferences (suggest on|off)');
|
|
188
|
+
configCmd
|
|
189
|
+
.command('get [key]')
|
|
190
|
+
.description('Show a setting (or all)')
|
|
191
|
+
.action(runWithErrorHandler(config_1.configGetCommand));
|
|
192
|
+
configCmd
|
|
193
|
+
.command('set <key> <value>')
|
|
194
|
+
.description('Change a setting, e.g. `ple config set suggest off`')
|
|
195
|
+
.action(runWithErrorHandler(config_1.configSetCommand));
|
|
196
|
+
return program;
|
|
197
|
+
}
|
package/package.json
CHANGED