UNPKG

@simbachain/simbats

Version:
128 lines 4.08 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileHandler = exports.promisifiedReadFile = void 0; const config_1 = require("./config"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * helps read file once we've found it * @param filePath * @param options * @returns */ exports.promisifiedReadFile = (filePath, options) => new Promise((resolve, reject) => { fs.readFile(filePath, options, (err, data) => { if (err) { return reject(err); } return resolve(data); }); }); class FileHandler { /** * transfer file from inputPath to outputPath * @param inputPath * @param outputPath * @param parseAsJson */ static async transferFile(inputPath, outputPath, parseAsJson = true) { const buf = await exports.promisifiedReadFile(inputPath, { flag: 'r' }); let parsed; let data; if (parseAsJson) { parsed = JSON.parse(buf.toString()); data = JSON.stringify(parsed); } else { data = buf; } config_1.SimbaConfig.log.info(`:: writing contents of ${inputPath} to ${outputPath}`); // before writing, need to recursively create path to outputPath this.makeDirectory(outputPath); fs.writeFileSync(outputPath, data); } /** * * @param data * @param downloadLocation * @returns {Promise<any>} */ static async download(data, downloadLocation) { FileHandler.makeDirectory(downloadLocation); const writer = fs.createWriteStream(downloadLocation); if (data.data) { data.data.pipe(writer); } else { data.pipe(writer); } return new Promise((resolve, reject) => { writer.on('finish', resolve); writer.on('error', reject); }); } /** * * @param filePath * @returns {Promise<any>} */ static async parsedFile(filePath) { const buf = await exports.promisifiedReadFile(filePath, { flag: 'r' }); return JSON.parse(buf.toString()); } /** * creates directory recursively * @param filePath */ static makeDirectory(filePath) { const dirName = path.dirname(filePath); config_1.SimbaConfig.log.info(`:: creating directory ${filePath}`); if (!fs.existsSync(dirName)) { fs.mkdirSync(dirName, { recursive: true }); } } /** * deletes a file * @param filePath */ static removeFile(filePath) { config_1.SimbaConfig.log.info(`:: deleting file ${filePath}`); if (fs.existsSync(filePath)) { fs.unlinkSync(filePath); } } /** * deletes a directory * @param filePath */ static removeDirectory(filePath) { try { config_1.SimbaConfig.log.info(`:: deleting directory ${filePath}`); fs.rmSync(filePath, { recursive: true }); } catch (err) { console.error(`Error while deleting ${filePath}.`); } } } exports.FileHandler = FileHandler; //# sourceMappingURL=filehandler.js.map