This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pull upstream * add initial attempt to load local prettier not global * prioritise local prettier over global * add local dependencies dont rely on global ones * remove unnecessary fallback arg as fn already returns a fallback * prettier
- Loading branch information
Showing
3 changed files
with
68 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const path = require("path") | ||
const resolve = require("resolve") | ||
const readPkgUp = require("read-pkg-up") | ||
|
||
// CREDIT: Shamelessly *borrowed* from prettier-vscode | ||
|
||
/** | ||
* Recursively search for a package.json upwards containing given package | ||
* as a dependency or devDependency. | ||
* @param {string} fspath file system path to start searching from | ||
* @param {string} pkgName package's name to search for | ||
* @returns {string} resolved path to prettier | ||
*/ | ||
function findPkg(fspath = process.cwd(), pkgName) { | ||
const res = readPkgUp.sync({ cwd: fspath, normalize: false }) | ||
const { root } = path.parse(fspath) | ||
if ( | ||
res.pkg && | ||
((res.pkg.dependencies && res.pkg.dependencies[pkgName]) || | ||
(res.pkg.devDependencies && res.pkg.devDependencies[pkgName])) | ||
) { | ||
return resolve.sync(pkgName, { basedir: res.path }) | ||
} else if (res.path) { | ||
const parent = path.resolve(path.dirname(res.path), "..") | ||
if (parent !== root) { | ||
return findPkg(parent, pkgName) | ||
} | ||
} | ||
return | ||
} | ||
|
||
/** | ||
* Require package explicitely installed relative to given path. | ||
* Fallback to bundled one if no pacakge was found bottom up. | ||
* @param {string} fspath file system path starting point to resolve package | ||
* @param {string} pkgName package's name to require | ||
* @returns module | ||
*/ | ||
function requireLocalPkg(fspath, pkgName) { | ||
const modulePath = findPkg(fspath, pkgName) | ||
if (modulePath) { | ||
try { | ||
return require(modulePath) | ||
} catch (e) { | ||
console.warn(`Failed to load ${pkgName} from ${modulePath}. Using bundled`) | ||
} | ||
} | ||
|
||
return require(pkgName) | ||
} | ||
|
||
module.exports = { requireLocalPkg } |