-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): simplify resolveBinAsync (#208)
(patch)
- Loading branch information
Showing
4 changed files
with
11 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,16 @@ | ||
import resolveCwd from 'resolve-cwd' | ||
import findParentDir from 'find-parent-dir' | ||
import pkgDir from 'pkg-dir' | ||
import path from 'path' | ||
|
||
// This code originally from https://github.com/thlorenz/resolve-bin | ||
// It's inlined here because we need to use resolveCwd | ||
function resolveBin(name: string, executable: string, cb: Function) { | ||
var mod | ||
try { | ||
mod = resolveCwd(name) | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
export async function resolveBinAsync(pkg: string, executable = pkg) { | ||
const packageDir = await pkgDir(resolveCwd(pkg)) | ||
|
||
findParentDir(mod, 'package.json', function (err, dir) { | ||
if (err) return cb(err) | ||
if (!packageDir) throw new Error(`Could not find package.json for '${pkg}'`) | ||
|
||
var pack = require(path.join(dir as string, 'package.json')) | ||
var binfield = pack.bin | ||
const {bin} = require(path.join(packageDir, 'package.json')) | ||
const binPath = typeof bin === 'object' ? bin[executable] : bin | ||
|
||
var binpath = typeof binfield === 'object' ? binfield[executable] : binfield | ||
if (!binpath) return cb(new Error('No bin `' + executable + '` in module `' + name + '`')) | ||
if (!binPath) throw new Error(`No bin '${executable}' in module '${pkg}'`) | ||
|
||
var bin = path.join(dir as string, binpath) | ||
cb(null, bin) | ||
}) | ||
} | ||
|
||
export function resolveBinAsync(pkg: string, executable = pkg) { | ||
return new Promise<string>((resolve, reject) => { | ||
resolveBin(pkg, executable, (err: any, bin: string) => { | ||
if (err) { | ||
reject(err) | ||
} | ||
resolve(bin) | ||
}) | ||
}) | ||
return path.join(packageDir, binPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2517,11 +2517,6 @@ | |
resolved "https://registry.yarnpkg.com/@types/expect/-/expect-1.20.4.tgz#8288e51737bf7e3ab5d7c77bfa695883745264e5" | ||
integrity sha512-Q5Vn3yjTDyCMV50TB6VRIbQNxSE4OmZR86VSbGaNpfUolm0iePBB4KdEEHmxoY5sT2+2DIvXW0rvMDP2nHZ4Mg== | ||
|
||
"@types/[email protected]": | ||
version "0.3.0" | ||
resolved "https://registry.yarnpkg.com/@types/find-parent-dir/-/find-parent-dir-0.3.0.tgz#0a8e9b1eb4fa77e6761d7a078daaed92ad2868f1" | ||
integrity sha512-jz1cHlySrflg1Xf6fhy8xe/Bc//eO7wkg7OQePQei4vIYHm9IIQs0Ed8pakV6qVuihTnmy+Le+xUYDardq7yNg== | ||
|
||
"@types/from2@^2.3.0": | ||
version "2.3.0" | ||
resolved "https://registry.yarnpkg.com/@types/from2/-/from2-2.3.0.tgz#5a3dd8d9db0363bc1ee6b98e0538fe0ae356ac3a" | ||
|
@@ -6293,11 +6288,6 @@ find-cache-dir@^2.1.0: | |
make-dir "^2.0.0" | ||
pkg-dir "^3.0.0" | ||
|
||
[email protected]: | ||
version "0.3.0" | ||
resolved "https://registry.yarnpkg.com/find-parent-dir/-/find-parent-dir-0.3.0.tgz#33c44b429ab2b2f0646299c5f9f718f376ff8d54" | ||
integrity sha1-M8RLQpqysvBkYpnF+fcY83b/jVQ= | ||
|
||
find-up@^1.0.0: | ||
version "1.1.2" | ||
resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f" | ||
|