on-zero 0.1.22 → 0.1.23
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/cjs/cli.cjs +17 -424
- package/dist/cjs/cli.js +7 -402
- package/dist/cjs/cli.js.map +2 -2
- package/dist/cjs/cli.native.js +15 -519
- package/dist/cjs/cli.native.js.map +1 -1
- package/dist/cjs/generate.cjs +370 -0
- package/dist/cjs/generate.js +339 -0
- package/dist/cjs/generate.js.map +6 -0
- package/dist/cjs/generate.native.js +464 -0
- package/dist/cjs/generate.native.js.map +1 -0
- package/dist/cjs/vite-plugin.cjs +37 -121
- package/dist/cjs/vite-plugin.js +41 -100
- package/dist/cjs/vite-plugin.js.map +1 -1
- package/dist/cjs/vite-plugin.native.js +47 -157
- package/dist/cjs/vite-plugin.native.js.map +1 -1
- package/dist/esm/cli.js +8 -388
- package/dist/esm/cli.js.map +2 -2
- package/dist/esm/cli.mjs +17 -402
- package/dist/esm/cli.mjs.map +1 -1
- package/dist/esm/cli.native.js +15 -497
- package/dist/esm/cli.native.js.map +1 -1
- package/dist/esm/generate.js +317 -0
- package/dist/esm/generate.js.map +6 -0
- package/dist/esm/generate.mjs +335 -0
- package/dist/esm/generate.mjs.map +1 -0
- package/dist/esm/generate.native.js +426 -0
- package/dist/esm/generate.native.js.map +1 -0
- package/dist/esm/vite-plugin.js +42 -102
- package/dist/esm/vite-plugin.js.map +1 -1
- package/dist/esm/vite-plugin.mjs +37 -121
- package/dist/esm/vite-plugin.mjs.map +1 -1
- package/dist/esm/vite-plugin.native.js +47 -157
- package/dist/esm/vite-plugin.native.js.map +1 -1
- package/package.json +2 -2
- package/readme.md +0 -29
- package/src/cli.ts +9 -646
- package/src/generate.ts +490 -0
- package/src/vite-plugin.ts +61 -189
- package/types/generate.d.ts +21 -0
- package/types/generate.d.ts.map +1 -0
- package/types/vite-plugin.d.ts +6 -29
- package/types/vite-plugin.d.ts.map +1 -1
package/dist/cjs/cli.cjs
CHANGED
|
@@ -1,131 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf,
|
|
7
|
-
__hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
10
|
-
get: () => from[key],
|
|
11
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
12
|
-
});
|
|
13
|
-
return to;
|
|
14
|
-
};
|
|
15
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
16
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
17
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
18
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
19
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
21
|
-
value: mod,
|
|
22
|
-
enumerable: !0
|
|
23
|
-
}) : target, mod));
|
|
24
|
-
var import_node_crypto = require("node:crypto"),
|
|
25
|
-
import_node_fs = require("node:fs"),
|
|
26
|
-
import_node_path = require("node:path"),
|
|
27
|
-
import_model = require("@sinclair/typebox-codegen/model"),
|
|
28
|
-
import_typescript = require("@sinclair/typebox-codegen/typescript"),
|
|
2
|
+
var import_node_path = require("node:path"),
|
|
29
3
|
import_citty = require("citty"),
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
let generateCache = {},
|
|
33
|
-
generateCachePath = "";
|
|
34
|
-
function getCacheDir() {
|
|
35
|
-
let dir = process.cwd();
|
|
36
|
-
for (; dir !== "/";) {
|
|
37
|
-
const nm = (0, import_node_path.resolve)(dir, "node_modules");
|
|
38
|
-
if ((0, import_node_fs.existsSync)(nm)) {
|
|
39
|
-
const cacheDir = (0, import_node_path.resolve)(nm, ".on-zero");
|
|
40
|
-
return (0, import_node_fs.existsSync)(cacheDir) || (0, import_node_fs.mkdirSync)(cacheDir, {
|
|
41
|
-
recursive: !0
|
|
42
|
-
}), cacheDir;
|
|
43
|
-
}
|
|
44
|
-
dir = (0, import_node_path.resolve)(dir, "..");
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
function loadCache() {
|
|
49
|
-
const cacheDir = getCacheDir();
|
|
50
|
-
if (cacheDir) {
|
|
51
|
-
generateCachePath = (0, import_node_path.resolve)(cacheDir, "generate-cache.json");
|
|
52
|
-
try {
|
|
53
|
-
generateCache = JSON.parse((0, import_node_fs.readFileSync)(generateCachePath, "utf-8"));
|
|
54
|
-
} catch {
|
|
55
|
-
generateCache = {};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
function saveCache() {
|
|
60
|
-
generateCachePath && (0, import_node_fs.writeFileSync)(generateCachePath, JSON.stringify(generateCache) + `
|
|
61
|
-
`, "utf-8");
|
|
62
|
-
}
|
|
63
|
-
function writeFileIfChanged(filePath, content) {
|
|
64
|
-
const contentHash = hash(content);
|
|
65
|
-
return generateCache[filePath] === contentHash && (0, import_node_fs.existsSync)(filePath) ? !1 : ((0, import_node_fs.writeFileSync)(filePath, content, "utf-8"), generateCache[filePath] = contentHash, !0);
|
|
66
|
-
}
|
|
67
|
-
const generateQueries = (0, import_citty.defineCommand)({
|
|
68
|
-
meta: {
|
|
69
|
-
name: "generate-queries",
|
|
70
|
-
description: "Generate server-side query validators from TypeScript query functions"
|
|
71
|
-
},
|
|
72
|
-
args: {
|
|
73
|
-
dir: {
|
|
74
|
-
type: "positional",
|
|
75
|
-
description: "Directory containing query files",
|
|
76
|
-
required: !1,
|
|
77
|
-
default: "."
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
async run({
|
|
81
|
-
args
|
|
82
|
-
}) {
|
|
83
|
-
const dir = (0, import_node_path.resolve)(args.dir),
|
|
84
|
-
{
|
|
85
|
-
readdirSync: readdirSync2
|
|
86
|
-
} = await import("node:fs"),
|
|
87
|
-
files = readdirSync2(dir).filter(f => f.endsWith(".ts")),
|
|
88
|
-
allQueries = [],
|
|
89
|
-
results = await Promise.all(files.map(async file => {
|
|
90
|
-
const filePath = (0, import_node_path.resolve)(dir, file),
|
|
91
|
-
queries = [];
|
|
92
|
-
try {
|
|
93
|
-
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8"),
|
|
94
|
-
sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, !0);
|
|
95
|
-
ts.forEachChild(sourceFile, node => {
|
|
96
|
-
if (ts.isVariableStatement(node)) {
|
|
97
|
-
if (!node.modifiers?.find(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
|
|
98
|
-
const declaration = node.declarationList.declarations[0];
|
|
99
|
-
if (!declaration || !ts.isVariableDeclaration(declaration)) return;
|
|
100
|
-
const name = declaration.name.getText(sourceFile);
|
|
101
|
-
if (declaration.initializer && ts.isArrowFunction(declaration.initializer)) {
|
|
102
|
-
const params = declaration.initializer.parameters;
|
|
103
|
-
let paramType = "void";
|
|
104
|
-
params.length > 0 && (paramType = params[0].type?.getText(sourceFile) || "unknown");
|
|
105
|
-
try {
|
|
106
|
-
const typeString = `type QueryParams = ${paramType}`,
|
|
107
|
-
model = import_typescript.TypeScriptToModel.Generate(typeString),
|
|
108
|
-
valibotCode = import_model.ModelToValibot.Generate(model);
|
|
109
|
-
queries.push({
|
|
110
|
-
name,
|
|
111
|
-
params: paramType,
|
|
112
|
-
valibotCode
|
|
113
|
-
});
|
|
114
|
-
} catch (err) {
|
|
115
|
-
console.error(`\u2717 ${name}: ${err}`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
} catch (err) {
|
|
121
|
-
console.error(`Error processing ${file}:`, err);
|
|
122
|
-
}
|
|
123
|
-
return queries;
|
|
124
|
-
}));
|
|
125
|
-
allQueries.push(...results.flat()), console.info(`\u2713 ${allQueries.length} query validators`);
|
|
126
|
-
}
|
|
127
|
-
}),
|
|
128
|
-
generate = (0, import_citty.defineCommand)({
|
|
4
|
+
import_generate = require("./generate.cjs");
|
|
5
|
+
const generateCommand = (0, import_citty.defineCommand)({
|
|
129
6
|
meta: {
|
|
130
7
|
name: "generate",
|
|
131
8
|
description: "Generate models, types, tables, and query validators"
|
|
@@ -152,304 +29,20 @@ const generateQueries = (0, import_citty.defineCommand)({
|
|
|
152
29
|
async run({
|
|
153
30
|
args
|
|
154
31
|
}) {
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
})))).filter(c => c.hasSchema).map(c => c.file),
|
|
170
|
-
[modelsOutput, typesOutput, tablesOutput, readmeOutput] = await Promise.all([Promise.resolve(generateModelsFile(allModelFiles)), Promise.resolve(generateTypesFile(filesWithSchema)), Promise.resolve(generateTablesFile(filesWithSchema)), Promise.resolve(generateReadmeFile())]),
|
|
171
|
-
filesChanged = (await Promise.all([Promise.resolve(writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "models.ts"), modelsOutput)), Promise.resolve(writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "types.ts"), typesOutput)), Promise.resolve(writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "tables.ts"), tablesOutput)), Promise.resolve(writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "README.md"), readmeOutput))])).filter(Boolean).length;
|
|
172
|
-
if (filesChanged > 0 && !silent && console.info(` \u{1F4DD} Updated ${filesChanged} file(s)`), (0, import_node_fs.existsSync)(queriesDir)) {
|
|
173
|
-
const queryFiles = (0, import_node_fs.readdirSync)(queriesDir).filter(f => f.endsWith(".ts")),
|
|
174
|
-
allQueries = (await Promise.all(queryFiles.map(async file => {
|
|
175
|
-
const filePath = (0, import_node_path.resolve)(queriesDir, file),
|
|
176
|
-
fileBaseName = (0, import_node_path.basename)(file, ".ts"),
|
|
177
|
-
queries = [];
|
|
178
|
-
try {
|
|
179
|
-
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8"),
|
|
180
|
-
sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, !0);
|
|
181
|
-
ts.forEachChild(sourceFile, node => {
|
|
182
|
-
if (ts.isVariableStatement(node)) {
|
|
183
|
-
if (!node.modifiers?.find(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
|
|
184
|
-
const declaration = node.declarationList.declarations[0];
|
|
185
|
-
if (!declaration || !ts.isVariableDeclaration(declaration)) return;
|
|
186
|
-
const name = declaration.name.getText(sourceFile);
|
|
187
|
-
if (name === "permission") return;
|
|
188
|
-
if (declaration.initializer && ts.isArrowFunction(declaration.initializer)) {
|
|
189
|
-
const params = declaration.initializer.parameters;
|
|
190
|
-
let paramType = "void";
|
|
191
|
-
params.length > 0 && (paramType = params[0].type?.getText(sourceFile) || "unknown");
|
|
192
|
-
try {
|
|
193
|
-
const typeString = `type QueryParams = ${paramType}`,
|
|
194
|
-
model = import_typescript.TypeScriptToModel.Generate(typeString),
|
|
195
|
-
valibotCode = import_model.ModelToValibot.Generate(model);
|
|
196
|
-
queries.push({
|
|
197
|
-
name,
|
|
198
|
-
params: paramType,
|
|
199
|
-
valibotCode,
|
|
200
|
-
sourceFile: fileBaseName
|
|
201
|
-
});
|
|
202
|
-
} catch (err) {
|
|
203
|
-
console.error(`\u2717 ${name}: ${err}`);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
} catch (err) {
|
|
209
|
-
console.error(`Error processing ${file}:`, err);
|
|
210
|
-
}
|
|
211
|
-
return queries;
|
|
212
|
-
}))).flat(),
|
|
213
|
-
groupedQueriesOutput = generateGroupedQueriesFile(allQueries),
|
|
214
|
-
syncedQueriesOutput = generateSyncedQueriesFile(allQueries),
|
|
215
|
-
groupedChanged = writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "groupedQueries.ts"), groupedQueriesOutput),
|
|
216
|
-
syncedChanged = writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "syncedQueries.ts"), syncedQueriesOutput),
|
|
217
|
-
queryFilesChanged = (groupedChanged ? 1 : 0) + (syncedChanged ? 1 : 0),
|
|
218
|
-
totalFilesChanged = filesChanged + queryFilesChanged;
|
|
219
|
-
if (totalFilesChanged > 0 && !silent && (groupedChanged && console.info(" \u{1F4DD} Updated groupedQueries.ts"), syncedChanged && console.info(" \u{1F4DD} Updated syncedQueries.ts"), console.info(`\u2713 ${allModelFiles.length} models (${filesWithSchema.length} schemas), ${allQueries.length} queries`)), totalFilesChanged > 0 && runAfter && args.after) try {
|
|
220
|
-
const {
|
|
221
|
-
execSync
|
|
222
|
-
} = await import("node:child_process");
|
|
223
|
-
execSync(args.after, {
|
|
224
|
-
stdio: "inherit",
|
|
225
|
-
env: {
|
|
226
|
-
...process.env,
|
|
227
|
-
ON_ZERO_GENERATED_DIR: generatedDir
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
} catch (err) {
|
|
231
|
-
console.error(`Error running after command: ${err}`);
|
|
232
|
-
}
|
|
233
|
-
saveCache();
|
|
234
|
-
} else {
|
|
235
|
-
if (filesChanged > 0 && !silent && console.info(`\u2713 ${allModelFiles.length} models (${filesWithSchema.length} schemas)`), filesChanged > 0 && runAfter && args.after) try {
|
|
236
|
-
const {
|
|
237
|
-
execSync
|
|
238
|
-
} = await import("node:child_process");
|
|
239
|
-
execSync(args.after, {
|
|
240
|
-
stdio: "inherit",
|
|
241
|
-
env: {
|
|
242
|
-
...process.env,
|
|
243
|
-
ON_ZERO_GENERATED_DIR: generatedDir
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
} catch (err) {
|
|
247
|
-
console.error(`Error running after command: ${err}`);
|
|
248
|
-
}
|
|
249
|
-
saveCache();
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
if (await runGenerate({
|
|
253
|
-
silent: args.watch,
|
|
254
|
-
runAfter: !0
|
|
255
|
-
}), args.watch) {
|
|
256
|
-
console.info(`\u{1F440} watching...
|
|
257
|
-
`);
|
|
258
|
-
const chokidar = await import("chokidar");
|
|
259
|
-
let debounceTimer = null;
|
|
260
|
-
const debouncedRegenerate = (path, event) => {
|
|
261
|
-
debounceTimer && clearTimeout(debounceTimer), console.info(`
|
|
262
|
-
${event} ${path}`), debounceTimer = setTimeout(() => {
|
|
263
|
-
runGenerate();
|
|
264
|
-
}, 1e3);
|
|
265
|
-
},
|
|
266
|
-
watcher = chokidar.watch([modelsDir, queriesDir], {
|
|
267
|
-
persistent: !0,
|
|
268
|
-
ignoreInitial: !0,
|
|
269
|
-
ignored: [generatedDir]
|
|
270
|
-
});
|
|
271
|
-
watcher.on("change", path => debouncedRegenerate(path, "\u{1F4DD}")), watcher.on("add", path => debouncedRegenerate(path, "\u2795")), watcher.on("unlink", path => debouncedRegenerate(path, "\u{1F5D1}\uFE0F ")), await new Promise(() => {});
|
|
272
|
-
}
|
|
32
|
+
const opts = {
|
|
33
|
+
dir: (0, import_node_path.resolve)(args.dir),
|
|
34
|
+
after: args.after
|
|
35
|
+
};
|
|
36
|
+
args.watch ? (await (0, import_generate.watch)(opts), await new Promise(() => {})) : await (0, import_generate.generate)(opts);
|
|
37
|
+
}
|
|
38
|
+
}),
|
|
39
|
+
main = (0, import_citty.defineCommand)({
|
|
40
|
+
meta: {
|
|
41
|
+
name: "on-zero",
|
|
42
|
+
description: "on-zero CLI tools"
|
|
43
|
+
},
|
|
44
|
+
subCommands: {
|
|
45
|
+
generate: generateCommand
|
|
273
46
|
}
|
|
274
47
|
});
|
|
275
|
-
function generateModelsFile(modelFiles) {
|
|
276
|
-
const modelNames = modelFiles.map(f => (0, import_node_path.basename)(f, ".ts")).sort(),
|
|
277
|
-
getImportName = name => name === "user" ? "userPublic" : name,
|
|
278
|
-
imports = modelNames.map(name => `import * as ${getImportName(name)} from '../models/${name}'`).join(`
|
|
279
|
-
`),
|
|
280
|
-
modelsObj = `export const models = {
|
|
281
|
-
${[...modelNames].sort((a, b) => getImportName(a).localeCompare(getImportName(b))).map(name => ` ${getImportName(name)},`).join(`
|
|
282
|
-
`)}
|
|
283
|
-
}`;
|
|
284
|
-
return `// auto-generated by: on-zero generate
|
|
285
|
-
${imports}
|
|
286
|
-
|
|
287
|
-
${modelsObj}
|
|
288
|
-
|
|
289
|
-
if (import.meta.hot) {
|
|
290
|
-
import.meta.hot.accept()
|
|
291
|
-
}
|
|
292
|
-
`;
|
|
293
|
-
}
|
|
294
|
-
function generateTypesFile(modelFiles) {
|
|
295
|
-
const modelNames = modelFiles.map(f => (0, import_node_path.basename)(f, ".ts")).sort(),
|
|
296
|
-
getSchemaName = name => name === "user" ? "userPublic" : name;
|
|
297
|
-
return `import type { TableInsertRow, TableUpdateRow } from 'on-zero'
|
|
298
|
-
import type * as schema from './tables'
|
|
299
|
-
|
|
300
|
-
${modelNames.map(name => {
|
|
301
|
-
const pascalName = name.charAt(0).toUpperCase() + name.slice(1),
|
|
302
|
-
schemaName = getSchemaName(name);
|
|
303
|
-
return `export type ${pascalName} = TableInsertRow<typeof schema.${schemaName}>
|
|
304
|
-
export type ${pascalName}Update = TableUpdateRow<typeof schema.${schemaName}>`;
|
|
305
|
-
}).join(`
|
|
306
|
-
|
|
307
|
-
`)}
|
|
308
|
-
`;
|
|
309
|
-
}
|
|
310
|
-
function generateTablesFile(modelFiles) {
|
|
311
|
-
const modelNames = modelFiles.map(f => (0, import_node_path.basename)(f, ".ts")).sort(),
|
|
312
|
-
getExportName = name => name === "user" ? "userPublic" : name;
|
|
313
|
-
return `// auto-generated by: on-zero generate
|
|
314
|
-
// this is separate from models as otherwise you end up with circular types :/
|
|
315
|
-
|
|
316
|
-
${modelNames.map(name => `export { schema as ${getExportName(name)} } from '../models/${name}'`).join(`
|
|
317
|
-
`)}
|
|
318
|
-
`;
|
|
319
|
-
}
|
|
320
|
-
function generateGroupedQueriesFile(queries) {
|
|
321
|
-
return `/**
|
|
322
|
-
* auto-generated by: on-zero generate
|
|
323
|
-
*
|
|
324
|
-
* grouped query re-exports for minification-safe query identity.
|
|
325
|
-
* this file re-exports all query modules - while this breaks tree-shaking,
|
|
326
|
-
* queries are typically small and few in number even in larger apps.
|
|
327
|
-
*/
|
|
328
|
-
${[...new Set(queries.map(q => q.sourceFile))].sort().map(file => `export * as ${file} from '../queries/${file}'`).join(`
|
|
329
|
-
`)}
|
|
330
|
-
`;
|
|
331
|
-
}
|
|
332
|
-
function generateSyncedQueriesFile(queries) {
|
|
333
|
-
const queryByFile = /* @__PURE__ */new Map();
|
|
334
|
-
for (const q of queries) queryByFile.has(q.sourceFile) || queryByFile.set(q.sourceFile, []), queryByFile.get(q.sourceFile).push(q);
|
|
335
|
-
const sortedFiles = Array.from(queryByFile.keys()).sort(),
|
|
336
|
-
imports = `// auto-generated by: on-zero generate
|
|
337
|
-
// server-side query definitions with validators
|
|
338
|
-
import { defineQuery, defineQueries } from '@rocicorp/zero'
|
|
339
|
-
import * as v from 'valibot'
|
|
340
|
-
import * as Queries from './groupedQueries'
|
|
341
|
-
`,
|
|
342
|
-
namespaceDefs = sortedFiles.map(file => {
|
|
343
|
-
const queryDefs = queryByFile.get(file).sort((a, b) => a.name.localeCompare(b.name)).map(q => {
|
|
344
|
-
const lines = q.valibotCode.split(`
|
|
345
|
-
`).filter(l => l.trim()),
|
|
346
|
-
schemaLineIndex = lines.findIndex(l => l.startsWith("export const QueryParams"));
|
|
347
|
-
let validatorDef = "";
|
|
348
|
-
if (schemaLineIndex !== -1) {
|
|
349
|
-
const schemaLines = [];
|
|
350
|
-
let openBraces = 0,
|
|
351
|
-
started = !1;
|
|
352
|
-
for (let i = schemaLineIndex; i < lines.length; i++) {
|
|
353
|
-
const line = lines[i],
|
|
354
|
-
cleaned = started ? line : line.replace("export const QueryParams = ", "");
|
|
355
|
-
if (schemaLines.push(cleaned), started = !0, openBraces += (cleaned.match(/\{/g) || []).length, openBraces -= (cleaned.match(/\}/g) || []).length, openBraces += (cleaned.match(/\(/g) || []).length, openBraces -= (cleaned.match(/\)/g) || []).length, openBraces === 0 && schemaLines.length > 0) break;
|
|
356
|
-
}
|
|
357
|
-
validatorDef = schemaLines.join(`
|
|
358
|
-
`);
|
|
359
|
-
}
|
|
360
|
-
if (q.params === "void" || !validatorDef) return ` ${q.name}: defineQuery(() => Queries.${file}.${q.name}()),`;
|
|
361
|
-
const indentedValidator = validatorDef.split(`
|
|
362
|
-
`).map((line, i) => i === 0 ? line : ` ${line}`).join(`
|
|
363
|
-
`);
|
|
364
|
-
return ` ${q.name}: defineQuery(
|
|
365
|
-
${indentedValidator},
|
|
366
|
-
({ args }) => Queries.${file}.${q.name}(args)
|
|
367
|
-
),`;
|
|
368
|
-
}).join(`
|
|
369
|
-
`);
|
|
370
|
-
return `const ${file} = {
|
|
371
|
-
${queryDefs}
|
|
372
|
-
}`;
|
|
373
|
-
}).join(`
|
|
374
|
-
|
|
375
|
-
`),
|
|
376
|
-
queriesObject = sortedFiles.map(file => ` ${file},`).join(`
|
|
377
|
-
`);
|
|
378
|
-
return `${imports}
|
|
379
|
-
${namespaceDefs}
|
|
380
|
-
|
|
381
|
-
export const queries = defineQueries({
|
|
382
|
-
${queriesObject}
|
|
383
|
-
})
|
|
384
|
-
`;
|
|
385
|
-
}
|
|
386
|
-
function generateReadmeFile() {
|
|
387
|
-
return `# generated
|
|
388
|
-
|
|
389
|
-
this folder is auto-generated by on-zero. do not edit files here directly.
|
|
390
|
-
|
|
391
|
-
## what's generated
|
|
392
|
-
|
|
393
|
-
- \`models.ts\` - exports all models from ../models
|
|
394
|
-
- \`types.ts\` - typescript types derived from table schemas
|
|
395
|
-
- \`tables.ts\` - exports table schemas for type inference
|
|
396
|
-
- \`groupedQueries.ts\` - namespaced query re-exports for client setup
|
|
397
|
-
- \`syncedQueries.ts\` - namespaced syncedQuery wrappers for server setup
|
|
398
|
-
|
|
399
|
-
## usage guidelines
|
|
400
|
-
|
|
401
|
-
**do not import generated files outside of the data folder.**
|
|
402
|
-
|
|
403
|
-
### queries
|
|
404
|
-
|
|
405
|
-
write your queries as plain functions in \`../queries/\` and import them directly:
|
|
406
|
-
|
|
407
|
-
\`\`\`ts
|
|
408
|
-
// \u2705 good - import from queries
|
|
409
|
-
import { channelMessages } from '~/data/queries/message'
|
|
410
|
-
\`\`\`
|
|
411
|
-
|
|
412
|
-
the generated query files are only used internally by zero client/server setup.
|
|
413
|
-
|
|
414
|
-
### types
|
|
415
|
-
|
|
416
|
-
you can import types from this folder, but prefer re-exporting from \`../types.ts\`:
|
|
417
|
-
|
|
418
|
-
\`\`\`ts
|
|
419
|
-
// \u274C okay but not preferred
|
|
420
|
-
import type { Message } from '~/data/generated/types'
|
|
421
|
-
|
|
422
|
-
// \u2705 better - re-export from types.ts
|
|
423
|
-
import type { Message } from '~/data/types'
|
|
424
|
-
\`\`\`
|
|
425
|
-
|
|
426
|
-
## regeneration
|
|
427
|
-
|
|
428
|
-
files are regenerated when you run:
|
|
429
|
-
|
|
430
|
-
\`\`\`bash
|
|
431
|
-
bun on-zero generate
|
|
432
|
-
\`\`\`
|
|
433
|
-
|
|
434
|
-
or in watch mode:
|
|
435
|
-
|
|
436
|
-
\`\`\`bash
|
|
437
|
-
bun on-zero generate --watch
|
|
438
|
-
\`\`\`
|
|
439
|
-
|
|
440
|
-
## more info
|
|
441
|
-
|
|
442
|
-
see the [on-zero readme](./node_modules/on-zero/README.md) for full documentation.
|
|
443
|
-
`;
|
|
444
|
-
}
|
|
445
|
-
const main = (0, import_citty.defineCommand)({
|
|
446
|
-
meta: {
|
|
447
|
-
name: "on-zero",
|
|
448
|
-
description: "on-zero CLI tools"
|
|
449
|
-
},
|
|
450
|
-
subCommands: {
|
|
451
|
-
generate,
|
|
452
|
-
"generate-queries": generateQueries
|
|
453
|
-
}
|
|
454
|
-
});
|
|
455
48
|
(0, import_citty.runMain)(main);
|