1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const errors_1 = require("./../errors");
|
5 | const guards_1 = require("../guards");
|
6 | const fs_1 = require("./fs");
|
7 | exports.PACKAGE_JSON_FILE = 'package.json';
|
8 | async 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 | }
|
27 | exports.readPackageJson = readPackageJson;
|