@sovereignfs/create 0.5.0 → 0.6.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/dist/create.mjs CHANGED
@@ -7,7 +7,7 @@ import { createInterface } from "readline/promises";
7
7
  // package.json
8
8
  var package_default = {
9
9
  name: "@sovereignfs/create",
10
- version: "0.5.0",
10
+ version: "0.6.0",
11
11
  type: "module",
12
12
  description: "npm create @sovereignfs: makes an instance repository, pinned to the toolkit (sv) of the same version.",
13
13
  license: "AGPL-3.0-or-later",
@@ -135,9 +135,22 @@ var CreateError = class extends Error {
135
135
  hint;
136
136
  name = "CreateError";
137
137
  };
138
+ var DEFAULT_APP_PORT = 3100;
139
+ function parsePort(value) {
140
+ const port = /^[0-9]+$/.test(value) ? Number(value) : Number.NaN;
141
+ if (!Number.isInteger(port) || port < 1024 || port > 65535) {
142
+ throw new CreateError(
143
+ `${value || "(nothing)"} is not a port an app can listen on.`,
144
+ 2,
145
+ "Use a whole number from 1024 to 65535: the image does not run as root, so it cannot bind a lower one."
146
+ );
147
+ }
148
+ return port;
149
+ }
138
150
  function readRequest(argv, env) {
139
151
  let kind;
140
152
  let remote;
153
+ let port;
141
154
  const words = [];
142
155
  for (let i = 0; i < argv.length; i += 1) {
143
156
  const word = argv[i];
@@ -145,11 +158,13 @@ function readRequest(argv, env) {
145
158
  else if (word === "--app") kind = "app";
146
159
  else if (word === "--remote") remote = argv[++i];
147
160
  else if (word.startsWith("--remote=")) remote = word.slice("--remote=".length);
161
+ else if (word === "--port") port = argv[++i] ?? "";
162
+ else if (word.startsWith("--port=")) port = word.slice("--port=".length);
148
163
  else if (word.startsWith("--"))
149
164
  throw new CreateError(
150
165
  `There is no option ${word}.`,
151
166
  2,
152
- "Use: npm create @sovereignfs instance <folder> [--remote URL]"
167
+ "Use: npm create @sovereignfs instance <folder> [--remote URL], or app <folder> [--port N]"
153
168
  );
154
169
  else words.push(word);
155
170
  }
@@ -166,7 +181,8 @@ function readRequest(argv, env) {
166
181
  2,
167
182
  "Use: npm create @sovereignfs instance <folder>"
168
183
  );
169
- return { kind, dir: words[0], remote };
184
+ port ??= env.npm_config_port === void 0 || env.npm_config_port === "" ? void 0 : env.npm_config_port;
185
+ return { kind, dir: words[0], remote, port: port === void 0 ? void 0 : parsePort(port) };
170
186
  }
171
187
  async function checkEmpty(dir) {
172
188
  try {
@@ -251,7 +267,7 @@ var APP_DEPENDENCIES = {
251
267
  "@dnd-kit/core": "^6.3.1",
252
268
  "@dnd-kit/sortable": "^10.0.0",
253
269
  "@sovereignfs/sdk": "^2.0.0",
254
- "@sovereignfs/ui": "^0.89.1",
270
+ "@sovereignfs/ui": "^0.92.0",
255
271
  next: "^16.4.3",
256
272
  pg: "^8.23.1",
257
273
  react: "^19.3.1",
@@ -305,7 +321,7 @@ async function* walk(dir, base = dir) {
305
321
  else yield path2.relative(base, full);
306
322
  }
307
323
  }
308
- function appPackageJson(names) {
324
+ function appPackageJson(names, port = DEFAULT_APP_PORT) {
309
325
  return `${JSON.stringify(
310
326
  {
311
327
  name: names.name,
@@ -314,9 +330,10 @@ function appPackageJson(names) {
314
330
  description: `${names.title}, an app for a Sovereign instance`,
315
331
  engines: { node: ">=24.15" },
316
332
  scripts: {
317
- dev: "next dev",
333
+ // The manifest's port, so `npm run dev` and the image listen where the instance and .env.example say.
334
+ dev: `next dev --port ${port}`,
318
335
  build: "next build",
319
- start: "node .next/standalone/server.js",
336
+ start: `PORT=\${PORT:-${port}} node .next/standalone/server.js`,
320
337
  typecheck: "tsc --noEmit",
321
338
  "manifest:check": "sovereign-manifest check"
322
339
  },
@@ -328,26 +345,29 @@ function appPackageJson(names) {
328
345
  )}
329
346
  `;
330
347
  }
331
- async function writeApp(target, names) {
348
+ async function writeApp(target, names, port = DEFAULT_APP_PORT) {
332
349
  await checkEmpty(target);
333
350
  const written = [];
334
- const replace = (text) => text.replaceAll("__PREFIX_LOWER__", names.prefix.toLowerCase()).replaceAll("__PREFIX__", names.prefix).replaceAll("__NAME__", names.name).replaceAll("__TITLE__", names.title);
351
+ const replace = (text) => text.replaceAll("__PREFIX_LOWER__", names.prefix.toLowerCase()).replaceAll("__PREFIX__", names.prefix).replaceAll("__NAME__", names.name).replaceAll("__TITLE__", names.title).replaceAll("__PORT__", String(port));
335
352
  for await (const file of walk(TEMPLATE)) {
336
353
  const out = path2.join(...file.split(path2.sep).map((part) => RENAMED[part] ?? part));
337
354
  const target_ = path2.join(target, out);
338
355
  await mkdir2(path2.dirname(target_), { recursive: true });
339
- await writeFile2(target_, replace(await readFile(path2.join(TEMPLATE, file), "utf8")));
356
+ let text = replace(await readFile(path2.join(TEMPLATE, file), "utf8"));
357
+ if (out === "sovereign.manifest.json") text = text.replace(/"port": \d+/, `"port": ${port}`);
358
+ await writeFile2(target_, text);
340
359
  written.push(out);
341
360
  }
342
- await writeFile2(path2.join(target, "package.json"), appPackageJson(names));
361
+ await writeFile2(path2.join(target, "package.json"), appPackageJson(names, port));
343
362
  written.push("package.json");
344
363
  return written.sort();
345
364
  }
346
365
  async function createApp(request, io2, exec2, cwd) {
347
366
  const target = path2.resolve(cwd, request.dir);
348
367
  const names = namesOf(target);
349
- const written = await writeApp(target, names);
350
- io2.err(`Wrote the files of ${names.name}.`);
368
+ const port = request.port ?? DEFAULT_APP_PORT;
369
+ const written = await writeApp(target, names, port);
370
+ io2.err(`Wrote the files of ${names.name}, on port ${port}.`);
351
371
  const step = async (label, command, args, hint) => {
352
372
  const done = await exec2(command, args, { cwd: target });
353
373
  if (done.code !== 0) {
@@ -380,7 +400,8 @@ async function createApp(request, io2, exec2, cwd) {
380
400
  io2.err(
381
401
  `Made ${names.name}. Next:
382
402
  cd ${request.dir}
383
- npm install && npm run build
403
+ npm install && npm run dev (http://localhost:${port})
404
+ npm run build
384
405
  docker build -t ${names.name} .
385
406
  Then host it with ./sv app add ${names.name} --image <the image> in your instance. See README.md.`
386
407
  );
@@ -444,8 +465,19 @@ async function main() {
444
465
  2,
445
466
  "Add a remote to an app with git remote add."
446
467
  );
447
- await createApp({ dir }, io, exec, process.cwd());
468
+ let { port } = request;
469
+ if (port === void 0 && io.interactive) {
470
+ const answer = await io.ask(`Which port should it listen on? (${DEFAULT_APP_PORT})`);
471
+ port = answer === "" ? DEFAULT_APP_PORT : parsePort(answer);
472
+ }
473
+ await createApp({ dir, port }, io, exec, process.cwd());
448
474
  } else {
475
+ if (request.port !== void 0)
476
+ throw new CreateError(
477
+ "--port is for an app.",
478
+ 2,
479
+ "An instance\u2019s apps each listen on the port their manifest names."
480
+ );
449
481
  await createInstance(
450
482
  { dir, remote: request.remote, cliVersion: package_default.version },
451
483
  io,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sovereignfs/create",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "npm create @sovereignfs: makes an instance repository, pinned to the toolkit (sv) of the same version.",
6
6
  "license": "AGPL-3.0-or-later",
@@ -52,8 +52,12 @@ Two things to know. A person is identified by `session.sub` and by nothing else.
52
52
 
53
53
  ```bash
54
54
  cp .env.example .env # point it at a core you can reach and a PostgreSQL database
55
- npm run dev
55
+ npm run dev # http://localhost:__PORT__
56
56
  ```
57
57
 
58
+ The port is `port` in `sovereign.manifest.json`, the one copy: the image listens on it, and `npm run dev`, `npm start` and
59
+ `__PREFIX___URL` in `.env.example` were made with it. To change it, change the manifest, the `dev` and `start` scripts and
60
+ `__PREFIX___URL` together.
61
+
58
62
  See [Using the SDK](https://github.com/sovereignfs/sovereign/blob/main/docs/using-the-sdk.md) for what `sv.as(person)` can call, and
59
63
  [the app manifest](https://github.com/sovereignfs/sovereign/blob/main/contracts/app-manifest.md) for what the instance gives and expects.
@@ -1,6 +1,6 @@
1
1
  # What the instance gives this app (contracts/app-manifest.md §3), for running it on your own machine against a core you can reach.
2
2
  # A secret may be given as NAME_FILE=/path instead of NAME=value.
3
- __PREFIX___URL=http://localhost:3000
3
+ __PREFIX___URL=http://localhost:__PORT__
4
4
  __PREFIX___CORE_URL=https://core.sovereign.local
5
5
  __PREFIX___CORE_CLIENT_ID=
6
6
  __PREFIX___CORE_CLIENT_SECRET=
@@ -3,7 +3,7 @@
3
3
  "prefix": "__PREFIX_LOWER__",
4
4
  "name": "__TITLE__",
5
5
  "description": "Notes, as a starting point for an app on a Sovereign instance",
6
- "port": 3000,
6
+ "port": 3100,
7
7
  "health": "/api/health",
8
8
  "launch": "/",
9
9
  "signIn": {