draftie 0.0.0-stage → 0.1.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/LICENSE +46 -0
- package/README.md +74 -2
- package/dist/draftie.js +296 -0
- package/dist/git.js +53 -0
- package/dist/state.js +63 -0
- package/package.json +32 -4
- package/skills/draftie/SKILL.md +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thanos Tourikas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
src/git.ts is adapted from Postplan
|
|
26
|
+
(https://www.npmjs.com/package/postplan), which is under the MIT License:
|
|
27
|
+
|
|
28
|
+
Copyright (c) 2026 t3dotgg
|
|
29
|
+
|
|
30
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
31
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
32
|
+
in the Software without restriction, including without limitation the rights
|
|
33
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
34
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
35
|
+
furnished to do so, subject to the following conditions:
|
|
36
|
+
|
|
37
|
+
The above copyright notice and this permission notice shall be included in all
|
|
38
|
+
copies or substantial portions of the Software.
|
|
39
|
+
|
|
40
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
41
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
42
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
43
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
44
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
45
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
46
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# draftie
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Upload one HTML file and get a stable link. For the plans, reports and mockups that agents
|
|
4
|
+
write. Uploading the same file again adds a version at the same link.
|
|
5
|
+
|
|
6
|
+
This is the command for a [draftie](https://github.com/thanostourik/draftie) server, a small app
|
|
7
|
+
that a team hosts for itself.
|
|
8
|
+
|
|
9
|
+
## Start
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npx draftie auth login
|
|
13
|
+
npx draftie upload plan.html
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
- `auth login` prints the server's address. Open it, sign in, create an API key, and paste the
|
|
17
|
+
key into the terminal. This works over SSH.
|
|
18
|
+
- `upload` prints the page's link, `URL:`, and the link that returns the exact bytes,
|
|
19
|
+
`Raw HTML:`.
|
|
20
|
+
|
|
21
|
+
The commands talk to `https://draftie.yeet.tourik.dev`. For another server, log in with its
|
|
22
|
+
address once: `npx draftie auth login --url https://draftie.example.com`. That server is then
|
|
23
|
+
the default.
|
|
24
|
+
|
|
25
|
+
## Commands
|
|
26
|
+
|
|
27
|
+
| Command | What it does |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `draftie upload <file>` | Uploads the file: a new page the first time, a new version after that |
|
|
30
|
+
| `draftie list` | Lists your pages; `--json` prints them as JSON |
|
|
31
|
+
| `draftie get <page>` | Prints a page's HTML, with your API key |
|
|
32
|
+
| `draftie set <page>` | Changes `--visibility`, `--expires` or `--description` |
|
|
33
|
+
| `draftie delete <page>` | Deletes the page and all its versions; `--yes` skips the question |
|
|
34
|
+
| `draftie claim` | Gives the pages that you uploaded without an API key to your account |
|
|
35
|
+
| `draftie auth login`, `draftie auth set <key>` | Save an API key |
|
|
36
|
+
| `draftie whoami` | Says which server and key the commands use |
|
|
37
|
+
|
|
38
|
+
`<page>` is a file that was uploaded, a page's link, or a page's id.
|
|
39
|
+
|
|
40
|
+
Options of `upload`:
|
|
41
|
+
|
|
42
|
+
- `--visibility private|public`: who sees the page. `private`, the default, is only you, signed
|
|
43
|
+
in. `public` is anyone with the link, without signing in.
|
|
44
|
+
- `--expires 7d`: delete the page after this time, such as `30m`, `12h`, `7d` or `2w`. `never`
|
|
45
|
+
removes the expiry. Without it a page is kept until you delete it.
|
|
46
|
+
- `--description <text>`: a short description for the list.
|
|
47
|
+
- `--new`: upload the file as a new page. `--page <id>`: add a version to that page.
|
|
48
|
+
|
|
49
|
+
Every command takes `--url <server>`. `DRAFTIE_URL` and `DRAFTIE_API_KEY` in the environment
|
|
50
|
+
override the saved server and key, which is how a CI job uploads.
|
|
51
|
+
|
|
52
|
+
## What the HTML may hold
|
|
53
|
+
|
|
54
|
+
A page is static. Inline CSS and images from `https:` or `data:` work. JavaScript, stylesheets
|
|
55
|
+
from another address, forms and iframes do not. The server refuses a file that holds a
|
|
56
|
+
`<script>`, an inline handler such as `onclick`, a `javascript:` link, a form, an iframe or a
|
|
57
|
+
`<link rel="stylesheet">`, and `upload` prints what it found. A file is UTF-8 and at most 2 MB.
|
|
58
|
+
|
|
59
|
+
## Where it keeps things
|
|
60
|
+
|
|
61
|
+
`~/.draftie/config.json` holds the server's address and one API key per server.
|
|
62
|
+
`~/.draftie/drafts.json` holds which file was uploaded to which page.
|
|
63
|
+
|
|
64
|
+
Each upload also sends the git branch, commit, commit subject and whether the working tree is
|
|
65
|
+
dirty, and in GitHub Actions the run's link and actor. The dashboard shows them per version.
|
|
66
|
+
|
|
67
|
+
## For agents
|
|
68
|
+
|
|
69
|
+
`skills/draftie/SKILL.md` in this package tells an agent how to write the HTML, upload it and
|
|
70
|
+
read a page back. For Claude Code, copy the folder `skills/draftie` to `~/.claude/skills/`.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT. `src/git.ts` is adapted from
|
|
75
|
+
[Postplan](https://www.npmjs.com/package/postplan), MIT, by t3dotgg.
|
package/dist/draftie.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import readline from "node:readline/promises";
|
|
7
|
+
import { Command, Option } from "commander";
|
|
8
|
+
import { ciDetails, gitDetails } from "./git.js";
|
|
9
|
+
import { apiKey, changeDrafts, draftsOf, savedKey, saveKey, serverUrl } from "./state.js";
|
|
10
|
+
const { version: VERSION } = createRequire(import.meta.url)("../package.json");
|
|
11
|
+
const VISIBILITIES = ["private", "public"];
|
|
12
|
+
const WHO_SEES = {
|
|
13
|
+
private: "only you",
|
|
14
|
+
public: "anyone with the link",
|
|
15
|
+
};
|
|
16
|
+
const DURATIONS = { m: 60_000, h: 3_600_000, d: 86_400_000, w: 604_800_000 };
|
|
17
|
+
class CliError extends Error {
|
|
18
|
+
status;
|
|
19
|
+
constructor(message, status) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.status = status;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const serverOf = ({ url }) => {
|
|
25
|
+
const server = serverUrl(url);
|
|
26
|
+
return { url: server, key: apiKey(server) };
|
|
27
|
+
};
|
|
28
|
+
async function send(server, method, address, body) {
|
|
29
|
+
const headers = { "user-agent": `draftie/${VERSION}` };
|
|
30
|
+
if (server.key !== undefined)
|
|
31
|
+
headers.authorization = `Bearer ${server.key}`;
|
|
32
|
+
if (body !== undefined)
|
|
33
|
+
headers["content-type"] = "application/json";
|
|
34
|
+
try {
|
|
35
|
+
return await fetch(address, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
const cause = error instanceof Error && error.cause instanceof Error ? error.cause.message : String(error);
|
|
39
|
+
throw new CliError(`cannot reach ${server.url}: ${cause}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function refused(response) {
|
|
43
|
+
const text = await response.text();
|
|
44
|
+
let message = text.trim() || `${response.status} ${response.statusText}`;
|
|
45
|
+
try {
|
|
46
|
+
message = JSON.parse(text).error ?? message;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// not JSON: yeet's own errors are plain text
|
|
50
|
+
}
|
|
51
|
+
return new CliError(message, response.status);
|
|
52
|
+
}
|
|
53
|
+
async function call(server, method, route, body) {
|
|
54
|
+
const response = await send(server, method, `${server.url}${route}`, body);
|
|
55
|
+
if (!response.ok)
|
|
56
|
+
throw await refused(response);
|
|
57
|
+
return (await response.json());
|
|
58
|
+
}
|
|
59
|
+
const listPages = async (server) => (await call(server, "GET", "/api/pages")).pages;
|
|
60
|
+
const isLink = (text) => /^https?:\/\//.test(text);
|
|
61
|
+
const withoutRaw = (link) => link.replace(/[?#].*$/, "").replace(/\/+$/, "").replace(/\/raw$/, "");
|
|
62
|
+
/** The id of the page that `page` names: a file that was uploaded, a page's link, or the id itself. */
|
|
63
|
+
async function pageId(server, page) {
|
|
64
|
+
const draft = draftsOf(server.url)[path.resolve(page)];
|
|
65
|
+
if (draft !== undefined)
|
|
66
|
+
return draft.id;
|
|
67
|
+
if (!isLink(page))
|
|
68
|
+
return page;
|
|
69
|
+
const link = withoutRaw(page);
|
|
70
|
+
const found = (await listPages(server)).find(({ url }) => link === url || link.startsWith(`${url}/`));
|
|
71
|
+
if (found === undefined)
|
|
72
|
+
throw new CliError(`${page} is not the link of one of your pages on ${server.url}`);
|
|
73
|
+
return found.id;
|
|
74
|
+
}
|
|
75
|
+
function expiresIn(text) {
|
|
76
|
+
if (text === "never")
|
|
77
|
+
return null;
|
|
78
|
+
const match = /^(\d+)([mhdw])$/.exec(text);
|
|
79
|
+
if (match === null)
|
|
80
|
+
throw new CliError("--expires takes a time such as 30m, 12h, 7d or 2w, or `never`");
|
|
81
|
+
return Number(match[1]) * DURATIONS[match[2]];
|
|
82
|
+
}
|
|
83
|
+
const changes = ({ visibility, expires, description }) => ({
|
|
84
|
+
visibility,
|
|
85
|
+
expiresIn: expires === undefined ? undefined : expiresIn(expires),
|
|
86
|
+
description: description === undefined ? undefined : description.trim() || null,
|
|
87
|
+
});
|
|
88
|
+
const when = (time) => `${new Date(time).toISOString().slice(0, 16).replace("T", " ")} UTC`;
|
|
89
|
+
function describe(page) {
|
|
90
|
+
console.log(`Visibility: ${page.visibility} (${WHO_SEES[page.visibility]})`);
|
|
91
|
+
console.log(`Expires: ${page.expiresAt === null ? "never" : when(page.expiresAt)}`);
|
|
92
|
+
}
|
|
93
|
+
async function checkedKey(server) {
|
|
94
|
+
const me = await call(server, "GET", "/api/me");
|
|
95
|
+
return `${me.userId}${me.keyName === null ? "" : `, with the key "${me.keyName}"`}`;
|
|
96
|
+
}
|
|
97
|
+
async function ask(question) {
|
|
98
|
+
const prompt = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
99
|
+
// `question` never settles when standard input ends, so the prompt's close answers "".
|
|
100
|
+
const answer = await Promise.race([prompt.question(question), new Promise((resolve) => prompt.once("close", () => resolve("")))]);
|
|
101
|
+
prompt.close();
|
|
102
|
+
return answer.trim();
|
|
103
|
+
}
|
|
104
|
+
const program = new Command().name("draftie").description("Upload one HTML file, get a stable link.").version(VERSION);
|
|
105
|
+
const urlOption = () => new Option("--url <url>", "the draftie server, instead of the saved one");
|
|
106
|
+
const visibilityOption = () => new Option("--visibility <who>", "who sees the page").choices(VISIBILITIES);
|
|
107
|
+
const expiresOption = () => new Option("--expires <time>", "delete the page after this long, such as 12h or 7d, or `never`");
|
|
108
|
+
program
|
|
109
|
+
.command("upload")
|
|
110
|
+
.description("Upload an HTML file. The same file again adds a version at the same link.")
|
|
111
|
+
.argument("<file>", "the HTML file")
|
|
112
|
+
.option("--page <id>", "add a version to this page")
|
|
113
|
+
.option("--new", "always create a new page")
|
|
114
|
+
.addOption(visibilityOption())
|
|
115
|
+
.addOption(expiresOption())
|
|
116
|
+
.option("--description <text>", "a short description, shown in the dashboard and in `draftie list`")
|
|
117
|
+
.addOption(urlOption())
|
|
118
|
+
.action(async (file, options) => {
|
|
119
|
+
const server = serverOf(options);
|
|
120
|
+
const resolved = path.resolve(file);
|
|
121
|
+
if (!fs.existsSync(resolved))
|
|
122
|
+
throw new CliError(`${resolved} does not exist`);
|
|
123
|
+
const bytes = fs.readFileSync(resolved);
|
|
124
|
+
const html = bytes.toString("utf8");
|
|
125
|
+
if (!Buffer.from(html, "utf8").equals(bytes))
|
|
126
|
+
throw new CliError(`${resolved} is not UTF-8 text`);
|
|
127
|
+
const known = options.new ? undefined : draftsOf(server.url)[resolved];
|
|
128
|
+
const pageId = options.new ? undefined : (options.page ?? known?.id);
|
|
129
|
+
const claimToken = known?.claimToken ?? (server.key === undefined ? `dfc_${randomBytes(32).toString("base64url")}` : undefined);
|
|
130
|
+
const receipt = await call(server, "POST", "/api/pages", {
|
|
131
|
+
html,
|
|
132
|
+
filename: path.basename(resolved),
|
|
133
|
+
pageId,
|
|
134
|
+
claimToken,
|
|
135
|
+
details: { ...gitDetails(path.dirname(resolved)), ...ciDetails(), cliVersion: VERSION },
|
|
136
|
+
...changes(options),
|
|
137
|
+
}).catch((error) => {
|
|
138
|
+
if (!(error instanceof CliError) || error.status !== 404 || pageId === undefined || pageId !== known?.id)
|
|
139
|
+
throw error;
|
|
140
|
+
throw new CliError(`${error.message}\nThis file was uploaded to page ${pageId} before. \`--new\` uploads it as a new page.`);
|
|
141
|
+
});
|
|
142
|
+
const draft = {
|
|
143
|
+
id: receipt.id,
|
|
144
|
+
url: receipt.url,
|
|
145
|
+
rawUrl: receipt.rawUrl,
|
|
146
|
+
version: receipt.version,
|
|
147
|
+
...(server.key === undefined && { claimToken }),
|
|
148
|
+
};
|
|
149
|
+
changeDrafts(server.url, (drafts) => ({ ...drafts, [resolved]: draft }));
|
|
150
|
+
if (receipt.unchanged)
|
|
151
|
+
console.log(`Unchanged: version ${receipt.version} already holds these bytes`);
|
|
152
|
+
else
|
|
153
|
+
console.log(pageId === undefined ? `Uploaded "${receipt.title}"` : `Uploaded version ${receipt.version} of "${receipt.title}"`);
|
|
154
|
+
console.log(`URL: ${receipt.url}`);
|
|
155
|
+
console.log(`Raw HTML: ${receipt.rawUrl}`);
|
|
156
|
+
console.log(`Page ID: ${receipt.id}`);
|
|
157
|
+
console.log(`Version: ${receipt.version}`);
|
|
158
|
+
describe(receipt);
|
|
159
|
+
if (server.key === undefined) {
|
|
160
|
+
console.warn("This page has no owner. Run `draftie auth login`, then `draftie claim`, to keep it past its expiry.");
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
program
|
|
164
|
+
.command("list")
|
|
165
|
+
.description("List your pages.")
|
|
166
|
+
.option("--json", "print the pages as JSON")
|
|
167
|
+
.addOption(urlOption())
|
|
168
|
+
.action(async (options) => {
|
|
169
|
+
const pages = await listPages(serverOf(options));
|
|
170
|
+
if (options.json)
|
|
171
|
+
return console.log(JSON.stringify(pages, null, 2));
|
|
172
|
+
if (pages.length === 0)
|
|
173
|
+
return console.log("No pages yet. Upload one with: draftie upload <file>");
|
|
174
|
+
for (const page of pages) {
|
|
175
|
+
const repo = page.repo === null ? "no repository" : `${page.repo.org}/${page.repo.name}`;
|
|
176
|
+
const expiry = page.expiresAt === null ? "" : ` · expires ${when(page.expiresAt)}`;
|
|
177
|
+
console.log(page.title);
|
|
178
|
+
console.log(` ${page.id} · ${repo} · v${page.versions} · ${page.visibility} · updated ${when(page.updatedAt)}${expiry}`);
|
|
179
|
+
console.log(` ${page.url}`);
|
|
180
|
+
if (page.description !== null)
|
|
181
|
+
console.log(` ${page.description}`);
|
|
182
|
+
console.log("");
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
program
|
|
186
|
+
.command("get")
|
|
187
|
+
.description("Print a page's HTML. Unlike curl, it sends your API key, which a page that is not public needs.")
|
|
188
|
+
.argument("<page>", "a page's link, with /v/<n> for one version, or its id, or a file that was uploaded")
|
|
189
|
+
.addOption(urlOption())
|
|
190
|
+
.action(async (page, options) => {
|
|
191
|
+
const server = serverOf(options);
|
|
192
|
+
const link = isLink(page)
|
|
193
|
+
? withoutRaw(page)
|
|
194
|
+
: (draftsOf(server.url)[path.resolve(page)]?.url ?? (await listPages(server)).find(({ id }) => id === page)?.url);
|
|
195
|
+
if (link === undefined)
|
|
196
|
+
throw new CliError(`no page ${page} of yours on ${server.url}; pass the page's link instead`);
|
|
197
|
+
const origin = new URL(link).origin;
|
|
198
|
+
// The key goes only to the server it belongs to, never to wherever a link points.
|
|
199
|
+
const host = origin === server.url ? server : { url: origin, key: savedKey(origin) };
|
|
200
|
+
const response = await send(host, "GET", `${link}/raw`);
|
|
201
|
+
if (!response.ok)
|
|
202
|
+
throw await refused(response);
|
|
203
|
+
process.stdout.write(Buffer.from(await response.arrayBuffer()));
|
|
204
|
+
});
|
|
205
|
+
program
|
|
206
|
+
.command("set")
|
|
207
|
+
.description("Change who sees a page, when it expires, or its description.")
|
|
208
|
+
.argument("<page>", "a page's link or id, or a file that was uploaded")
|
|
209
|
+
.addOption(visibilityOption())
|
|
210
|
+
.addOption(expiresOption())
|
|
211
|
+
.option("--description <text>", "a short description; an empty one removes it")
|
|
212
|
+
.addOption(urlOption())
|
|
213
|
+
.action(async (page, options) => {
|
|
214
|
+
const server = serverOf(options);
|
|
215
|
+
const changed = await call(server, "PATCH", `/api/pages/${await pageId(server, page)}`, changes(options));
|
|
216
|
+
console.log(`"${changed.title}"`);
|
|
217
|
+
console.log(`URL: ${changed.url}`);
|
|
218
|
+
describe(changed);
|
|
219
|
+
if (changed.description !== null)
|
|
220
|
+
console.log(`Description: ${changed.description}`);
|
|
221
|
+
});
|
|
222
|
+
program
|
|
223
|
+
.command("delete")
|
|
224
|
+
.description("Delete a page and all its versions. Nothing can bring it back.")
|
|
225
|
+
.argument("<page>", "a page's link or id, or a file that was uploaded")
|
|
226
|
+
.option("--yes", "do not ask first")
|
|
227
|
+
.addOption(urlOption())
|
|
228
|
+
.action(async (page, options) => {
|
|
229
|
+
const server = serverOf(options);
|
|
230
|
+
const id = await pageId(server, page);
|
|
231
|
+
if (!options.yes) {
|
|
232
|
+
if (!process.stdin.isTTY)
|
|
233
|
+
throw new CliError("pass --yes to delete without being asked");
|
|
234
|
+
const answer = await ask(`Delete page ${id} and all its versions? Nothing can bring it back. [y/N] `);
|
|
235
|
+
if (answer.toLowerCase() !== "y")
|
|
236
|
+
throw new CliError("Nothing deleted.");
|
|
237
|
+
}
|
|
238
|
+
await call(server, "DELETE", `/api/pages/${id}`);
|
|
239
|
+
changeDrafts(server.url, (drafts) => Object.fromEntries(Object.entries(drafts).filter(([, draft]) => draft.id !== id)));
|
|
240
|
+
console.log(`Deleted page ${id}`);
|
|
241
|
+
});
|
|
242
|
+
program
|
|
243
|
+
.command("claim")
|
|
244
|
+
.description("Give the pages that this computer uploaded without an API key to your account.")
|
|
245
|
+
.addOption(urlOption())
|
|
246
|
+
.action(async (options) => {
|
|
247
|
+
const server = serverOf(options);
|
|
248
|
+
const pages = Object.values(draftsOf(server.url)).flatMap(({ id, claimToken }) => (claimToken === undefined ? [] : [{ id, claimToken }]));
|
|
249
|
+
if (pages.length === 0)
|
|
250
|
+
return console.log("No page here was uploaded without an API key.");
|
|
251
|
+
const { claimed } = await call(server, "POST", "/api/claim", { pages });
|
|
252
|
+
changeDrafts(server.url, (drafts) => Object.fromEntries(Object.entries(drafts).map(([file, { claimToken, ...draft }]) => [file, claimed.includes(draft.id) ? draft : { ...draft, claimToken }])));
|
|
253
|
+
console.log(`Claimed ${claimed.length} of ${pages.length} pages${claimed.length === 0 ? "" : `: ${claimed.join(", ")}`}`);
|
|
254
|
+
if (claimed.length < pages.length)
|
|
255
|
+
console.log("The others expired, were deleted, or belong to an account already.");
|
|
256
|
+
});
|
|
257
|
+
const auth = program.command("auth").description("Manage the API key that the other commands send.");
|
|
258
|
+
auth
|
|
259
|
+
.command("login")
|
|
260
|
+
.description("Save an API key that you create in the dashboard. Works over SSH: you paste the key.")
|
|
261
|
+
.addOption(urlOption())
|
|
262
|
+
.action(async (options) => {
|
|
263
|
+
const url = serverUrl(options.url);
|
|
264
|
+
console.log(`Open ${url} in a browser, sign in, and create an API key.\n`);
|
|
265
|
+
const key = await ask("Paste the API key: ");
|
|
266
|
+
if (key === "")
|
|
267
|
+
throw new CliError("No key entered. Nothing saved.");
|
|
268
|
+
const who = await checkedKey({ url, key });
|
|
269
|
+
saveKey(url, key, options.url !== undefined);
|
|
270
|
+
console.log(`\nSaved. You upload to ${url} as ${who}.`);
|
|
271
|
+
});
|
|
272
|
+
auth
|
|
273
|
+
.command("set")
|
|
274
|
+
.description("Save an API key without being asked for it.")
|
|
275
|
+
.argument("<key>", "the API key")
|
|
276
|
+
.addOption(urlOption())
|
|
277
|
+
.action(async (key, options) => {
|
|
278
|
+
const url = serverUrl(options.url);
|
|
279
|
+
const who = await checkedKey({ url, key });
|
|
280
|
+
saveKey(url, key, options.url !== undefined);
|
|
281
|
+
console.log(`Saved. You upload to ${url} as ${who}.`);
|
|
282
|
+
});
|
|
283
|
+
program
|
|
284
|
+
.command("whoami")
|
|
285
|
+
.description("Say which server and which API key the commands use.")
|
|
286
|
+
.addOption(urlOption())
|
|
287
|
+
.action(async (options) => {
|
|
288
|
+
const server = serverOf(options);
|
|
289
|
+
if (server.key === undefined)
|
|
290
|
+
throw new CliError(`no API key for ${server.url}; run \`draftie auth login\``);
|
|
291
|
+
console.log(`${server.url} as ${await checkedKey(server)}`);
|
|
292
|
+
});
|
|
293
|
+
program.parseAsync().catch((error) => {
|
|
294
|
+
console.error(error instanceof CliError ? error.message : error);
|
|
295
|
+
process.exit(1);
|
|
296
|
+
});
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Adapted from Postplan's bin/postplan.js, MIT, Copyright (c) 2026 t3dotgg. See ../LICENSE.
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
function git(args, cwd) {
|
|
5
|
+
try {
|
|
6
|
+
return execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function parseRemote(remote) {
|
|
13
|
+
if (!remote)
|
|
14
|
+
return {};
|
|
15
|
+
const cleaned = remote.replace(/\.git$/, "");
|
|
16
|
+
const ssh = /^[^@]+@([^:]+):([^/]+)\/(.+)$/.exec(cleaned);
|
|
17
|
+
if (ssh)
|
|
18
|
+
return { host: ssh[1], org: ssh[2], name: path.basename(ssh[3]) };
|
|
19
|
+
if (URL.canParse(cleaned)) {
|
|
20
|
+
const url = new URL(cleaned);
|
|
21
|
+
const parts = url.pathname.split("/").filter(Boolean);
|
|
22
|
+
if (parts.length >= 2)
|
|
23
|
+
return { host: url.hostname, org: parts[0], name: parts.at(-1) };
|
|
24
|
+
}
|
|
25
|
+
const parts = cleaned.split("/").filter(Boolean);
|
|
26
|
+
return parts.length >= 2 ? { org: parts.at(-2), name: parts.at(-1) } : {};
|
|
27
|
+
}
|
|
28
|
+
/** What git says about the folder `cwd`. Every value is null outside a repository. */
|
|
29
|
+
export function gitDetails(cwd) {
|
|
30
|
+
const root = git(["rev-parse", "--show-toplevel"], cwd);
|
|
31
|
+
const remote = parseRemote(git(["config", "--get", "remote.origin.url"], cwd));
|
|
32
|
+
const status = git(["status", "--porcelain"], cwd);
|
|
33
|
+
return {
|
|
34
|
+
repoHost: remote.host ?? null,
|
|
35
|
+
repoOrg: remote.org ?? (root ? path.basename(path.dirname(root)) : null),
|
|
36
|
+
repoName: remote.name ?? (root ? path.basename(root) : null),
|
|
37
|
+
gitBranch: git(["rev-parse", "--abbrev-ref", "HEAD"], cwd),
|
|
38
|
+
gitCommit: git(["rev-parse", "HEAD"], cwd),
|
|
39
|
+
gitSubject: git(["log", "-1", "--format=%s"], cwd),
|
|
40
|
+
gitDirty: status === null ? null : status.length > 0,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** The CI run that uploads, when GitHub Actions runs the CLI. */
|
|
44
|
+
export function ciDetails() {
|
|
45
|
+
const env = process.env;
|
|
46
|
+
if (env.GITHUB_ACTIONS !== "true")
|
|
47
|
+
return {};
|
|
48
|
+
const server = env.GITHUB_SERVER_URL ?? "https://github.com";
|
|
49
|
+
return {
|
|
50
|
+
ciRunUrl: env.GITHUB_REPOSITORY && env.GITHUB_RUN_ID ? `${server}/${env.GITHUB_REPOSITORY}/actions/runs/${env.GITHUB_RUN_ID}` : null,
|
|
51
|
+
ciActor: env.GITHUB_ACTOR ?? null,
|
|
52
|
+
};
|
|
53
|
+
}
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const DEFAULT_URL = "https://draftie.yeet.tourik.dev";
|
|
5
|
+
const DIR = path.join(os.homedir(), ".draftie");
|
|
6
|
+
const CONFIG_PATH = path.join(DIR, "config.json");
|
|
7
|
+
const DRAFTS_PATH = path.join(DIR, "drafts.json");
|
|
8
|
+
const LOCK_PATH = path.join(DIR, "drafts.lock");
|
|
9
|
+
function read(file, fallback) {
|
|
10
|
+
if (!fs.existsSync(file))
|
|
11
|
+
return fallback;
|
|
12
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
13
|
+
}
|
|
14
|
+
/** Replaces the file in one step, so that another `draftie` never reads half of it. */
|
|
15
|
+
function write(file, value) {
|
|
16
|
+
fs.mkdirSync(DIR, { recursive: true, mode: 0o700 });
|
|
17
|
+
const temporary = `${file}.${process.pid}`;
|
|
18
|
+
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
19
|
+
fs.renameSync(temporary, file);
|
|
20
|
+
}
|
|
21
|
+
const readConfig = () => read(CONFIG_PATH, { keys: {} });
|
|
22
|
+
export function serverUrl(override) {
|
|
23
|
+
return (override ?? process.env.DRAFTIE_URL ?? readConfig().url ?? DEFAULT_URL).replace(/\/+$/, "");
|
|
24
|
+
}
|
|
25
|
+
export const savedKey = (url) => readConfig().keys[url];
|
|
26
|
+
export const apiKey = (url) => process.env.DRAFTIE_API_KEY ?? savedKey(url);
|
|
27
|
+
export function saveKey(url, key, makeDefault) {
|
|
28
|
+
const config = readConfig();
|
|
29
|
+
write(CONFIG_PATH, { ...config, ...(makeDefault && { url }), keys: { ...config.keys, [url]: key } });
|
|
30
|
+
}
|
|
31
|
+
export const draftsOf = (url) => read(DRAFTS_PATH, {})[url] ?? {};
|
|
32
|
+
/** Runs `change` while no other `draftie` on this computer changes the drafts. */
|
|
33
|
+
function withDraftsLock(change) {
|
|
34
|
+
fs.mkdirSync(DIR, { recursive: true, mode: 0o700 });
|
|
35
|
+
const waitingSince = Date.now();
|
|
36
|
+
while (true) {
|
|
37
|
+
try {
|
|
38
|
+
fs.mkdirSync(LOCK_PATH);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error.code !== "EEXIST")
|
|
43
|
+
throw error;
|
|
44
|
+
// a draftie that died while it held the lock never removes it
|
|
45
|
+
if (Date.now() - waitingSince > 2000)
|
|
46
|
+
break;
|
|
47
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
change();
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
fs.rmSync(LOCK_PATH, { recursive: true, force: true });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Changes the server's drafts as they are on disk now: parallel uploads each add their own. */
|
|
58
|
+
export function changeDrafts(url, change) {
|
|
59
|
+
withDraftsLock(() => {
|
|
60
|
+
const all = read(DRAFTS_PATH, {});
|
|
61
|
+
write(DRAFTS_PATH, { ...all, [url]: change(all[url] ?? {}) });
|
|
62
|
+
});
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "draftie",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Upload one HTML file, get a stable link. For the plans, reports and mockups that agents write.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/thanostourik/draftie.git",
|
|
10
|
+
"directory": "cli"
|
|
11
|
+
},
|
|
12
|
+
"bin": {
|
|
13
|
+
"draftie": "dist/draftie.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"skills/"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc -p .",
|
|
21
|
+
"typecheck": "tsc -p . --noEmit",
|
|
22
|
+
"prepack": "pnpm build"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"commander": "^15.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^24.0.0",
|
|
32
|
+
"typescript": "^7.0.2"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: draftie
|
|
3
|
+
description: Publish one HTML file with draftie and return its link, or read a page from a draftie link. Use when the user asks to publish, share or upload a plan, report, analysis, write-up or mockup as HTML, or gives a link whose path starts with /api/d/.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# draftie
|
|
7
|
+
|
|
8
|
+
draftie hosts single HTML files. `draftie upload` returns a stable link. Uploading the same file
|
|
9
|
+
again adds a version at the same link.
|
|
10
|
+
|
|
11
|
+
## Read a page from a link
|
|
12
|
+
|
|
13
|
+
Run the CLI. Do not use a browser or web search: a page that is not public needs the API key,
|
|
14
|
+
which only the CLI sends.
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npx draftie get '<link>' > /tmp/draftie-page.html
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
- The link may end in `/raw` or not. `<link>/v/3` is version 3.
|
|
21
|
+
- If the command fails, report its message. Do not replace the page with a guess.
|
|
22
|
+
|
|
23
|
+
## Write the HTML
|
|
24
|
+
|
|
25
|
+
Write one complete, static HTML file that needs nothing but itself. No JavaScript runs in a
|
|
26
|
+
draftie page, and `draftie upload` refuses a file that holds any.
|
|
27
|
+
|
|
28
|
+
Works:
|
|
29
|
+
|
|
30
|
+
- Inline CSS in a `<style>` block or in `style` attributes.
|
|
31
|
+
- Images, video and audio from `https:` addresses or `data:` URLs.
|
|
32
|
+
- Links to other pages. A link with `target="_blank"` opens a new tab.
|
|
33
|
+
- `<details>` and CSS such as `:target` and `:checked`, for parts that open, close or switch.
|
|
34
|
+
- A `<title>`. It becomes the page's name.
|
|
35
|
+
|
|
36
|
+
Does not work:
|
|
37
|
+
|
|
38
|
+
- JavaScript: `<script>`, inline handlers such as `onclick`, and `javascript:` links.
|
|
39
|
+
- `<link rel="stylesheet">`, web fonts from an address, and CSS `@import`. Put the CSS inline.
|
|
40
|
+
- Forms, `<iframe>`, `<object>`, `<embed>`, and `<meta http-equiv="refresh">`.
|
|
41
|
+
|
|
42
|
+
Never put secrets, tokens or passwords in the file. The file must be UTF-8 and at most 2 MB.
|
|
43
|
+
|
|
44
|
+
## Upload
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
npx draftie upload <file>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
It prints `URL:`, the link for people, and `Raw HTML:`, the link for agents. Give the user the
|
|
51
|
+
`URL:` line.
|
|
52
|
+
|
|
53
|
+
- `--visibility private|public` sets who sees the page. `private`, the default, is only the
|
|
54
|
+
user, signed in. `public` is anyone with the link, without signing in. Use `public` only when
|
|
55
|
+
the user wants someone else to open the link.
|
|
56
|
+
- `--expires 7d` deletes the page after that time: `30m`, `12h`, `7d`, `2w`, or `never`.
|
|
57
|
+
- `--description "<text>"` sets a short description for the page list.
|
|
58
|
+
- The CLI remembers which file went to which page. `--new` uploads the file as a new page.
|
|
59
|
+
`--page <id>` adds a version to another page.
|
|
60
|
+
- "Unchanged" means the file's bytes equal the latest version, so no version was added.
|
|
61
|
+
|
|
62
|
+
If the upload fails with "an upload needs an API key", tell the user to run
|
|
63
|
+
`npx draftie auth login`. Only they can sign in.
|
|
64
|
+
|
|
65
|
+
If it fails with "this HTML holds …", remove what the message names and upload again.
|
|
66
|
+
|
|
67
|
+
## Change or delete a page
|
|
68
|
+
|
|
69
|
+
`<page>` is the file that was uploaded, the page's link, or its id.
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
npx draftie set <page> --visibility public
|
|
73
|
+
npx draftie set <page> --expires never
|
|
74
|
+
npx draftie delete <page> --yes
|
|
75
|
+
npx draftie list
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`delete` erases the page and every version, and nothing can bring it back. Run it only when the
|
|
79
|
+
user asks.
|