UNPKG

@sap-cloud-sdk/util

Version:

SAP Cloud SDK for JavaScript general utilities

39 lines 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findProjectRoot = findProjectRoot; exports.readJSON = readJSON; const fs_1 = require("fs"); const path_1 = require("path"); const logger_1 = require("./logger"); const logger = (0, logger_1.createLogger)({ package: 'util', messageContext: 'fs' }); /** * @internal */ function findProjectRoot(path, lastPath = path) { if (!path) { return lastPath; } const inProject = (0, fs_1.readdirSync)(path).includes('package.json') || (0, fs_1.readdirSync)(path).includes('node_modules') || path.includes('node_modules'); if (!inProject) { return lastPath; } return findProjectRoot((0, path_1.resolve)(path, '..'), path); } /** * Read a JSON file from the file system. * @param path - The path to the JSON file. * @returns An object parsed from the JSON file. */ function readJSON(path) { if ((0, fs_1.existsSync)(path)) { return JSON.parse((0, fs_1.readFileSync)(path, 'utf8')); } logger.warn(`File "${path}" does not exist, return empty object.`); return {}; } //# sourceMappingURL=fs.js.map