tthr 0.0.33 → 0.0.35
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/chunk-LXODXQ5V.js +418 -0
- package/dist/chunk-ZWLVHKNL.js +414 -0
- package/dist/generate-26HERX3T.js +8 -0
- package/dist/generate-YQ4QRPXF.js +8 -0
- package/dist/index.js +655 -575
- package/package.json +6 -4
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
// src/commands/generate.ts
|
|
2
|
+
import chalk2 from "chalk";
|
|
3
|
+
import ora from "ora";
|
|
4
|
+
import fs3 from "fs-extra";
|
|
5
|
+
import path3 from "path";
|
|
6
|
+
|
|
7
|
+
// src/utils/auth.ts
|
|
8
|
+
import chalk from "chalk";
|
|
9
|
+
import fs from "fs-extra";
|
|
10
|
+
import path from "path";
|
|
11
|
+
var CONFIG_DIR = path.join(process.env.HOME || process.env.USERPROFILE || "", ".tether");
|
|
12
|
+
var CREDENTIALS_FILE = path.join(CONFIG_DIR, "credentials.json");
|
|
13
|
+
async function getCredentials() {
|
|
14
|
+
try {
|
|
15
|
+
if (await fs.pathExists(CREDENTIALS_FILE)) {
|
|
16
|
+
return await fs.readJSON(CREDENTIALS_FILE);
|
|
17
|
+
}
|
|
18
|
+
} catch {
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
async function requireAuth() {
|
|
23
|
+
const credentials = await getCredentials();
|
|
24
|
+
if (!credentials) {
|
|
25
|
+
console.error(chalk.red("\n\u2717 Not logged in\n"));
|
|
26
|
+
console.log(chalk.dim("Run `tthr login` to authenticate\n"));
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
if (credentials.expiresAt) {
|
|
30
|
+
const expiresAt = new Date(credentials.expiresAt);
|
|
31
|
+
if (/* @__PURE__ */ new Date() > expiresAt) {
|
|
32
|
+
console.error(chalk.red("\n\u2717 Session expired\n"));
|
|
33
|
+
console.log(chalk.dim("Run `tthr login` to authenticate\n"));
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return credentials;
|
|
38
|
+
}
|
|
39
|
+
async function saveCredentials(credentials) {
|
|
40
|
+
await fs.ensureDir(CONFIG_DIR);
|
|
41
|
+
await fs.writeJSON(CREDENTIALS_FILE, credentials, { spaces: 2, mode: 384 });
|
|
42
|
+
}
|
|
43
|
+
async function clearCredentials() {
|
|
44
|
+
try {
|
|
45
|
+
await fs.remove(CREDENTIALS_FILE);
|
|
46
|
+
} catch {
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/utils/config.ts
|
|
51
|
+
import fs2 from "fs-extra";
|
|
52
|
+
import path2 from "path";
|
|
53
|
+
var DEFAULT_CONFIG = {
|
|
54
|
+
schema: "./tether/schema.ts",
|
|
55
|
+
functions: "./tether/functions",
|
|
56
|
+
output: "./tether/_generated",
|
|
57
|
+
dev: {
|
|
58
|
+
port: 3001,
|
|
59
|
+
host: "localhost"
|
|
60
|
+
},
|
|
61
|
+
database: {
|
|
62
|
+
walMode: true
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
async function loadConfig(cwd = process.cwd()) {
|
|
66
|
+
const configPath = path2.resolve(cwd, "tether.config.ts");
|
|
67
|
+
if (!await fs2.pathExists(configPath)) {
|
|
68
|
+
return DEFAULT_CONFIG;
|
|
69
|
+
}
|
|
70
|
+
const configSource = await fs2.readFile(configPath, "utf-8");
|
|
71
|
+
const config = {};
|
|
72
|
+
const schemaMatch = configSource.match(/schema\s*:\s*['"]([^'"]+)['"]/);
|
|
73
|
+
if (schemaMatch) {
|
|
74
|
+
config.schema = schemaMatch[1];
|
|
75
|
+
}
|
|
76
|
+
const functionsMatch = configSource.match(/functions\s*:\s*['"]([^'"]+)['"]/);
|
|
77
|
+
if (functionsMatch) {
|
|
78
|
+
config.functions = functionsMatch[1];
|
|
79
|
+
}
|
|
80
|
+
const outputMatch = configSource.match(/output\s*:\s*['"]([^'"]+)['"]/);
|
|
81
|
+
if (outputMatch) {
|
|
82
|
+
config.output = outputMatch[1];
|
|
83
|
+
}
|
|
84
|
+
const envMatch = configSource.match(/environment\s*:\s*['"]([^'"]+)['"]/);
|
|
85
|
+
if (envMatch) {
|
|
86
|
+
config.environment = envMatch[1];
|
|
87
|
+
}
|
|
88
|
+
const portMatch = configSource.match(/port\s*:\s*(\d+)/);
|
|
89
|
+
if (portMatch) {
|
|
90
|
+
config.dev = { ...config.dev, port: parseInt(portMatch[1], 10) };
|
|
91
|
+
}
|
|
92
|
+
const hostMatch = configSource.match(/host\s*:\s*['"]([^'"]+)['"]/);
|
|
93
|
+
if (hostMatch) {
|
|
94
|
+
config.dev = { ...config.dev, host: hostMatch[1] };
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
...DEFAULT_CONFIG,
|
|
98
|
+
...config,
|
|
99
|
+
dev: { ...DEFAULT_CONFIG.dev, ...config.dev },
|
|
100
|
+
database: { ...DEFAULT_CONFIG.database, ...config.database }
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function resolvePath(configPath, cwd = process.cwd()) {
|
|
104
|
+
const normalised = configPath.replace(/^\.\//, "");
|
|
105
|
+
return path2.resolve(cwd, normalised);
|
|
106
|
+
}
|
|
107
|
+
async function detectFramework(cwd = process.cwd()) {
|
|
108
|
+
const packageJsonPath = path2.resolve(cwd, "package.json");
|
|
109
|
+
if (!await fs2.pathExists(packageJsonPath)) {
|
|
110
|
+
return "unknown";
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const packageJson = await fs2.readJson(packageJsonPath);
|
|
114
|
+
const deps = {
|
|
115
|
+
...packageJson.dependencies,
|
|
116
|
+
...packageJson.devDependencies
|
|
117
|
+
};
|
|
118
|
+
if (deps.nuxt || deps["@nuxt/kit"]) {
|
|
119
|
+
return "nuxt";
|
|
120
|
+
}
|
|
121
|
+
if (deps.next) {
|
|
122
|
+
return "next";
|
|
123
|
+
}
|
|
124
|
+
if (deps["@sveltejs/kit"]) {
|
|
125
|
+
return "sveltekit";
|
|
126
|
+
}
|
|
127
|
+
if (deps.vite && !deps.nuxt && !deps.next && !deps["@sveltejs/kit"]) {
|
|
128
|
+
return "vite";
|
|
129
|
+
}
|
|
130
|
+
return "vanilla";
|
|
131
|
+
} catch {
|
|
132
|
+
return "unknown";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function getFrameworkDevCommand(framework) {
|
|
136
|
+
switch (framework) {
|
|
137
|
+
case "nuxt":
|
|
138
|
+
return "nuxt dev";
|
|
139
|
+
case "next":
|
|
140
|
+
return "next dev";
|
|
141
|
+
case "sveltekit":
|
|
142
|
+
return "vite dev";
|
|
143
|
+
case "vite":
|
|
144
|
+
return "vite dev";
|
|
145
|
+
case "vanilla":
|
|
146
|
+
return null;
|
|
147
|
+
// No default dev server for vanilla
|
|
148
|
+
default:
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/commands/generate.ts
|
|
154
|
+
function parseSchemaFile(source) {
|
|
155
|
+
const tables = [];
|
|
156
|
+
const schemaMatch = source.match(/defineSchema\s*\(\s*\{([\s\S]*)\}\s*\)/);
|
|
157
|
+
if (!schemaMatch) return tables;
|
|
158
|
+
const schemaContent = schemaMatch[1];
|
|
159
|
+
const tableStartRegex = /(\w+)\s*:\s*\{/g;
|
|
160
|
+
let match;
|
|
161
|
+
while ((match = tableStartRegex.exec(schemaContent)) !== null) {
|
|
162
|
+
const tableName = match[1];
|
|
163
|
+
const startOffset = match.index + match[0].length;
|
|
164
|
+
let braceCount = 1;
|
|
165
|
+
let endOffset = startOffset;
|
|
166
|
+
for (let i = startOffset; i < schemaContent.length && braceCount > 0; i++) {
|
|
167
|
+
const char = schemaContent[i];
|
|
168
|
+
if (char === "{") braceCount++;
|
|
169
|
+
else if (char === "}") braceCount--;
|
|
170
|
+
endOffset = i;
|
|
171
|
+
}
|
|
172
|
+
const columnsContent = schemaContent.slice(startOffset, endOffset);
|
|
173
|
+
const columns = parseColumns(columnsContent);
|
|
174
|
+
tables.push({ name: tableName, columns });
|
|
175
|
+
}
|
|
176
|
+
return tables;
|
|
177
|
+
}
|
|
178
|
+
function parseColumns(content) {
|
|
179
|
+
const columns = [];
|
|
180
|
+
const columnRegex = /(\w+)\s*:\s*(\w+)\s*\(\s*\)([^,\n}]*)/g;
|
|
181
|
+
let match;
|
|
182
|
+
while ((match = columnRegex.exec(content)) !== null) {
|
|
183
|
+
const name = match[1];
|
|
184
|
+
const schemaType = match[2];
|
|
185
|
+
const modifiers = match[3] || "";
|
|
186
|
+
const nullable = !modifiers.includes(".notNull()") && !modifiers.includes(".primaryKey()");
|
|
187
|
+
const primaryKey = modifiers.includes(".primaryKey()");
|
|
188
|
+
columns.push({
|
|
189
|
+
name,
|
|
190
|
+
type: schemaTypeToTS(schemaType),
|
|
191
|
+
nullable,
|
|
192
|
+
primaryKey
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return columns;
|
|
196
|
+
}
|
|
197
|
+
function schemaTypeToTS(schemaType) {
|
|
198
|
+
switch (schemaType) {
|
|
199
|
+
case "text":
|
|
200
|
+
return "string";
|
|
201
|
+
case "integer":
|
|
202
|
+
return "number";
|
|
203
|
+
case "real":
|
|
204
|
+
return "number";
|
|
205
|
+
case "boolean":
|
|
206
|
+
return "boolean";
|
|
207
|
+
case "timestamp":
|
|
208
|
+
return "string";
|
|
209
|
+
case "json":
|
|
210
|
+
return "unknown";
|
|
211
|
+
case "blob":
|
|
212
|
+
return "Uint8Array";
|
|
213
|
+
default:
|
|
214
|
+
return "unknown";
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function tableNameToInterface(tableName) {
|
|
218
|
+
let name = tableName;
|
|
219
|
+
if (name.endsWith("ies")) {
|
|
220
|
+
name = name.slice(0, -3) + "y";
|
|
221
|
+
} else if (name.endsWith("s") && !name.endsWith("ss")) {
|
|
222
|
+
name = name.slice(0, -1);
|
|
223
|
+
}
|
|
224
|
+
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
225
|
+
}
|
|
226
|
+
function generateDbFile(tables) {
|
|
227
|
+
const lines = [
|
|
228
|
+
"// Auto-generated by Tether CLI - do not edit manually",
|
|
229
|
+
`// Generated at: ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
230
|
+
"",
|
|
231
|
+
"import { createDatabaseProxy, type TetherDatabase } from '@tthr/client';",
|
|
232
|
+
""
|
|
233
|
+
];
|
|
234
|
+
for (const table of tables) {
|
|
235
|
+
const interfaceName = tableNameToInterface(table.name);
|
|
236
|
+
lines.push(`export interface ${interfaceName} {`);
|
|
237
|
+
for (const col of table.columns) {
|
|
238
|
+
const typeStr = col.nullable ? `${col.type} | null` : col.type;
|
|
239
|
+
lines.push(` ${col.name}: ${typeStr};`);
|
|
240
|
+
}
|
|
241
|
+
lines.push("}");
|
|
242
|
+
lines.push("");
|
|
243
|
+
}
|
|
244
|
+
lines.push("export interface Schema {");
|
|
245
|
+
for (const table of tables) {
|
|
246
|
+
const interfaceName = tableNameToInterface(table.name);
|
|
247
|
+
lines.push(` ${table.name}: ${interfaceName};`);
|
|
248
|
+
}
|
|
249
|
+
lines.push("}");
|
|
250
|
+
lines.push("");
|
|
251
|
+
lines.push("// Database client with typed tables");
|
|
252
|
+
lines.push("// This is a proxy that will be populated by the Tether runtime");
|
|
253
|
+
lines.push("export const db: TetherDatabase<Schema> = createDatabaseProxy<Schema>();");
|
|
254
|
+
lines.push("");
|
|
255
|
+
return lines.join("\n");
|
|
256
|
+
}
|
|
257
|
+
async function parseFunctionsDir(functionsDir) {
|
|
258
|
+
const functions = [];
|
|
259
|
+
if (!await fs3.pathExists(functionsDir)) {
|
|
260
|
+
return functions;
|
|
261
|
+
}
|
|
262
|
+
const files = await fs3.readdir(functionsDir);
|
|
263
|
+
for (const file of files) {
|
|
264
|
+
if (!file.endsWith(".ts") && !file.endsWith(".js")) continue;
|
|
265
|
+
const filePath = path3.join(functionsDir, file);
|
|
266
|
+
const stat = await fs3.stat(filePath);
|
|
267
|
+
if (!stat.isFile()) continue;
|
|
268
|
+
const moduleName = file.replace(/\.(ts|js)$/, "");
|
|
269
|
+
const source = await fs3.readFile(filePath, "utf-8");
|
|
270
|
+
const exportRegex = /export\s+const\s+(\w+)\s*=\s*(query|mutation|action)\s*\(/g;
|
|
271
|
+
let match;
|
|
272
|
+
while ((match = exportRegex.exec(source)) !== null) {
|
|
273
|
+
functions.push({
|
|
274
|
+
name: match[1],
|
|
275
|
+
moduleName
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return functions;
|
|
280
|
+
}
|
|
281
|
+
async function generateApiFile(functionsDir) {
|
|
282
|
+
const functions = await parseFunctionsDir(functionsDir);
|
|
283
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
284
|
+
for (const fn of functions) {
|
|
285
|
+
if (!moduleMap.has(fn.moduleName)) {
|
|
286
|
+
moduleMap.set(fn.moduleName, []);
|
|
287
|
+
}
|
|
288
|
+
moduleMap.get(fn.moduleName).push(fn.name);
|
|
289
|
+
}
|
|
290
|
+
const lines = [
|
|
291
|
+
"// Auto-generated by Tether CLI - do not edit manually",
|
|
292
|
+
`// Generated at: ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
293
|
+
"",
|
|
294
|
+
"import { createApiProxy } from '@tthr/client';",
|
|
295
|
+
"",
|
|
296
|
+
"/**",
|
|
297
|
+
" * API function reference type for useQuery/useMutation.",
|
|
298
|
+
' * The _name property contains the function path (e.g., "users.list").',
|
|
299
|
+
" */",
|
|
300
|
+
"export interface ApiFunction<TArgs = unknown, TResult = unknown> {",
|
|
301
|
+
" _name: string;",
|
|
302
|
+
" _args?: TArgs;",
|
|
303
|
+
" _result?: TResult;",
|
|
304
|
+
"}",
|
|
305
|
+
""
|
|
306
|
+
];
|
|
307
|
+
if (moduleMap.size > 0) {
|
|
308
|
+
for (const [moduleName, fnNames] of moduleMap) {
|
|
309
|
+
const interfaceName = moduleName.charAt(0).toUpperCase() + moduleName.slice(1) + "Api";
|
|
310
|
+
lines.push(`export interface ${interfaceName} {`);
|
|
311
|
+
for (const fnName of fnNames) {
|
|
312
|
+
lines.push(` ${fnName}: ApiFunction;`);
|
|
313
|
+
}
|
|
314
|
+
lines.push("}");
|
|
315
|
+
lines.push("");
|
|
316
|
+
}
|
|
317
|
+
lines.push("export interface Api {");
|
|
318
|
+
for (const moduleName of moduleMap.keys()) {
|
|
319
|
+
const interfaceName = moduleName.charAt(0).toUpperCase() + moduleName.slice(1) + "Api";
|
|
320
|
+
lines.push(` ${moduleName}: ${interfaceName};`);
|
|
321
|
+
}
|
|
322
|
+
lines.push("}");
|
|
323
|
+
} else {
|
|
324
|
+
lines.push("/**");
|
|
325
|
+
lines.push(" * Flexible API type that allows access to any function path.");
|
|
326
|
+
lines.push(" * Functions are accessed as api.moduleName.functionName");
|
|
327
|
+
lines.push(" * The actual types depend on your function definitions.");
|
|
328
|
+
lines.push(" */");
|
|
329
|
+
lines.push("export type Api = {");
|
|
330
|
+
lines.push(" [module: string]: {");
|
|
331
|
+
lines.push(" [fn: string]: ApiFunction;");
|
|
332
|
+
lines.push(" };");
|
|
333
|
+
lines.push("};");
|
|
334
|
+
}
|
|
335
|
+
lines.push("");
|
|
336
|
+
lines.push("// API client proxy - provides typed access to your functions");
|
|
337
|
+
lines.push("// On the client: returns { _name } references for useQuery/useMutation");
|
|
338
|
+
lines.push("// In Tether functions: calls the actual function implementation");
|
|
339
|
+
lines.push("export const api = createApiProxy<Api>();");
|
|
340
|
+
lines.push("");
|
|
341
|
+
return lines.join("\n");
|
|
342
|
+
}
|
|
343
|
+
async function generateTypes(options = {}) {
|
|
344
|
+
const config = await loadConfig();
|
|
345
|
+
const schemaPath = resolvePath(config.schema);
|
|
346
|
+
const outputDir = resolvePath(config.output);
|
|
347
|
+
const functionsDir = resolvePath(config.functions);
|
|
348
|
+
if (!await fs3.pathExists(schemaPath)) {
|
|
349
|
+
throw new Error(`Schema file not found: ${schemaPath}`);
|
|
350
|
+
}
|
|
351
|
+
const schemaSource = await fs3.readFile(schemaPath, "utf-8");
|
|
352
|
+
const tables = parseSchemaFile(schemaSource);
|
|
353
|
+
await fs3.ensureDir(outputDir);
|
|
354
|
+
await fs3.writeFile(
|
|
355
|
+
path3.join(outputDir, "db.ts"),
|
|
356
|
+
generateDbFile(tables)
|
|
357
|
+
);
|
|
358
|
+
await fs3.writeFile(
|
|
359
|
+
path3.join(outputDir, "api.ts"),
|
|
360
|
+
await generateApiFile(functionsDir)
|
|
361
|
+
);
|
|
362
|
+
await fs3.writeFile(
|
|
363
|
+
path3.join(outputDir, "index.ts"),
|
|
364
|
+
`// Auto-generated by Tether CLI - do not edit manually
|
|
365
|
+
export * from './db';
|
|
366
|
+
export * from './api';
|
|
367
|
+
`
|
|
368
|
+
);
|
|
369
|
+
return { tables, outputDir };
|
|
370
|
+
}
|
|
371
|
+
async function generateCommand() {
|
|
372
|
+
await requireAuth();
|
|
373
|
+
const configPath = path3.resolve(process.cwd(), "tether.config.ts");
|
|
374
|
+
if (!await fs3.pathExists(configPath)) {
|
|
375
|
+
console.log(chalk2.red("\nError: Not a Tether project"));
|
|
376
|
+
console.log(chalk2.dim("Run `tthr init` to create a new project\n"));
|
|
377
|
+
process.exit(1);
|
|
378
|
+
}
|
|
379
|
+
console.log(chalk2.bold("\n\u26A1 Generating types from schema\n"));
|
|
380
|
+
const spinner = ora("Reading schema...").start();
|
|
381
|
+
try {
|
|
382
|
+
spinner.text = "Generating types...";
|
|
383
|
+
const { tables, outputDir } = await generateTypes();
|
|
384
|
+
if (tables.length === 0) {
|
|
385
|
+
spinner.warn("No tables found in schema");
|
|
386
|
+
console.log(chalk2.dim(" Make sure your schema uses defineSchema({ ... })\n"));
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
spinner.succeed(`Types generated for ${tables.length} table(s)`);
|
|
390
|
+
console.log("\n" + chalk2.green("\u2713") + " Tables:");
|
|
391
|
+
for (const table of tables) {
|
|
392
|
+
console.log(chalk2.dim(` - ${table.name} (${table.columns.length} columns)`));
|
|
393
|
+
}
|
|
394
|
+
const relativeOutput = path3.relative(process.cwd(), outputDir);
|
|
395
|
+
console.log("\n" + chalk2.green("\u2713") + " Generated files:");
|
|
396
|
+
console.log(chalk2.dim(` ${relativeOutput}/db.ts`));
|
|
397
|
+
console.log(chalk2.dim(` ${relativeOutput}/api.ts`));
|
|
398
|
+
console.log(chalk2.dim(` ${relativeOutput}/index.ts
|
|
399
|
+
`));
|
|
400
|
+
} catch (error) {
|
|
401
|
+
spinner.fail("Failed to generate types");
|
|
402
|
+
console.error(chalk2.red(error instanceof Error ? error.message : "Unknown error"));
|
|
403
|
+
process.exit(1);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export {
|
|
408
|
+
getCredentials,
|
|
409
|
+
requireAuth,
|
|
410
|
+
saveCredentials,
|
|
411
|
+
clearCredentials,
|
|
412
|
+
loadConfig,
|
|
413
|
+
resolvePath,
|
|
414
|
+
detectFramework,
|
|
415
|
+
getFrameworkDevCommand,
|
|
416
|
+
generateTypes,
|
|
417
|
+
generateCommand
|
|
418
|
+
};
|