@alloy-js/core 0.19.0-dev.0 → 0.19.0-dev.12

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.
Files changed (105) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/src/components/AppendFile.d.ts +90 -0
  3. package/dist/src/components/AppendFile.d.ts.map +1 -0
  4. package/dist/src/components/AppendFile.js +226 -0
  5. package/dist/src/components/CopyFile.d.ts +12 -0
  6. package/dist/src/components/CopyFile.d.ts.map +1 -0
  7. package/dist/src/components/CopyFile.js +15 -0
  8. package/dist/src/components/TemplateFile.d.ts +84 -0
  9. package/dist/src/components/TemplateFile.d.ts.map +1 -0
  10. package/dist/src/components/TemplateFile.js +133 -0
  11. package/dist/src/components/UpdateFile.d.ts +34 -0
  12. package/dist/src/components/UpdateFile.d.ts.map +1 -0
  13. package/dist/src/components/UpdateFile.js +66 -0
  14. package/dist/src/components/index.d.ts +4 -0
  15. package/dist/src/components/index.d.ts.map +1 -1
  16. package/dist/src/components/index.js +4 -0
  17. package/dist/src/components/stc/index.d.ts +4 -0
  18. package/dist/src/components/stc/index.d.ts.map +1 -1
  19. package/dist/src/components/stc/index.js +4 -0
  20. package/dist/src/context/source-directory.d.ts +3 -3
  21. package/dist/src/context/source-directory.d.ts.map +1 -1
  22. package/dist/src/context/source-file.d.ts +4 -0
  23. package/dist/src/context/source-file.d.ts.map +1 -1
  24. package/dist/src/debug.d.ts.map +1 -1
  25. package/dist/src/debug.js +4 -1
  26. package/dist/src/host/alloy-host.browser.d.ts +11 -0
  27. package/dist/src/host/alloy-host.browser.d.ts.map +1 -0
  28. package/dist/src/host/alloy-host.browser.js +31 -0
  29. package/dist/src/host/alloy-host.d.ts +11 -0
  30. package/dist/src/host/alloy-host.d.ts.map +1 -0
  31. package/dist/src/host/alloy-host.js +143 -0
  32. package/dist/src/host/interface.d.ts +144 -0
  33. package/dist/src/host/interface.d.ts.map +1 -0
  34. package/dist/src/host/interface.js +1 -0
  35. package/dist/src/index.browser.d.ts +1 -1
  36. package/dist/src/index.browser.d.ts.map +1 -1
  37. package/dist/src/index.browser.js +2 -2
  38. package/dist/src/reactivity.js +2 -2
  39. package/dist/src/render.d.ts +10 -2
  40. package/dist/src/render.d.ts.map +1 -1
  41. package/dist/src/render.js +20 -1
  42. package/dist/src/resource.d.ts +80 -0
  43. package/dist/src/resource.d.ts.map +1 -0
  44. package/dist/src/resource.js +118 -0
  45. package/dist/src/scheduler.d.ts +6 -0
  46. package/dist/src/scheduler.d.ts.map +1 -1
  47. package/dist/src/scheduler.js +36 -0
  48. package/dist/src/write-output.d.ts +1 -1
  49. package/dist/src/write-output.d.ts.map +1 -1
  50. package/dist/src/write-output.js +40 -21
  51. package/dist/test/components/append-file.test.d.ts +2 -0
  52. package/dist/test/components/append-file.test.d.ts.map +1 -0
  53. package/dist/test/components/append-file.test.js +281 -0
  54. package/dist/test/components/copy-file.test.d.ts +2 -0
  55. package/dist/test/components/copy-file.test.d.ts.map +1 -0
  56. package/dist/test/components/copy-file.test.js +94 -0
  57. package/dist/test/components/source-file.test.d.ts.map +1 -1
  58. package/dist/test/components/template-file.test.d.ts +2 -0
  59. package/dist/test/components/template-file.test.d.ts.map +1 -0
  60. package/dist/test/components/template-file.test.js +133 -0
  61. package/dist/test/components/update-file.test.d.ts +2 -0
  62. package/dist/test/components/update-file.test.d.ts.map +1 -0
  63. package/dist/test/components/update-file.test.js +169 -0
  64. package/dist/test/rendering/formatting.test.d.ts.map +1 -1
  65. package/dist/testing/extend-expect.d.ts +0 -15
  66. package/dist/testing/extend-expect.d.ts.map +1 -1
  67. package/dist/testing/extend-expect.js +89 -12
  68. package/dist/testing/extend-expect.test.d.ts +2 -0
  69. package/dist/testing/extend-expect.test.d.ts.map +1 -0
  70. package/dist/testing/extend-expect.test.js +149 -0
  71. package/dist/tsconfig.tsbuildinfo +1 -1
  72. package/package.json +4 -4
  73. package/src/components/AppendFile.tsx +294 -0
  74. package/src/components/CopyFile.tsx +29 -0
  75. package/src/components/TemplateFile.tsx +193 -0
  76. package/src/components/UpdateFile.tsx +86 -0
  77. package/src/components/index.tsx +4 -0
  78. package/src/components/stc/index.ts +4 -0
  79. package/src/context/source-directory.ts +5 -3
  80. package/src/context/source-file.ts +5 -0
  81. package/src/debug.ts +4 -1
  82. package/src/host/alloy-host.browser.ts +56 -0
  83. package/src/host/alloy-host.ts +160 -0
  84. package/src/host/interface.ts +153 -0
  85. package/src/index.browser.ts +1 -1
  86. package/src/reactivity.ts +2 -2
  87. package/src/render.ts +44 -5
  88. package/src/resource.ts +152 -0
  89. package/src/scheduler.ts +39 -0
  90. package/src/write-output.ts +49 -19
  91. package/temp/api.json +2009 -546
  92. package/test/components/append-file.test.tsx +275 -0
  93. package/test/components/copy-file.test.tsx +98 -0
  94. package/test/components/source-file.test.tsx +5 -2
  95. package/test/components/template-file.test.tsx +127 -0
  96. package/test/components/update-file.test.tsx +214 -0
  97. package/test/rendering/formatting.test.tsx +9 -3
  98. package/testing/extend-expect.test.tsx +126 -0
  99. package/testing/extend-expect.ts +123 -23
  100. package/testing/vitest.d.ts +9 -1
  101. package/tsconfig.json +2 -1
  102. package/dist/src/write-output.browser.d.ts +0 -2
  103. package/dist/src/write-output.browser.d.ts.map +0 -1
  104. package/dist/src/write-output.browser.js +0 -4
  105. package/src/write-output.browser.ts +0 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/core",
3
- "version": "0.19.0-dev.0",
3
+ "version": "0.19.0-dev.12",
4
4
  "description": "",
5
5
  "main": "./dist/src/index.js",
6
6
  "exports": {
@@ -26,8 +26,8 @@
26
26
  }
27
27
  },
28
28
  "browser": {
29
- "./dist/src/write-output.js": "./dist/src/write-output.browser.js",
30
- "./src/write-output.ts": "./src/write-output.browser.ts"
29
+ "./dist/src/host/alloy-host.js": "./dist/src/host/alloy-host.browser.js",
30
+ "./src/host/alloy-host.ts": "./src/host/alloy-host.browser.ts"
31
31
  },
32
32
  "keywords": [],
33
33
  "author": "brian.terlson@microsoft.com",
@@ -47,7 +47,7 @@
47
47
  "concurrently": "^9.1.2",
48
48
  "typescript": "^5.8.3",
49
49
  "vite": "^6.2.6",
50
- "vitest": "^3.1.1"
50
+ "vitest": "^3.2.4"
51
51
  },
52
52
  "type": "module",
53
53
  "scripts": {
@@ -0,0 +1,294 @@
1
+ import { computed } from "@vue/reactivity";
2
+ import { join } from "pathe";
3
+ import { useContext } from "../context.js";
4
+ import { SourceDirectoryContext } from "../context/source-directory.js";
5
+ import { createFileResource } from "../resource.js";
6
+ import { Children, isComponentCreator } from "../runtime/component.js";
7
+ import { childrenArray } from "../utils.jsx";
8
+ import { SourceFile } from "./SourceFile.jsx";
9
+
10
+ // Regular expression for finding all sigils at once
11
+ const SIGIL_REGEX = /^(\s*)(.*alloy-(.+)-(start|end).*)$/gm;
12
+
13
+ interface SigilInfo {
14
+ id: string;
15
+ start: number | null;
16
+ end: number | null;
17
+ }
18
+
19
+ export interface AppendFileProps {
20
+ /**
21
+ * The path to the file to read and append content to.
22
+ */
23
+ path: string;
24
+
25
+ /**
26
+ * List of region IDs to append to. Defaults to ["append"] if not specified.
27
+ * Each region corresponds to an AppendRegion child component.
28
+ */
29
+ regions?: string[];
30
+
31
+ /**
32
+ * AppendRegion children components that define content to append.
33
+ */
34
+ children?: Children;
35
+ }
36
+
37
+ /**
38
+ * A component that reads a file and returns content with new content appended
39
+ * at the end or within specific regions marked by alloy-\{region name\}-start/alloy-\{region name\}-end sigils.
40
+ *
41
+ * The component can append content in two ways:
42
+ * 1. **Simple append**: Content is appended to the end of the file
43
+ * 2. **Region-based append**: Content is appended before the end sigil on its own line
44
+ *
45
+ * Region sigils are line-based - any line containing "alloy-\{region name\}-start" or "alloy-\{region name\}-end"
46
+ * is considered a sigil.
47
+ *
48
+ * @example
49
+ * Simple append to end of file:
50
+ * ```tsx
51
+ * <AppendFile path="output.txt">
52
+ * <AppendRegion id="append">New content to add</AppendRegion>
53
+ * </AppendFile>
54
+ *
55
+ * // Returns:
56
+ * // Original file content
57
+ * // New content to add
58
+ * ```
59
+ *
60
+ * @example
61
+ * Append to specific regions:
62
+ * ```tsx
63
+ * // File content before:
64
+ * // Header content
65
+ * // <!-- alloy-main-start -->
66
+ * // <!-- alloy-main-end -->
67
+ * // Footer content
68
+ *
69
+ * <AppendFile path="template.html" regions={["main"]}>
70
+ * <AppendRegion id="main">New main content</AppendRegion>
71
+ * </AppendFile>
72
+ *
73
+ * // Returns:
74
+ * // Header content
75
+ * // <!-- alloy-main-start -->
76
+ * // New main content
77
+ * // <!-- alloy-main-end -->
78
+ * // Footer content
79
+ * ```
80
+ */
81
+ export function AppendFile(props: AppendFileProps): Children {
82
+ const regions = props.regions || ["append"];
83
+
84
+ // Get all children and filter for AppendRegion components
85
+ const children = childrenArray(() => props.children);
86
+ const appendRegions: Record<string, Children> = {};
87
+
88
+ // Check if we have any AppendRegion components
89
+ let hasAppendRegions = false;
90
+ for (const child of children) {
91
+ if (isComponentCreator(child, AppendRegion)) {
92
+ hasAppendRegions = true;
93
+ const regionProps = child.props as AppendRegionProps;
94
+ let content: Children;
95
+
96
+ if ("children" in regionProps && regionProps.children !== undefined) {
97
+ content = regionProps.children;
98
+ } else if ("content" in regionProps) {
99
+ content = regionProps.content;
100
+ } else {
101
+ throw new Error(
102
+ `AppendRegion "${regionProps.id}" must have either children or content`,
103
+ );
104
+ }
105
+
106
+ appendRegions[regionProps.id] = content;
107
+ }
108
+ }
109
+
110
+ // If no AppendRegion components found, treat all children as content for the default "append" region
111
+ if (!hasAppendRegions && children.length > 0) {
112
+ appendRegions["append"] = children;
113
+ }
114
+
115
+ // Validate that all requested regions have corresponding AppendRegion children
116
+ for (const regionId of regions) {
117
+ if (!(regionId in appendRegions)) {
118
+ throw new Error(
119
+ `Region "${regionId}" specified but no corresponding AppendRegion child found`,
120
+ );
121
+ }
122
+ }
123
+
124
+ // Read existing file content or start with empty string
125
+ const parentDirectory = useContext(SourceDirectoryContext)!;
126
+ const fullPath = join(
127
+ parentDirectory ? parentDirectory.path : "",
128
+ props.path,
129
+ );
130
+
131
+ const currentContents = createFileResource(fullPath);
132
+ const newFileContent = computed(() => {
133
+ if (currentContents.loading) {
134
+ return;
135
+ }
136
+
137
+ const fileContent = currentContents.error ? "" : currentContents.data!;
138
+
139
+ // Find all sigils in the file
140
+ const sigilInfo: Record<string, SigilInfo> = {};
141
+ const endSigils: Array<{
142
+ regionId: string;
143
+ index: number;
144
+ indent: string;
145
+ line: string;
146
+ }> = [];
147
+
148
+ // Reset regex and find all sigils
149
+ SIGIL_REGEX.lastIndex = 0;
150
+ let match;
151
+ while ((match = SIGIL_REGEX.exec(fileContent)) !== null) {
152
+ const indent = match[1];
153
+ const fullLine = match[0];
154
+ const regionId = match[3];
155
+ const sigilType = match[4];
156
+ const index = match.index!;
157
+
158
+ // Initialize sigil info for this region if not exists
159
+ if (!sigilInfo[regionId]) {
160
+ sigilInfo[regionId] = { id: regionId, start: null, end: null };
161
+ }
162
+
163
+ if (sigilType === "start") {
164
+ sigilInfo[regionId].start = index;
165
+ } else if (sigilType === "end") {
166
+ sigilInfo[regionId].end = index;
167
+
168
+ // If this is a region we care about, track it for processing
169
+ if (regions.includes(regionId)) {
170
+ endSigils.push({
171
+ regionId,
172
+ index,
173
+ indent,
174
+ line: fullLine,
175
+ });
176
+ }
177
+ }
178
+ }
179
+
180
+ // Validate regions - check for unclosed regions
181
+ for (const regionId of regions) {
182
+ const info = sigilInfo[regionId];
183
+ if (info && info.start !== null && info.end === null) {
184
+ throw new Error(
185
+ `Region "${regionId}" has start sigil but no corresponding end sigil`,
186
+ );
187
+ }
188
+ }
189
+
190
+ // Check if we have any sigils to process
191
+ if (endSigils.length === 0) {
192
+ // No sigils found, append all regions to the end
193
+ const result: Children[] = [fileContent];
194
+ for (const regionId of regions) {
195
+ if (fileContent && !fileContent.endsWith("\n")) {
196
+ result.push("\n");
197
+ }
198
+ result.push(appendRegions[regionId]);
199
+ }
200
+ return (
201
+ <SourceFile path={props.path} filetype="text/plain">
202
+ {result}
203
+ </SourceFile>
204
+ );
205
+ }
206
+
207
+ // Sort end sigils by their position in the file
208
+ endSigils.sort((a, b) => a.index - b.index);
209
+
210
+ // Process content with sigils
211
+ const result: Children[] = [];
212
+ let lastIndex = 0;
213
+
214
+ // Process each end sigil in order
215
+ for (const { regionId, index, indent, line } of endSigils) {
216
+ // Add content before this sigil
217
+ if (index > lastIndex) {
218
+ const beforeContent = fileContent.substring(lastIndex, index);
219
+ if (beforeContent) {
220
+ result.push(beforeContent);
221
+ }
222
+ }
223
+
224
+ // Add the new content with proper indentation
225
+ result.push(indent);
226
+ result.push(appendRegions[regionId]);
227
+ result.push("\n");
228
+
229
+ // Add the sigil line
230
+ result.push(line);
231
+
232
+ // Update last processed index
233
+ lastIndex = index + line.length;
234
+ }
235
+
236
+ // Add any remaining content after the last sigil
237
+ if (lastIndex < fileContent.length) {
238
+ const remainingPart = fileContent.substring(lastIndex);
239
+ if (remainingPart) {
240
+ result.push(remainingPart);
241
+ }
242
+ }
243
+ return result;
244
+ });
245
+
246
+ return (
247
+ <SourceFile path={props.path} filetype="text/plain">
248
+ {newFileContent}
249
+ </SourceFile>
250
+ );
251
+ }
252
+
253
+ export interface AppendRegionPropsWithChildren {
254
+ /**
255
+ * The ID of the region.
256
+ */
257
+ id: string;
258
+
259
+ /**
260
+ * The content to append to the region.
261
+ */
262
+ children: Children;
263
+ }
264
+
265
+ export interface AppendRegionPropsWithContent {
266
+ /**
267
+ * The ID of the region.
268
+ */
269
+ id: string;
270
+
271
+ /**
272
+ * The content to append to the region.
273
+ */
274
+ content: Children;
275
+ }
276
+
277
+ export interface AppendRegionPropsBase {
278
+ /**
279
+ * The ID of the region.
280
+ */
281
+ id: string;
282
+ }
283
+
284
+ export type AppendRegionProps =
285
+ | AppendRegionPropsWithChildren
286
+ | AppendRegionPropsWithContent
287
+ | AppendRegionPropsBase;
288
+
289
+ export function AppendRegion(props: AppendRegionProps) {
290
+ /**
291
+ * This component does nothing except hold props which are retrieved by
292
+ * the `AppendFile` component.
293
+ */
294
+ }
@@ -0,0 +1,29 @@
1
+ import { join } from "pathe";
2
+ import { useContext } from "../context.js";
3
+ import { SourceDirectoryContext } from "../context/source-directory.js";
4
+ import { CopyFileContext } from "../context/source-file.js";
5
+ import { getContext } from "../reactivity.js";
6
+
7
+ export interface CopyFileProps {
8
+ /**
9
+ * The path to write the copy to, relative to the containing directory.
10
+ */
11
+ path: string;
12
+
13
+ /**
14
+ * The path to the file to copy.
15
+ */
16
+ src: string;
17
+ }
18
+
19
+ export function CopyFile(props: CopyFileProps) {
20
+ const parentDirectory = useContext(SourceDirectoryContext)!;
21
+ const context: CopyFileContext = {
22
+ path: join(parentDirectory ? parentDirectory.path : "", props.path),
23
+ sourcePath: props.src,
24
+ };
25
+ parentDirectory?.addContent(context);
26
+ const nodeContext = getContext()!;
27
+ nodeContext.meta ??= {};
28
+ nodeContext.meta.copyFile = context;
29
+ }
@@ -0,0 +1,193 @@
1
+ import { computed } from "@vue/reactivity";
2
+ import { createFileResource } from "../resource.js";
3
+ import { Children, isComponentCreator } from "../runtime/component.js";
4
+ import { childrenArray } from "../utils.jsx";
5
+ import { SourceFile } from "./SourceFile.jsx";
6
+
7
+ export interface TemplateFileProps {
8
+ /**
9
+ * The path to write the compiled template to.
10
+ */
11
+ path: string;
12
+
13
+ /**
14
+ * The file path of the template.
15
+ */
16
+ src: string;
17
+
18
+ /**
19
+ * Template variable children components.
20
+ */
21
+ children?: Children;
22
+ }
23
+
24
+ /**
25
+ * A component that reads a template file and replaces variable placeholders
26
+ * with actual values.
27
+ *
28
+ * Template files can contain variable placeholders in the format
29
+ * `{{ variable_name }}` which will be replaced with values from `TemplateVariable`
30
+ * children components. Whitespace around variable names is ignored, so
31
+ * `{{ name }}`, `{{name}}`, and `{{ name }}` are all equivalent.
32
+ *
33
+ * @example
34
+ * Basic usage with template variables:
35
+ * ```tsx
36
+ * // Template file content (greeting.txt):
37
+ * // "Hello {{ name }}! You are {{ age }} years old."
38
+ *
39
+ * <TemplateFile src="greeting.txt" path="output.txt">
40
+ * <TemplateVariable name="name" value="John" />
41
+ * <TemplateVariable name="age" value="25" />
42
+ * </TemplateFile>
43
+ * ```
44
+ *
45
+ * @example
46
+ * Using children instead of value prop:
47
+ * ```tsx
48
+ * // Template file content (welcome.txt):
49
+ * // "Welcome {{ greeting }}!"
50
+ *
51
+ * <TemplateFile src="welcome.txt" path="output.txt">
52
+ * <TemplateVariable name="greeting">Hello World</TemplateVariable>
53
+ * </TemplateFile>
54
+ * ```
55
+ *
56
+ * @example
57
+ * Complex template with multiple variables:
58
+ * ```tsx
59
+ * // Template file content (profile.txt):
60
+ * // "Name: {{ name }}\nAge: {{ age }}\nLocation: {{ location }}"
61
+ *
62
+ * <TemplateFile src="profile.txt" path="profile-output.txt">
63
+ * <TemplateVariable name="name" value="Alice" />
64
+ * <TemplateVariable name="age">30</TemplateVariable>
65
+ * <TemplateVariable name="location" value="New York" />
66
+ * </TemplateFile>
67
+ * ```
68
+ */
69
+ export function TemplateFile(props: TemplateFileProps): Children {
70
+ // Get all children and filter for TemplateVariable components
71
+ const children = childrenArray(() => props.children);
72
+ const templateVariables: Record<string, Children> = {};
73
+
74
+ // Extract variable values from TemplateVariable children
75
+ for (const child of children) {
76
+ if (!isComponentCreator(child, TemplateVariable)) {
77
+ continue;
78
+ }
79
+
80
+ const variableProps = child.props as TemplateVariableProps;
81
+ let value: Children;
82
+
83
+ if ("children" in variableProps && variableProps.children !== undefined) {
84
+ value = variableProps.children;
85
+ } else if ("value" in variableProps) {
86
+ value = variableProps.value;
87
+ } else {
88
+ throw new Error(
89
+ `TemplateVariable "${variableProps.name}" must have either children or value`,
90
+ );
91
+ }
92
+
93
+ templateVariables[variableProps.name] = value;
94
+ }
95
+
96
+ const templateResource = createFileResource(props.src);
97
+ const fileContent = computed(() => {
98
+ if (templateResource.loading) {
99
+ return;
100
+ }
101
+
102
+ if (templateResource.error) {
103
+ throw new Error(
104
+ `Failed to read template file "${props.src}": ${templateResource.error}`,
105
+ );
106
+ }
107
+
108
+ const templateContent = templateResource.data!;
109
+
110
+ // Parse template and replace variables
111
+ const result: Children[] = [];
112
+ let lastIndex = 0;
113
+
114
+ // Match {{ var_name }} patterns
115
+ const variableRegex = /\{\{\s*(\w+)\s*\}\}/g;
116
+ let match: RegExpExecArray | null;
117
+
118
+ while ((match = variableRegex.exec(templateContent)) !== null) {
119
+ const [fullMatch, variableName] = match;
120
+ const matchStart = match.index;
121
+
122
+ // Add content before the variable
123
+ if (matchStart > lastIndex) {
124
+ const beforeContent = templateContent.slice(lastIndex, matchStart);
125
+ if (beforeContent) {
126
+ result.push(beforeContent);
127
+ }
128
+ }
129
+
130
+ // Add the variable value
131
+ if (variableName in templateVariables) {
132
+ result.push(templateVariables[variableName]);
133
+ } else {
134
+ throw new Error(
135
+ `Template variable "${variableName}" not found in TemplateVariable children`,
136
+ );
137
+ }
138
+
139
+ lastIndex = matchStart + fullMatch.length;
140
+ }
141
+
142
+ // Add remaining content after the last variable
143
+ if (lastIndex < templateContent.length) {
144
+ const remainingContent = templateContent.slice(lastIndex);
145
+ if (remainingContent) {
146
+ result.push(remainingContent);
147
+ }
148
+ }
149
+
150
+ return result;
151
+ });
152
+
153
+ return (
154
+ <SourceFile path={props.path} filetype="text/plain">
155
+ {fileContent}
156
+ </SourceFile>
157
+ );
158
+ }
159
+
160
+ export interface TemplateVariablePropsWithChildren {
161
+ /**
162
+ * The name of the variable.
163
+ */
164
+ name: string;
165
+
166
+ /**
167
+ * The value of the variable.
168
+ */
169
+ children: Children;
170
+ }
171
+
172
+ export interface TemplateVariablePropsWithValue {
173
+ /**
174
+ * The name of the variable.
175
+ */
176
+ name: string;
177
+
178
+ /**
179
+ * The value of the variable.
180
+ */
181
+ value: string;
182
+ }
183
+
184
+ export type TemplateVariableProps =
185
+ | TemplateVariablePropsWithChildren
186
+ | TemplateVariablePropsWithValue;
187
+
188
+ export function TemplateVariable(props: TemplateVariableProps) {
189
+ /**
190
+ * This component does nothing except hold props which are retrieved by
191
+ * the `TemplateFile` component.
192
+ */
193
+ }
@@ -0,0 +1,86 @@
1
+ import { computed } from "@vue/reactivity";
2
+ import { join } from "pathe";
3
+ import { useContext } from "../context.js";
4
+ import { SourceDirectoryContext } from "../context/source-directory.js";
5
+ import { createFileResource } from "../resource.js";
6
+ import { Children } from "../runtime/component.js";
7
+ import { SourceFile } from "./SourceFile.jsx";
8
+ /**
9
+ * Props for the UpdateFile component.
10
+ */
11
+ export interface UpdateFileProps {
12
+ /** The relative path to the file to update or create */
13
+ path: string;
14
+ /** Optional path to a file containing default content to use when the target file doesn't exist */
15
+ defaultContentPath?: string;
16
+ /** Optional default content to use when the target file doesn't exist */
17
+ defaultContent?: Children;
18
+ /** Function that receives the current file contents and returns the new content */
19
+ children: (currentContents: string | null) => Children;
20
+ }
21
+
22
+ /**
23
+ * A component for updating existing files or initializing new files.
24
+ *
25
+ * This component allows you to read the current contents of a file and generate
26
+ * new content based on those contents. If the file doesn't exist, it can use
27
+ * default content from either a file path or inline content.
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * <UpdateFile path="config.json" defaultContent="{}">
32
+ * {(currentContents) => {
33
+ * const config = currentContents ? JSON.parse(currentContents) : {};
34
+ * config.newProperty = "value";
35
+ * return JSON.stringify(config, null, 2);
36
+ * }}
37
+ * </UpdateFile>
38
+ * ```
39
+ */
40
+ export function UpdateFile(props: UpdateFileProps) {
41
+ const parentDirectory = useContext(SourceDirectoryContext)!;
42
+ const fullPath = join(
43
+ parentDirectory ? parentDirectory.path : "",
44
+ props.path,
45
+ );
46
+ const fileResource = createFileResource(fullPath);
47
+
48
+ const newContent = computed(() => {
49
+ if (fileResource.loading) {
50
+ return;
51
+ }
52
+
53
+ if (fileResource.error) {
54
+ if ((fileResource.error as any).code === "ENOENT") {
55
+ if ("defaultContentPath" in props) {
56
+ const defaultContentResource = createFileResource(
57
+ props.defaultContentPath!,
58
+ );
59
+ return computed(() => {
60
+ if (defaultContentResource.loading) {
61
+ return;
62
+ }
63
+
64
+ if (defaultContentResource.error) {
65
+ throw defaultContentResource.error;
66
+ }
67
+
68
+ return props.children(defaultContentResource.data!);
69
+ });
70
+ } else {
71
+ return props.children(null);
72
+ }
73
+ }
74
+
75
+ throw fileResource.error;
76
+ }
77
+
78
+ return props.children(fileResource.data!);
79
+ });
80
+
81
+ return (
82
+ <SourceFile path={props.path} filetype="text/plain">
83
+ {newContent}
84
+ </SourceFile>
85
+ );
86
+ }
@@ -1,4 +1,6 @@
1
+ export * from "./AppendFile.jsx";
1
2
  export * from "./Block.js";
3
+ export * from "./CopyFile.jsx";
2
4
  export * from "./Declaration.js";
3
5
  export * from "./For.js";
4
6
  export * from "./Indent.jsx";
@@ -16,4 +18,6 @@ export * from "./SourceDirectory.js";
16
18
  export * from "./SourceFile.js";
17
19
  export * from "./StatementList.jsx";
18
20
  export * from "./Switch.jsx";
21
+ export * from "./TemplateFile.jsx";
22
+ export * from "./UpdateFile.jsx";
19
23
  export * from "./Wrap.jsx";
@@ -1,7 +1,9 @@
1
1
  import { stc } from "../../stc.js";
2
2
  import * as base from "../index.js";
3
3
 
4
+ export const AppendFile = stc(base.AppendFile);
4
5
  export const Block = stc(base.Block);
6
+ export const CopyFile = stc(base.CopyFile);
5
7
  export const Declaration = stc(base.Declaration);
6
8
  export const For = stc(base.For);
7
9
  export const Indent = stc(base.Indent);
@@ -18,6 +20,8 @@ export const StatementList = stc(base.StatementList);
18
20
  export const SourceDirectory = stc(base.SourceDirectory);
19
21
  export const SourceFile = stc(base.SourceFile);
20
22
  export const Switch = stc(base.Switch);
23
+ export const TemplateFile = stc(base.TemplateFile);
24
+ export const UpdateFile = stc(base.UpdateFile);
21
25
  export const Wrap = stc(base.Wrap);
22
26
  export const ReferenceOrContent = stc(base.ReferenceOrContent);
23
27
 
@@ -1,9 +1,11 @@
1
1
  import { ComponentContext, createNamedContext } from "../context.js";
2
- import type { SourceFileContext } from "./source-file.js";
2
+ import type { CopyFileContext, SourceFileContext } from "./source-file.js";
3
3
 
4
4
  export interface SourceDirectoryContext {
5
- contents: (SourceDirectoryContext | SourceFileContext)[];
6
- addContent(content: SourceDirectoryContext | SourceFileContext): void;
5
+ contents: (SourceDirectoryContext | SourceFileContext | CopyFileContext)[];
6
+ addContent(
7
+ content: SourceDirectoryContext | SourceFileContext | CopyFileContext,
8
+ ): void;
7
9
  path: string;
8
10
  }
9
11
 
@@ -2,6 +2,11 @@ import { ComponentContext, createNamedContext } from "../context.js";
2
2
  import type { Refkey } from "../refkey.js";
3
3
  import { ComponentDefinition } from "../runtime/component.js";
4
4
 
5
+ export interface CopyFileContext {
6
+ path: string;
7
+ sourcePath: string;
8
+ }
9
+
5
10
  export interface SourceFileContext {
6
11
  path: string;
7
12
  filetype: string;