UNPKG

1.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const errors_1 = require("./../errors");
5const guards_1 = require("../guards");
6const fs_1 = require("./fs");
7exports.PACKAGE_JSON_FILE = 'package.json';
8async function readPackageJson(dir, exceptionType = 'user') {
9 try {
10 const f = await fs_1.fsReadFile(path.resolve(dir, exports.PACKAGE_JSON_FILE), { encoding: 'utf8' });
11 const o = JSON.parse(f);
12 if (!guards_1.isPackageJson(o)) {
13 throw new errors_1.JobException(`Invalid format in ${exports.PACKAGE_JSON_FILE}.`, exceptionType);
14 }
15 return o;
16 }
17 catch (e) {
18 if (e.code === 'ENOENT') {
19 throw new errors_1.JobException(`Missing ${exports.PACKAGE_JSON_FILE} in app.`, exceptionType);
20 }
21 else if (e instanceof SyntaxError) {
22 throw new errors_1.JobException(`Invalid JSON in ${exports.PACKAGE_JSON_FILE}.`, exceptionType);
23 }
24 throw e;
25 }
26}
27exports.readPackageJson = readPackageJson;