@velastack/patterns 0.2.1 → 0.2.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.
Files changed (35) hide show
  1. package/dist/chunks/{collections-wIKcIKzY.js → collections-1Z0NCLM-.js} +2 -2
  2. package/dist/chunks/{collections-wIKcIKzY.js.map → collections-1Z0NCLM-.js.map} +1 -1
  3. package/dist/chunks/{generate.runtime-CQWygx-F.js → generate.runtime-BHoIbpFW.js} +2 -2
  4. package/dist/chunks/{generate.runtime-CQWygx-F.js.map → generate.runtime-BHoIbpFW.js.map} +1 -1
  5. package/dist/chunks/{generate.runtime-BlK7yHff.js → generate.runtime-BhgkUhnZ.js} +2 -2
  6. package/dist/chunks/{generate.runtime-BlK7yHff.js.map → generate.runtime-BhgkUhnZ.js.map} +1 -1
  7. package/dist/chunks/{generate.runtime-4QI3es6J.js → generate.runtime-BiQzi7ie.js} +2 -2
  8. package/dist/chunks/{generate.runtime-4QI3es6J.js.map → generate.runtime-BiQzi7ie.js.map} +1 -1
  9. package/dist/chunks/{generate.runtime-CNrtLDCf.js → generate.runtime-C3kgmLls.js} +2 -2
  10. package/dist/chunks/{generate.runtime-CNrtLDCf.js.map → generate.runtime-C3kgmLls.js.map} +1 -1
  11. package/dist/chunks/{generate.runtime-C4wcEIc9.js → generate.runtime-CbSifpZG.js} +3 -3
  12. package/dist/chunks/{generate.runtime-C4wcEIc9.js.map → generate.runtime-CbSifpZG.js.map} +1 -1
  13. package/dist/chunks/{generate.runtime-BX0QcpGg.js → generate.runtime-CflCO_6s.js} +2 -2
  14. package/dist/chunks/{generate.runtime-BX0QcpGg.js.map → generate.runtime-CflCO_6s.js.map} +1 -1
  15. package/dist/chunks/{generate.runtime-CQ0Veyk4.js → generate.runtime-CqOjFGKT.js} +3 -3
  16. package/dist/chunks/{generate.runtime-CQ0Veyk4.js.map → generate.runtime-CqOjFGKT.js.map} +1 -1
  17. package/dist/chunks/{generate.runtime-DOBfTVW3.js → generate.runtime-DUFPsIYY.js} +3 -3
  18. package/dist/chunks/{generate.runtime-DOBfTVW3.js.map → generate.runtime-DUFPsIYY.js.map} +1 -1
  19. package/dist/chunks/{generate.runtime-CAzHPuHk.js → generate.runtime-DYySjH4f.js} +2 -2
  20. package/dist/chunks/{generate.runtime-CAzHPuHk.js.map → generate.runtime-DYySjH4f.js.map} +1 -1
  21. package/dist/chunks/{generate.runtime-BmW0th7X.js → generate.runtime-JtRQn3Yl.js} +2 -2
  22. package/dist/chunks/{generate.runtime-BmW0th7X.js.map → generate.runtime-JtRQn3Yl.js.map} +1 -1
  23. package/dist/chunks/pocketbase-059B1oot.js +228 -0
  24. package/dist/chunks/pocketbase-059B1oot.js.map +1 -0
  25. package/dist/chunks/{shared-BZlvCHX8.js → shared-CaHmNkZE.js} +2 -2
  26. package/dist/chunks/{shared-BZlvCHX8.js.map → shared-CaHmNkZE.js.map} +1 -1
  27. package/dist/chunks/{ui-DZS2SAQg.js → ui-DAtVMiVs.js} +2 -2
  28. package/dist/chunks/{ui-DZS2SAQg.js.map → ui-DAtVMiVs.js.map} +1 -1
  29. package/dist/chunks/{write-result-Cb3r0sCO.js → write-result-BHCOxcsA.js} +2 -2
  30. package/dist/chunks/{write-result-Cb3r0sCO.js.map → write-result-BHCOxcsA.js.map} +1 -1
  31. package/dist/index.js +50 -50
  32. package/dist/index.js.map +1 -1
  33. package/package.json +1 -1
  34. package/dist/chunks/pocketbase-fERkp8b8.js +0 -123
  35. package/dist/chunks/pocketbase-fERkp8b8.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velastack/patterns",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Registry of VelaStack patterns: generators and modifications applied to SvelteKit projects by the VelaStack CLI.",
5
5
  "type": "module",
6
6
  "private": false,
@@ -1,123 +0,0 @@
1
- import { i as e, n as t } from "./constants-PBCzh8eY.js";
2
- import n from "node:path";
3
- import { existsSync as r, readFileSync as i, readdirSync as a } from "node:fs";
4
- import o from "pocketbase";
5
- import { detect as s } from "package-manager-detector";
6
- import { resolveCommand as c } from "package-manager-detector/commands";
7
- import l from "cross-spawn";
8
- import u from "node:net";
9
- //#region src/runtime/net.ts
10
- function d() {
11
- return Math.floor(Math.random() * 1e4) + 1e4;
12
- }
13
- function f(e, t, n = 60) {
14
- return new Promise((r, i) => {
15
- let a = 0, o = () => {
16
- a++;
17
- let s = new u.Socket();
18
- s.once("error", () => {
19
- if (s.destroy(), a === n) {
20
- i(/* @__PURE__ */ Error(`Timed out waiting for port ${e} to be available`));
21
- return;
22
- }
23
- setTimeout(o, 500);
24
- }), s.connect(e, t, () => {
25
- s.destroy(), r();
26
- });
27
- };
28
- o();
29
- });
30
- }
31
- function p(e, t = 60) {
32
- return new Promise((n, r) => {
33
- let i = 0, a = async () => {
34
- i++;
35
- try {
36
- if ((await fetch(`${e}/api/health`)).status === 200) {
37
- n();
38
- return;
39
- }
40
- } catch {}
41
- if (i === t) {
42
- r(/* @__PURE__ */ Error(`Timed out waiting for health check at ${e}/api/health`));
43
- return;
44
- }
45
- setTimeout(a, 500);
46
- };
47
- a();
48
- });
49
- }
50
- //#endregion
51
- //#region src/runtime/pocketbase.ts
52
- var m = 1100;
53
- function h() {
54
- return new Promise((e) => setTimeout(e, m));
55
- }
56
- function g(t, r, i) {
57
- let o = `${r}_${t}.js`, s = a(n.join(i.root, e)).filter((e) => e.endsWith(o)).sort().pop();
58
- if (s) return n.join(i.root, e, s);
59
- }
60
- function _(e) {
61
- let t = n.join(e, "node_modules", ".vite", "_pocketbase_metadata.json");
62
- return r(t) ? JSON.parse(i(t, "utf8")) : null;
63
- }
64
- async function v(e, t, n) {
65
- let r = !1;
66
- for (let i = 1; i <= 3; i++) try {
67
- await e.collection("_superusers").authWithPassword(t, n), r = !0;
68
- break;
69
- } catch {
70
- i < 3 && await new Promise((e) => setTimeout(e, 100));
71
- }
72
- if (!r) throw Error("Failed to authenticate with PocketBase. Check your superuser credentials.");
73
- }
74
- async function y(i, a) {
75
- let u = n.join(i, t), m = n.join(i, e), h = "localhost";
76
- if (!r(u)) throw Error("PocketBase data directory does not exist");
77
- let g = _(i);
78
- if (g && g.pocketbaseUrl) {
79
- let e = new o(g.pocketbaseUrl);
80
- await v(e, process.env.POCKETBASE_SUPERUSER_EMAIL, process.env.POCKETBASE_SUPERUSER_PASSWORD), await a(e);
81
- return;
82
- }
83
- let y = d(), x = [
84
- "pocketbase-server",
85
- "serve",
86
- "--dir",
87
- u,
88
- "--migrationsDir",
89
- m,
90
- "--http",
91
- `${h}:${y}`
92
- ], S = (await s({ cwd: i }))?.name ?? "npm", { command: C, args: w } = c(S, "execute", x);
93
- S === "npm" && w.unshift("--yes");
94
- let T = process.platform !== "win32", E = l(C, w, {
95
- cwd: i,
96
- stdio: "ignore",
97
- detached: T
98
- }), D = new Promise((e, t) => {
99
- E.once("exit", (e, n) => {
100
- t(/* @__PURE__ */ Error(`pocketbase-server exited before listening on ${h}:${y} (${n ?? `code ${e}`})`));
101
- }), E.once("error", t);
102
- });
103
- try {
104
- await Promise.race([(async () => {
105
- await f(y, h), await p(`http://${h}:${y}`);
106
- })(), D]);
107
- let e = new o(`http://${h}:${y}`);
108
- await v(e, process.env.POCKETBASE_SUPERUSER_EMAIL, process.env.POCKETBASE_SUPERUSER_PASSWORD), await a(e);
109
- } finally {
110
- b(E, T);
111
- }
112
- }
113
- function b(e, t) {
114
- if (t && e.pid) try {
115
- process.kill(-e.pid, "SIGTERM");
116
- return;
117
- } catch {}
118
- e.kill();
119
- }
120
- //#endregion
121
- export { g as getMigrationFile, h as migrationDelay, y as withPocketbase };
122
-
123
- //# sourceMappingURL=pocketbase-fERkp8b8.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pocketbase-fERkp8b8.js","names":[],"sources":["../../src/runtime/net.ts","../../src/runtime/pocketbase.ts"],"sourcesContent":["import net from \"node:net\";\n\n// Generate a random port\nexport function randomPort() {\n return Math.floor(Math.random() * 10000) + 10000;\n}\n\n// Wait for the port to be available. A cold `npx pocketbase-server` on a CI\n// runner can take well over the five seconds the old default allowed.\nexport function waitForPort(\n port: number,\n host: string,\n maxAttempts = 60,\n): Promise<void> {\n return new Promise((resolve, reject) => {\n let attempts = 0;\n\n const tryConnect = () => {\n attempts++;\n\n const socket = new net.Socket();\n\n const onError = () => {\n socket.destroy();\n\n if (attempts === maxAttempts) {\n reject(\n new Error(`Timed out waiting for port ${port} to be available`),\n );\n return;\n }\n\n setTimeout(tryConnect, 500);\n };\n\n socket.once(\"error\", onError);\n\n socket.connect(port, host, () => {\n socket.destroy();\n resolve();\n });\n };\n\n tryConnect();\n });\n}\n\n// Wait for the health check to be successful\nexport function waitForHealth(url: string, maxAttempts = 60): Promise<void> {\n return new Promise((resolve, reject) => {\n let attempts = 0;\n\n const tryHealth = async () => {\n attempts++;\n\n try {\n const response = await fetch(`${url}/api/health`);\n if (response.status === 200) {\n resolve();\n return;\n }\n } catch {\n // Ignore fetch errors and continue retrying\n }\n\n if (attempts === maxAttempts) {\n reject(\n new Error(`Timed out waiting for health check at ${url}/api/health`),\n );\n return;\n }\n\n setTimeout(tryHealth, 500);\n };\n\n tryHealth();\n });\n}\n","import { existsSync, readdirSync, readFileSync } from \"node:fs\";\nimport path from \"node:path\";\nimport PocketBase from \"pocketbase\";\nimport { detect } from \"package-manager-detector\";\nimport { resolveCommand } from \"package-manager-detector/commands\";\nimport spawn from \"cross-spawn\";\nimport { randomPort, waitForPort, waitForHealth } from \"./net\";\nimport type { Options } from \"../core/types\";\nimport { DATA_DIR, MIGRATIONS_DIR } from \"../core/constants\";\n\n/**\n * PocketBase names migration files with a 1-second-resolution Unix timestamp\n * (e.g. `1776099881_created_users.js`). Two schema operations issued in the\n * same wall-clock second produce files that sort alphabetically by their\n * action/collection suffix — which reverses the intended order whenever a\n * later migration depends on an earlier one. Wait out the full second between\n * migration-producing calls so the timestamps stay distinct.\n */\nconst MIGRATION_DELAY_MS = 1100;\n\nexport function migrationDelay(): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, MIGRATION_DELAY_MS));\n}\n\nexport function getMigrationFile(\n collectionName: string,\n action: \"created\" | \"deleted\" | \"updated\",\n options: Options,\n): string | undefined {\n const ending = `${action}_${collectionName}.js`;\n\n // find last file with ending in migrations dir\n const migrationFiles = readdirSync(\n path.join(options.root, MIGRATIONS_DIR),\n ).filter((file) => file.endsWith(ending));\n\n const migrationFile = migrationFiles.sort().pop();\n\n if (!migrationFile) {\n return undefined;\n }\n\n return path.join(options.root, MIGRATIONS_DIR, migrationFile);\n}\n\nexport function getPocketbaseMetadata(cwd: string): {\n pocketbaseUrl: string;\n vitePort: number;\n} | null {\n const metadataPath = path.join(\n cwd,\n \"node_modules\",\n \".vite\",\n \"_pocketbase_metadata.json\",\n );\n if (existsSync(metadataPath)) {\n return JSON.parse(readFileSync(metadataPath, \"utf8\"));\n }\n return null;\n}\n\nexport async function authWithRetries(\n pb: PocketBase,\n email: string,\n password: string,\n) {\n // Retry authentication up to 3 times with 100ms backoff\n let authSuccess = false;\n for (let attempt = 1; attempt <= 3; attempt++) {\n try {\n await pb.collection(\"_superusers\").authWithPassword(email, password);\n authSuccess = true;\n break;\n } catch {\n if (attempt < 3) {\n await new Promise((resolve) => setTimeout(resolve, 100));\n }\n }\n }\n\n if (!authSuccess) {\n throw new Error(\n \"Failed to authenticate with PocketBase. Check your superuser credentials.\",\n );\n }\n}\n\nexport async function withPocketbase(\n cwd: string,\n fn: (pb: PocketBase) => Promise<void>,\n) {\n const dir = path.join(cwd, DATA_DIR);\n const migrationsDir = path.join(cwd, MIGRATIONS_DIR);\n const host = \"localhost\";\n\n if (!existsSync(dir)) {\n throw new Error(\"PocketBase data directory does not exist\");\n }\n\n // First check if the server is already running by reading node_modules/.vite/_pocketbase_metadata.json\n const metadata = getPocketbaseMetadata(cwd);\n if (metadata) {\n if (metadata.pocketbaseUrl) {\n const pb = new PocketBase(metadata.pocketbaseUrl);\n await authWithRetries(\n pb,\n process.env.POCKETBASE_SUPERUSER_EMAIL!,\n process.env.POCKETBASE_SUPERUSER_PASSWORD!,\n );\n await fn(pb);\n return;\n }\n }\n\n const port = randomPort();\n const commandArgs = [\n \"pocketbase-server\",\n \"serve\",\n \"--dir\",\n dir,\n \"--migrationsDir\",\n migrationsDir,\n \"--http\",\n `${host}:${port}`,\n ];\n\n const packageManager = (await detect({ cwd }))?.name ?? \"npm\";\n const { command, args } = resolveCommand(\n packageManager,\n \"execute\",\n commandArgs,\n )!;\n\n // adding --yes as the first parameter helps avoiding the \"Need to install the following packages:\" message\n if (packageManager === \"npm\") args.unshift(\"--yes\");\n\n // Start the PocketBase server process. The package manager wrapper (`npx`)\n // spawns the real binary as a grandchild; put the tree in its own process\n // group so it can be killed as a whole, otherwise `pocketbase serve` outlives\n // us and keeps the data directory open.\n const detached = process.platform !== \"win32\";\n const serverProcess = spawn(command, args, {\n cwd,\n stdio: \"ignore\",\n detached,\n });\n\n // A wrapper that dies before the server listens (missing binary, bad\n // arguments) would otherwise only surface as a port timeout a minute later.\n const exited = new Promise<never>((_, reject) => {\n serverProcess.once(\"exit\", (code, signal) => {\n reject(\n new Error(\n `pocketbase-server exited before listening on ${host}:${port} (${signal ?? `code ${code}`})`,\n ),\n );\n });\n serverProcess.once(\"error\", reject);\n });\n\n try {\n // Wait for the server to be ready\n await Promise.race([\n (async () => {\n await waitForPort(port, host);\n await waitForHealth(`http://${host}:${port}`);\n })(),\n exited,\n ]);\n\n const pb = new PocketBase(`http://${host}:${port}`);\n await authWithRetries(\n pb,\n process.env.POCKETBASE_SUPERUSER_EMAIL!,\n process.env.POCKETBASE_SUPERUSER_PASSWORD!,\n );\n await fn(pb);\n } finally {\n // Ensure we always clean up the process tree\n stopProcessTree(serverProcess, detached);\n }\n}\n\nfunction stopProcessTree(\n child: ReturnType<typeof spawn>,\n detached: boolean,\n): void {\n if (detached && child.pid) {\n try {\n process.kill(-child.pid, \"SIGTERM\");\n return;\n } catch {\n // Group already gone or unsupported; fall through to the pl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