UNPKG

1.51 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.findPackageData = findPackageData;
7
8function _path() {
9 const data = require("path");
10
11 _path = function () {
12 return data;
13 };
14
15 return data;
16}
17
18var _utils = require("./utils");
19
20const PACKAGE_FILENAME = "package.json";
21
22function* findPackageData(filepath) {
23 let pkg = null;
24 const directories = [];
25 let isPackage = true;
26
27 let dirname = _path().dirname(filepath);
28
29 while (!pkg && _path().basename(dirname) !== "node_modules") {
30 directories.push(dirname);
31 pkg = yield* readConfigPackage(_path().join(dirname, PACKAGE_FILENAME));
32
33 const nextLoc = _path().dirname(dirname);
34
35 if (dirname === nextLoc) {
36 isPackage = false;
37 break;
38 }
39
40 dirname = nextLoc;
41 }
42
43 return {
44 filepath,
45 directories,
46 pkg,
47 isPackage
48 };
49}
50
51const readConfigPackage = (0, _utils.makeStaticFileCache)((filepath, content) => {
52 let options;
53
54 try {
55 options = JSON.parse(content);
56 } catch (err) {
57 err.message = `${filepath}: Error while parsing JSON - ${err.message}`;
58 throw err;
59 }
60
61 if (!options) throw new Error(`${filepath}: No config detected`);
62
63 if (typeof options !== "object") {
64 throw new Error(`${filepath}: Config returned typeof ${typeof options}`);
65 }
66
67 if (Array.isArray(options)) {
68 throw new Error(`${filepath}: Expected config object but found array`);
69 }
70
71 return {
72 filepath,
73 dirname: _path().dirname(filepath),
74 options
75 };
76});
\No newline at end of file