下载时增加版本判断

This commit is contained in:
2025-03-17 16:12:49 +08:00
parent b86366e244
commit f7d8926443
5 changed files with 33 additions and 5 deletions

View File

@ -37,6 +37,19 @@ if(CMAKE_JS_RESULT EQUAL 0)
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
# ReleaseVersion
if(CMAKE_JS_RESULT EQUAL 0)
execute_process(
COMMAND node ${CMAKE_SOURCE_DIR}/thirdpart/cmake-js-util.js --release
RESULT_VARIABLE CMAKE_JS_RESULT
OUTPUT_VARIABLE RELEASE_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(CMAKE_JS_RESULT EQUAL 0)
message(STATUS "RELEASE_VERSION: ${RELEASE_VERSION}")
add_compile_definitions(RELEASE_VERSION="${RELEASE_VERSION}")
endif()
endif()
# NAPI
if(CMAKE_JS_RESULT EQUAL 0)

View File

@ -4,6 +4,10 @@
static Napi::Object Init(Napi::Env env, Napi::Object exports)
{
#ifdef RELEASE_VERSION
exports.Set("__release__", Napi::String::New(env, RELEASE_VERSION));
#endif
InitMatAPI(env, exports);
InitVideoCaptureAPI(env, exports);
InitUtilAPI(env, exports);

View File

@ -3,13 +3,17 @@ import fs from "fs";
import path from "path";
import { C } from "./common";
const URLS = {
GITHUB: `https://github.com/kangkang520/node-addons/releases/download/cv{{version}}/cv_{{platform}}_{{arch}}.node`,
URNAS: `http://git.urnas.cn:5200/yizhi-js-lib/opencv/releases/download/{{version}}/cv_{{platform}}_{{arch}}.node`,
}
function releaseVersion() { return require("../../package.json").releaseVersion }
function getURL(template: string) {
const version = require("../../package.json").releaseVersion;
let platform = "";
let arch = "";
switch (os.platform()) {
@ -30,7 +34,7 @@ function getURL(template: string) {
throw new Error(`Unsupported architecture: ${os.arch()}, Please compile the addon yourself.`);
}
return template.replaceAll("{{version}}", version).replaceAll("{{platform}}", platform).replaceAll("{{arch}}", arch);
return template.replaceAll("{{version}}", releaseVersion()).replaceAll("{{platform}}", platform).replaceAll("{{arch}}", arch);
}
async function getStream() {
@ -48,7 +52,12 @@ async function getStream() {
export async function downloadAddon(savename?: string) {
const defaultAddon = path.resolve(process.cwd(), C("ADDON_PATH"));
const saveName = savename ? path.resolve(path.dirname(defaultAddon), savename) : defaultAddon;
if (fs.existsSync(saveName)) return saveName;
if (fs.existsSync(saveName)) {
try {
const addon = require(saveName);
if (addon.__release__ === releaseVersion()) return saveName;
} catch (err) { }
}
await fs.promises.mkdir(path.dirname(saveName), { recursive: true });

View File

@ -33,6 +33,7 @@ async function testImage() {
}
async function testMovie() {
await cv.downloadAddon();
const cap = new cv.VideoCapture("test_data/movie.mp4");
if (cap.grab()) {
const im = cap.retrieve();

View File

@ -16,3 +16,4 @@ if (args.includes("--include")) runCmakeJS(["print-cmakejs-include"]);
else if (args.includes("--src")) runCmakeJS(["print-cmakejs-src"]);
else if (args.includes("--lib")) runCmakeJS(["print-cmakejs-lib"]);
else if (args.includes("--napi")) console.log(require("node-addon-api").include.replace(/^"/, "").replace(/"$/, ""));
else if (args.includes("--release")) console.log(require("../package.json").releaseVersion);