-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ts
145 lines (121 loc) · 3.49 KB
/
setup.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
#! node.exe
import decompress from "decompress";
import { createWriteStream, existsSync, mkdirSync, rmSync } from "fs";
import { get } from "https";
import { join } from "path";
import { format } from "util";
async function _getLatestRelease(
owner: string,
repo: string,
): Promise<string> {
const options = {
hostname: "api.github.com",
path: `/repos/${owner}/${repo}/releases/latest`,
method: "GET",
headers: {
"User-Agent": "node.js",
Accept: "application/vnd.github.v3+json",
},
};
return new Promise((resolve, reject) => {
get(options, (res) => {
let data: string = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
if (res.statusCode === 200) {
const latestRelease = JSON.parse(data);
resolve(latestRelease.tag_name);
} else {
reject(
`Failed to fetch the latest release. Status code: ${res.statusCode}`,
);
}
});
}).on("error", (err) => {
reject(`Error: ${err.message}`);
});
});
}
function _downloadFile(sourceUrl: string, to: string, cb: Function): void {
get(sourceUrl, (response) => {
if (response.statusCode === 200) {
const fileStream = createWriteStream(to);
response.pipe(fileStream);
fileStream.on("close", () => {
cb();
});
} else if (
[301, 302].some((statusCode) => response.statusCode === statusCode)
) {
const redirectedUrl: string = response.headers.location as string;
_downloadFile(redirectedUrl, to, cb);
return;
} else {
throw new Error(
`Failed to download ncnn: ${response.statusCode} ${response.statusMessage}`,
);
}
}).on("error", (err: Error) => {
console.error(`Error downloading file: ${err.message}`);
});
}
function _extractFile(archive: string, destPath: string): void {
console.log("extracting: " + archive);
decompress(archive, destPath, {
strip: 1,
}).then(() => {
rmSync(archive, { recursive: true });
});
}
function install(url: string, destPath: string): void {
const tempFilePath: string = join(__dirname, "temp.zip");
//mkdir
mkdirSync(destPath, { recursive: true });
console.log("fetching: " + url);
_downloadFile(url, tempFilePath, () => {
_extractFile(tempFilePath, destPath);
console.log("ncnn-CLI installation complete.");
});
_downloadModels(destPath);
}
function _downloadModels(destPath: string): void {
const sourceUrl: string =
"https://github.com/iamspdarsan/pixteroid/releases/download/0.0.0/model.weights.zip";
const tempFilePath: string = join(__dirname, "models.zip");
const modelPath: string = join(destPath, "models");
_downloadFile(sourceUrl, tempFilePath, () => {
_extractFile(tempFilePath, modelPath);
console.log("Model weights downloaded.");
});
}
async function main(): Promise<void> {
const destpath: string = join(__dirname, "..", "..", "bin/ncnn");
if (existsSync(destpath)) {
return;
}
const downloadPath: string =
"https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan/releases/download/%s/realesrgan-ncnn-vulkan-%s-%s";
const version: Awaited<string> = await _getLatestRelease(
"xinntao",
"Real-ESRGAN-ncnn-vulkan",
);
if (process.platform === "darwin") {
install(format(downloadPath, version, version, "macos.zip"), destpath);
} else if (process.platform === "win32") {
install(
format(downloadPath, version, version, "windows.zip"),
destpath,
);
} else if (process.platform === "linux") {
install(
format(downloadPath, version, version, "ubuntu.zip"),
destpath,
);
}
}
main().catch((err: Error) => {
console.log(err);
process.exit(1);
});