@timber-js/app 0.2.0-alpha.111 → 0.2.0-alpha.112

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timber-js/app",
3
- "version": "0.2.0-alpha.111",
3
+ "version": "0.2.0-alpha.112",
4
4
  "description": "Vite-native React framework built for Servers and Serverless Platforms — correct HTTP semantics, real status codes, pages that work without JavaScript",
5
5
  "keywords": [
6
6
  "cloudflare-workers",
@@ -8,9 +8,10 @@
8
8
  */
9
9
 
10
10
  import { readFileSync, writeFileSync, existsSync } from 'node:fs';
11
- import { join, resolve } from 'node:path';
11
+ import { join } from 'node:path';
12
12
  import { scanRoutes } from './routing/scanner.js';
13
13
  import type { SegmentNode } from './routing/types.js';
14
+ import { resolveAppDir } from './plugin-context.js';
14
15
 
15
16
  /**
16
17
  * Collect all unique bracket-keyed dynamic segment names from the route tree.
@@ -151,11 +152,7 @@ export interface SchemaSyncResult {
151
152
  * Run the schema sync: scan filesystem, diff against schema, append missing.
152
153
  */
153
154
  export function runSchemaSync(projectRoot: string): SchemaSyncResult {
154
- const appDir = resolve(projectRoot, 'app');
155
-
156
- if (!existsSync(appDir)) {
157
- throw new Error(`[timber] No app/ directory found at ${appDir}`);
158
- }
155
+ const appDir = resolveAppDir(projectRoot);
159
156
 
160
157
  const filesystemSegments = collectDynamicSegments(appDir);
161
158
 
@@ -1,137 +0,0 @@
1
- import { t as scanRoutes } from "./scanner-DmfoB7iK.js";
2
- import { join, resolve } from "node:path";
3
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
4
- //#region src/cli-schema-sync.ts
5
- /**
6
- * `timber schema sync` — scan filesystem for dynamic segments and append
7
- * missing entries to app/schema.ts with `codec.string` defaults.
8
- *
9
- * Only adds, never removes or modifies existing entries.
10
- *
11
- * Design doc: design/41-global-params.md §Auto-Scaffolding
12
- */
13
- /**
14
- * Collect all unique bracket-keyed dynamic segment names from the route tree.
15
- * Returns entries like '[artistSlug]', '[...slug]', '[[...topic]]'.
16
- */
17
- function collectDynamicSegments(appDir) {
18
- const tree = scanRoutes(appDir);
19
- const segments = /* @__PURE__ */ new Set();
20
- walkSegments(tree.root, segments);
21
- return [...segments].sort();
22
- }
23
- function walkSegments(node, segments) {
24
- if (node.paramName) segments.add(node.segmentName);
25
- for (const child of node.children) walkSegments(child, segments);
26
- for (const slot of Object.values(node.slots)) walkSegments(slot, segments);
27
- }
28
- /**
29
- * Parse existing bracket keys from an app/schema.ts file.
30
- * Uses regex to find keys in segmentParams: { '[key]': ... } blocks.
31
- */
32
- function parseExistingSchemaKeys(content) {
33
- const keys = [];
34
- const re = /['"](\[{1,2}(?:\.{3})?\w+\]{1,2})['"]\s*:/g;
35
- let m;
36
- while ((m = re.exec(content)) !== null) keys.push(m[1]);
37
- return keys;
38
- }
39
- /** Schema file extensions to check. */
40
- var SCHEMA_EXTENSIONS = [
41
- ".ts",
42
- ".tsx",
43
- ".js",
44
- ".jsx"
45
- ];
46
- /**
47
- * Find the schema file path if it exists.
48
- */
49
- function findSchemaFile(appDir) {
50
- for (const ext of SCHEMA_EXTENSIONS) {
51
- const p = join(appDir, `schema${ext}`);
52
- if (existsSync(p)) return p;
53
- }
54
- }
55
- /**
56
- * Generate the content for a new app/schema.ts with the given segment keys.
57
- */
58
- function generateNewSchema(segments) {
59
- const lines = [];
60
- lines.push("import { defineSchema, codec } from '@timber-js/app';");
61
- lines.push("");
62
- lines.push("export default defineSchema({");
63
- lines.push(" segmentParams: {");
64
- for (const seg of segments) lines.push(` '${seg}': codec.string,`);
65
- lines.push(" },");
66
- lines.push("});");
67
- lines.push("");
68
- return lines.join("\n");
69
- }
70
- /**
71
- * Append missing segment entries to an existing schema file.
72
- * Inserts new entries before the closing `}` of segmentParams.
73
- */
74
- function appendToSchema(content, newSegments) {
75
- const spIndex = content.indexOf("segmentParams");
76
- if (spIndex === -1) throw new Error("[timber] Could not find segmentParams in schema file. Expected a defineSchema({ segmentParams: { ... } }) call.");
77
- const openBrace = content.indexOf("{", spIndex + 13);
78
- if (openBrace === -1) throw new Error("[timber] Could not find opening brace for segmentParams.");
79
- let depth = 1;
80
- let i = openBrace + 1;
81
- while (i < content.length && depth > 0) {
82
- if (content[i] === "{") depth++;
83
- else if (content[i] === "}") depth--;
84
- i++;
85
- }
86
- const closeBrace = i - 1;
87
- const existingLine = content.slice(openBrace, closeBrace).match(/^(\s+)['"]\[/m);
88
- const indent = existingLine ? existingLine[1] : " ";
89
- const insertion = newSegments.map((seg) => `${indent}'${seg}': codec.string,`).join("\n");
90
- const before = content.slice(0, closeBrace);
91
- const after = content.slice(closeBrace);
92
- return before + (before.trimEnd().endsWith(",") ? "\n" : "\n") + insertion + "\n" + after;
93
- }
94
- /**
95
- * Run the schema sync: scan filesystem, diff against schema, append missing.
96
- */
97
- function runSchemaSync(projectRoot) {
98
- const appDir = resolve(projectRoot, "app");
99
- if (!existsSync(appDir)) throw new Error(`[timber] No app/ directory found at ${appDir}`);
100
- const filesystemSegments = collectDynamicSegments(appDir);
101
- if (filesystemSegments.length === 0) return {
102
- filesystemSegments: [],
103
- existingSegments: [],
104
- addedSegments: [],
105
- created: false
106
- };
107
- const schemaPath = findSchemaFile(appDir);
108
- if (!schemaPath) {
109
- writeFileSync(join(appDir, "schema.ts"), generateNewSchema(filesystemSegments), "utf-8");
110
- return {
111
- filesystemSegments,
112
- existingSegments: [],
113
- addedSegments: filesystemSegments,
114
- created: true
115
- };
116
- }
117
- const content = readFileSync(schemaPath, "utf-8");
118
- const existingSegments = parseExistingSchemaKeys(content);
119
- const missing = filesystemSegments.filter((seg) => !existingSegments.includes(seg));
120
- if (missing.length === 0) return {
121
- filesystemSegments,
122
- existingSegments,
123
- addedSegments: [],
124
- created: false
125
- };
126
- writeFileSync(schemaPath, appendToSchema(content, missing), "utf-8");
127
- return {
128
- filesystemSegments,
129
- existingSegments,
130
- addedSegments: missing,
131
- created: false
132
- };
133
- }
134
- //#endregion
135
- export { runSchemaSync as i, findSchemaFile as n, parseExistingSchemaKeys as r, collectDynamicSegments as t };
136
-
137
- //# sourceMappingURL=cli-schema-sync-BvvuK8Rp.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cli-schema-sync-BvvuK8Rp.js","names":[],"sources":["../../src/cli-schema-sync.ts"],"sourcesContent":["/**\n * `timber schema sync` — scan filesystem for dynamic segments and append\n * missing entries to app/schema.ts with `codec.string` defaults.\n *\n * Only adds, never removes or modifies existing entries.\n *\n * Design doc: design/41-global-params.md §Auto-Scaffolding\n */\n\nimport { readFileSync, writeFileSync, existsSync } from 'node:fs';\nimport { join, resolve } from 'node:path';\nimport { scanRoutes } from './routing/scanner.js';\nimport type { SegmentNode } from './routing/types.js';\n\n/**\n * Collect all unique bracket-keyed dynamic segment names from the route tree.\n * Returns entries like '[artistSlug]', '[...slug]', '[[...topic]]'.\n */\nexport function collectDynamicSegments(appDir: string): string[] {\n const tree = scanRoutes(appDir);\n const segments = new Set<string>();\n walkSegments(tree.root, segments);\n return [...segments].sort();\n}\n\nfunction walkSegments(node: SegmentNode, segments: Set<string>): void {\n if (node.paramName) {\n // Reconstruct bracket key from segmentName (already has brackets)\n segments.add(node.segmentName);\n }\n for (const child of node.children) {\n walkSegments(child, segments);\n }\n for (const slot of Object.values(node.slots)) {\n walkSegments(slot, segments);\n }\n}\n\n/**\n * Parse existing bracket keys from an app/schema.ts file.\n * Uses regex to find keys in segmentParams: { '[key]': ... } blocks.\n */\nexport function parseExistingSchemaKeys(content: string): string[] {\n const keys: string[] = [];\n // Match quoted bracket keys like '[artistSlug]', '[...slug]', '[[...topic]]'\n const re = /['\"](\\[{1,2}(?:\\.{3})?\\w+\\]{1,2})['\"]\\s*:/g;\n let m: RegExpExecArray | null;\n while ((m = re.exec(content)) !== null) {\n keys.push(m[1]);\n }\n return keys;\n}\n\n/** Schema file extensions to check. */\nconst SCHEMA_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];\n\n/**\n * Find the schema file path if it exists.\n */\nexport function findSchemaFile(appDir: string): string | undefined {\n for (const ext of SCHEMA_EXTENSIONS) {\n const p = join(appDir, `schema${ext}`);\n if (existsSync(p)) return p;\n }\n return undefined;\n}\n\n/**\n * Generate the content for a new app/schema.ts with the given segment keys.\n */\nfunction generateNewSchema(segments: string[]): string {\n const lines: string[] = [];\n lines.push(\"import { defineSchema, codec } from '@timber-js/app';\");\n lines.push('');\n lines.push('export default defineSchema({');\n lines.push(' segmentParams: {');\n for (const seg of segments) {\n lines.push(` '${seg}': codec.string,`);\n }\n lines.push(' },');\n lines.push('});');\n lines.push('');\n return lines.join('\\n');\n}\n\n/**\n * Append missing segment entries to an existing schema file.\n * Inserts new entries before the closing `}` of segmentParams.\n */\nfunction appendToSchema(content: string, newSegments: string[]): string {\n // Find the closing brace of segmentParams object.\n // Strategy: find the last entry in segmentParams block, insert after it.\n // Look for the pattern: segmentParams: { ... }\n // We find the closing `},` or `}` that ends segmentParams.\n\n // Find \"segmentParams\" key position\n const spIndex = content.indexOf('segmentParams');\n if (spIndex === -1) {\n throw new Error(\n '[timber] Could not find segmentParams in schema file. ' +\n 'Expected a defineSchema({ segmentParams: { ... } }) call.'\n );\n }\n\n // Find the opening brace after segmentParams\n const openBrace = content.indexOf('{', spIndex + 'segmentParams'.length);\n if (openBrace === -1) {\n throw new Error('[timber] Could not find opening brace for segmentParams.');\n }\n\n // Count braces to find the matching close\n let depth = 1;\n let i = openBrace + 1;\n while (i < content.length && depth > 0) {\n if (content[i] === '{') depth++;\n else if (content[i] === '}') depth--;\n i++;\n }\n // i now points to just after the closing brace\n const closeBrace = i - 1;\n\n // Detect indentation from existing entries\n const existingLine = content.slice(openBrace, closeBrace).match(/^(\\s+)['\"]\\[/m);\n const indent = existingLine ? existingLine[1] : ' ';\n\n // Build insertion text\n const insertion = newSegments.map((seg) => `${indent}'${seg}': codec.string,`).join('\\n');\n\n // Insert before the closing brace\n const before = content.slice(0, closeBrace);\n const after = content.slice(closeBrace);\n\n // Add newline if the content before doesn't end with one\n const separator = before.trimEnd().endsWith(',') ? '\\n' : '\\n';\n\n return before + separator + insertion + '\\n' + after;\n}\n\nexport interface SchemaSyncResult {\n /** Segments found in the filesystem. */\n filesystemSegments: string[];\n /** Segments already defined in app/schema.ts. */\n existingSegments: string[];\n /** Segments that were added. */\n addedSegments: string[];\n /** Whether a new schema file was created. */\n created: boolean;\n}\n\n/**\n * Run the schema sync: scan filesystem, diff against schema, append missing.\n */\nexport function runSchemaSync(projectRoot: string): SchemaSyncResult {\n const appDir = resolve(projectRoot, 'app');\n\n if (!existsSync(appDir)) {\n throw new Error(`[timber] No app/ directory found at ${appDir}`);\n }\n\n const filesystemSegments = collectDynamicSegments(appDir);\n\n if (filesystemSegments.length === 0) {\n return {\n filesystemSegments: [],\n existingSegments: [],\n addedSegments: [],\n created: false,\n };\n }\n\n const schemaPath = findSchemaFile(appDir);\n\n if (!schemaPath) {\n // Create new schema file\n const newPath = join(appDir, 'schema.ts');\n writeFileSync(newPath, generateNewSchema(filesystemSegments), 'utf-8');\n return {\n filesystemSegments,\n existingSegments: [],\n addedSegments: filesystemSegments,\n created: true,\n };\n }\n\n // Read existing schema and find missing segments\n const content = readFileSync(schemaPath, 'utf-8');\n const existingSegments = parseExistingSchemaKeys(content);\n const missing = filesystemSegments.filter((seg) => !existingSegments.includes(seg));\n\n if (missing.length === 0) {\n return {\n filesystemSegments,\n existingSegments,\n addedSegments: [],\n created: false,\n };\n }\n\n // Append missing segments\n const updated = appendToSchema(content, missing);\n writeFileSync(schemaPath, updated, 'utf-8');\n\n return {\n filesystemSegments,\n existingSegments,\n addedSegments: missing,\n created: false,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkBA,SAAgB,uBAAuB,QAA0B;CAC/D,MAAM,OAAO,WAAW,OAAO;CAC/B,MAAM,2BAAW,IAAI,KAAa;AAClC,cAAa,KAAK,MAAM,SAAS;AACjC,QAAO,CAAC,GAAG,SAAS,CAAC,MAAM;;AAG7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