-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfilesystem.ts
More file actions
67 lines (54 loc) · 1.45 KB
/
filesystem.ts
File metadata and controls
67 lines (54 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {
Client,
ClientConfigBuilder,
} from "@polywrap/client-js";
import { fileSystemPlugin } from "@polywrap/file-system-plugin-js";
import { Uri } from "@polywrap/core-js";
const { uri } = Uri.from("wrapscan.io/polywrap/file-system@1.0");
const main = async () => {
const builder = new ClientConfigBuilder();
builder.setPackage(uri, fileSystemPlugin({}));
const client = new Client(builder.build());
const filePath = "./fs-example.txt";
const data = "Hello world!";
const writeFile = await client.invoke<boolean>({
uri: uri,
method: "writeFile",
args: {
path: filePath,
data: new TextEncoder().encode(data),
},
});
if (!writeFile.ok) {
throw Error("Error writing file: " + writeFile.error);
}
console.log("File created!");
const readFile = await client.invoke<Uint8Array>({
uri,
method: "readFile",
args: {
path: filePath,
},
});
if (!readFile.ok) {
throw Error("Error read file: " + readFile.error);
}
const decodedContent = new TextDecoder().decode(readFile.value);
console.log("Content from file: " + decodedContent);
const removeFile = await client.invoke<boolean>({
uri,
method: "rm",
args: {
path: filePath,
},
});
if (!removeFile.ok) {
throw Error("Error removing file file: " + removeFile.error);
}
console.log("File removed!");
};
main()
.then()
.catch((e) => {
console.error("Error in file system example: ", e);
});