Skip to content

Commit

Permalink
refactor(server): simplify resolveBinAsync (#208)
Browse files Browse the repository at this point in the history
(patch)
  • Loading branch information
wKovacs64 authored Apr 24, 2020
1 parent 02ccc7d commit ff5cab0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 45 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@
"@types/cross-spawn": "^6.0.1",
"@types/debug": "^4.1.5",
"@types/diff": "^4.0.2",
"@types/find-parent-dir": "0.3.0",
"@types/from2": "^2.3.0",
"@types/fs-extra": "^8.1.0",
"@types/fs-readdir-recursive": "1.0.0",
"@types/gulp-if": "^0.0.33",
"@types/jest": "^25.1.3",
"@types/mem-fs-editor": "^5.1.1",
"@types/mem-fs": "^1.1.2",
"@types/mem-fs-editor": "^5.1.1",
"@types/merge-stream": "^1.1.2",
"@types/node": "^13.7.4",
"@types/node-fetch": "^2.5.6",
Expand All @@ -54,8 +53,8 @@
"@types/react": "16.9.34",
"@types/readable-stream": "^2.3.5",
"@types/through2": "^2.0.34",
"@types/vinyl-fs": "^2.4.11",
"@types/vinyl": "^2.0.4",
"@types/vinyl-fs": "^2.4.11",
"@wessberg/cjs-to-esm-transformer": "^0.0.19",
"@wessberg/rollup-plugin-ts": "^1.2.24",
"cpy-cli": "^3.1.0",
Expand All @@ -72,12 +71,12 @@
"pretty-quick": "2.0.1",
"prompt": "^1.0.0",
"rimraf": "3.0.2",
"rollup": "^2.6.1",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.2",
"rollup": "^2.6.1",
"semver": "^7.3.2",
"ts-jest": "24.3.0",
"tsdx": "^0.13.1",
Expand Down
1 change: 0 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@types/pump": "^1.1.0",
"cross-spawn": "^7.0.2",
"fast-glob": "^3.2.2",
"find-parent-dir": "0.3.0",
"from2": "^2.3.0",
"gulp-if": "^3.0.0",
"merge-stream": "^2.0.0",
Expand Down
38 changes: 8 additions & 30 deletions packages/server/src/resolve-bin-async.ts
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)
}
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ff5cab0

Please sign in to comment.