forked from udarrr/opencv4nodejs-prebuilt-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload.js
102 lines (92 loc) · 2.81 KB
/
download.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const pack_1 = require("../build_release_opencv/pack");
const Downloader = require("nodejs-file-downloader");
const packageJson = require("../package.json");
const path = require("path");
const { existsSync } = require("fs");
const file = `opencv_${process.platform}_${packageJson.opencv4nodejs.autoBuildOpencvVersion}_${process.arch}.tgz`;
console.log(file);
async function downloadLib() {
let libUrl = `https://github.com/udarrr/opencv4nodejs-prebuilt-install/releases/download/v${packageJson.version}/${file}`;
try {
if (process.platform === "darwin" && process.arch !== "arm64") {
const pathToLib = `${path.join(
process.cwd(),
"osOpencvWorlds",
"darwin",
file
)}`;
const pathToDir = path.join(process.cwd(), "osOpencvWorlds", "darwin");
await downloadIfExist(pathToDir, pathToLib, libUrl);
await pack_1.Pack.unpack(pathToDir, pathToLib);
} else if (process.platform === "darwin" && process.arch === "arm64") {
const pathToLib = `${path.join(
process.cwd(),
"osOpencvWorlds",
"darwinM1",
file
)}`;
const pathToDir = `${path.join(
process.cwd(),
"osOpencvWorlds",
"darwinM1"
)}`;
await downloadIfExist(pathToDir, pathToLib, libUrl);
await pack_1.Pack.unpack(pathToDir, pathToLib);
} else if (process.platform === "linux") {
const pathToLib = `${path.join(
process.cwd(),
"osOpencvWorlds",
"linux",
file
)}`;
const pathToDir = `${path.join(
process.cwd(),
"osOpencvWorlds",
"linux"
)}`;
await downloadIfExist(pathToDir, pathToLib, libUrl);
await pack_1.Pack.unpack(pathToDir, pathToLib);
} else if (process.platform === "win32") {
const pathToLib = `${path.join(
process.cwd(),
"osOpencvWorlds",
"win32",
file
)}`;
const pathToDir = `${path.join(
process.cwd(),
"osOpencvWorlds",
"win32"
)}`;
await downloadIfExist(pathToDir, pathToLib, libUrl);
await pack_1.Pack.unpack(pathToDir, pathToLib);
}
await new Promise((res) =>
setTimeout(() => {
res("done");
}, 5000)
);
console.log("Lib has been downloaded");
} catch (error) {
console.log("Download failed", error);
}
}
async function downloadIfExist(pathToDir, pathToLib, libUrl) {
if (!existsSync(pathToLib)) {
console.log(
JSON.stringify({
url: libUrl,
directory: pathToDir
})
);
const downloaderInstance = new Downloader({
url: libUrl,
directory: pathToDir
});
const report = await downloaderInstance.download();
console.log(JSON.stringify(report));
}
}
downloadLib();