UNPKG

@creditkarma/dynamic-config

Version:

Dynamic Config for Node.js backed by Consul and Vault

76 lines 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findDir = exports.findFile = exports.parseContent = exports.readFile = exports.fileExists = void 0; const fs = require("fs"); const path = require("path"); function fileExists(filePath) { return new Promise((resolve, reject) => { fs.exists(filePath, (exists) => { if (exists) { resolve(); } else { reject(new Error(`File[${filePath}] doesn't exists`)); } }); }); } exports.fileExists = fileExists; function readFile(filePath) { return new Promise((resolve, reject) => { fs.readFile(filePath, (err, data) => { if (err) { reject(err); } else { resolve(data.toString('utf-8')); } }); }); } exports.readFile = readFile; // Should we fail if one file fails? function parseContent(content) { return new Promise((resolve, reject) => { try { resolve(JSON.parse(content)); } catch (e) { reject(e); } }); } exports.parseContent = parseContent; function findFile(filePath, paths) { const firstPath = path.resolve(process.cwd(), filePath); if (fs.existsSync(firstPath) && fs.statSync(firstPath).isFile) { return firstPath; } else { for (const next of paths) { const nextPath = path.resolve(process.cwd(), next, filePath); if (fs.existsSync(nextPath) && fs.statSync(nextPath).isFile) { return nextPath; } } } return null; } exports.findFile = findFile; function findDir(dirName, paths) { const firstPath = path.resolve(process.cwd(), dirName); if (fs.existsSync(firstPath) && fs.statSync(firstPath).isDirectory) { return firstPath; } else { for (const next of paths) { const nextPath = path.resolve(process.cwd(), next, dirName); if (fs.existsSync(nextPath) && fs.statSync(nextPath).isDirectory) { return nextPath; } } } return null; } exports.findDir = findDir; //# sourceMappingURL=file.js.map