diff --git a/install/download.js b/install/download.js index 8e7f2b7fe..2b7504112 100644 --- a/install/download.js +++ b/install/download.js @@ -4,6 +4,7 @@ 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); @@ -12,33 +13,29 @@ async function download() { try { if (process.platform === "darwin" && process.arch !== "arm64") { - await new Downloader({ - url: libUrl, - directory: path.join(process.cwd(), "osOpencvWorlds", "darwin"), - }).download(); + const pathToLib = `${path.join(process.cwd(), "osOpencvWorlds", "darwin", file)}`; + const pathToDir = path.join(process.cwd(), "osOpencvWorlds", "darwin"); - await pack_1.Pack.unpack(`${path.join(process.cwd(), "osOpencvWorlds", "darwin")}`, `${path.join(process.cwd(), "osOpencvWorlds", "darwin", file)}`); + await downloadIfExist(pathToDir, pathToLib, libUrl); + await pack_1.Pack.unpack(pathToDir, pathToLib); } else if (process.platform === "darwin" && process.arch === "arm64") { - await new Downloader({ - url: libUrl, - directory: path.join(process.cwd(), "osOpencvWorlds", "darwinM1"), - }).download(); + const pathToLib = `${path.join(process.cwd(), "osOpencvWorlds", "darwinM1", file)}`; + const pathToDir = `${path.join(process.cwd(), "osOpencvWorlds", "darwinM1")}`; - await pack_1.Pack.unpack(`${path.join(process.cwd(), "osOpencvWorlds", "darwinM1")}`, `${path.join(process.cwd(), "osOpencvWorlds", "darwinM1", file)}`); + await downloadIfExist(pathToDir, pathToLib, libUrl); + await pack_1.Pack.unpack(pathToDir, pathToLib); } else if (process.platform === "linux") { - await new Downloader({ - url: libUrl, - directory: path.join(process.cwd(), "osOpencvWorlds", "linux"), - }).download(); + const pathToLib = `${path.join(process.cwd(), "osOpencvWorlds", "linux", file)}`; + const pathToDir = `${path.join(process.cwd(), "osOpencvWorlds", "linux")}`; - await pack_1.Pack.unpack(`${path.join(process.cwd(), "osOpencvWorlds", "linux")}`, `${path.join(process.cwd(), "osOpencvWorlds", "linux", file)}`); + await downloadIfExist(pathToDir, pathToLib); + await pack_1.Pack.unpack(pathToDir, pathToLib, libUrl); } else if (process.platform === "win32") { - await new Downloader({ - url: libUrl, - directory: path.join(process.cwd(), "osOpencvWorlds", "win32"), - }).download(); + const pathToLib = `${path.join(process.cwd(), "osOpencvWorlds", "win32", file)}`; + const pathToDir = `${path.join(process.cwd(), "osOpencvWorlds", "win32")}`; - await pack_1.Pack.unpack(`${path.join(process.cwd(), "osOpencvWorlds", "win32")}`, `${path.join(process.cwd(), "osOpencvWorlds", "win32", file)}`); + await downloadIfExist(pathToDir, pathToLib, libUrl); + await pack_1.Pack.unpack(pathToDir, pathToLib); } await new Promise((res) => setTimeout(() => { @@ -50,4 +47,14 @@ async function download() { console.log("Download failed", error); } } + +async function downloadIfExist(pathToDir, pathToLib, libUrl) { + if (!existsSync(pathToLib)) { + await new Downloader({ + url: libUrl, + directory: pathToDir, + }).download(); + } +} + download(); diff --git a/package-lock.json b/package-lock.json index 9e124e1be..9d727c52d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opencv4nodejs-prebuilt-install", - "version": "4.1.195", + "version": "4.1.196", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "opencv4nodejs-prebuilt-install", - "version": "4.1.195", + "version": "4.1.196", "hasInstallScript": true, "license": "MIT", "os": [ diff --git a/package.json b/package.json index 3e7a7e173..ae3393f64 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencv4nodejs-prebuilt-install", - "version": "4.1.195", + "version": "4.1.196", "description": "Asynchronous OpenCV 4.x nodejs bindings with JavaScript and TypeScript API.", "keywords": [ "opencv",