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

Search name #35

Closed
wants to merge 3 commits into from
Closed
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
32 changes: 27 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,37 @@ var uv = (process.versions.uv || '').split('.')[0]

module.exports = load

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

load.path = function (dir) {
dir = path.resolve(dir || '.')
function loadPackageJSON(dir, name, attempts) {
attempts = attempts || 1
if (attempts > 5) {
throw new Error('Can\'t resolve main package.json file')
}
var mainPath = attempts === 1 ? dir : Array(attempts).join("../") + dir
if (fs.existsSync(path.resolve(mainPath))) {
return {dir: mainPath, packageJSON: runtimeRequire.main.require(mainPath + 'package.json')}
} else {
return loadPackageJSON(dir, name, attempts + 1)
}
}


load.path = function (dir, name) {
try {
var name = runtimeRequire(path.join(dir, 'package.json')).name.toUpperCase().replace(/-/g, '_')
var packageJSON
if (name) {
loadPackageJSONReturn = loadPackageJSON(dir, name)
packageJSON = loadPackageJSONReturn.packageJSON
dir = path.resolve(loadPackageJSONReturn.dir)
} else {
dir = path.resolve(dir || '.')
var packageJSONPath = path.join(dir, 'package.json')
packageJSON = runtimeRequire(packageJSONPath)
}
var name = packageJSON.name.toUpperCase().replace(/-/g, '_')
if (process.env[name + '_PREBUILD']) dir = process.env[name + '_PREBUILD']
} catch (err) {}

Expand Down