UNPKG

1.81 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5const fs = require('fs');
6const pkgUp = require('./pkgUp').default;
7
8/** @type {(str: string) => string} */
9function stripBOM(str) {
10 return str.replace(/^\uFEFF/, '');
11}
12
13/**
14 * Derived significantly from read-pkg-up@2.0.0. See license below.
15 *
16 * @copyright Sindre Sorhus
17 * MIT License
18 *
19 * Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
20 *
21 * Permission is hereby granted, free of charge, to any person obtaining a copy
22 * of this software and associated documentation files (the "Software"), to deal
23 * in the Software without restriction, including without limitation the rights
24 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 * copies of the Software, and to permit persons to whom the Software is
26 * furnished to do so, subject to the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 * THE SOFTWARE.
38 */
39/** @type {import('./readPkgUp').default} */
40exports.default = function readPkgUp(opts) {
41 const fp = pkgUp(opts);
42
43 if (!fp) {
44 return {};
45 }
46
47 try {
48 return {
49 pkg: JSON.parse(stripBOM(fs.readFileSync(fp, { encoding: 'utf-8' }))),
50 path: fp,
51 };
52 } catch (e) {
53 return {};
54 }
55};