cmake-ts
Version:
cmake-js rewrite in typescript to support advanced build configurations
6 lines (5 loc) • 2.48 kB
JavaScript
import{createRequire as u}from"module";import d from"fs";import l from"path";function h(t){return t==="linux"?d.existsSync("/etc/alpine-release")?"musl":"glibc":t==="darwin"?"libc":t==="win32"?"msvc":"unknown"}class g{level=2;setLevel(e="info"){this.level=e==="trace"?4:e==="debug"?3:e==="info"?2:e==="warn"?1:e==="error"?0:-1}error(...e){this.level>=0&&console.error("\x1B[31m[ERROR cmake-ts]\x1B[0m",...e)}warn(...e){this.level>=1&&console.warn("\x1B[33m[WARN cmake-ts]\x1B[0m",...e)}info(...e){this.level>=2&&console.info("\x1B[32m[INFO cmake-ts]\x1B[0m",...e)}log(...e){return this.info(...e)}debug(...e){this.level>=3&&console.debug("\x1B[34m[DEBUG cmake-ts]\x1B[0m",...e)}trace(...e){this.level>=4&&console.trace("\x1B[34m[TRACE cmake-ts]\x1B[0m",...e)}}const s=new g;function f(t){return t instanceof Error&&t.stack!==void 0?t.stack:String(t)}class b{buildDir;manifest;constructor(e){this.buildDir=e;const n=l.resolve(e,"manifest.json");if(!d.existsSync(n))throw new Error(`Manifest file not found at ${n}`);try{s.debug(`Reading and parsing manifest file at ${n}`);const o=d.readFileSync(n,"utf-8");this.manifest=JSON.parse(o)}catch(o){throw new Error(`Failed to read and parse the manifest file at ${n}: ${f(o)}`)}}findCompatibleConfigs(e){const n=this.getConfigKeys(),o=[];for(const i of n)try{const r=this.getConfig(i);if(r.os!==e.os||r.arch!==e.arch||r.libc!==e.libc){s.debug(`Config ${i} is not compatible with the current runtime. Skipping...`);continue}const a=this.getAddonPath(i);o.push([r,l.resolve(this.buildDir,a)])}catch(r){s.warn(`Failed to parse config ${i}: ${f(r)}`)}if(o.length===0)throw new Error(`No compatible zeromq.js addon found for ${e.os} ${e.arch} ${e.libc}. The candidates were:
${n.join(`
`)}`);return o.sort(([i,r],[a,c])=>(a.abi??0)-(i.abi??0)),o}getConfigKeys(){return Object.keys(this.manifest)}getConfig(e){return JSON.parse(e)}getAddonPath(e){return this.manifest[e]}}function p(){return{os:process.platform,arch:process.arch,libc:h(process.platform)}}function y(t){let e;try{const n=p(),i=new b(t).findCompatibleConfigs(n),r=typeof require=="function"?require:u(import.meta.url);for(const[a,c]of i)try{s.debug(`Loading addon at ${c}`),e=r(c);break}catch(m){s.warn(`Failed to load addon at ${c}: ${f(m)}
Trying others...`)}}catch(n){throw new Error(`Failed to load zeromq.js addon.node: ${f(n)}`)}if(e===void 0)throw new Error("No compatible zeromq.js addon found");return e}export{y as loadAddon};
//# sourceMappingURL=loader.mjs.map