workspace-tools
Version:
A collection of utilities that are useful in a git-controlled monorepo managed by one of these tools:
52 lines • 2.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageInfoAsync = exports.getPackageInfo = void 0;
const fs_1 = __importDefault(require("fs"));
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const infoFromPackageJson_1 = require("./infoFromPackageJson");
const logging_1 = require("./logging");
/**
* Read package.json from the given path if it exists.
* Logs a warning if it doesn't exist, or there's an error reading or parsing it.
* @returns The package info, or undefined if it doesn't exist or can't be read
*/
function getPackageInfo(cwd) {
const packageJsonPath = path_1.default.join(cwd, "package.json");
try {
if (!fs_1.default.existsSync(packageJsonPath)) {
(0, logging_1.logVerboseWarning)(`File does not exist: ${packageJsonPath}`);
return undefined;
}
const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf-8"));
return (0, infoFromPackageJson_1.infoFromPackageJson)(packageJson, packageJsonPath);
}
catch (e) {
(0, logging_1.logVerboseWarning)(`Error reading or parsing ${packageJsonPath}: ${e?.message || e}`);
}
}
exports.getPackageInfo = getPackageInfo;
/**
* Read package.json from the given path if it exists.
* Logs a warning if it doesn't exist, or there's an error reading or parsing it.
* @returns The package info, or undefined if it doesn't exist or can't be read
*/
async function getPackageInfoAsync(cwd) {
const packageJsonPath = path_1.default.join(cwd, "package.json");
try {
if (!fs_1.default.existsSync(packageJsonPath)) {
(0, logging_1.logVerboseWarning)(`File does not exist: ${packageJsonPath}`);
return undefined;
}
const packageJson = JSON.parse(await promises_1.default.readFile(packageJsonPath, "utf-8"));
return (0, infoFromPackageJson_1.infoFromPackageJson)(packageJson, packageJsonPath);
}
catch (e) {
(0, logging_1.logVerboseWarning)(`Error reading or parsing ${packageJsonPath}: ${e?.message || e}`);
}
}
exports.getPackageInfoAsync = getPackageInfoAsync;
//# sourceMappingURL=getPackageInfo.js.map