UNPKG

@auto-it/package-json-utils

Version:

Shared utilities for parsing information from a package.json

45 lines 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAuthor = exports.getRepo = exports.loadPackageJson = void 0; const tslib_1 = require("tslib"); const fs = tslib_1.__importStar(require("fs")); const path_1 = tslib_1.__importDefault(require("path")); const util_1 = require("util"); const parse_github_url_1 = tslib_1.__importDefault(require("parse-github-url")); const parse_author_1 = tslib_1.__importDefault(require("parse-author")); const readFile = util_1.promisify(fs.readFile); /** Load and parse the root package json for the project */ async function loadPackageJson(root = "./") { return JSON.parse(await readFile(path_1.default.join(root, "package.json"), "utf-8")); } exports.loadPackageJson = loadPackageJson; /** Try to the the owner/repo from the package.json */ async function getRepo() { const { repository } = await loadPackageJson(); if (!repository) { return; } const { owner, name } = parse_github_url_1.default(typeof repository === "string" ? repository : repository.url) || {}; if (!owner || !name) { return; } return { repo: name, owner, }; } exports.getRepo = getRepo; /** Get the author from a package.json */ async function getAuthor() { const packageJson = await loadPackageJson(); if (!packageJson.author) { return; } const { author } = packageJson; if (typeof author === "string") { return parse_author_1.default(author); } return author; } exports.getAuthor = getAuthor; //# sourceMappingURL=index.js.map