@kodentuzo/spotcraft 0.6.0 → 0.6.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/README.md CHANGED
@@ -48,10 +48,14 @@ and `AUTH_SECRET`; `dev db reset` moves it to a schema.
48
48
 
49
49
  - **One per repository**, found from the folder you run it in: the nearest folder up with
50
50
  `.git` or `pnpm-workspace.yaml`. Every deployable in a monorepo shares it, so their tables can
51
- point at the same local users. It is the Compose project `spotcraft-dev-<repo folder>`, with
52
- its data in its own volume.
53
- - **Ports** are picked from the free ones on the first run and kept in `.spotcraft/dev.json`
54
- at the repository's root, so `.env` stays valid. `.spotcraft/` ignores itself in git.
51
+ point at the same local users. It is the Compose project
52
+ `spotcraft-dev-<repo folder>-<hash>`, with its data in its own volume. The hash is 6 hex
53
+ characters of the root's absolute path, so two checkouts or worktrees with the same folder
54
+ name get their own stacks. `dev status` prints the name; `dev ls` lists every stack without
55
+ the `spotcraft-dev-` prefix.
56
+ - **Ports and the name** are picked on the first run and kept in `.spotcraft/dev.json` at the
57
+ repository's root, so `.env` stays valid. A stack made by 0.6.0 keeps its name,
58
+ `spotcraft-dev-<repo folder>`. `.spotcraft/` ignores itself in git.
55
59
  - **Postgres:** the server's pinned image and login rules, the same roles and grants (the deploy
56
60
  script's SQL), and the same provisioning code as the agent (`@kodentuzo/spotcraft/provision`).
57
61
  Every app has its own schema and role in the `apps` database. Tests fail if the copies of the
@@ -66,7 +70,7 @@ and `AUTH_SECRET`; `dev db reset` moves it to a schema.
66
70
 
67
71
  Everything listens on `127.0.0.1` only. The passwords are fixed (`local-<name>-password` for an
68
72
  app), which is fine for throwaway data on your own machine and nowhere else. To remove a stack
69
- and its data: `docker compose -p spotcraft-dev-<repo> down -v`.
73
+ and its data: `docker compose -p <name from dev status> down -v`.
70
74
 
71
75
  ## Developing
72
76
 
package/dist/cli.js CHANGED
@@ -122,8 +122,9 @@ export async function main(argv) {
122
122
  const stacks = await listStacks();
123
123
  if (stacks.length === 0)
124
124
  print('No local stacks.');
125
+ const width = Math.max(...stacks.map(({ name }) => name.length));
125
126
  for (const { name, status } of stacks)
126
- print(`${name.padEnd(30)} ${status}`);
127
+ print(`${name.padEnd(width)} ${status}`);
127
128
  return 0;
128
129
  }
129
130
  case 'status': {
package/dist/stack.d.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  /**
2
2
  * The local stack of one repository (ADR 0037): Postgres set up like the Spotcraft server's,
3
- * the user pool, and Mailpit to catch email, as the Compose project `spotcraft-dev-<repo>`.
4
- * Tests keep the values below equal to the server's files.
3
+ * the user pool, and Mailpit to catch email, as the Compose project
4
+ * `spotcraft-dev-<repo>-<hash>`. Tests keep the values below equal to the server's files.
5
5
  */
6
6
  export declare const PROJECT_PREFIX = "spotcraft-dev-";
7
+ /**
8
+ * A new stack's Compose project: the repository folder's slug and 6 hex characters of a hash of
9
+ * its absolute path, so two checkouts with the same folder name get their own stacks.
10
+ */
11
+ export declare function projectName(root: string, slug: string): string;
7
12
  /** The server's Postgres image (infra/server/compose.yaml). */
8
13
  export declare const POSTGRES_IMAGE = "postgres:18.7-trixie@sha256:2ea8630df176ad090ec92136dee850397c5af8781e4bcf2e4162593691c61bbc";
9
14
  /** Catches every email apps send (SMTP), with a web inbox. */
@@ -19,7 +24,7 @@ export declare const ADMIN_SQL = "SELECT NOT EXISTS (SELECT FROM pg_roles WHERE
19
24
  export declare const appPassword: (name: string) => string;
20
25
  /** A repository's stack: where it keeps its state, and the ports it was given. */
21
26
  export interface Stack {
22
- /** The Compose project, `spotcraft-dev-<repo>`. */
27
+ /** The Compose project, `spotcraft-dev-<repo>-<hash>` (or `spotcraft-dev-<repo>` from 0.6.0). */
23
28
  project: string;
24
29
  /** `.spotcraft/` in the repository's root. */
25
30
  stateDir: string;
@@ -35,8 +40,10 @@ export interface Stack {
35
40
  /** A free port on 127.0.0.1, as the system hands one out. */
36
41
  declare function freePort(): Promise<number>;
37
42
  /**
38
- * The repository's stack, from `.spotcraft/dev.json`, or a new one: free ports picked once and
39
- * kept, so URLs in `.env` stay the same. `.spotcraft/` ignores itself in git.
43
+ * The repository's stack, from `.spotcraft/dev.json`, or a new one: free ports and the project
44
+ * name picked once and kept, so URLs in `.env` stay the same. A `dev.json` from 0.6.0 has no
45
+ * project name; it keeps that version's `spotcraft-dev-<slug>`, now written down.
46
+ * `.spotcraft/` ignores itself in git.
40
47
  */
41
48
  export declare function loadStack(root: string, slug: string, ports?: typeof freePort): Promise<Stack>;
42
49
  export declare const adminUrl: (stack: Stack) => string;
package/dist/stack.js CHANGED
@@ -1,15 +1,25 @@
1
1
  import { spawn } from 'node:child_process';
2
- import { randomBytes } from 'node:crypto';
2
+ import { createHash, randomBytes } from 'node:crypto';
3
3
  import { mkdir, readFile, writeFile } from 'node:fs/promises';
4
4
  import { createServer } from 'node:net';
5
5
  import { join } from 'node:path';
6
6
  import { appRoleName, INSTANCE } from "./provision.js";
7
7
  /**
8
8
  * The local stack of one repository (ADR 0037): Postgres set up like the Spotcraft server's,
9
- * the user pool, and Mailpit to catch email, as the Compose project `spotcraft-dev-<repo>`.
10
- * Tests keep the values below equal to the server's files.
9
+ * the user pool, and Mailpit to catch email, as the Compose project
10
+ * `spotcraft-dev-<repo>-<hash>`. Tests keep the values below equal to the server's files.
11
11
  */
12
12
  export const PROJECT_PREFIX = 'spotcraft-dev-';
13
+ /**
14
+ * A new stack's Compose project: the repository folder's slug and 6 hex characters of a hash of
15
+ * its absolute path, so two checkouts with the same folder name get their own stacks.
16
+ */
17
+ export function projectName(root, slug) {
18
+ const hash = createHash('sha256').update(root).digest('hex').slice(0, 6);
19
+ return `${PROJECT_PREFIX}${slug}-${hash}`;
20
+ }
21
+ /** A name Compose accepts as a project name. */
22
+ const isProjectName = (name) => typeof name === 'string' && /^[a-z0-9][a-z0-9_-]*$/.test(name);
13
23
  /** The server's Postgres image (infra/server/compose.yaml). */
14
24
  export const POSTGRES_IMAGE = 'postgres:18.7-trixie@sha256:2ea8630df176ad090ec92136dee850397c5af8781e4bcf2e4162593691c61bbc';
15
25
  /** Catches every email apps send (SMTP), with a web inbox. */
@@ -82,17 +92,28 @@ function freePort() {
82
92
  });
83
93
  }
84
94
  /**
85
- * The repository's stack, from `.spotcraft/dev.json`, or a new one: free ports picked once and
86
- * kept, so URLs in `.env` stay the same. `.spotcraft/` ignores itself in git.
95
+ * The repository's stack, from `.spotcraft/dev.json`, or a new one: free ports and the project
96
+ * name picked once and kept, so URLs in `.env` stay the same. A `dev.json` from 0.6.0 has no
97
+ * project name; it keeps that version's `spotcraft-dev-<slug>`, now written down.
98
+ * `.spotcraft/` ignores itself in git.
87
99
  */
88
100
  export async function loadStack(root, slug, ports = freePort) {
89
101
  const stateDir = join(root, '.spotcraft');
90
102
  const file = join(stateDir, 'dev.json');
91
- const project = `${PROJECT_PREFIX}${slug}`;
103
+ const save = async (stack) => {
104
+ const { stateDir: _, ...saved } = stack;
105
+ await writeFile(file, `${JSON.stringify(saved, null, 2)}\n`);
106
+ };
92
107
  try {
93
108
  const saved = JSON.parse(await readFile(file, 'utf8'));
94
- if (saved.ports && saved.authSecret)
95
- return { project, stateDir, ...saved };
109
+ if (saved.ports && saved.authSecret) {
110
+ const { ports, authSecret } = saved;
111
+ if (isProjectName(saved.project))
112
+ return { project: saved.project, stateDir, ports, authSecret };
113
+ const stack = { project: `${PROJECT_PREFIX}${slug}`, stateDir, ports, authSecret };
114
+ await save(stack);
115
+ return stack;
116
+ }
96
117
  }
97
118
  catch (error) {
98
119
  if (error.code !== 'ENOENT' && !(error instanceof SyntaxError))
@@ -109,15 +130,14 @@ export async function loadStack(root, slug, ports = freePort) {
109
130
  }
110
131
  };
111
132
  const stack = {
112
- project,
133
+ project: projectName(root, slug),
113
134
  stateDir,
114
135
  ports: { postgres: await next(), auth: await next(), smtp: await next(), mail: await next() },
115
136
  authSecret: randomBytes(32).toString('hex'),
116
137
  };
117
138
  await mkdir(stateDir, { recursive: true });
118
139
  await writeFile(join(stateDir, '.gitignore'), '*\n');
119
- const { project: _, stateDir: __, ...saved } = stack;
120
- await writeFile(file, `${JSON.stringify(saved, null, 2)}\n`);
140
+ await save(stack);
121
141
  return stack;
122
142
  }
123
143
  export const adminUrl = (stack) => `postgres://spotcraft_agent:${ADMIN_PASSWORD}@127.0.0.1:${stack.ports.postgres}/postgres`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kodentuzo/spotcraft",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "The spotcraft command: a local stack that matches a Spotcraft server, per repository",
5
5
  "keywords": [
6
6
  "spotcraft",