-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-action.example.ts
More file actions
104 lines (85 loc) · 3.42 KB
/
event-action.example.ts
File metadata and controls
104 lines (85 loc) · 3.42 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import path from "path";
import { cp2 } from "./c-promise2";
import { config } from "./config";
import { ShellProcess } from "./utils/ShellProcess";
import { logr } from "./utils/logr";
import * as fs from "fs";
type TActionParams = { path: string; actionId: string; prevId: string };
export const fsEventAction = cp2.promisify(function* ({
path,
actionId,
prevId,
}: TActionParams) {
// @ts-ignore
const wasmPath: string = yield genWasm(actionId, prevId);
yield useWasmInApps(wasmPath);
});
const genWasm = cp2.promisify(function* (actionId: string, prevId: string) {
const genCmd = `GOOS=js CGO_ENABLED=0 GOARCH=wasm go build -o zcn-${actionId}.wasm github.com/0chain/gosdk/wasmsdk`;
const rmCmd = `rm zcn-${prevId}.wasm`;
const genProcess = new ShellProcess({ cwd: config.watchDir }, "wasm-gen");
genProcess.addOr(rmCmd).addAnd(genCmd);
yield genProcess.exec();
const wasmPath = `${config.watchDir}/zcn-${actionId}.wasm`;
logr.ok(`WASM generated: ${wasmPath}`);
return wasmPath;
});
const useWasmInApps = cp2.promisify(function* (wasmPath: string) {
for (const wasmDest of config.wasmDest) {
const wasmDir = path.join(config.webAppsDir, wasmDest);
// Delete all existing zcn.wasm files in wasmDir
const deleteWasmProcess = new ShellProcess(
{ cwd: wasmDir },
"delete-wasm-files"
);
const deleteWasmCmd = `find . -name 'zcn*.wasm' -exec rm {} +`; // Deleting all zcn.wasm files
deleteWasmProcess.addAnd(deleteWasmCmd);
yield deleteWasmProcess.exec();
// Copy the new wasm in wasmPath to wasmDir
const copyWasmProcess = new ShellProcess(
{ cwd: config.webAppsDir },
"copy-new-wasm"
);
const copyWasmCmd = `cp ${wasmPath} ${wasmDir}`;
copyWasmProcess.addAnd(copyWasmCmd);
yield copyWasmProcess.exec();
// You may use nodejs logics if cancellation doesnt matter to your use case
// for example, here these logics are not long running
// Update zcn.js file content
const filePath = path.join(wasmDir, "zcn.js");
updateZcnJs(filePath, wasmPath);
reloadWebapp(wasmDir);
}
});
// Function to update zcn.js file content
function updateZcnJs(filePath: string, wasmPath: string): void {
try {
const fileContent = fs.readFileSync(filePath, "utf-8");
// Define the pattern to search for
const searchPattern = /.*await fetch\('\/zcn.*'\).*/g;
// Find the line number containing the pattern
const lines = fileContent.split("\n");
const lineIndex = lines.findIndex((line) => searchPattern.test(line));
if (lineIndex !== -1) {
// Replace the line with the updated content
const wasmFileName = path.basename(wasmPath);
lines[lineIndex] = ` await fetch('/${wasmFileName}?v=20221230'),`;
// Write the modified content back to the file
fs.writeFileSync(filePath, lines.join("\n"), "utf-8");
logr.ok(`Updated zcn.js: ${filePath}`);
} else {
logr.error(`Pattern not found in ${filePath}`);
}
} catch (error) {
logr.error(`Error updating zcn.js: ${error}`);
}
}
function reloadWebapp(wasmDir: string) {
const appDir = path.dirname(wasmDir);
// Fast refreshing the webapp
const randomRefreshTriggeringFilePath = path.join(appDir, "i18n.js");
fs.utimesSync(randomRefreshTriggeringFilePath, new Date(), new Date());
// hard reloads nextjs dev server
// const nextConfigPath = path.join(appDir, "next.config.js");
// fs.utimesSync(nextConfigPath, new Date(), new Date());
}