@alloy-js/core 0.19.0-dev.12 → 0.19.0-dev.3
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/src/components/index.d.ts +0 -4
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +0 -4
- package/dist/src/components/stc/index.d.ts +0 -4
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +0 -4
- package/dist/src/context/source-directory.d.ts +3 -3
- package/dist/src/context/source-directory.d.ts.map +1 -1
- package/dist/src/context/source-file.d.ts +0 -4
- package/dist/src/context/source-file.d.ts.map +1 -1
- package/dist/src/debug.d.ts.map +1 -1
- package/dist/src/debug.js +1 -4
- package/dist/src/index.browser.d.ts +1 -1
- package/dist/src/index.browser.d.ts.map +1 -1
- package/dist/src/index.browser.js +2 -2
- package/dist/src/render.d.ts +2 -10
- package/dist/src/render.d.ts.map +1 -1
- package/dist/src/render.js +1 -20
- package/dist/src/scheduler.d.ts +0 -6
- package/dist/src/scheduler.d.ts.map +1 -1
- package/dist/src/scheduler.js +0 -36
- package/dist/src/write-output.browser.d.ts +2 -0
- package/dist/src/write-output.browser.d.ts.map +1 -0
- package/dist/src/write-output.browser.js +4 -0
- package/dist/src/write-output.d.ts +1 -1
- package/dist/src/write-output.d.ts.map +1 -1
- package/dist/src/write-output.js +21 -40
- package/dist/test/components/source-file.test.d.ts.map +1 -1
- package/dist/test/rendering/formatting.test.d.ts.map +1 -1
- package/dist/testing/extend-expect.js +54 -60
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/index.tsx +0 -4
- package/src/components/stc/index.ts +0 -4
- package/src/context/source-directory.ts +3 -5
- package/src/context/source-file.ts +0 -5
- package/src/debug.ts +1 -4
- package/src/index.browser.ts +1 -1
- package/src/render.ts +5 -44
- package/src/scheduler.ts +0 -39
- package/src/write-output.browser.ts +4 -0
- package/src/write-output.ts +19 -49
- package/temp/api.json +423 -1886
- package/test/components/source-file.test.tsx +2 -5
- package/test/rendering/formatting.test.tsx +3 -9
- package/testing/extend-expect.ts +58 -74
- package/testing/vitest.d.ts +0 -4
- package/dist/src/components/AppendFile.d.ts +0 -90
- package/dist/src/components/AppendFile.d.ts.map +0 -1
- package/dist/src/components/AppendFile.js +0 -226
- package/dist/src/components/CopyFile.d.ts +0 -12
- package/dist/src/components/CopyFile.d.ts.map +0 -1
- package/dist/src/components/CopyFile.js +0 -15
- package/dist/src/components/TemplateFile.d.ts +0 -84
- package/dist/src/components/TemplateFile.d.ts.map +0 -1
- package/dist/src/components/TemplateFile.js +0 -133
- package/dist/src/components/UpdateFile.d.ts +0 -34
- package/dist/src/components/UpdateFile.d.ts.map +0 -1
- package/dist/src/components/UpdateFile.js +0 -66
- package/dist/src/host/alloy-host.browser.d.ts +0 -11
- package/dist/src/host/alloy-host.browser.d.ts.map +0 -1
- package/dist/src/host/alloy-host.browser.js +0 -31
- package/dist/src/host/alloy-host.d.ts +0 -11
- package/dist/src/host/alloy-host.d.ts.map +0 -1
- package/dist/src/host/alloy-host.js +0 -143
- package/dist/src/host/interface.d.ts +0 -144
- package/dist/src/host/interface.d.ts.map +0 -1
- package/dist/src/host/interface.js +0 -1
- package/dist/src/resource.d.ts +0 -80
- package/dist/src/resource.d.ts.map +0 -1
- package/dist/src/resource.js +0 -118
- package/dist/test/components/append-file.test.d.ts +0 -2
- package/dist/test/components/append-file.test.d.ts.map +0 -1
- package/dist/test/components/append-file.test.js +0 -281
- package/dist/test/components/copy-file.test.d.ts +0 -2
- package/dist/test/components/copy-file.test.d.ts.map +0 -1
- package/dist/test/components/copy-file.test.js +0 -94
- package/dist/test/components/template-file.test.d.ts +0 -2
- package/dist/test/components/template-file.test.d.ts.map +0 -1
- package/dist/test/components/template-file.test.js +0 -133
- package/dist/test/components/update-file.test.d.ts +0 -2
- package/dist/test/components/update-file.test.d.ts.map +0 -1
- package/dist/test/components/update-file.test.js +0 -169
- package/src/components/AppendFile.tsx +0 -294
- package/src/components/CopyFile.tsx +0 -29
- package/src/components/TemplateFile.tsx +0 -193
- package/src/components/UpdateFile.tsx +0 -86
- package/src/host/alloy-host.browser.ts +0 -56
- package/src/host/alloy-host.ts +0 -160
- package/src/host/interface.ts +0 -153
- package/src/resource.ts +0 -152
- package/test/components/append-file.test.tsx +0 -275
- package/test/components/copy-file.test.tsx +0 -98
- package/test/components/template-file.test.tsx +0 -127
- package/test/components/update-file.test.tsx +0 -214
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
-
import { computed } from "@vue/reactivity";
|
|
3
|
-
import { createFileResource } from "../resource.js";
|
|
4
|
-
import { isComponentCreator } from "../runtime/component.js";
|
|
5
|
-
import { childrenArray } from "../utils.js";
|
|
6
|
-
import { SourceFile } from "./SourceFile.js";
|
|
7
|
-
/**
|
|
8
|
-
* A component that reads a template file and replaces variable placeholders
|
|
9
|
-
* with actual values.
|
|
10
|
-
*
|
|
11
|
-
* Template files can contain variable placeholders in the format
|
|
12
|
-
* `{{ variable_name }}` which will be replaced with values from `TemplateVariable`
|
|
13
|
-
* children components. Whitespace around variable names is ignored, so
|
|
14
|
-
* `{{ name }}`, `{{name}}`, and `{{ name }}` are all equivalent.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* Basic usage with template variables:
|
|
18
|
-
* ```tsx
|
|
19
|
-
* // Template file content (greeting.txt):
|
|
20
|
-
* // "Hello {{ name }}! You are {{ age }} years old."
|
|
21
|
-
*
|
|
22
|
-
* <TemplateFile src="greeting.txt" path="output.txt">
|
|
23
|
-
* <TemplateVariable name="name" value="John" />
|
|
24
|
-
* <TemplateVariable name="age" value="25" />
|
|
25
|
-
* </TemplateFile>
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* Using children instead of value prop:
|
|
30
|
-
* ```tsx
|
|
31
|
-
* // Template file content (welcome.txt):
|
|
32
|
-
* // "Welcome {{ greeting }}!"
|
|
33
|
-
*
|
|
34
|
-
* <TemplateFile src="welcome.txt" path="output.txt">
|
|
35
|
-
* <TemplateVariable name="greeting">Hello World</TemplateVariable>
|
|
36
|
-
* </TemplateFile>
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* Complex template with multiple variables:
|
|
41
|
-
* ```tsx
|
|
42
|
-
* // Template file content (profile.txt):
|
|
43
|
-
* // "Name: {{ name }}\nAge: {{ age }}\nLocation: {{ location }}"
|
|
44
|
-
*
|
|
45
|
-
* <TemplateFile src="profile.txt" path="profile-output.txt">
|
|
46
|
-
* <TemplateVariable name="name" value="Alice" />
|
|
47
|
-
* <TemplateVariable name="age">30</TemplateVariable>
|
|
48
|
-
* <TemplateVariable name="location" value="New York" />
|
|
49
|
-
* </TemplateFile>
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export function TemplateFile(props) {
|
|
53
|
-
// Get all children and filter for TemplateVariable components
|
|
54
|
-
const children = childrenArray(() => props.children);
|
|
55
|
-
const templateVariables = {};
|
|
56
|
-
|
|
57
|
-
// Extract variable values from TemplateVariable children
|
|
58
|
-
for (const child of children) {
|
|
59
|
-
if (!isComponentCreator(child, TemplateVariable)) {
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
const variableProps = child.props;
|
|
63
|
-
let value;
|
|
64
|
-
if ("children" in variableProps && variableProps.children !== undefined) {
|
|
65
|
-
value = variableProps.children;
|
|
66
|
-
} else if ("value" in variableProps) {
|
|
67
|
-
value = variableProps.value;
|
|
68
|
-
} else {
|
|
69
|
-
throw new Error(`TemplateVariable "${variableProps.name}" must have either children or value`);
|
|
70
|
-
}
|
|
71
|
-
templateVariables[variableProps.name] = value;
|
|
72
|
-
}
|
|
73
|
-
const templateResource = createFileResource(props.src);
|
|
74
|
-
const fileContent = computed(() => {
|
|
75
|
-
if (templateResource.loading) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
if (templateResource.error) {
|
|
79
|
-
throw new Error(`Failed to read template file "${props.src}": ${templateResource.error}`);
|
|
80
|
-
}
|
|
81
|
-
const templateContent = templateResource.data;
|
|
82
|
-
|
|
83
|
-
// Parse template and replace variables
|
|
84
|
-
const result = [];
|
|
85
|
-
let lastIndex = 0;
|
|
86
|
-
|
|
87
|
-
// Match {{ var_name }} patterns
|
|
88
|
-
const variableRegex = /\{\{\s*(\w+)\s*\}\}/g;
|
|
89
|
-
let match;
|
|
90
|
-
while ((match = variableRegex.exec(templateContent)) !== null) {
|
|
91
|
-
const [fullMatch, variableName] = match;
|
|
92
|
-
const matchStart = match.index;
|
|
93
|
-
|
|
94
|
-
// Add content before the variable
|
|
95
|
-
if (matchStart > lastIndex) {
|
|
96
|
-
const beforeContent = templateContent.slice(lastIndex, matchStart);
|
|
97
|
-
if (beforeContent) {
|
|
98
|
-
result.push(beforeContent);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Add the variable value
|
|
103
|
-
if (variableName in templateVariables) {
|
|
104
|
-
result.push(templateVariables[variableName]);
|
|
105
|
-
} else {
|
|
106
|
-
throw new Error(`Template variable "${variableName}" not found in TemplateVariable children`);
|
|
107
|
-
}
|
|
108
|
-
lastIndex = matchStart + fullMatch.length;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Add remaining content after the last variable
|
|
112
|
-
if (lastIndex < templateContent.length) {
|
|
113
|
-
const remainingContent = templateContent.slice(lastIndex);
|
|
114
|
-
if (remainingContent) {
|
|
115
|
-
result.push(remainingContent);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return result;
|
|
119
|
-
});
|
|
120
|
-
return _$createComponent(SourceFile, {
|
|
121
|
-
get path() {
|
|
122
|
-
return props.path;
|
|
123
|
-
},
|
|
124
|
-
filetype: "text/plain",
|
|
125
|
-
children: fileContent
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
export function TemplateVariable(props) {
|
|
129
|
-
/**
|
|
130
|
-
* This component does nothing except hold props which are retrieved by
|
|
131
|
-
* the `TemplateFile` component.
|
|
132
|
-
*/
|
|
133
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Children } from "../runtime/component.js";
|
|
2
|
-
/**
|
|
3
|
-
* Props for the UpdateFile component.
|
|
4
|
-
*/
|
|
5
|
-
export interface UpdateFileProps {
|
|
6
|
-
/** The relative path to the file to update or create */
|
|
7
|
-
path: string;
|
|
8
|
-
/** Optional path to a file containing default content to use when the target file doesn't exist */
|
|
9
|
-
defaultContentPath?: string;
|
|
10
|
-
/** Optional default content to use when the target file doesn't exist */
|
|
11
|
-
defaultContent?: Children;
|
|
12
|
-
/** Function that receives the current file contents and returns the new content */
|
|
13
|
-
children: (currentContents: string | null) => Children;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* A component for updating existing files or initializing new files.
|
|
17
|
-
*
|
|
18
|
-
* This component allows you to read the current contents of a file and generate
|
|
19
|
-
* new content based on those contents. If the file doesn't exist, it can use
|
|
20
|
-
* default content from either a file path or inline content.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```tsx
|
|
24
|
-
* <UpdateFile path="config.json" defaultContent="{}">
|
|
25
|
-
* {(currentContents) => {
|
|
26
|
-
* const config = currentContents ? JSON.parse(currentContents) : {};
|
|
27
|
-
* config.newProperty = "value";
|
|
28
|
-
* return JSON.stringify(config, null, 2);
|
|
29
|
-
* }}
|
|
30
|
-
* </UpdateFile>
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export declare function UpdateFile(props: UpdateFileProps): Children;
|
|
34
|
-
//# sourceMappingURL=UpdateFile.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"UpdateFile.d.ts","sourceRoot":"","sources":["../../../src/components/UpdateFile.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAEnD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,mGAAmG;IACnG,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,yEAAyE;IACzE,cAAc,CAAC,EAAE,QAAQ,CAAC;IAC1B,mFAAmF;IACnF,QAAQ,EAAE,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,KAAK,QAAQ,CAAC;CACxD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,eAAe,YA8ChD"}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
-
import { computed } from "@vue/reactivity";
|
|
3
|
-
import { join } from "pathe";
|
|
4
|
-
import { useContext } from "../context.js";
|
|
5
|
-
import { SourceDirectoryContext } from "../context/source-directory.js";
|
|
6
|
-
import { createFileResource } from "../resource.js";
|
|
7
|
-
import { SourceFile } from "./SourceFile.js";
|
|
8
|
-
/**
|
|
9
|
-
* Props for the UpdateFile component.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A component for updating existing files or initializing new files.
|
|
14
|
-
*
|
|
15
|
-
* This component allows you to read the current contents of a file and generate
|
|
16
|
-
* new content based on those contents. If the file doesn't exist, it can use
|
|
17
|
-
* default content from either a file path or inline content.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```tsx
|
|
21
|
-
* <UpdateFile path="config.json" defaultContent="{}">
|
|
22
|
-
* {(currentContents) => {
|
|
23
|
-
* const config = currentContents ? JSON.parse(currentContents) : {};
|
|
24
|
-
* config.newProperty = "value";
|
|
25
|
-
* return JSON.stringify(config, null, 2);
|
|
26
|
-
* }}
|
|
27
|
-
* </UpdateFile>
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export function UpdateFile(props) {
|
|
31
|
-
const parentDirectory = useContext(SourceDirectoryContext);
|
|
32
|
-
const fullPath = join(parentDirectory ? parentDirectory.path : "", props.path);
|
|
33
|
-
const fileResource = createFileResource(fullPath);
|
|
34
|
-
const newContent = computed(() => {
|
|
35
|
-
if (fileResource.loading) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
if (fileResource.error) {
|
|
39
|
-
if (fileResource.error.code === "ENOENT") {
|
|
40
|
-
if ("defaultContentPath" in props) {
|
|
41
|
-
const defaultContentResource = createFileResource(props.defaultContentPath);
|
|
42
|
-
return computed(() => {
|
|
43
|
-
if (defaultContentResource.loading) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
if (defaultContentResource.error) {
|
|
47
|
-
throw defaultContentResource.error;
|
|
48
|
-
}
|
|
49
|
-
return props.children(defaultContentResource.data);
|
|
50
|
-
});
|
|
51
|
-
} else {
|
|
52
|
-
return props.children(null);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
throw fileResource.error;
|
|
56
|
-
}
|
|
57
|
-
return props.children(fileResource.data);
|
|
58
|
-
});
|
|
59
|
-
return _$createComponent(SourceFile, {
|
|
60
|
-
get path() {
|
|
61
|
-
return props.path;
|
|
62
|
-
},
|
|
63
|
-
filetype: "text/plain",
|
|
64
|
-
children: newContent
|
|
65
|
-
});
|
|
66
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { AlloyFileInterface, AlloyHostInterface } from "./interface.js";
|
|
2
|
-
export declare const AlloyHost: AlloyHostInterface;
|
|
3
|
-
export declare class AlloyFile implements AlloyFileInterface {
|
|
4
|
-
private source;
|
|
5
|
-
constructor(source: string);
|
|
6
|
-
text(): Promise<string>;
|
|
7
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
8
|
-
bytes(): Promise<Uint8Array>;
|
|
9
|
-
stream(): ReadableStream<Uint8Array>;
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=alloy-host.browser.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"alloy-host.browser.d.ts","sourceRoot":"","sources":["../../../src/host/alloy-host.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAExE,eAAO,MAAM,SAAS,EAAE,kBAyBvB,CAAC;AAEF,qBAAa,SAAU,YAAW,kBAAkB;IACtC,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAE5B,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC;IAMvB,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAMnC,KAAK,IAAI,OAAO,CAAC,UAAU,CAAC;IAMlC,MAAM,IAAI,cAAc,CAAC,UAAU,CAAC;CAKrC"}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export const AlloyHost = {
|
|
2
|
-
read(source) {
|
|
3
|
-
return new AlloyFile(source);
|
|
4
|
-
},
|
|
5
|
-
async write(destination, content) {
|
|
6
|
-
throw new Error("File system write operations are not supported in the browser");
|
|
7
|
-
},
|
|
8
|
-
async exists(source) {
|
|
9
|
-
throw new Error("File system exists operations are not supported in the browser");
|
|
10
|
-
},
|
|
11
|
-
async mkdir(path) {
|
|
12
|
-
throw new Error("File system mkdir operations are not supported in the browser");
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
export class AlloyFile {
|
|
16
|
-
constructor(source) {
|
|
17
|
-
this.source = source;
|
|
18
|
-
}
|
|
19
|
-
async text() {
|
|
20
|
-
throw new Error("File system read operations are not supported in the browser");
|
|
21
|
-
}
|
|
22
|
-
async arrayBuffer() {
|
|
23
|
-
throw new Error("File system read operations are not supported in the browser");
|
|
24
|
-
}
|
|
25
|
-
async bytes() {
|
|
26
|
-
throw new Error("File system read operations are not supported in the browser");
|
|
27
|
-
}
|
|
28
|
-
stream() {
|
|
29
|
-
throw new Error("File system read operations are not supported in the browser");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { AlloyFileInterface, AlloyHostInterface } from "./interface.js";
|
|
2
|
-
export declare const AlloyHost: AlloyHostInterface;
|
|
3
|
-
export declare class AlloyFile implements AlloyFileInterface {
|
|
4
|
-
private source;
|
|
5
|
-
constructor(source: string);
|
|
6
|
-
text(): Promise<string>;
|
|
7
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
8
|
-
bytes(): Promise<Uint8Array>;
|
|
9
|
-
stream(): ReadableStream<Uint8Array>;
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=alloy-host.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"alloy-host.d.ts","sourceRoot":"","sources":["../../../src/host/alloy-host.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAExE,eAAO,MAAM,SAAS,EAAE,kBAsFvB,CAAC;AAEF,qBAAa,SAAU,YAAW,kBAAkB;IACtC,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAE5B,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC;IAUvB,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAcnC,KAAK,IAAI,OAAO,CAAC,UAAU,CAAC;IAWlC,MAAM,IAAI,cAAc,CAAC,UAAU,CAAC;CA6BrC"}
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { createReadStream, createWriteStream } from "fs";
|
|
2
|
-
import { access, mkdir, readFile, writeFile } from "fs/promises";
|
|
3
|
-
export const AlloyHost = {
|
|
4
|
-
read(source) {
|
|
5
|
-
return new AlloyFile(source);
|
|
6
|
-
},
|
|
7
|
-
async write(destination, content) {
|
|
8
|
-
if (typeof content === "string") {
|
|
9
|
-
//eslint-disable-next-line no-useless-catch
|
|
10
|
-
try {
|
|
11
|
-
await writeFile(destination, content, "utf8");
|
|
12
|
-
} catch (e) {
|
|
13
|
-
// get good callstacks
|
|
14
|
-
throw e;
|
|
15
|
-
}
|
|
16
|
-
} else if (content instanceof ArrayBuffer) {
|
|
17
|
-
//eslint-disable-next-line no-useless-catch
|
|
18
|
-
try {
|
|
19
|
-
await writeFile(destination, new Uint8Array(content));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
// get good callstacks
|
|
22
|
-
throw e;
|
|
23
|
-
}
|
|
24
|
-
} else if (content instanceof Uint8Array) {
|
|
25
|
-
//eslint-disable-next-line no-useless-catch
|
|
26
|
-
try {
|
|
27
|
-
await writeFile(destination, content);
|
|
28
|
-
} catch (e) {
|
|
29
|
-
// get good callstacks
|
|
30
|
-
throw e;
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
// content is ReadableStream<Uint8Array>
|
|
34
|
-
//eslint-disable-next-line no-useless-catch
|
|
35
|
-
try {
|
|
36
|
-
const writeStream = createWriteStream(destination);
|
|
37
|
-
const reader = content.getReader();
|
|
38
|
-
try {
|
|
39
|
-
while (true) {
|
|
40
|
-
const {
|
|
41
|
-
done,
|
|
42
|
-
value
|
|
43
|
-
} = await reader.read();
|
|
44
|
-
if (done) break;
|
|
45
|
-
await new Promise((resolve, reject) => {
|
|
46
|
-
writeStream.write(value, err => {
|
|
47
|
-
if (err) reject(err);else resolve();
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
} finally {
|
|
52
|
-
reader.releaseLock();
|
|
53
|
-
await new Promise((resolve, reject) => {
|
|
54
|
-
writeStream.end(err => {
|
|
55
|
-
if (err) reject(err);else resolve();
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
} catch (e) {
|
|
60
|
-
// get good callstacks
|
|
61
|
-
throw e;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
async exists(source) {
|
|
66
|
-
try {
|
|
67
|
-
await access(source);
|
|
68
|
-
return true;
|
|
69
|
-
} catch {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
async mkdir(path) {
|
|
74
|
-
//eslint-disable-next-line no-useless-catch
|
|
75
|
-
try {
|
|
76
|
-
await mkdir(path, {
|
|
77
|
-
recursive: true
|
|
78
|
-
});
|
|
79
|
-
} catch (e) {
|
|
80
|
-
// get good callstacks
|
|
81
|
-
throw e;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
export class AlloyFile {
|
|
86
|
-
constructor(source) {
|
|
87
|
-
this.source = source;
|
|
88
|
-
}
|
|
89
|
-
async text() {
|
|
90
|
-
//eslint-disable-next-line no-useless-catch
|
|
91
|
-
try {
|
|
92
|
-
return await readFile(this.source, "utf8");
|
|
93
|
-
} catch (e) {
|
|
94
|
-
// get good callstacks
|
|
95
|
-
throw e;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
async arrayBuffer() {
|
|
99
|
-
//eslint-disable-next-line no-useless-catch
|
|
100
|
-
try {
|
|
101
|
-
const buffer = await readFile(this.source);
|
|
102
|
-
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
103
|
-
} catch (e) {
|
|
104
|
-
// get good callstacks
|
|
105
|
-
throw e;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
async bytes() {
|
|
109
|
-
//eslint-disable-next-line no-useless-catch
|
|
110
|
-
try {
|
|
111
|
-
const buffer = await readFile(this.source);
|
|
112
|
-
return new Uint8Array(buffer);
|
|
113
|
-
} catch (e) {
|
|
114
|
-
// get good callstacks
|
|
115
|
-
throw e;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
stream() {
|
|
119
|
-
//eslint-disable-next-line no-useless-catch
|
|
120
|
-
try {
|
|
121
|
-
const nodeStream = createReadStream(this.source);
|
|
122
|
-
return new ReadableStream({
|
|
123
|
-
start(controller) {
|
|
124
|
-
nodeStream.on("data", chunk => {
|
|
125
|
-
controller.enqueue(new Uint8Array(chunk));
|
|
126
|
-
});
|
|
127
|
-
nodeStream.on("end", () => {
|
|
128
|
-
controller.close();
|
|
129
|
-
});
|
|
130
|
-
nodeStream.on("error", err => {
|
|
131
|
-
controller.error(err);
|
|
132
|
-
});
|
|
133
|
-
},
|
|
134
|
-
cancel() {
|
|
135
|
-
nodeStream.destroy();
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
} catch (e) {
|
|
139
|
-
// get good callstacks
|
|
140
|
-
throw e;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interface for the Alloy host file system operations.
|
|
3
|
-
*
|
|
4
|
-
* This interface abstracts file system operations to allow different
|
|
5
|
-
* implementations across different environments (Node.js, browser, etc.).
|
|
6
|
-
*/
|
|
7
|
-
export interface AlloyHostInterface {
|
|
8
|
-
/**
|
|
9
|
-
* Read a file from the file system.
|
|
10
|
-
*
|
|
11
|
-
* @param source - The path to the file to read
|
|
12
|
-
* @returns An AlloyFileInterface for accessing the file content in different formats
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* const file = AlloyHost.read('./config.json');
|
|
17
|
-
* const content = await file.text();
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
read(source: string): AlloyFileInterface;
|
|
21
|
-
/**
|
|
22
|
-
* Write content to a file in the file system.
|
|
23
|
-
*
|
|
24
|
-
* Supports writing different types of content including strings, binary data,
|
|
25
|
-
* and streams. For strings, content is written with UTF-8 encoding.
|
|
26
|
-
*
|
|
27
|
-
* @param destination - The path where the file should be written
|
|
28
|
-
* @param content - The content to write (string, ArrayBuffer, Uint8Array, or ReadableStream)
|
|
29
|
-
* @returns A Promise that resolves when the write operation is complete
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* // Write a string
|
|
34
|
-
* await AlloyHost.write('./output.txt', 'Hello, world!');
|
|
35
|
-
*
|
|
36
|
-
* // Write binary data
|
|
37
|
-
* const data = new Uint8Array([72, 101, 108, 108, 111]);
|
|
38
|
-
* await AlloyHost.write('./binary.dat', data);
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
write(destination: string, content: string | ArrayBuffer | Uint8Array | ReadableStream<Uint8Array>): Promise<void>;
|
|
42
|
-
/**
|
|
43
|
-
* Check if a file or directory exists at the given path.
|
|
44
|
-
*
|
|
45
|
-
* @param source - The path to check for existence
|
|
46
|
-
* @returns A Promise that resolves to true if the path exists, false otherwise
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* const fileExists = await AlloyHost.exists('./config.json');
|
|
51
|
-
* if (fileExists) {
|
|
52
|
-
* // File exists, safe to read
|
|
53
|
-
* }
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
exists(source: string): Promise<boolean>;
|
|
57
|
-
/**
|
|
58
|
-
* Create a directory at the specified path.
|
|
59
|
-
*
|
|
60
|
-
* Creates the directory and any necessary parent directories recursively.
|
|
61
|
-
* If the directory already exists, this operation succeeds without error.
|
|
62
|
-
*
|
|
63
|
-
* @param path - The path of the directory to create
|
|
64
|
-
* @returns A Promise that resolves when the directory creation is complete
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```typescript
|
|
68
|
-
* await AlloyHost.mkdir('./output/nested/directory');
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
mkdir(path: string): Promise<void>;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Interface for reading file content in different formats.
|
|
75
|
-
*
|
|
76
|
-
* This interface provides multiple ways to access the same file content,
|
|
77
|
-
* allowing consumers to choose the most appropriate format for their use case.
|
|
78
|
-
*/
|
|
79
|
-
export interface AlloyFileInterface {
|
|
80
|
-
/**
|
|
81
|
-
* Read the file content as a UTF-8 encoded string.
|
|
82
|
-
*
|
|
83
|
-
* @returns A Promise that resolves to the file content as a string
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```typescript
|
|
87
|
-
* const file = AlloyHost.read('./config.json');
|
|
88
|
-
* const jsonString = await file.text();
|
|
89
|
-
* const config = JSON.parse(jsonString);
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
text(): Promise<string>;
|
|
93
|
-
/**
|
|
94
|
-
* Read the file content as an ArrayBuffer.
|
|
95
|
-
*
|
|
96
|
-
* Useful for working with binary data or when you need a raw buffer
|
|
97
|
-
* that can be used with various JavaScript APIs.
|
|
98
|
-
*
|
|
99
|
-
* @returns A Promise that resolves to the file content as an ArrayBuffer
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* ```typescript
|
|
103
|
-
* const file = AlloyHost.read('./image.png');
|
|
104
|
-
* const buffer = await file.arrayBuffer();
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
108
|
-
/**
|
|
109
|
-
* Read the file content as a Uint8Array.
|
|
110
|
-
*
|
|
111
|
-
* Convenient for working with binary data when you need direct access
|
|
112
|
-
* to individual bytes.
|
|
113
|
-
*
|
|
114
|
-
* @returns A Promise that resolves to the file content as a Uint8Array
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```typescript
|
|
118
|
-
* const file = AlloyHost.read('./binary.dat');
|
|
119
|
-
* const bytes = await file.bytes();
|
|
120
|
-
* console.log('First byte:', bytes[0]);
|
|
121
|
-
* ```
|
|
122
|
-
*/
|
|
123
|
-
bytes(): Promise<Uint8Array>;
|
|
124
|
-
/**
|
|
125
|
-
* Get a readable stream of the file content.
|
|
126
|
-
*
|
|
127
|
-
* Useful for processing large files without loading the entire content
|
|
128
|
-
* into memory at once, or for piping data to other streams.
|
|
129
|
-
*
|
|
130
|
-
* @returns A ReadableStream that yields Uint8Array chunks of the file content
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* ```typescript
|
|
134
|
-
* const file = AlloyHost.read('./large-file.dat');
|
|
135
|
-
* const stream = file.stream();
|
|
136
|
-
*
|
|
137
|
-
* for await (const chunk of stream) {
|
|
138
|
-
* console.log('Received chunk:', chunk);
|
|
139
|
-
* }
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
|
-
stream(): ReadableStream<Uint8Array>;
|
|
143
|
-
}
|
|
144
|
-
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/host/interface.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;;;;;;;;OAWG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,kBAAkB,CAAC;IAEzC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK,CACH,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,GAAG,WAAW,GAAG,UAAU,GAAG,cAAc,CAAC,UAAU,CAAC,GACtE,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEzC;;;;;;;;;;;;;OAaG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAED;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;;;;;;;;OAWG;IACH,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAExB;;;;;;;;;;;;;OAaG;IACH,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IAEpC;;;;;;;;;;;;;;OAcG;IACH,KAAK,IAAI,OAAO,CAAC,UAAU,CAAC,CAAC;IAE7B;;;;;;;;;;;;;;;;;OAiBG;IACH,MAAM,IAAI,cAAc,CAAC,UAAU,CAAC,CAAC;CACtC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/src/resource.d.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Ref } from "@vue/reactivity";
|
|
2
|
-
/**
|
|
3
|
-
* Represents an external resource fetched asynchronously.
|
|
4
|
-
*/
|
|
5
|
-
export interface Resource<T> {
|
|
6
|
-
/**
|
|
7
|
-
* The data if it's been loaded successfully. Null when not yet loaded or
|
|
8
|
-
* there was an error.
|
|
9
|
-
*/
|
|
10
|
-
data: T | null;
|
|
11
|
-
/**
|
|
12
|
-
* Whether the resource is still being fetched.
|
|
13
|
-
*/
|
|
14
|
-
loading: boolean;
|
|
15
|
-
/**
|
|
16
|
-
* The error loading the resource, if any.
|
|
17
|
-
*/
|
|
18
|
-
error: null | Error;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Create a resource that fetches data asynchronously.
|
|
22
|
-
*
|
|
23
|
-
* This function has two overloads:
|
|
24
|
-
* 1. Simple fetcher - fetches data once when the resource is created
|
|
25
|
-
* 2. Reactive fetcher - fetches data when a reactive source changes
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* // Simple usage - fetches data once when created
|
|
30
|
-
* const userResource = createResource(async () => {
|
|
31
|
-
* const response = await fetch('/api/user');
|
|
32
|
-
* return response.json();
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* // Access the resource state
|
|
36
|
-
* console.log(userResource.loading); // true initially
|
|
37
|
-
* console.log(userResource.data); // null initially
|
|
38
|
-
* console.log(userResource.error); // null initially
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```typescript
|
|
43
|
-
* // Reactive usage - fetches data when the ref changes
|
|
44
|
-
* const userId = ref(1);
|
|
45
|
-
*
|
|
46
|
-
* const userResource = createResource(userId, async (id) => {
|
|
47
|
-
* const response = await fetch(`/api/user/${id}`);
|
|
48
|
-
* return response.json();
|
|
49
|
-
* });
|
|
50
|
-
*
|
|
51
|
-
* // The fetcher will be called automatically when userId changes
|
|
52
|
-
* userId.value = 2; // This triggers a new fetch with id=2
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export declare function createResource<U>(fetcher: () => Promise<U>): Resource<U>;
|
|
56
|
-
/**
|
|
57
|
-
* Create a resource that fetches data asynchronously based on a reactive source.
|
|
58
|
-
*/
|
|
59
|
-
export declare function createResource<T, U>(refSource: Ref<T> | (() => T), fetcher: (input: T) => Promise<U>): Resource<U>;
|
|
60
|
-
/**
|
|
61
|
-
* Create a resource that reads a file from the file system.
|
|
62
|
-
*
|
|
63
|
-
* This is a convenience function that creates a resource for reading file content
|
|
64
|
-
* using the AlloyHost file system API. The file is read as text when the resource
|
|
65
|
-
* is created.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* // Read a configuration file
|
|
70
|
-
* const configResource = createFileResource('./config.json');
|
|
71
|
-
*
|
|
72
|
-
* // Access the file content
|
|
73
|
-
* if (!configResource.loading && !configResource.error) {
|
|
74
|
-
* const configText = configResource.data; // string content of the file
|
|
75
|
-
* const config = JSON.parse(configText);
|
|
76
|
-
* }
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
export declare function createFileResource(path: string): Resource<string>;
|
|
80
|
-
//# sourceMappingURL=resource.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../../src/resource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAKvD;;GAEG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC;IACzB;;;OAGG;IACH,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IAEf;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,KAAK,EAAE,IAAI,GAAG,KAAK,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC1E;;GAEG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,EACjC,SAAS,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAC7B,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAChC,QAAQ,CAAC,CAAC,CAAC,CAAC;AA4Df;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,oBAI9C"}
|