@svgrid/enterprise 2.3.1 → 2.4.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/cdn/{pdfmake-C6pyKjX0.js → pdfmake-DVbZqKeD.js} +2297 -2327
- package/dist/cdn/rolldown-runtime-DtPi1Y-2.js +13 -0
- package/dist/cdn/{smart.export-CfW2zpyq.js → smart.export-D9FF4wAB.js} +54 -51
- package/dist/cdn/svgrid-enterprise.svelte-external.js +7540 -7511
- package/dist/cdn/{vfs_fonts-BktEULEs.js → vfs_fonts-iHMTEkpG.js} +1 -1
- package/dist/designer/assets/GridMenus-CW-rnHS9.js +7 -0
- package/dist/designer/assets/SvDateTimePicker-Cygij-dr.css +1 -0
- package/dist/designer/assets/SvDateTimePicker-DOBYyLeN.js +1 -0
- package/dist/designer/assets/SvGridChart-BEDNTdc5.js +2 -0
- package/dist/designer/assets/SvGridChart-Vu4Nvjiv.css +1 -0
- package/dist/designer/assets/SvGridChartPanel-x3lzSxuf.js +10 -0
- package/dist/designer/assets/SvGridChartView-CAKhPELi.js +1 -0
- package/dist/designer/assets/SvGridDropdown-CAYG7Y-O.js +1 -0
- package/dist/designer/assets/SvGridDropdown-Crwb2P3Q.css +1 -0
- package/dist/designer/assets/SvListBox-BMSfFJJT.css +1 -0
- package/dist/designer/assets/SvListBox-BVzVfCG3.js +1 -0
- package/dist/designer/assets/chart-DHEgt4lN.js +1 -0
- package/dist/designer/assets/disclose-version-CIm4S9xS.js +2 -0
- package/dist/designer/assets/dismissable-2EZKoens.js +1 -0
- package/dist/designer/assets/dismissable-BF-9TJSd.css +1 -0
- package/dist/designer/assets/easing-zaypKrr0.js +3 -0
- package/dist/designer/assets/export-format-DwjuTfrQ.js +6 -0
- package/dist/designer/assets/index-oYok52zd.css +1 -0
- package/dist/designer/assets/{index-DsDgp9Xq.js → index-okIcRP9P.js} +141 -3102
- package/dist/designer/assets/js-scroller.svelte-tHanKJx0.js +110 -0
- package/dist/designer/assets/jszip.min-CX2fPs3O.js +2 -0
- package/dist/designer/assets/pdfmake-BpIYFHoG.js +242 -0
- package/dist/designer/assets/popover-CUkLsvRX.js +1 -0
- package/dist/designer/assets/rolldown-runtime-Dd_uD5pT.js +1 -0
- package/dist/designer/assets/smart.export-CDCwbFH4.js +37 -0
- package/dist/designer/assets/src-BvsJ00AO.css +1 -0
- package/dist/designer/assets/src-CIla2aVG.js +2842 -0
- package/dist/designer/assets/vfs_fonts-BV-Ul9v2.js +1 -0
- package/dist/designer/index.html +21 -2
- package/dist/node/studio.js +220 -27
- package/package.json +5 -5
- package/src/sources/introspect-supabase.ts +2 -2
- package/src/studio/emit-project.ts +1 -1
- package/src/studio/init-flow.test.ts +364 -239
- package/src/studio/init-flow.ts +409 -358
- package/dist/designer/assets/GridMenus-BtWVk9Ab.js +0 -7
- package/dist/designer/assets/SvGridChartPanel-DWlBO2SD.js +0 -10
- package/dist/designer/assets/SvGridChartView-8c8Mb4j8.js +0 -1
- package/dist/designer/assets/index-tTY_Dx4P.css +0 -1
- package/dist/designer/assets/jszip.min-fkJdmAmj.js +0 -2
- package/dist/designer/assets/pdfmake-DeCsnyl9.js +0 -242
- package/dist/designer/assets/smart.export-BZlSCE8T.js +0 -35
- package/dist/designer/assets/vfs_fonts-eX2NpmfX.js +0 -1
package/src/studio/init-flow.ts
CHANGED
|
@@ -1,358 +1,409 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* runStudioInit - the guided "build a CRUD app" flow behind `svgrid-studio init`.
|
|
3
|
-
*
|
|
4
|
-
* Asks a handful of questions (where the data lives, which tables, which pages,
|
|
5
|
-
* what it looks like), then builds a project with `crudAppFromSchemas` - the same
|
|
6
|
-
* generator the designer's "New app" wizard uses - and writes a runnable app plus
|
|
7
|
-
* its `studio.config.json`.
|
|
8
|
-
*
|
|
9
|
-
* Prompts, the filesystem, and the database are all injected, so the orchestration
|
|
10
|
-
* is pure and unit-testable and this file stays node-free (no process / fs / driver
|
|
11
|
-
* imports). The CLI bin supplies readline, node:fs, and the DB drivers.
|
|
12
|
-
*/
|
|
13
|
-
import type { EntitySchema } from '../schema.js'
|
|
14
|
-
import type { StudioIO } from './cli.js'
|
|
15
|
-
import { crudAppFromSchemas, type CrudEditingMode, type CrudScreenKind, type CrudSuiteOptions } from './screen-suites.js'
|
|
16
|
-
import { starterDatasets, getStarterDataset } from './samples/datasets.js'
|
|
17
|
-
import { introspectDatabase, listDatabaseTables, countTableRows, type DbExecute, type SqlDialectName } from './introspect-db.js'
|
|
18
|
-
import { introspectOpenApi } from './introspect-openapi.js'
|
|
19
|
-
import { introspectJson } from './introspect.js'
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
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
|
-
const
|
|
92
|
-
|
|
93
|
-
return
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (!flags.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (!url)
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
const
|
|
194
|
-
if (
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
prompts.say(
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
1
|
+
/**
|
|
2
|
+
* runStudioInit - the guided "build a CRUD app" flow behind `svgrid-studio init`.
|
|
3
|
+
*
|
|
4
|
+
* Asks a handful of questions (where the data lives, which tables, which pages,
|
|
5
|
+
* what it looks like), then builds a project with `crudAppFromSchemas` - the same
|
|
6
|
+
* generator the designer's "New app" wizard uses - and writes a runnable app plus
|
|
7
|
+
* its `studio.config.json`.
|
|
8
|
+
*
|
|
9
|
+
* Prompts, the filesystem, and the database are all injected, so the orchestration
|
|
10
|
+
* is pure and unit-testable and this file stays node-free (no process / fs / driver
|
|
11
|
+
* imports). The CLI bin supplies readline, node:fs, and the DB drivers.
|
|
12
|
+
*/
|
|
13
|
+
import type { EntitySchema } from '../schema.js'
|
|
14
|
+
import type { StudioIO } from './cli.js'
|
|
15
|
+
import { crudAppFromSchemas, type CrudEditingMode, type CrudScreenKind, type CrudSuiteOptions } from './screen-suites.js'
|
|
16
|
+
import { starterDatasets, getStarterDataset } from './samples/datasets.js'
|
|
17
|
+
import { introspectDatabase, listDatabaseTables, countTableRows, type DbExecute, type SqlDialectName } from './introspect-db.js'
|
|
18
|
+
import { introspectOpenApi } from './introspect-openapi.js'
|
|
19
|
+
import { introspectJson } from './introspect.js'
|
|
20
|
+
import { introspectSupabaseTable, listSupabaseTables } from '../sources/introspect-supabase.js'
|
|
21
|
+
import { buildConnectionString, isFileDialect, type SqlConnectionParts } from './db-connect-string.js'
|
|
22
|
+
import { emitStudioAppBundle } from './emit-project.js'
|
|
23
|
+
import { mergeManaged, skipUserOwned } from './scaffold.js'
|
|
24
|
+
import { getStudioTheme, studioThemes } from './themes.js'
|
|
25
|
+
import { serializeProject, type DataSourceKind, type EntityDataSource, type ProjectTheme, type StudioProject } from './project.js'
|
|
26
|
+
|
|
27
|
+
/** Terminal I/O, injected so the flow can be scripted in tests. */
|
|
28
|
+
export type PromptIO = {
|
|
29
|
+
/** Ask a question; `def` is used when the user just hits Enter. */
|
|
30
|
+
ask: (question: string, def?: string) => Promise<string>
|
|
31
|
+
/** Print a line (progress, headings, results). */
|
|
32
|
+
say: (line: string) => void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Live-database access, injected by the bin (drivers are node-only). */
|
|
36
|
+
export type DbGateway = {
|
|
37
|
+
/** Make sure the dialect's driver is installed; report why not if it isn't. */
|
|
38
|
+
ensureDriver: (dialect: SqlDialectName) => Promise<{ ok: boolean; message?: string }>
|
|
39
|
+
connect: (dialect: SqlDialectName, url: string) => Promise<DbExecute>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Read a REST endpoint or an OpenAPI document (injected: the bin uses fetch/fs). */
|
|
43
|
+
export type FetchText = (source: string) => Promise<string>
|
|
44
|
+
|
|
45
|
+
export type InitFlags = {
|
|
46
|
+
/** Where to write the app. Default `.`. */
|
|
47
|
+
out?: string
|
|
48
|
+
/** Skip the source question and go straight to a live database. */
|
|
49
|
+
db?: SqlDialectName
|
|
50
|
+
/** Connection string for `db`. */
|
|
51
|
+
url?: string
|
|
52
|
+
/** Supabase project URL - skips the source question and both prompts. */
|
|
53
|
+
supabaseUrl?: string
|
|
54
|
+
/** Supabase anon key. */
|
|
55
|
+
supabaseKey?: string
|
|
56
|
+
/** Pick a starter dataset by id, skipping the source question. */
|
|
57
|
+
dataset?: string
|
|
58
|
+
/** Theme preset id. */
|
|
59
|
+
theme?: string
|
|
60
|
+
dark?: boolean
|
|
61
|
+
/** App title (skips the question). */
|
|
62
|
+
title?: string
|
|
63
|
+
/** Take every default; ask nothing that has one. */
|
|
64
|
+
yes?: boolean
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type InitResult = {
|
|
68
|
+
project: StudioProject
|
|
69
|
+
/** Paths written, in order. */
|
|
70
|
+
written: string[]
|
|
71
|
+
/** What to tell the user to do next. */
|
|
72
|
+
nextSteps: string[]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const SOURCES = [
|
|
76
|
+
{ key: 'sample', label: 'Sample data', hint: 'a seeded pair of related tables - see a working app immediately' },
|
|
77
|
+
{ key: 'db', label: 'An existing database', hint: 'Postgres, MySQL, SQL Server or SQLite - reads your tables' },
|
|
78
|
+
{ key: 'supabase', label: 'Supabase', hint: 'project URL + anon key, read over the REST API - no driver needed' },
|
|
79
|
+
{ key: 'pglite', label: 'Local Postgres in the browser (PGlite)', hint: 'real SQL, persists, no server to set up' },
|
|
80
|
+
{ key: 'rest', label: 'A REST API or OpenAPI spec', hint: 'infers the tables from the endpoint' },
|
|
81
|
+
] as const
|
|
82
|
+
type SourceKey = (typeof SOURCES)[number]['key']
|
|
83
|
+
|
|
84
|
+
const DIALECTS: SqlDialectName[] = ['postgres', 'supabase', 'mysql', 'mssql', 'sqlite']
|
|
85
|
+
|
|
86
|
+
/** Ask a numbered-list question; returns the chosen index. */
|
|
87
|
+
async function pick(prompts: PromptIO, heading: string, items: { label: string; hint?: string }[], def = 1): Promise<number> {
|
|
88
|
+
prompts.say('')
|
|
89
|
+
prompts.say(heading)
|
|
90
|
+
items.forEach((item, i) => prompts.say(` ${i + 1}. ${item.label}${item.hint ? ` - ${item.hint}` : ''}`))
|
|
91
|
+
const answer = (await prompts.ask('Pick a number:', String(def))).trim()
|
|
92
|
+
const n = Number(answer)
|
|
93
|
+
return Number.isFinite(n) && n >= 1 && n <= items.length ? n - 1 : def - 1
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const isYes = (answer: string, def: boolean): boolean => {
|
|
97
|
+
const a = answer.trim().toLowerCase()
|
|
98
|
+
if (!a) return def
|
|
99
|
+
return a === 'y' || a === 'yes'
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Parse "all", "none", a comma list of names, or a list of 1-based numbers. */
|
|
103
|
+
export function parseSelection(answer: string, items: string[]): string[] {
|
|
104
|
+
const raw = answer.trim()
|
|
105
|
+
if (!raw || raw.toLowerCase() === 'all' || raw === '*') return items
|
|
106
|
+
if (raw.toLowerCase() === 'none') return []
|
|
107
|
+
const picked: string[] = []
|
|
108
|
+
for (const part of raw.split(',').map((p) => p.trim()).filter(Boolean)) {
|
|
109
|
+
const n = Number(part)
|
|
110
|
+
if (Number.isFinite(n) && n >= 1 && n <= items.length) {
|
|
111
|
+
const item = items[n - 1]!
|
|
112
|
+
if (!picked.includes(item)) picked.push(item)
|
|
113
|
+
continue
|
|
114
|
+
}
|
|
115
|
+
const match = items.find((i) => i.toLowerCase() === part.toLowerCase())
|
|
116
|
+
if (match && !picked.includes(match)) picked.push(match)
|
|
117
|
+
}
|
|
118
|
+
return picked
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The rows of a REST response: a bare array, or the usual wrapper key. */
|
|
122
|
+
function unwrapRows(body: unknown): Record<string, unknown>[] {
|
|
123
|
+
if (Array.isArray(body)) return body as Record<string, unknown>[]
|
|
124
|
+
if (body && typeof body === 'object') {
|
|
125
|
+
for (const key of ['data', 'items', 'results', 'rows', 'records']) {
|
|
126
|
+
const v = (body as Record<string, unknown>)[key]
|
|
127
|
+
if (Array.isArray(v)) return v as Record<string, unknown>[]
|
|
128
|
+
}
|
|
129
|
+
return [body as Record<string, unknown>]
|
|
130
|
+
}
|
|
131
|
+
return []
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
type Gathered = {
|
|
135
|
+
schemas: EntitySchema[]
|
|
136
|
+
sources: Record<string, EntityDataSource>
|
|
137
|
+
seed: Record<string, Record<string, unknown>[]>
|
|
138
|
+
kind: DataSourceKind
|
|
139
|
+
title?: string
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Step 1-2: where does the data come from, and what tables does it have? */
|
|
143
|
+
async function gatherData(flags: InitFlags, prompts: PromptIO, db: DbGateway | null, fetchText: FetchText | null): Promise<Gathered> {
|
|
144
|
+
let source: SourceKey = 'sample'
|
|
145
|
+
if (flags.db) source = 'db'
|
|
146
|
+
else if (flags.supabaseUrl) source = 'supabase'
|
|
147
|
+
else if (flags.dataset) source = 'sample'
|
|
148
|
+
else if (!flags.yes) source = SOURCES[await pick(prompts, 'Where should the app get its data?', [...SOURCES])]!.key
|
|
149
|
+
|
|
150
|
+
if (source === 'db') return gatherFromDatabase(flags, prompts, db)
|
|
151
|
+
if (source === 'supabase') return gatherFromSupabase(flags, prompts)
|
|
152
|
+
if (source === 'rest') return gatherFromRest(prompts, fetchText)
|
|
153
|
+
|
|
154
|
+
// Sample data, stored in memory or in PGlite.
|
|
155
|
+
let datasetId = flags.dataset ?? starterDatasets[0]!.id
|
|
156
|
+
if (!flags.dataset && !flags.yes) {
|
|
157
|
+
const i = await pick(prompts, 'Pick a starter dataset:', starterDatasets.map((d) => ({ label: d.name, hint: d.description })))
|
|
158
|
+
datasetId = starterDatasets[i]!.id
|
|
159
|
+
}
|
|
160
|
+
const dataset = getStarterDataset(datasetId)
|
|
161
|
+
if (!dataset) throw new Error(`Unknown dataset "${datasetId}". Try: ${starterDatasets.map((d) => d.id).join(', ')}`)
|
|
162
|
+
const { entities, seed } = dataset.build()
|
|
163
|
+
const kind: DataSourceKind = source === 'pglite' ? 'pglite' : 'memory'
|
|
164
|
+
const sources: Record<string, EntityDataSource> = {}
|
|
165
|
+
for (const e of entities) sources[e.name] = kind === 'pglite' ? { kind: 'pglite', table: e.name } : { kind: 'memory' }
|
|
166
|
+
prompts.say(` Using ${dataset.name} (${entities.length} tables).`)
|
|
167
|
+
return { schemas: entities, sources, seed, kind, title: dataset.name }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function gatherFromDatabase(flags: InitFlags, prompts: PromptIO, db: DbGateway | null): Promise<Gathered> {
|
|
171
|
+
if (!db) throw new Error('Connecting to a database is not available here.')
|
|
172
|
+
let dialect = flags.db
|
|
173
|
+
if (!dialect) {
|
|
174
|
+
const i = await pick(prompts, 'Which database?', DIALECTS.map((d) => ({ label: d })))
|
|
175
|
+
dialect = DIALECTS[i]!
|
|
176
|
+
}
|
|
177
|
+
let url = flags.url ?? ''
|
|
178
|
+
if (!url) {
|
|
179
|
+
if (isFileDialect(dialect)) {
|
|
180
|
+
url = (await prompts.ask('Database file:', './data.db')).trim()
|
|
181
|
+
} else {
|
|
182
|
+
const answer = (await prompts.ask('Connection string (blank to enter host/port instead):', '')).trim()
|
|
183
|
+
url = answer || buildConnectionString(dialect, await askConnectionParts(prompts))
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (!url) throw new Error('A connection string is required.')
|
|
187
|
+
|
|
188
|
+
const driver = await db.ensureDriver(dialect)
|
|
189
|
+
if (!driver.ok) throw new Error(driver.message ?? `Could not install the ${dialect} driver.`)
|
|
190
|
+
|
|
191
|
+
prompts.say(' Connecting...')
|
|
192
|
+
const execute = await db.connect(dialect, url)
|
|
193
|
+
const tables = await listDatabaseTables(dialect, execute)
|
|
194
|
+
if (tables.length === 0) throw new Error('Connected, but the database has no tables.')
|
|
195
|
+
|
|
196
|
+
const counts = await countTableRows(dialect, execute, tables)
|
|
197
|
+
prompts.say('')
|
|
198
|
+
prompts.say(`Found ${tables.length} ${tables.length === 1 ? 'table' : 'tables'}:`)
|
|
199
|
+
counts.forEach((t, i) => prompts.say(` ${i + 1}. ${t.name}${t.rows == null ? '' : ` (${t.rows} rows)`}`))
|
|
200
|
+
const answer = flags.yes ? 'all' : await prompts.ask('Which tables? (all, or a comma list of names/numbers)', 'all')
|
|
201
|
+
const picked = parseSelection(answer, tables)
|
|
202
|
+
if (picked.length === 0) throw new Error('No tables picked - nothing to build.')
|
|
203
|
+
|
|
204
|
+
const schemas: EntitySchema[] = []
|
|
205
|
+
const sources: Record<string, EntityDataSource> = {}
|
|
206
|
+
for (const table of picked) {
|
|
207
|
+
try {
|
|
208
|
+
const schema = await introspectDatabase({ dialect, table, execute })
|
|
209
|
+
schemas.push(schema)
|
|
210
|
+
sources[schema.name] = { kind: 'sql', table, dialect }
|
|
211
|
+
} catch (e) {
|
|
212
|
+
// One unreadable table must never sink the run.
|
|
213
|
+
prompts.say(` ! Skipped ${table}: ${e instanceof Error ? e.message : String(e)}`)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (schemas.length === 0) throw new Error('None of the picked tables could be read.')
|
|
217
|
+
prompts.say(` Read ${schemas.length} ${schemas.length === 1 ? 'table' : 'tables'}.`)
|
|
218
|
+
return { schemas, sources, seed: {}, kind: 'sql' }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Supabase reads over its own REST API, so this needs no driver and no
|
|
223
|
+
* DbGateway - just the project URL and the anon key.
|
|
224
|
+
*/
|
|
225
|
+
async function gatherFromSupabase(flags: InitFlags, prompts: PromptIO): Promise<Gathered> {
|
|
226
|
+
const url = (flags.supabaseUrl ?? (await prompts.ask('Project URL:', ''))).trim()
|
|
227
|
+
const key = (flags.supabaseKey ?? (await prompts.ask('Anon key:', ''))).trim()
|
|
228
|
+
if (!url || !key) throw new Error('Both the project URL and the anon key are required.')
|
|
229
|
+
|
|
230
|
+
prompts.say(' Reading the API...')
|
|
231
|
+
const tables = await listSupabaseTables(url, key)
|
|
232
|
+
let picked: string[]
|
|
233
|
+
if (tables.length) {
|
|
234
|
+
prompts.say('')
|
|
235
|
+
prompts.say(`Found ${tables.length} ${tables.length === 1 ? 'table' : 'tables'}:`)
|
|
236
|
+
tables.forEach((t, i) => prompts.say(` ${i + 1}. ${t}`))
|
|
237
|
+
const answer = flags.yes ? 'all' : await prompts.ask('Which tables? (all, or a comma list of names/numbers)', 'all')
|
|
238
|
+
picked = parseSelection(answer, tables)
|
|
239
|
+
} else {
|
|
240
|
+
// The API doc is often CORS-blocked or restricted; naming the tables by hand
|
|
241
|
+
// still works because each one is introspected individually.
|
|
242
|
+
prompts.say(" Couldn't list the tables (the API doc may be restricted for this key).")
|
|
243
|
+
const typed = await prompts.ask('Table names (comma separated):', '')
|
|
244
|
+
picked = typed.split(',').map((t) => t.trim()).filter(Boolean)
|
|
245
|
+
}
|
|
246
|
+
if (picked.length === 0) throw new Error('No tables picked - nothing to build.')
|
|
247
|
+
|
|
248
|
+
const schemas: EntitySchema[] = []
|
|
249
|
+
const sources: Record<string, EntityDataSource> = {}
|
|
250
|
+
for (const table of picked) {
|
|
251
|
+
const schema = await introspectSupabaseTable({ url, key, table })
|
|
252
|
+
if (!schema?.fields.length) {
|
|
253
|
+
prompts.say(` ! Skipped ${table}: no columns could be read.`)
|
|
254
|
+
continue
|
|
255
|
+
}
|
|
256
|
+
schemas.push(schema as EntitySchema)
|
|
257
|
+
sources[schema.name] = { kind: 'supabase', table, url, key }
|
|
258
|
+
}
|
|
259
|
+
if (schemas.length === 0) throw new Error('None of the picked tables could be read.')
|
|
260
|
+
prompts.say(` Read ${schemas.length} ${schemas.length === 1 ? 'table' : 'tables'}.`)
|
|
261
|
+
return { schemas, sources, seed: {}, kind: 'supabase' }
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async function askConnectionParts(prompts: PromptIO): Promise<SqlConnectionParts> {
|
|
265
|
+
return {
|
|
266
|
+
host: (await prompts.ask('Host:', 'localhost')).trim(),
|
|
267
|
+
port: (await prompts.ask('Port:', '')).trim(),
|
|
268
|
+
database: (await prompts.ask('Database:', 'app')).trim(),
|
|
269
|
+
user: (await prompts.ask('User:', 'postgres')).trim(),
|
|
270
|
+
password: (await prompts.ask('Password:', '')).trim(),
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function gatherFromRest(prompts: PromptIO, fetchText: FetchText | null): Promise<Gathered> {
|
|
275
|
+
if (!fetchText) throw new Error('Reading a REST endpoint is not available here.')
|
|
276
|
+
const src = (await prompts.ask('OpenAPI spec (file or URL), or a REST endpoint returning rows:', '')).trim()
|
|
277
|
+
if (!src) throw new Error('A spec path/URL or endpoint is required.')
|
|
278
|
+
const text = await fetchText(src)
|
|
279
|
+
|
|
280
|
+
// An OpenAPI document describes every resource; a plain endpoint gives one.
|
|
281
|
+
if (/"(openapi|swagger|paths)"\s*:/.test(text)) {
|
|
282
|
+
const { entities, sources, warnings } = introspectOpenApi(text)
|
|
283
|
+
for (const w of warnings) prompts.say(` ! ${w}`)
|
|
284
|
+
if (entities.length === 0) throw new Error('The spec described no collection resources.')
|
|
285
|
+
prompts.say(` Read ${entities.length} REST ${entities.length === 1 ? 'resource' : 'resources'}.`)
|
|
286
|
+
return { schemas: entities, sources, seed: {}, kind: 'rest' }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const rows = unwrapRows(JSON.parse(text) as unknown)
|
|
290
|
+
if (rows.length === 0) throw new Error('The endpoint returned no rows to read a shape from.')
|
|
291
|
+
const url = new URL(src)
|
|
292
|
+
const path = url.pathname.replace(/^\/+|\/+$/g, '')
|
|
293
|
+
const suggested = path.split('/').filter(Boolean).pop() || 'items'
|
|
294
|
+
const name = ((await prompts.ask('Table name:', suggested)).trim() || suggested).replace(/\W+/g, '_')
|
|
295
|
+
const schema = introspectJson(name, rows)
|
|
296
|
+
prompts.say(` Read ${schema.fields.length} fields from ${rows.length} rows.`)
|
|
297
|
+
return {
|
|
298
|
+
schemas: [schema],
|
|
299
|
+
sources: { [name]: { kind: 'rest', baseUrl: url.origin, path, method: 'GET', params: [] } },
|
|
300
|
+
seed: {},
|
|
301
|
+
kind: 'rest',
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Step 3: which pages does each table get, and how are rows edited? */
|
|
306
|
+
async function askScreens(schemas: EntitySchema[], flags: InitFlags, prompts: PromptIO): Promise<Record<string, CrudSuiteOptions>> {
|
|
307
|
+
const perEntity: Record<string, CrudSuiteOptions> = {}
|
|
308
|
+
let editing: CrudEditingMode = 'form'
|
|
309
|
+
let full = true
|
|
310
|
+
|
|
311
|
+
if (!flags.yes) {
|
|
312
|
+
full = isYes(await prompts.ask('\nGenerate a list, an edit form and a record page for each table? (Y/n)', 'y'), true)
|
|
313
|
+
const modes: { label: string; value: CrudEditingMode }[] = [
|
|
314
|
+
{ label: 'A popup form', value: 'form' },
|
|
315
|
+
{ label: 'In the grid (like a spreadsheet)', value: 'inline' },
|
|
316
|
+
{ label: 'On the record page', value: 'detail' },
|
|
317
|
+
]
|
|
318
|
+
editing = modes[await pick(prompts, 'How should rows be edited?', modes)]!.value
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
for (const schema of schemas) {
|
|
322
|
+
const hasChildren = schemas.some((e) => e.fields.some((f) => f.type === 'relation' && f.relation?.entity === schema.name))
|
|
323
|
+
const screens: CrudScreenKind[] = full
|
|
324
|
+
? hasChildren || editing === 'detail' ? ['list', 'form', 'detail'] : ['list', 'form']
|
|
325
|
+
: ['list']
|
|
326
|
+
perEntity[schema.name] = { screens, editing }
|
|
327
|
+
}
|
|
328
|
+
return perEntity
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Step 4: the app's look. */
|
|
332
|
+
async function askTheme(flags: InitFlags, prompts: PromptIO): Promise<ProjectTheme | undefined> {
|
|
333
|
+
let preset = flags.theme?.trim().toLowerCase()
|
|
334
|
+
if (preset && !getStudioTheme(preset)) {
|
|
335
|
+
prompts.say(` ! Unknown theme "${preset}" - using tailwind.`)
|
|
336
|
+
preset = undefined
|
|
337
|
+
}
|
|
338
|
+
if (!preset && !flags.yes) {
|
|
339
|
+
const i = await pick(prompts, 'Theme:', studioThemes.map((t) => ({ label: t.name, hint: t.id })))
|
|
340
|
+
preset = studioThemes[i]?.id
|
|
341
|
+
}
|
|
342
|
+
preset ??= 'tailwind'
|
|
343
|
+
const dark = flags.dark ?? (flags.yes ? false : isYes(await prompts.ask('Dark mode? (y/N)', 'n'), false))
|
|
344
|
+
return { preset, mode: dark ? 'dark' : 'light' }
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Merge-write the generated files, preserving edits outside managed regions. */
|
|
348
|
+
async function writeAll(files: { path: string; contents: string; userOwned?: boolean }[], outDir: string, io: StudioIO): Promise<string[]> {
|
|
349
|
+
const written: string[] = []
|
|
350
|
+
const prefix = !outDir || outDir === '.' ? '' : outDir.replace(/[\\/]+$/, '') + '/'
|
|
351
|
+
for (const file of files) {
|
|
352
|
+
const path = prefix + file.path
|
|
353
|
+
const existing = await io.readFile(path)
|
|
354
|
+
if (skipUserOwned(file, existing != null)) continue
|
|
355
|
+
await io.writeFile(path, mergeManaged(existing, file.contents))
|
|
356
|
+
written.push(path)
|
|
357
|
+
}
|
|
358
|
+
return written
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The whole guided flow: ask, build, write. Returns the project and what was
|
|
363
|
+
* written so the caller can print next steps (or assert them in a test).
|
|
364
|
+
*/
|
|
365
|
+
export async function runStudioInit(
|
|
366
|
+
flags: InitFlags,
|
|
367
|
+
prompts: PromptIO,
|
|
368
|
+
db: DbGateway | null,
|
|
369
|
+
io: StudioIO,
|
|
370
|
+
fetchText: FetchText | null = null,
|
|
371
|
+
): Promise<InitResult> {
|
|
372
|
+
prompts.say('SvGrid Studio - let\'s build your app.')
|
|
373
|
+
|
|
374
|
+
const data = await gatherData(flags, prompts, db, fetchText)
|
|
375
|
+
const perEntity = await askScreens(data.schemas, flags, prompts)
|
|
376
|
+
const theme = await askTheme(flags, prompts)
|
|
377
|
+
|
|
378
|
+
const defaultTitle = flags.title ?? data.title ?? 'My app'
|
|
379
|
+
const title = flags.yes || flags.title ? defaultTitle : (await prompts.ask('\nApp name:', defaultTitle)).trim() || defaultTitle
|
|
380
|
+
const outDir = flags.out ?? (flags.yes ? '.' : (await prompts.ask('Write it where?', '.')).trim() || '.')
|
|
381
|
+
|
|
382
|
+
const project = crudAppFromSchemas(data.schemas, {
|
|
383
|
+
title,
|
|
384
|
+
dataSource: data.kind,
|
|
385
|
+
sources: data.sources,
|
|
386
|
+
seed: data.seed,
|
|
387
|
+
perEntity,
|
|
388
|
+
overviewDashboard: true,
|
|
389
|
+
...(theme ? { theme } : {}),
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
const files = emitStudioAppBundle(project)
|
|
393
|
+
const written = await writeAll(files, outDir, io)
|
|
394
|
+
// The design itself, so `svgrid-studio designer` reopens exactly this app.
|
|
395
|
+
const configPath = (!outDir || outDir === '.' ? '' : outDir.replace(/[\\/]+$/, '') + '/') + 'studio.config.json'
|
|
396
|
+
await io.writeFile(configPath, serializeProject(project))
|
|
397
|
+
written.push(configPath)
|
|
398
|
+
|
|
399
|
+
prompts.say('')
|
|
400
|
+
prompts.say(`Built "${title}": ${data.schemas.length} ${data.schemas.length === 1 ? 'table' : 'tables'}, ${project.screens.length} screens, ${written.length} files.`)
|
|
401
|
+
|
|
402
|
+
const nextSteps = [
|
|
403
|
+
...(outDir && outDir !== '.' ? [`cd ${outDir}`] : []),
|
|
404
|
+
'npm install',
|
|
405
|
+
'npm run dev',
|
|
406
|
+
'svgrid-studio designer # keep editing it visually',
|
|
407
|
+
]
|
|
408
|
+
return { project, written, nextSteps }
|
|
409
|
+
}
|