Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept 2 different optional directories to search for prebuilds #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 48 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs')
var path = require('path')
var os = require('os')
var path = require('path')

// Workaround to fix webpack's build warnings: 'the request of a dependency is an expression'
var runtimeRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require // eslint-disable-line
Expand All @@ -17,32 +17,30 @@ var uv = (process.versions.uv || '').split('.')[0]

module.exports = load

function load (dir) {
return runtimeRequire(load.path(dir))
function load (dir1, dir2) {
return runtimeRequire(load.path(dir1, dir2))
}

load.path = function (dir) {
dir = path.resolve(dir || '.')

try {
var name = runtimeRequire(path.join(dir, 'package.json')).name.toUpperCase().replace(/-/g, '_')
if (process.env[name + '_PREBUILD']) dir = process.env[name + '_PREBUILD']
} catch (err) {}

if (!prebuildsOnly) {
var release = getFirst(path.join(dir, 'build/Release'), matchBuild)
if (release) return release

var debug = getFirst(path.join(dir, 'build/Debug'), matchBuild)
if (debug) return debug
load.path = function (dir1, dir2) {
// check first optional directory
var dir = checkPath(dir1);
if (dir) {
return dir;
}

var prebuild = resolve(dir)
if (prebuild) return prebuild
// check second optional directory
dir = checkPath(dir2);
if (dir) {
return dir;
}

// check exe path
var nearby = resolve(path.dirname(process.execPath))
if (nearby) return nearby
if (nearby) {
return nearby;
}

// fail
var target = [
'platform=' + platform,
'arch=' + arch,
Expand All @@ -56,16 +54,38 @@ load.path = function (dir) {
typeof __webpack_require__ === 'function' ? 'webpack=true' : '' // eslint-disable-line
].filter(Boolean).join(' ')

throw new Error('No native build was found for ' + target + '\n loaded from: ' + dir + '\n')
throw new Error('No native build was found for ' + target + '\n loaded from 1: ' + dir1 + '\n loaded from 2: ' + dir2 + '\n')
}

function checkPath(dir) {
dir = path.resolve(dir || '.')

try {
var name = runtimeRequire(path.join(dir, 'package.json')).name.toUpperCase().replace(/-/g, '_')
if (process.env[name + '_PREBUILD'])
dir = process.env[name + '_PREBUILD']
} catch (err) {}

function resolve (dir) {
// Find most specific flavor first
var prebuilds = path.join(dir, 'prebuilds', platform + '-' + arch)
var parsed = readdirSync(prebuilds).map(parseTags)
var candidates = parsed.filter(matchTags(runtime, abi))
var winner = candidates.sort(compareTags(runtime))[0]
if (winner) return path.join(prebuilds, winner.file)
if (!prebuildsOnly) {
var release = getFirst(path.join(dir, 'build/Release'), matchBuild)
if (release)
return release;

var debug = getFirst(path.join(dir, 'build/Debug'), matchBuild)
if (debug)
return debug;
}

return resolve(dir);
}

function resolve (dir) {
// Find most specific flavor first
var prebuilds = path.join(dir, 'prebuilds', platform + '-' + arch)
var parsed = readdirSync(prebuilds).map(parseTags)
var candidates = parsed.filter(matchTags(runtime, abi))
var winner = candidates.sort(compareTags(runtime))[0]
return winner ? path.join(prebuilds, winner.file) : null;
}

function readdirSync (dir) {
Expand Down