create-nextblock 0.15.0 → 0.15.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 (23) hide show
  1. package/bin/create-nextblock.js +23 -2
  2. package/docker-template/.dockerignore +2 -1
  3. package/package.json +1 -1
  4. package/scripts/sync-template.js +97 -0
  5. package/templates/nextblock-template/.dockerignore +2 -1
  6. package/templates/nextblock-template/README.md +57 -34
  7. package/templates/nextblock-template/app/api/cron/reset-sandbox/sandboxResetSql.ts +525 -1
  8. package/templates/nextblock-template/app/cms/settings/site-scripts/components/SiteScriptManager.tsx +13 -5
  9. package/templates/nextblock-template/docs/04-DATABASE-AND-AUTH.md +2 -1
  10. package/templates/nextblock-template/docs/08-NEXTBLOCK-CORTEX-AI-ARCHITECTURE.md +26 -11
  11. package/templates/nextblock-template/docs/11-SELF-HOSTED-DOCKER.md +9 -0
  12. package/templates/nextblock-template/docs/12-VERCEL-DEPLOYMENT.md +8 -0
  13. package/templates/nextblock-template/docs/13-STAYING-UP-TO-DATE.md +372 -151
  14. package/templates/nextblock-template/docs/README.md +2 -0
  15. package/templates/nextblock-template/gitignore +3 -0
  16. package/templates/nextblock-template/lib/onboarding/status.ts +11 -5
  17. package/templates/nextblock-template/lib/setup/migrations-bundle.ts +35 -0
  18. package/templates/nextblock-template/lib/updates/check-upstream.ts +167 -46
  19. package/templates/nextblock-template/package.json +6 -1
  20. package/templates/nextblock-template/tools/build-migrate.mjs +102 -209
  21. package/templates/nextblock-template/tools/lib/migrate-core.mjs +569 -0
  22. package/templates/nextblock-template/tools/update.mjs +1285 -0
  23. package/templates/nextblock-template/tsconfig.tsbuildinfo +0 -1
@@ -1,209 +1,102 @@
1
- // Milestone 4 — environment-aware, zero-config build-time schema migration hook.
2
- //
3
- // Runs BEFORE `next build` (wired in apps/nextblock/project.json for nx/Vercel and in
4
- // the app's `prebuild` script for standalone/Docker `npm run build`). It applies any
5
- // pending, forward-only migrations to the tenant's Postgres so the deployed app and its
6
- // schema move together — additively, transactionally, and tracked exactly like the
7
- // Supabase CLI in supabase_migrations.schema_migrations.
8
- //
9
- // Gating (no manual config required):
10
- // * On Vercel: run only when VERCEL_ENV === 'production'. Preview/development builds
11
- // are skipped so feature previews never touch live data structures.
12
- // * Off Vercel (npm create / local / Docker): run only when NEXTBLOCK_BUILD_MIGRATE === '1'
13
- // (the /setup wizard and the create/docker setup scripts write this into .env).
14
- //
15
- // Safety: this NEVER fails the build. A missing flag, missing POSTGRES_URL, or an
16
- // unreachable database logs a warning and exits 0 so static packaging always proceeds.
17
- //
18
- // The SQL constants + apply loop are intentionally kept in lockstep with
19
- // apps/nextblock/lib/setup/schema-apply.ts (the /setup wizard's applier). This file is
20
- // plain ESM with no app/server-only imports so it loads safely in any build context.
21
-
22
- import { existsSync } from 'node:fs';
23
- import { readdir, readFile } from 'node:fs/promises';
24
- import path from 'node:path';
25
-
26
- const log = (msg) => console.log(`[build-migrate] ${msg}`);
27
-
28
- /** Best-effort .env.local / .env load (dotenv is a dependency; tolerate its absence). */
29
- async function loadEnv() {
30
- try {
31
- const dotenv = await import('dotenv');
32
- for (const file of ['.env.local', '.env']) {
33
- const p = path.join(process.cwd(), file);
34
- if (existsSync(p)) dotenv.config({ path: p, override: false, quiet: true });
35
- }
36
- } catch {
37
- /* dotenv unavailable env may already be injected by the platform */
38
- }
39
- }
40
-
41
- /** Decide whether the hook should run, with a human-readable reason for the log. */
42
- function evaluateGate() {
43
- const vercelEnv = process.env.VERCEL_ENV;
44
- if (vercelEnv) {
45
- return vercelEnv === 'production'
46
- ? { run: true, reason: 'VERCEL_ENV=production' }
47
- : { run: false, reason: `VERCEL_ENV=${vercelEnv} — preview/development build, skipping` };
48
- }
49
- if (process.env.NEXTBLOCK_BUILD_MIGRATE === '1') {
50
- return { run: true, reason: 'NEXTBLOCK_BUILD_MIGRATE=1' };
51
- }
52
- return { run: false, reason: 'NEXTBLOCK_BUILD_MIGRATE is not set skipping' };
53
- }
54
-
55
- /**
56
- * Locate the migrations dir: monorepo keeps them at libs/db/src/supabase/migrations
57
- * (found via the nearest nx.json); a standalone create-nextblock project materializes
58
- * them at <projectRoot>/supabase/migrations. Mirrors schema-apply.ts.
59
- */
60
- function resolveMigrationsDir() {
61
- let dir = process.cwd();
62
- for (let i = 0; i < 8; i++) {
63
- if (existsSync(path.join(dir, 'nx.json'))) {
64
- const monorepo = path.join(dir, 'libs', 'db', 'src', 'supabase', 'migrations');
65
- if (existsSync(monorepo)) return monorepo;
66
- break;
67
- }
68
- const parent = path.dirname(dir);
69
- if (parent === dir) break;
70
- dir = parent;
71
- }
72
- dir = process.cwd();
73
- for (let i = 0; i < 8; i++) {
74
- const standalone = path.join(dir, 'supabase', 'migrations');
75
- if (existsSync(standalone)) return standalone;
76
- const parent = path.dirname(dir);
77
- if (parent === dir) break;
78
- dir = parent;
79
- }
80
- return null;
81
- }
82
-
83
- // --- SQL kept in sync with apps/nextblock/lib/setup/schema-apply.ts ---------------
84
- const TRACKING_DDL =
85
- 'create schema if not exists supabase_migrations;' +
86
- 'create table if not exists supabase_migrations.schema_migrations ' +
87
- '(version text primary key, name text, statements text[]);';
88
-
89
- const GRANTS_SQL =
90
- 'grant usage on schema public to anon, authenticated, service_role;' +
91
- 'grant select on all tables in schema public to anon;' +
92
- 'grant all on all tables in schema public to authenticated, service_role;' +
93
- 'grant all on all sequences in schema public to anon, authenticated, service_role;' +
94
- 'grant execute on all functions in schema public to anon, authenticated, service_role;';
95
-
96
- function recordSql(version, file) {
97
- return (
98
- `insert into supabase_migrations.schema_migrations (version, name) ` +
99
- `values ('${version}', '${file.replace(/'/g, "''")}') on conflict (version) do nothing;`
100
- );
101
- }
102
-
103
- /** Wrap a migration file + its version record in one transaction (all-or-nothing). */
104
- function transactionalMigration(version, file, sqlText) {
105
- return `begin;\n${sqlText}\n;\n${recordSql(version, file)}\ncommit;`;
106
- }
107
-
108
- /** Apply pending migrations over a direct Postgres connection. Never throws. */
109
- async function applyViaPostgres(dbUrl, files, readSql) {
110
- let postgres;
111
- try {
112
- ({ default: postgres } = await import('postgres'));
113
- } catch {
114
- return { ok: false, applied: 0, error: 'the "postgres" package is not installed' };
115
- }
116
-
117
- const db = postgres(dbUrl, { ssl: 'require', onnotice: () => undefined, max: 1 });
118
- let applied = 0;
119
- try {
120
- await db.unsafe(TRACKING_DDL);
121
- const rows = await db`select version from supabase_migrations.schema_migrations`;
122
- const done = new Set(rows.map((r) => r.version));
123
-
124
- // Self-heal stale history: recorded versions but a core table missing means the
125
- // schema was wiped without clearing history (separate schema) — re-apply all.
126
- if (done.size > 0) {
127
- const sentinel = await db`select to_regclass('public.site_settings')::text as t`;
128
- if (!sentinel[0]?.t) {
129
- await db`delete from supabase_migrations.schema_migrations`;
130
- done.clear();
131
- }
132
- }
133
-
134
- for (const file of files) {
135
- const version = file.split('_')[0];
136
- if (done.has(version)) continue;
137
- const sqlText = await readSql(file);
138
- await db.unsafe(transactionalMigration(version, file, sqlText));
139
- applied += 1;
140
- }
141
-
142
- await db.unsafe(GRANTS_SQL);
143
- await db.unsafe("notify pgrst, 'reload schema';");
144
- return { ok: true, applied };
145
- } catch (caught) {
146
- const message = caught instanceof Error ? caught.message : 'unknown error applying migrations';
147
- const unreachable = /ENOTFOUND|ENOENT|EAI_AGAIN|getaddrinfo|ECONNREFUSED|ETIMEDOUT/i.test(message);
148
- return {
149
- ok: false,
150
- applied,
151
- error: unreachable
152
- ? `could not reach the database host (${message}); use the Session pooler connection string for IPv4 build networks`
153
- : message,
154
- };
155
- } finally {
156
- try {
157
- await db.end({ timeout: 5 });
158
- } catch {
159
- /* closing should never mask the result */
160
- }
161
- }
162
- }
163
-
164
- async function main() {
165
- await loadEnv();
166
-
167
- const gate = evaluateGate();
168
- if (!gate.run) {
169
- log(`Skipping build-time migrations (${gate.reason}).`);
170
- return;
171
- }
172
- log(`Build-time migrations enabled (${gate.reason}).`);
173
-
174
- const dbUrl = process.env.POSTGRES_URL || process.env.DATABASE_URL;
175
- if (!dbUrl) {
176
- log(
177
- 'No POSTGRES_URL/DATABASE_URL is set — skipping. Apply the schema with the /setup wizard or "npm run db:migrate".',
178
- );
179
- return;
180
- }
181
-
182
- const dir = resolveMigrationsDir();
183
- if (!dir) {
184
- log('Could not locate the migrations directory — skipping.');
185
- return;
186
- }
187
-
188
- const files = (await readdir(dir)).filter((name) => /^\d+_.*\.sql$/.test(name)).sort();
189
- const result = await applyViaPostgres(dbUrl, files, (file) =>
190
- readFile(path.join(dir, file), 'utf8'),
191
- );
192
-
193
- if (result.ok) {
194
- log(result.applied > 0 ? `Applied ${result.applied} pending migration(s).` : 'Schema already up to date.');
195
- } else {
196
- log(`WARNING: could not apply migrations: ${result.error}.`);
197
- log('Continuing the build — apply later with "npm run db:migrate".');
198
- }
199
- }
200
-
201
- // Guarantee a clean exit code regardless of outcome: infrastructure issues must never
202
- // break web packaging.
203
- main()
204
- .catch((err) => {
205
- log(`WARNING: ${err instanceof Error ? err.message : String(err)}.`);
206
- })
207
- .finally(() => {
208
- process.exit(0);
209
- });
1
+ // Environment-aware, zero-config build-time schema migration hook.
2
+ //
3
+ // Runs BEFORE `next build` (wired in apps/nextblock/project.json for nx/Vercel and in
4
+ // the app's `prebuild` script for standalone/Docker `npm run build`). It applies any
5
+ // pending, forward-only migrations to the tenant's Postgres so the deployed app and its
6
+ // schema move together — additively, transactionally, and tracked exactly like the
7
+ // Supabase CLI in supabase_migrations.schema_migrations.
8
+ //
9
+ // Gating (no manual config required):
10
+ // * On Vercel: run only when VERCEL_ENV === 'production'. Preview/development builds
11
+ // are skipped so feature previews never touch live data structures.
12
+ // * Off Vercel (npm create / local / Docker): run only when NEXTBLOCK_BUILD_MIGRATE === '1'
13
+ // (the /setup wizard and the create/docker setup scripts write this into .env).
14
+ //
15
+ // Safety: this NEVER fails the build. A missing flag, a missing connection string, an
16
+ // unreachable database or even a broken ./lib/migrate-core.mjs logs a warning and
17
+ // exits 0 so static packaging always proceeds. That is why the engine is loaded with a
18
+ // guarded dynamic import rather than a top-level one: an unresolvable top-level import
19
+ // is a hard ESM error that no try/catch inside main() could contain.
20
+ //
21
+ // The engine itself (discovery + the two backends) lives in ./lib/migrate-core.mjs and is
22
+ // shared with `npm run update` (tools/update.mjs), so the two can no longer drift.
23
+
24
+ const log = (msg) => console.log(`[build-migrate] ${msg}`);
25
+
26
+ /** Decide whether the hook should run, with a human-readable reason for the log. */
27
+ function evaluateGate() {
28
+ const vercelEnv = process.env.VERCEL_ENV;
29
+ if (vercelEnv) {
30
+ return vercelEnv === 'production'
31
+ ? { run: true, reason: 'VERCEL_ENV=production' }
32
+ : { run: false, reason: `VERCEL_ENV=${vercelEnv} — preview/development build, skipping` };
33
+ }
34
+ if (process.env.NEXTBLOCK_BUILD_MIGRATE === '1') {
35
+ return { run: true, reason: 'NEXTBLOCK_BUILD_MIGRATE=1' };
36
+ }
37
+ return { run: false, reason: 'NEXTBLOCK_BUILD_MIGRATE is not set skipping' };
38
+ }
39
+
40
+ async function main() {
41
+ let core;
42
+ try {
43
+ core = await import('./lib/migrate-core.mjs');
44
+ } catch (err) {
45
+ log(`WARNING: could not load the migration engine: ${err?.message ?? err}.`);
46
+ log('Continuing the build apply later with "npm run update".');
47
+ return;
48
+ }
49
+
50
+ // Load .env BEFORE the gate: off Vercel, NEXTBLOCK_BUILD_MIGRATE is never a real process
51
+ // env var — it lives in .env.local / .env, written there by the /setup wizard and the
52
+ // create/docker setup scripts. Evaluating the gate first would read `undefined` and
53
+ // silently skip every build-time migration on standalone and local installs.
54
+ await core.loadEnvFiles(process.cwd());
55
+
56
+ const gate = evaluateGate();
57
+ if (!gate.run) {
58
+ log(`Skipping build-time migrations (${gate.reason}).`);
59
+ return;
60
+ }
61
+ log(`Build-time migrations enabled (${gate.reason}).`);
62
+
63
+ const backend = core.describeBackend();
64
+ if (backend.kind === 'none') {
65
+ log(`No database connection is configured (${backend.detail}) skipping.`);
66
+ log('Apply the schema with the /setup wizard or "npm run update".');
67
+ return;
68
+ }
69
+
70
+ const { files, sources } = core.collectMigrations(process.cwd());
71
+ if (files.length === 0) {
72
+ log('Could not locate any migrations — skipping.');
73
+ return;
74
+ }
75
+ for (const source of sources) {
76
+ if (source.contributed > 0) {
77
+ log(`Using ${source.contributed} migration(s) from the ${source.label} copy (${source.dir}).`);
78
+ }
79
+ }
80
+
81
+ const result = await core.applyMigrations(files);
82
+ if (result.ok) {
83
+ log(
84
+ result.applied > 0
85
+ ? `Applied ${result.applied} pending migration(s) via ${result.backend}.`
86
+ : 'Schema already up to date.',
87
+ );
88
+ } else {
89
+ log(`WARNING: could not apply migrations: ${result.error}.`);
90
+ log('Continuing the build apply later with "npm run update".');
91
+ }
92
+ }
93
+
94
+ // Guarantee a clean exit code regardless of outcome: infrastructure issues must never
95
+ // break web packaging.
96
+ main()
97
+ .catch((err) => {
98
+ log(`WARNING: ${err instanceof Error ? err.message : String(err)}.`);
99
+ })
100
+ .finally(() => {
101
+ process.exit(0);
102
+ });