UNPKG

@laoban/filesops-node

Version:

File operations (interfaces are in utils). This mainly decouples from the actual version of the files software and makes file operations easy to test

72 lines (71 loc) 4.38 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileOpsNode = exports.fileOpsNodeWithFetchFn = void 0; //Copyright (c)2020-2023 Philip Rice. <br />Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the Software), to dealin the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: <br />The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED AS const fs_1 = require("fs"); const crypto_1 = require("crypto"); const path_1 = __importDefault(require("path")); const proxy_agent_1 = require("proxy-agent"); const node_fetch_1 = __importDefault(require("node-fetch")); const agent = new proxy_agent_1.ProxyAgent(); function loadFile(fileName) { return fs_1.promises.readFile(fileName).then(buffer => buffer.toString('utf-8')); } function loadUrl(fetch, fileOrUrl, headers) { const init = { agent, headers: Object.assign({}, headers || {}) }; return fetch(fileOrUrl, init).then((res) => __awaiter(this, void 0, void 0, function* () { let text = yield res.text(); if (res.status >= 400) throw Error(`Cannot load file [${fileOrUrl}] . Status is ${res.status}\n Response was ${text}`); return text; }), error => { console.error(error); throw error; }); } const loadFileOrUrl = (fetch) => (fileOrUrl, headers) => fileOrUrl.includes("://") ? loadUrl(fetch, fileOrUrl, headers) : loadFile(fileOrUrl); const fileOpsNodeWithFetchFn = (fetch, speedUpLogs) => { const actualSpeedUpLogs = speedUpLogs !== false; return { isDirectory: filename => fs_1.promises.lstat(filename).then(s => s.isDirectory(), e => false), isFile: filename => fs_1.promises.lstat(filename).then(s => s.isFile(), e => false), digest: (s) => { const hash = (0, crypto_1.createHash)('sha256'); hash.update(s); return hash.digest('hex'); }, loadFileOrUrl: loadFileOrUrl(fetch), createDir: dir => fs_1.promises.mkdir(dir, { recursive: true }), saveFile: (filename, text) => fs_1.promises.writeFile(filename, text), log: (filename, text) => { const textWithNewLine = text + '\n'; if (actualSpeedUpLogs) return fs_1.promises.writeFile(filename, textWithNewLine, { flag: 'a+' }); (0, fs_1.writeFileSync)(filename, textWithNewLine, { flag: 'a+' }); // because logs want to be written 'right now' return Promise.resolve(); }, listFiles: (root) => fs_1.promises.readdir(root), listFileWithType: (root) => fs_1.promises.readdir(root, { withFileTypes: true }), removeDirectory: (filename, recursive) => fs_1.promises.rm(filename, { recursive, force: true }), removeFile: (filename) => fs_1.promises.rm(filename, { force: true }), join(...parts) { return path_1.default.join(...parts); }, relative(from, to) { return path_1.default.relative(from, to); } }; }; exports.fileOpsNodeWithFetchFn = fileOpsNodeWithFetchFn; const fileOpsNode = (speedUpLogs) => { return (0, exports.fileOpsNodeWithFetchFn)(node_fetch_1.default, speedUpLogs); }; exports.fileOpsNode = fileOpsNode;