Skip to content

Commit

Permalink
[utils] [new] create internal replacement for pkg-up and read-pkg-up
Browse files Browse the repository at this point in the history
  • Loading branch information
mgwalker authored and ljharb committed May 6, 2021
1 parent 7c382f0 commit 1571913
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
8 changes: 5 additions & 3 deletions utils/CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
- `fileExistsWithCaseSync`: add `strict` argument ([#1262], thanks [@sergei-startsev])
- add `visit`, to support dynamic imports ([#1660], [#2212], thanks [@maxkomarychev], [@aladdin-add], [@Hypnosphi])
- create internal replacement for `pkg-up` and `read-pkg-up` ([#2047], [@mgwalker])

## v2.6.2 - 2021-08-08

Expand Down Expand Up @@ -96,6 +97,7 @@ Yanked due to critical issue with cache key resulting from #839.

[#2212]: https://github.com/import-js/eslint-plugin-import/pull/2212
[#2160]: https://github.com/import-js/eslint-plugin-import/pull/2160
[#2047]: https://github.com/import-js/eslint-plugin-import/pull/2047
[#2026]: https://github.com/import-js/eslint-plugin-import/pull/2026
[#1786]: https://github.com/import-js/eslint-plugin-import/pull/1786
[#1671]: https://github.com/import-js/eslint-plugin-import/pull/1671
Expand All @@ -121,15 +123,15 @@ Yanked due to critical issue with cache key resulting from #839.
[@brettz9]: https://github.com/brettz9
[@christophercurrie]: https://github.com/christophercurrie
[@hulkish]: https://github.com/hulkish
[@Hypnosphi]: https://github.com/Hypnosphi
[@iamnapo]: https://github.com/iamnapo
[@JounQin]: https://github.com/JounQin
[@kaiyoma]: https://github.com/kaiyoma
[@manuth]: https://github.com/manuth
[@maxkomarychev]: https://github.com/maxkomarychev
[@mgwalker]: https://github.com/mgwalker
[@pmcelhaney]: https://github.com/pmcelhaney
[@sergei-startsev]: https://github.com/sergei-startsev
[@sompylasar]: https://github.com/sompylasar
[@timkraut]: https://github.com/timkraut
[@vikr01]: https://github.com/vikr01
[@maxkomarychev]: https://github.com/maxkomarychev
[@aladdin-add]: https://github.com/aladdin-add
[@Hypnosphi]: https://github.com/Hypnosphi
1 change: 1 addition & 0 deletions utils/package.json
Expand Up @@ -27,6 +27,7 @@
"homepage": "https://github.com/import-js/eslint-plugin-import#readme",
"dependencies": {
"debug": "^3.2.7",
"find-up": "^2.1.0",
"pkg-dir": "^2.0.0"
}
}
8 changes: 8 additions & 0 deletions utils/pkgUp.js
@@ -0,0 +1,8 @@
'use strict';
exports.__esModule = true;

const findUp = require('find-up');

exports.default = function pkgUp(opts) {
return findUp.sync('package.json', opts);
};
52 changes: 52 additions & 0 deletions utils/readPkgUp.js
@@ -0,0 +1,52 @@
'use strict';
exports.__esModule = true;

const fs = require('fs');
const pkgUp = require('./pkgUp').default;

function stripBOM(str) {
return str.replace(/^\uFEFF/, '');
}

/**
* Derived significantly from read-pkg-up@2.0.0. See license below.
*
* @copyright Sindre Sorhus
* MIT License
*
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
exports.default = function readPkgUp(opts) {
const fp = pkgUp(opts.cwd);

if (!fp) {
return {};
}

try {
return {
pkg: JSON.parse(stripBOM(fs.readFileSync(fp, { encoding: 'utf-8' }))),
path: fp,
};
} catch (e) {
return {};
}
};

0 comments on commit 1571913

Please sign in to comment.