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.
- package/bin/create-nextblock.js +23 -2
- package/docker-template/.dockerignore +2 -1
- package/package.json +1 -1
- package/scripts/sync-template.js +97 -0
- package/templates/nextblock-template/.dockerignore +2 -1
- package/templates/nextblock-template/README.md +57 -34
- package/templates/nextblock-template/app/api/cron/reset-sandbox/sandboxResetSql.ts +525 -1
- package/templates/nextblock-template/app/cms/settings/site-scripts/components/SiteScriptManager.tsx +13 -5
- package/templates/nextblock-template/docs/04-DATABASE-AND-AUTH.md +2 -1
- package/templates/nextblock-template/docs/08-NEXTBLOCK-CORTEX-AI-ARCHITECTURE.md +26 -11
- package/templates/nextblock-template/docs/11-SELF-HOSTED-DOCKER.md +9 -0
- package/templates/nextblock-template/docs/12-VERCEL-DEPLOYMENT.md +8 -0
- package/templates/nextblock-template/docs/13-STAYING-UP-TO-DATE.md +372 -151
- package/templates/nextblock-template/docs/README.md +2 -0
- package/templates/nextblock-template/gitignore +3 -0
- package/templates/nextblock-template/lib/onboarding/status.ts +11 -5
- package/templates/nextblock-template/lib/setup/migrations-bundle.ts +35 -0
- package/templates/nextblock-template/lib/updates/check-upstream.ts +167 -46
- package/templates/nextblock-template/package.json +6 -1
- package/templates/nextblock-template/tools/build-migrate.mjs +102 -209
- package/templates/nextblock-template/tools/lib/migrate-core.mjs +569 -0
- package/templates/nextblock-template/tools/update.mjs +1285 -0
- package/templates/nextblock-template/tsconfig.tsbuildinfo +0 -1
|
@@ -1,209 +1,102 @@
|
|
|
1
|
-
//
|
|
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
|
|
16
|
-
// unreachable database
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
`
|
|
99
|
-
|
|
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
|
+
});
|