@sovereignfs/create 0.0.0-stage → 0.1.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/LICENSE +661 -0
- package/README.md +10 -2
- package/dist/create.mjs +473 -0
- package/package.json +37 -3
- package/template/app/Dockerfile +29 -0
- package/template/app/README.md +48 -0
- package/template/app/_dockerignore +7 -0
- package/template/app/_env.example +9 -0
- package/template/app/_gitignore +7 -0
- package/template/app/_tsconfig.json +21 -0
- package/template/app/app/actions.ts +25 -0
- package/template/app/app/api/health/route.ts +16 -0
- package/template/app/app/auth/[action]/route.ts +7 -0
- package/template/app/app/globals.css +27 -0
- package/template/app/app/layout.tsx +14 -0
- package/template/app/app/page.tsx +49 -0
- package/template/app/app/ui.tsx +5 -0
- package/template/app/instrumentation.ts +7 -0
- package/template/app/lib/auth.ts +15 -0
- package/template/app/lib/config.ts +20 -0
- package/template/app/lib/crypto.ts +29 -0
- package/template/app/lib/db.ts +10 -0
- package/template/app/lib/migrate.ts +44 -0
- package/template/app/lib/sovereign.ts +13 -0
- package/template/app/lib/stores.ts +101 -0
- package/template/app/migrations/001_init.sql +27 -0
- package/template/app/next.config.mjs +8 -0
- package/template/app/scripts/manifest.mjs +25 -0
- package/template/app/sovereign.manifest.json +19 -0
package/dist/create.mjs
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/main.ts
|
|
4
|
+
import { execFile } from "child_process";
|
|
5
|
+
import { createInterface } from "readline/promises";
|
|
6
|
+
|
|
7
|
+
// package.json
|
|
8
|
+
var package_default = {
|
|
9
|
+
name: "@sovereignfs/create",
|
|
10
|
+
version: "0.1.2",
|
|
11
|
+
type: "module",
|
|
12
|
+
description: "npm create @sovereignfs: makes an instance repository, pinned to the toolkit (sv) of the same version.",
|
|
13
|
+
license: "AGPL-3.0-or-later",
|
|
14
|
+
repository: {
|
|
15
|
+
type: "git",
|
|
16
|
+
url: "git+https://github.com/sovereignfs/sovereign.git",
|
|
17
|
+
directory: "tools/create"
|
|
18
|
+
},
|
|
19
|
+
homepage: "https://github.com/sovereignfs/sovereign/tree/main/tools/create#readme",
|
|
20
|
+
bugs: {
|
|
21
|
+
url: "https://github.com/sovereignfs/sovereign/issues"
|
|
22
|
+
},
|
|
23
|
+
bin: {
|
|
24
|
+
create: "./dist/create.mjs"
|
|
25
|
+
},
|
|
26
|
+
files: [
|
|
27
|
+
"dist",
|
|
28
|
+
"template"
|
|
29
|
+
],
|
|
30
|
+
engines: {
|
|
31
|
+
node: ">=24.15"
|
|
32
|
+
},
|
|
33
|
+
scripts: {
|
|
34
|
+
build: "tsup",
|
|
35
|
+
typecheck: "tsc --noEmit"
|
|
36
|
+
},
|
|
37
|
+
devDependencies: {
|
|
38
|
+
"@sovereignfs/tsconfig": "workspace:*",
|
|
39
|
+
"@types/node": "catalog:",
|
|
40
|
+
ajv: "^8.20.0",
|
|
41
|
+
"ajv-formats": "^3.0.1",
|
|
42
|
+
semver: "^7.8.5",
|
|
43
|
+
"@types/semver": "^7.7.1",
|
|
44
|
+
tsup: "catalog:",
|
|
45
|
+
typescript: "catalog:"
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/app.ts
|
|
50
|
+
import { mkdir as mkdir2, readFile, readdir as readdir2, writeFile as writeFile2 } from "fs/promises";
|
|
51
|
+
import path2 from "path";
|
|
52
|
+
import { fileURLToPath } from "url";
|
|
53
|
+
|
|
54
|
+
// src/create.ts
|
|
55
|
+
import { chmod, mkdir, readdir, writeFile } from "fs/promises";
|
|
56
|
+
import path from "path";
|
|
57
|
+
|
|
58
|
+
// src/templates.ts
|
|
59
|
+
var LAUNCHER = `#!/bin/sh
|
|
60
|
+
# Runs the toolkit this repository pins (@sovereignfs/cli in package.json), and nothing else.
|
|
61
|
+
# It never uses npx, which can fetch an unrelated package of the same name from the registry.
|
|
62
|
+
set -eu
|
|
63
|
+
|
|
64
|
+
here=$(cd "$(dirname "$0")" && pwd -P)
|
|
65
|
+
|
|
66
|
+
if ! command -v node >/dev/null 2>&1; then
|
|
67
|
+
echo "sv needs Node 24 or newer, and node was not found. Install it from https://nodejs.org." >&2
|
|
68
|
+
exit 2
|
|
69
|
+
fi
|
|
70
|
+
major=$(node -p 'process.versions.node.split(".")[0]')
|
|
71
|
+
if [ "$major" -lt 24 ]; then
|
|
72
|
+
echo "sv needs Node 24 or newer, and this is Node $(node -v). Install a newer one from https://nodejs.org." >&2
|
|
73
|
+
exit 2
|
|
74
|
+
fi
|
|
75
|
+
|
|
76
|
+
cli="$here/node_modules/@sovereignfs/cli/dist/sv.mjs"
|
|
77
|
+
if [ ! -f "$cli" ]; then
|
|
78
|
+
echo "Installing the toolkit this repository pins (npm ci)..." >&2
|
|
79
|
+
if ! (cd "$here" && npm ci --ignore-scripts --no-audit --no-fund >&2); then
|
|
80
|
+
echo "npm ci failed. Check your network, then run ./sv again." >&2
|
|
81
|
+
exit 1
|
|
82
|
+
fi
|
|
83
|
+
fi
|
|
84
|
+
|
|
85
|
+
SV_ROOT="$here" exec node "$cli" "$@"
|
|
86
|
+
`;
|
|
87
|
+
var GITIGNORE = `node_modules/
|
|
88
|
+
.DS_Store
|
|
89
|
+
# Keys never belong here. The operator's key is kept outside the repository (~/.config/sovereign/age.key).
|
|
90
|
+
*.key
|
|
91
|
+
age.key
|
|
92
|
+
.env
|
|
93
|
+
.env.*
|
|
94
|
+
`;
|
|
95
|
+
function readme(name) {
|
|
96
|
+
return `# ${name}
|
|
97
|
+
|
|
98
|
+
A Sovereign instance. Everything that decides what runs is in this repository; everything the server makes for itself (databases,
|
|
99
|
+
generated secrets) is on the server, and in your backups.
|
|
100
|
+
|
|
101
|
+
\`\`\`bash
|
|
102
|
+
./sv setup # the domain, the server, how to deploy
|
|
103
|
+
./sv server bootstrap # prepare a fresh machine
|
|
104
|
+
./sv push # check, commit and push
|
|
105
|
+
./sv deploy # put it on the server
|
|
106
|
+
\`\`\`
|
|
107
|
+
|
|
108
|
+
\`./sv\` runs the toolkit pinned in \`package.json\`. To move to a newer release: \`./sv upgrade\`, then review the change.
|
|
109
|
+
`;
|
|
110
|
+
}
|
|
111
|
+
function packageJson(name, cliVersion) {
|
|
112
|
+
return `${JSON.stringify(
|
|
113
|
+
{
|
|
114
|
+
name,
|
|
115
|
+
version: "0.0.0",
|
|
116
|
+
private: true,
|
|
117
|
+
description: "A Sovereign instance. Run ./sv.",
|
|
118
|
+
engines: { node: ">=24.15" },
|
|
119
|
+
dependencies: { "@sovereignfs/cli": cliVersion }
|
|
120
|
+
},
|
|
121
|
+
null,
|
|
122
|
+
2
|
|
123
|
+
)}
|
|
124
|
+
`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/create.ts
|
|
128
|
+
var CreateError = class extends Error {
|
|
129
|
+
constructor(message, exitCode, hint) {
|
|
130
|
+
super(message);
|
|
131
|
+
this.exitCode = exitCode;
|
|
132
|
+
this.hint = hint;
|
|
133
|
+
}
|
|
134
|
+
exitCode;
|
|
135
|
+
hint;
|
|
136
|
+
name = "CreateError";
|
|
137
|
+
};
|
|
138
|
+
function readRequest(argv, env) {
|
|
139
|
+
let kind;
|
|
140
|
+
let remote;
|
|
141
|
+
const words = [];
|
|
142
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
143
|
+
const word = argv[i];
|
|
144
|
+
if (word === "--instance") kind = "instance";
|
|
145
|
+
else if (word === "--app") kind = "app";
|
|
146
|
+
else if (word === "--remote") remote = argv[++i];
|
|
147
|
+
else if (word.startsWith("--remote=")) remote = word.slice("--remote=".length);
|
|
148
|
+
else if (word.startsWith("--"))
|
|
149
|
+
throw new CreateError(
|
|
150
|
+
`There is no option ${word}.`,
|
|
151
|
+
2,
|
|
152
|
+
"Use: npm create @sovereignfs instance <folder> [--remote URL]"
|
|
153
|
+
);
|
|
154
|
+
else words.push(word);
|
|
155
|
+
}
|
|
156
|
+
if (kind === void 0 && (words[0] === "instance" || words[0] === "app"))
|
|
157
|
+
kind = words.shift();
|
|
158
|
+
if (kind === void 0) {
|
|
159
|
+
const set = (value) => value !== void 0 && value !== "" && value !== "false";
|
|
160
|
+
if (set(env.npm_config_instance)) kind = "instance";
|
|
161
|
+
else if (set(env.npm_config_app)) kind = "app";
|
|
162
|
+
}
|
|
163
|
+
if (words.length > 1)
|
|
164
|
+
throw new CreateError(
|
|
165
|
+
`Too many arguments: ${words.slice(1).join(" ")}.`,
|
|
166
|
+
2,
|
|
167
|
+
"Use: npm create @sovereignfs instance <folder>"
|
|
168
|
+
);
|
|
169
|
+
return { kind, dir: words[0], remote };
|
|
170
|
+
}
|
|
171
|
+
async function checkEmpty(dir) {
|
|
172
|
+
try {
|
|
173
|
+
if ((await readdir(dir)).length > 0) {
|
|
174
|
+
throw new CreateError(
|
|
175
|
+
`${dir} is not empty.`,
|
|
176
|
+
2,
|
|
177
|
+
"Name a folder that does not exist yet, or is empty. Nothing was written."
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
if (error.code !== "ENOENT") throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async function createInstance(request, io2, exec2, cwd) {
|
|
185
|
+
const target = path.resolve(cwd, request.dir);
|
|
186
|
+
await checkEmpty(target);
|
|
187
|
+
const name = path.basename(target).toLowerCase().replace(/[^a-z0-9-_.]+/g, "-").replace(/^[-_.]+/, "") || "instance";
|
|
188
|
+
await mkdir(target, { recursive: true });
|
|
189
|
+
await writeFile(path.join(target, "package.json"), packageJson(name, request.cliVersion));
|
|
190
|
+
await writeFile(path.join(target, "sv"), LAUNCHER);
|
|
191
|
+
await chmod(path.join(target, "sv"), 493);
|
|
192
|
+
await writeFile(path.join(target, ".gitignore"), GITIGNORE);
|
|
193
|
+
await writeFile(path.join(target, "README.md"), readme(name));
|
|
194
|
+
io2.err(`Wrote the files of ${name}.`);
|
|
195
|
+
const step = async (label, command, args, hint) => {
|
|
196
|
+
const done = await exec2(command, args, { cwd: target });
|
|
197
|
+
if (done.code !== 0) {
|
|
198
|
+
throw new CreateError(
|
|
199
|
+
`${label} failed: ${(done.stderr || done.stdout).trim().split("\n").slice(-3).join(" ")}`,
|
|
200
|
+
1,
|
|
201
|
+
hint
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
await step(
|
|
206
|
+
"git init",
|
|
207
|
+
"git",
|
|
208
|
+
["init", "--initial-branch=main"],
|
|
209
|
+
"Is git installed? Then run the rest by hand: see README.md."
|
|
210
|
+
);
|
|
211
|
+
await step(
|
|
212
|
+
"Resolving the toolkit",
|
|
213
|
+
"npm",
|
|
214
|
+
["install", "--package-lock-only", "--ignore-scripts", "--no-audit", "--no-fund"],
|
|
215
|
+
`Check the network, then run npm install --package-lock-only in ${request.dir}, and ./sv.`
|
|
216
|
+
);
|
|
217
|
+
await step(
|
|
218
|
+
"Installing the toolkit",
|
|
219
|
+
"npm",
|
|
220
|
+
["ci", "--ignore-scripts", "--no-audit", "--no-fund"],
|
|
221
|
+
`Run ./sv in ${request.dir}: it installs the toolkit if it is missing.`
|
|
222
|
+
);
|
|
223
|
+
if (request.remote) {
|
|
224
|
+
await step(
|
|
225
|
+
"git remote add",
|
|
226
|
+
"git",
|
|
227
|
+
["remote", "add", "origin", request.remote],
|
|
228
|
+
"Add it later with git remote add origin <url>."
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
await step(
|
|
232
|
+
"git add",
|
|
233
|
+
"git",
|
|
234
|
+
["add", "--", "package.json", "package-lock.json", "sv", ".gitignore", "README.md"],
|
|
235
|
+
""
|
|
236
|
+
);
|
|
237
|
+
await step(
|
|
238
|
+
"The first commit",
|
|
239
|
+
"git",
|
|
240
|
+
["commit", "-m", "Create the instance"],
|
|
241
|
+
"git needs a name and an email: git config --global user.name ... and user.email ..., then commit by hand."
|
|
242
|
+
);
|
|
243
|
+
io2.err(`Made ${name}. Next:
|
|
244
|
+
cd ${request.dir}
|
|
245
|
+
./sv setup`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/versions.ts
|
|
249
|
+
var APP_DEPENDENCIES = {
|
|
250
|
+
// `@sovereignfs/ui` is one bundle that imports these two, which it lists as optional peers: an app has to have them for the bundle to load.
|
|
251
|
+
"@dnd-kit/core": "^6.3.1",
|
|
252
|
+
"@dnd-kit/sortable": "^10.0.0",
|
|
253
|
+
"@sovereignfs/sdk": "^2.0.0",
|
|
254
|
+
"@sovereignfs/ui": "^0.89.1",
|
|
255
|
+
next: "^16.4.3",
|
|
256
|
+
pg: "^8.23.1",
|
|
257
|
+
react: "^19.3.1",
|
|
258
|
+
"react-dom": "^19.3.1"
|
|
259
|
+
};
|
|
260
|
+
var APP_DEV_DEPENDENCIES = {
|
|
261
|
+
"@types/node": "^24.19.2",
|
|
262
|
+
"@types/pg": "^8.23.1",
|
|
263
|
+
"@types/react": "^19.3.0",
|
|
264
|
+
"@types/react-dom": "^19.3.0",
|
|
265
|
+
typescript: "~6.0.3"
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// src/app.ts
|
|
269
|
+
var TEMPLATE = fileURLToPath(new URL("../template/app", import.meta.url));
|
|
270
|
+
var NAME = /^[a-z][a-z0-9-]{0,30}[a-z0-9]$/;
|
|
271
|
+
var RENAMED = {
|
|
272
|
+
_gitignore: ".gitignore",
|
|
273
|
+
_dockerignore: ".dockerignore",
|
|
274
|
+
"_env.example": ".env.example",
|
|
275
|
+
// A published package may not carry a file called tsconfig.json.
|
|
276
|
+
"_tsconfig.json": "tsconfig.json"
|
|
277
|
+
};
|
|
278
|
+
function namesOf(dir) {
|
|
279
|
+
const name = path2.basename(path2.resolve(dir)).toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
280
|
+
if (!NAME.test(name)) {
|
|
281
|
+
throw new CreateError(
|
|
282
|
+
`\u201C${path2.basename(dir)}\u201D is not a name an app can have.`,
|
|
283
|
+
2,
|
|
284
|
+
"Use 2 to 32 lower-case letters, digits and hyphens, starting with a letter. Nothing was written."
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
const prefix = name.replace(/-/g, "").toUpperCase();
|
|
288
|
+
if (prefix === "SV") {
|
|
289
|
+
throw new CreateError(
|
|
290
|
+
"SV is core\u2019s own prefix, so no app can be called sv.",
|
|
291
|
+
2,
|
|
292
|
+
"Choose another name."
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
const title = name.split("-").map((word) => word[0]?.toUpperCase() + word.slice(1)).join(" ");
|
|
296
|
+
return { name, prefix, title };
|
|
297
|
+
}
|
|
298
|
+
async function* walk(dir, base = dir) {
|
|
299
|
+
for (const entry of await readdir2(dir, { withFileTypes: true })) {
|
|
300
|
+
const full = path2.join(dir, entry.name);
|
|
301
|
+
if (entry.isDirectory()) yield* walk(full, base);
|
|
302
|
+
else yield path2.relative(base, full);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function appPackageJson(names) {
|
|
306
|
+
return `${JSON.stringify(
|
|
307
|
+
{
|
|
308
|
+
name: names.name,
|
|
309
|
+
version: "0.1.0",
|
|
310
|
+
private: true,
|
|
311
|
+
description: `${names.title}, an app for a Sovereign instance`,
|
|
312
|
+
engines: { node: ">=24.15" },
|
|
313
|
+
scripts: {
|
|
314
|
+
dev: "next dev",
|
|
315
|
+
build: "next build",
|
|
316
|
+
start: "node .next/standalone/server.js",
|
|
317
|
+
typecheck: "tsc --noEmit",
|
|
318
|
+
"manifest:check": "node scripts/manifest.mjs",
|
|
319
|
+
"manifest:write": "node scripts/manifest.mjs --write"
|
|
320
|
+
},
|
|
321
|
+
dependencies: APP_DEPENDENCIES,
|
|
322
|
+
devDependencies: APP_DEV_DEPENDENCIES
|
|
323
|
+
},
|
|
324
|
+
null,
|
|
325
|
+
2
|
|
326
|
+
)}
|
|
327
|
+
`;
|
|
328
|
+
}
|
|
329
|
+
async function writeApp(target, names) {
|
|
330
|
+
await checkEmpty(target);
|
|
331
|
+
const written = [];
|
|
332
|
+
const replace = (text, label2) => text.replaceAll("__PREFIX__", names.prefix).replaceAll("__NAME__", names.name).replaceAll("__TITLE__", names.title).replaceAll("__MANIFEST_LABEL__", label2 ?? "");
|
|
333
|
+
const manifestText = replace(
|
|
334
|
+
await readFile(path2.join(TEMPLATE, "sovereign.manifest.json"), "utf8")
|
|
335
|
+
);
|
|
336
|
+
const label = JSON.stringify(JSON.stringify(JSON.parse(manifestText)));
|
|
337
|
+
for await (const file of walk(TEMPLATE)) {
|
|
338
|
+
const parts = file.split(path2.sep);
|
|
339
|
+
const last = parts.pop();
|
|
340
|
+
const out = path2.join(...parts, RENAMED[last] ?? last);
|
|
341
|
+
const target_ = path2.join(target, out);
|
|
342
|
+
await mkdir2(path2.dirname(target_), { recursive: true });
|
|
343
|
+
await writeFile2(target_, replace(await readFile(path2.join(TEMPLATE, file), "utf8"), label));
|
|
344
|
+
written.push(out);
|
|
345
|
+
}
|
|
346
|
+
await writeFile2(path2.join(target, "package.json"), appPackageJson(names));
|
|
347
|
+
written.push("package.json");
|
|
348
|
+
return written.sort();
|
|
349
|
+
}
|
|
350
|
+
async function createApp(request, io2, exec2, cwd) {
|
|
351
|
+
const target = path2.resolve(cwd, request.dir);
|
|
352
|
+
const names = namesOf(target);
|
|
353
|
+
const written = await writeApp(target, names);
|
|
354
|
+
io2.err(`Wrote the files of ${names.name}.`);
|
|
355
|
+
const step = async (label, command, args, hint) => {
|
|
356
|
+
const done = await exec2(command, args, { cwd: target });
|
|
357
|
+
if (done.code !== 0) {
|
|
358
|
+
throw new CreateError(
|
|
359
|
+
`${label} failed: ${(done.stderr || done.stdout).trim().split("\n").slice(-3).join(" ")}`,
|
|
360
|
+
1,
|
|
361
|
+
hint
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
await step(
|
|
366
|
+
"git init",
|
|
367
|
+
"git",
|
|
368
|
+
["init", "--initial-branch=main"],
|
|
369
|
+
"Is git installed? Then run the rest by hand: see README.md."
|
|
370
|
+
);
|
|
371
|
+
await step(
|
|
372
|
+
"Resolving the packages",
|
|
373
|
+
"npm",
|
|
374
|
+
["install", "--package-lock-only", "--ignore-scripts", "--no-audit", "--no-fund"],
|
|
375
|
+
`Check the network, then run npm install in ${request.dir}.`
|
|
376
|
+
);
|
|
377
|
+
await step("git add", "git", ["add", "--", ...written, "package-lock.json"], "");
|
|
378
|
+
await step(
|
|
379
|
+
"The first commit",
|
|
380
|
+
"git",
|
|
381
|
+
["commit", "-m", "Create the app"],
|
|
382
|
+
"git needs a name and an email: git config --global user.name ... and user.email ..., then commit by hand."
|
|
383
|
+
);
|
|
384
|
+
io2.err(
|
|
385
|
+
`Made ${names.name}. Next:
|
|
386
|
+
cd ${request.dir}
|
|
387
|
+
npm install && npm run build
|
|
388
|
+
docker build -t ${names.name} .
|
|
389
|
+
Then host it with ./sv app add ${names.name} --image <the image> in your instance. See README.md.`
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/main.ts
|
|
394
|
+
var exec = (command, args, options) => new Promise((resolve) => {
|
|
395
|
+
execFile(
|
|
396
|
+
command,
|
|
397
|
+
[...args],
|
|
398
|
+
{ cwd: options.cwd, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 },
|
|
399
|
+
(error, stdout, stderr) => {
|
|
400
|
+
resolve({
|
|
401
|
+
code: error ? typeof error.code === "number" ? error.code : 1 : 0,
|
|
402
|
+
stdout,
|
|
403
|
+
stderr: error && typeof error.code === "string" ? error.message : stderr
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
);
|
|
407
|
+
});
|
|
408
|
+
var io = {
|
|
409
|
+
err: (line) => process.stderr.write(`${line}
|
|
410
|
+
`),
|
|
411
|
+
interactive: Boolean(process.stdin.isTTY && process.stderr.isTTY),
|
|
412
|
+
async ask(question) {
|
|
413
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
414
|
+
try {
|
|
415
|
+
return (await rl.question(`${question} `)).trim();
|
|
416
|
+
} finally {
|
|
417
|
+
rl.close();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
async function main() {
|
|
422
|
+
try {
|
|
423
|
+
const request = readRequest(process.argv.slice(2), process.env);
|
|
424
|
+
let { kind, dir } = request;
|
|
425
|
+
if (kind === void 0) {
|
|
426
|
+
if (!io.interactive)
|
|
427
|
+
throw new CreateError(
|
|
428
|
+
"Say what to make.",
|
|
429
|
+
2,
|
|
430
|
+
"npm create @sovereignfs instance <folder>, or app <folder>"
|
|
431
|
+
);
|
|
432
|
+
kind = await io.ask("What do you want to make (instance or app)?") === "app" ? "app" : "instance";
|
|
433
|
+
}
|
|
434
|
+
if (dir === void 0) {
|
|
435
|
+
if (!io.interactive)
|
|
436
|
+
throw new CreateError(
|
|
437
|
+
"Name the folder to make.",
|
|
438
|
+
2,
|
|
439
|
+
"npm create @sovereignfs instance <folder>"
|
|
440
|
+
);
|
|
441
|
+
dir = await io.ask("In which folder?");
|
|
442
|
+
if (dir === "") throw new CreateError("No folder was named.", 2);
|
|
443
|
+
}
|
|
444
|
+
if (kind === "app") {
|
|
445
|
+
if (request.remote)
|
|
446
|
+
throw new CreateError(
|
|
447
|
+
"--remote is for an instance.",
|
|
448
|
+
2,
|
|
449
|
+
"Add a remote to an app with git remote add."
|
|
450
|
+
);
|
|
451
|
+
await createApp({ dir }, io, exec, process.cwd());
|
|
452
|
+
} else {
|
|
453
|
+
await createInstance(
|
|
454
|
+
{ dir, remote: request.remote, cliVersion: package_default.version },
|
|
455
|
+
io,
|
|
456
|
+
exec,
|
|
457
|
+
process.cwd()
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
return 0;
|
|
461
|
+
} catch (error) {
|
|
462
|
+
if (error instanceof CreateError) {
|
|
463
|
+
io.err(error.message);
|
|
464
|
+
if (error.hint) io.err(error.hint);
|
|
465
|
+
return error.exitCode;
|
|
466
|
+
}
|
|
467
|
+
io.err(`create failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
468
|
+
return 1;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
main().then((code) => {
|
|
472
|
+
process.exitCode = code;
|
|
473
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sovereignfs/create",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "npm create @sovereignfs: makes an instance repository, pinned to the toolkit (sv) of the same version.",
|
|
6
|
+
"license": "AGPL-3.0-or-later",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/sovereignfs/sovereign.git",
|
|
10
|
+
"directory": "tools/create"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/sovereignfs/sovereign/tree/main/tools/create#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/sovereignfs/sovereign/issues"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"create": "./dist/create.mjs"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"template"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=24.15"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@sovereignfs/tsconfig": "0.0.0",
|
|
28
|
+
"@types/node": "^24.19.2",
|
|
29
|
+
"ajv": "^8.20.0",
|
|
30
|
+
"ajv-formats": "^3.0.1",
|
|
31
|
+
"semver": "^7.8.5",
|
|
32
|
+
"@types/semver": "^7.7.1",
|
|
33
|
+
"tsup": "^9.0.0",
|
|
34
|
+
"typescript": "~6.0.3"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup",
|
|
38
|
+
"typecheck": "tsc --noEmit"
|
|
39
|
+
}
|
|
6
40
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# This app as one image (spec/app-manifest.md §4): a process that is not root, configured by its environment alone, listening on
|
|
2
|
+
# 3000, that applies its own migrations when it starts. `docker build -t __NAME__ .`
|
|
3
|
+
|
|
4
|
+
ARG NODE_VERSION=24
|
|
5
|
+
|
|
6
|
+
FROM node:${NODE_VERSION}-alpine AS build
|
|
7
|
+
WORKDIR /app
|
|
8
|
+
COPY package.json package-lock.json ./
|
|
9
|
+
RUN npm ci --ignore-scripts --no-audit --no-fund
|
|
10
|
+
COPY . .
|
|
11
|
+
ENV NEXT_TELEMETRY_DISABLED=1
|
|
12
|
+
RUN npm run build
|
|
13
|
+
|
|
14
|
+
FROM node:${NODE_VERSION}-alpine AS run
|
|
15
|
+
ENV NODE_ENV=production \
|
|
16
|
+
NEXT_TELEMETRY_DISABLED=1 \
|
|
17
|
+
PORT=3000 \
|
|
18
|
+
HOSTNAME=0.0.0.0
|
|
19
|
+
WORKDIR /app
|
|
20
|
+
COPY --from=build --chown=node:node /app/.next/standalone ./
|
|
21
|
+
COPY --from=build --chown=node:node /app/.next/static ./.next/static
|
|
22
|
+
# The migrations are read from disk when the server starts, and Next does not trace them.
|
|
23
|
+
COPY --from=build --chown=node:node /app/migrations ./migrations
|
|
24
|
+
USER node
|
|
25
|
+
EXPOSE 3000
|
|
26
|
+
CMD ["node", "server.js"]
|
|
27
|
+
|
|
28
|
+
# What the instance reads to host this app, without pulling the image. Keep it equal to sovereign.manifest.json: `npm run manifest:check`, or `npm run manifest:write` to copy it over.
|
|
29
|
+
LABEL io.github.sovereignfs.app-manifest=__MANIFEST_LABEL__
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# **TITLE**
|
|
2
|
+
|
|
3
|
+
An app for a [Sovereign](https://github.com/sovereignfs/sovereign) instance, made with `npm create @sovereignfs app`. It signs people
|
|
4
|
+
in through the instance's core, keeps its own data in its own PostgreSQL database, and can be hosted by the instance's toolkit as it
|
|
5
|
+
stands. It is a starting point: a notes page, to show the pieces. Change it into your app.
|
|
6
|
+
|
|
7
|
+
## What is here
|
|
8
|
+
|
|
9
|
+
| Where | What |
|
|
10
|
+
| ----------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
11
|
+
| `sovereign.manifest.json` | What the instance needs to know to host it: its address paths, the scopes it asks for, its port and health path |
|
|
12
|
+
| `Dockerfile` | One image, not root, on port 3000. Its `io.github.sovereignfs.app-manifest` label is the manifest |
|
|
13
|
+
| `app/auth/[action]/route.ts` | Sign-in, sign-out, core's back-channel logout, and core's notice that a person deleted their account |
|
|
14
|
+
| `lib/auth.ts`, `lib/stores.ts` | The SDK's sign-in, and where it keeps sessions and refresh tokens (your database; tokens encrypted) |
|
|
15
|
+
| `app/api/health/route.ts` | Answers 200 when the database does |
|
|
16
|
+
| `instrumentation.ts`, `lib/migrate.ts`, `migrations/` | The app applies its own migrations when it starts, each once |
|
|
17
|
+
| `app/page.tsx` | A page behind sign-in, using [`@sovereignfs/ui`](https://www.npmjs.com/package/@sovereignfs/ui) |
|
|
18
|
+
|
|
19
|
+
## Build and host it
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
docker build -t ghcr.io/you/__NAME__:0.1.0 .
|
|
23
|
+
docker push ghcr.io/you/__NAME__:0.1.0
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
then, in your instance repository:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
./sv app add __NAME__ --image ghcr.io/you/__NAME__:0.1.0
|
|
30
|
+
./sv push && ./sv deploy
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`sv` reads the manifest from the image's label, shows you the scopes it asks for, gives it its address, its database and its secrets,
|
|
34
|
+
and registers it with core. Change `sovereign.manifest.json`, then `npm run manifest:write` to copy it into the Dockerfile, and
|
|
35
|
+
build again; `npm run manifest:check` says whether they agree.
|
|
36
|
+
|
|
37
|
+
Two things to know. A person is identified by `session.sub` and by nothing else. And when you add data about people, delete it in
|
|
38
|
+
`onAccountDeleted` in `lib/auth.ts`, which core calls when someone deletes their account.
|
|
39
|
+
|
|
40
|
+
## Run it on your machine
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
cp .env.example .env # point it at a core you can reach and a PostgreSQL database
|
|
44
|
+
npm run dev
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
See [Using the SDK](https://github.com/sovereignfs/sovereign/blob/main/docs/using-the-sdk.md) for what `sv.as(person)` can call, and
|
|
48
|
+
[the app manifest](https://github.com/sovereignfs/sovereign/blob/main/spec/app-manifest.md) for what the instance gives and expects.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# What the instance gives this app (spec/app-manifest.md §3), for running it on your own machine against a core you can reach.
|
|
2
|
+
# A secret may be given as NAME_FILE=/path instead of NAME=value.
|
|
3
|
+
__PREFIX___URL=http://localhost:3000
|
|
4
|
+
__PREFIX___CORE_URL=https://core.example.test
|
|
5
|
+
__PREFIX___CORE_CLIENT_ID=
|
|
6
|
+
__PREFIX___CORE_CLIENT_SECRET=
|
|
7
|
+
__PREFIX___DB_DIALECT=postgres
|
|
8
|
+
__PREFIX___POSTGRES_URL=postgres://user:password@localhost:5432/__NAME__
|
|
9
|
+
__PREFIX___AUTH_SECRET=change-me-to-something-long-and-random
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
5
|
+
"allowJs": false,
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noEmit": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"module": "esnext",
|
|
11
|
+
"moduleResolution": "bundler",
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"jsx": "preserve",
|
|
15
|
+
"incremental": true,
|
|
16
|
+
"plugins": [{ "name": "next" }],
|
|
17
|
+
"paths": { "@/*": ["./*"] }
|
|
18
|
+
},
|
|
19
|
+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
|
20
|
+
"exclude": ["node_modules"]
|
|
21
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use server';
|
|
2
|
+
|
|
3
|
+
import { revalidatePath } from 'next/cache';
|
|
4
|
+
import { auth } from '@/lib/auth';
|
|
5
|
+
import { db } from '@/lib/db';
|
|
6
|
+
|
|
7
|
+
/** Every server action is a public POST endpoint, so each one asks who is calling before it does anything. */
|
|
8
|
+
export async function addNote(formData: FormData): Promise<void> {
|
|
9
|
+
const session = await auth().requireSession('/');
|
|
10
|
+
const body = String(formData.get('body') ?? '')
|
|
11
|
+
.trim()
|
|
12
|
+
.slice(0, 2000);
|
|
13
|
+
if (body === '') return;
|
|
14
|
+
await db().query('INSERT INTO notes (owner, body) VALUES ($1, $2)', [session.sub, body]);
|
|
15
|
+
revalidatePath('/');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function deleteNote(formData: FormData): Promise<void> {
|
|
19
|
+
const session = await auth().requireSession('/');
|
|
20
|
+
const id = Number(formData.get('id'));
|
|
21
|
+
if (!Number.isSafeInteger(id)) return;
|
|
22
|
+
// Only the person's own: the owner is part of the condition and not a check made before it.
|
|
23
|
+
await db().query('DELETE FROM notes WHERE id = $1 AND owner = $2', [id, session.sub]);
|
|
24
|
+
revalidatePath('/');
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { db } from '@/lib/db';
|
|
2
|
+
|
|
3
|
+
export const dynamic = 'force-dynamic';
|
|
4
|
+
|
|
5
|
+
/** The instance asks this before it sends anyone to the app (the manifest's `health`). It answers 200 once the database does. */
|
|
6
|
+
export async function GET() {
|
|
7
|
+
try {
|
|
8
|
+
await db().query('SELECT 1');
|
|
9
|
+
return new Response('ok', { headers: { 'cache-control': 'no-store' } });
|
|
10
|
+
} catch {
|
|
11
|
+
return new Response('database unavailable', {
|
|
12
|
+
status: 503,
|
|
13
|
+
headers: { 'cache-control': 'no-store' },
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|