-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_updates.ts
207 lines (179 loc) · 5.69 KB
/
auto_updates.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import axios from "npm:axios";
import fs from "node:fs";
import unzipper from "npm:unzipper";
import path from "node:path";
import { spawn } from "node:child_process";
import process from "node:process";
// import { isWindows } from "https://deno.land/[email protected]/os/mod.ts";
"node:path"
const appInfos: any[] = [];
async function fetchZipToBundle() {
for (const info of appInfos) {
if (!info) continue;
const dwebAppBase =
"https://raw.githubusercontent.com/BFChainMeta/awesome-bfmeta/main/src/dweb-apps";
const zipUrl = path.join(
dwebAppBase,
info.appName,
info.version,
info.zipFullName,
);
const srcPath = info.appPath;
try {
await downloadOriginZip(zipUrl, srcPath, srcPath);
await buildBundle(info, path.join(srcPath, "usr/www"), info.bundlePath);
} catch (error) {
console.error(`Error in fetchZipToBundle: ${error}`);
}
}
}
async function downloadOriginZip(
fileUrl: string,
srcPath: string,
desPath: string,
): Promise<void> {
const { headers } = await axios.head(fileUrl);
const totalLength = headers["content-length"];
const normalizedSrcPath = path.normalize(srcPath);
const appPath = normalizedSrcPath.split(path.sep)[1];
const appName = appPath.split("-")[0]; // .filter(part => part.includes('apps') && part.includes('-'))[0].split('-')[0];
console.log("\n-----------开始处理---" + appName + "-----------");
const writer = fs.createWriteStream(path.join(srcPath, "file.zip"));
const response = await axios({
url: fileUrl,
method: "GET",
responseType: "stream",
});
let downloadedLength = 0;
response.data.on("data", (chunk: any) => {
downloadedLength += chunk.length;
const percentage = (downloadedLength / parseInt(totalLength)) * 100; // Calculate the percentage downloaded
process.stdout.write(`Downloading... ${percentage.toFixed(2)}%\r`); // Print the percentage in the same line.
});
// Pipe the response stream to the file write stream
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", () => {
fs.createReadStream(path.join(srcPath, "file.zip"))
.pipe(unzipper.Extract({ path: desPath }))
.on("close", () => {
console.log("\n解压成功.");
resolve();
});
});
writer.on("error", reject);
});
}
async function buildBundle(info: any, resourcePath: string, outputPath: string) {
console.log("开始打包");
const command = "plaoc";
let params = [
"bundle",
resourcePath,
"--id",
info.appId,
"--version",
info.version,
"--out",
outputPath
];
const cmd = [command, ...params];
const cmdStr = cmd.join(' ');
let runCmd;
// Deno.build.os 会根据操作系统类型返回 ‘windows’ 'darwin' 或 'linux'
if (Deno.build.os === 'windows') {
runCmd = ['cmd.exe', '/c', cmdStr];
} else {
runCmd = cmd;
}
const p = Deno.run({
cmd: runCmd,
stdout: "piped",
stderr: "piped"
});
const { code } = await p.status();
if (code === 0) {
const rawOutput = await p.output();
await Deno.stdout.write(rawOutput);
} else {
const rawError = await p.stderrOutput();
const errorString = new TextDecoder().decode(rawError);
console.log(errorString);
}
}
async function fecthAppInfo(
appInfo: { appName: string; version: string; configLink: string },
) {
const response = await fetch(appInfo.configLink);
const infoJson = await response.json();
const bundle_url = infoJson["bundle_url"];
const appId = infoJson["id"];
const zipFullName = bundle_url.split("./")[1];
const zipName = zipFullName.split(".zip")[0];
const appConfigs = {
"appId": appId,
"appName": appInfo.appName,
"version": appInfo.version,
"zipName": zipName,
"zipFullName": zipFullName,
};
await creatAppDirs(appConfigs, infoJson);
return appConfigs;
}
async function fetchApplist() {
const appListUrl =
"https://raw.githubusercontent.com/BFChainMeta/awesome-bfmeta/main/src/dweb-apps/applist.json";
const baseUrl =
"https://raw.githubusercontent.com/BFChainMeta/awesome-bfmeta/main/src/dweb-apps";
const response = await fetch(appListUrl);
const jsonData = await response.json();
const applist = jsonData["applist"];
const list = Object.keys(applist).map((key) => {
return {
"appName": key,
"version": applist[key].latest,
"configLink": `${baseUrl}/${key}/${applist[key].latest}/metadata.json`,
};
});
console.log('获取最新app列表')
return list;
}
async function creatAppDirs(appInfo: any, metadata: any) {
const appsRoot = "./apps";
if (!fs.existsSync(appsRoot)) {
fs.mkdirSync(appsRoot, { recursive: true });
}
const appPath = path.join(appsRoot, appInfo.appName + "-" + appInfo.version);
if (!fs.existsSync(appPath)) {
fs.mkdirSync(appPath, { recursive: true });
}
const bundlePath = path.join(appPath, appInfo.version);
if (!fs.existsSync(bundlePath)) {
fs.mkdirSync(bundlePath, { recursive: true });
}
await fs.writeFile(
appPath + "/metadata.json",
JSON.stringify(metadata),
"utf-8",
(err) => {
if (err) {
console.error(err);
}
},
);
appInfo["appPath"] = appPath;
appInfo["bundlePath"] = bundlePath;
}
//读取网络app配置,并创建对应目录
async function buildConfig() {
const applist = await fetchApplist();
for (const index in applist) {
// let app = applist[0];
const app = applist[index];
const appInfo = await fecthAppInfo(app);
appInfos.push(appInfo);
}
console.log('已更新所有app信息')
}
await buildConfig();
await fetchZipToBundle();