UNPKG

@beenotung/tslib

Version:
156 lines 5.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.iterateFileByLine = exports.readJsonFileSync = exports.readJsonFile = exports.scanRecursivelySync = exports.scanRecursively = exports.hasDirectory = exports.hasFile = exports.exist = exports.exists = exports.mkdir = exports.stat = exports.lstat = exports.rename = exports.unlink = exports.readdir = exports.writeFile = exports.readFile = exports.copyFile = void 0; const tslib_1 = require("tslib"); const fs = tslib_1.__importStar(require("fs")); const path = tslib_1.__importStar(require("path")); const util = tslib_1.__importStar(require("util")); exports.copyFile = util.promisify(fs.copyFile); /** * resolve :: Buffer * reject :: NodeJS.ErrnoException * */ exports.readFile = util.promisify(fs.readFile); exports.writeFile = util.promisify(fs.writeFile); exports.readdir = util.promisify(fs.readdir); exports.unlink = util.promisify(fs.unlink); exports.rename = util.promisify(fs.rename); /** Does not dereference symbolic links. */ exports.lstat = util.promisify(fs.lstat); /** Does dereference symbolic links */ exports.stat = util.promisify(fs.stat); exports.mkdir = util.promisify(fs.mkdir); exports.exists = util.promisify(fs.exists); function isNoFileError(e) { if (e.code === 'ENOENT') { return true; } return Promise.reject(e); } function not(e) { return e === true ? false : e; } /**@deprecated*/ function exist(filename) { return exports.stat(filename) .then(() => true) .catch(e => not(isNoFileError(e))); } exports.exist = exist; function hasFile(filename) { return exports.stat(filename) .then(stat => stat.isFile()) .catch(e => not(isNoFileError(e))); } exports.hasFile = hasFile; function hasDirectory(filename) { return exports.stat(filename) .then(stat => stat.isDirectory()) .catch(e => not(isNoFileError(e))); } exports.hasDirectory = hasDirectory; /** @deprecated moved to write-stream.ts */ var write_stream_1 = require("./write-stream"); Object.defineProperty(exports, "writeStream", { enumerable: true, get: function () { return write_stream_1.writeStream; } }); function scanRecursively(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { entryPath, onFile, onDir, onComplete, dereferenceSymbolicLinks, skipDir, } = args; const checkStat = dereferenceSymbolicLinks ? exports.stat : exports.lstat; const check = (pathname, basename) => tslib_1.__awaiter(this, void 0, void 0, function* () { const stat = yield checkStat(pathname); if (stat.isDirectory()) { if (onDir) { yield onDir(pathname, basename); } if (skipDir && skipDir(pathname, basename)) { return; } const names = yield exports.readdir(pathname); for (const basename of names) { const childPathname = path.join(pathname, basename); yield check(childPathname, basename); } return; } if (onFile && stat.isFile()) { yield onFile(pathname, basename); return; } }); yield check(entryPath, path.basename(entryPath)); if (onComplete) { yield onComplete(); } }); } exports.scanRecursively = scanRecursively; function scanRecursivelySync(args) { const { entryPath, onFile, onDir, onComplete, dereferenceSymbolicLinks, skipDir, } = args; const checkStat = dereferenceSymbolicLinks ? fs.statSync : fs.lstatSync; const check = (pathname, basename) => { const stat = checkStat(pathname); if (stat.isDirectory()) { if (onDir) { onDir(pathname, basename); } if (skipDir && skipDir(pathname, basename)) { return; } const names = fs.readdirSync(pathname); for (const basename of names) { const childPathname = path.join(pathname, basename); check(childPathname, basename); } return; } if (onFile && stat.isFile()) { onFile(pathname, basename); return; } }; check(entryPath, path.basename(entryPath)); if (onComplete) { onComplete(); } } exports.scanRecursivelySync = scanRecursivelySync; function readJsonFile(file) { return exports.readFile(file).then(buffer => JSON.parse(buffer.toString())); } exports.readJsonFile = readJsonFile; function readJsonFileSync(file) { return JSON.parse(fs.readFileSync(file).toString()); } exports.readJsonFileSync = readJsonFileSync; function* iterateFileByLine(file, options) { const batchSize = (options === null || options === void 0 ? void 0 : options.batchSize) || 8 * 1024 * 1024; const encoding = options === null || options === void 0 ? void 0 : options.encoding; const buffer = Buffer.alloc(batchSize); const fd = fs.openSync(file, 'r'); if (options) { options.close = () => fs.closeSync(fd); } let acc = Buffer.alloc(0); for (;;) { const read = fs.readSync(fd, buffer, 0, batchSize, null); if (read === 0) { break; } acc = Buffer.concat([acc, buffer], acc.length + read); for (;;) { const idx = acc.indexOf(10); if (idx === -1) { break; } const line = acc.slice(0, idx); yield line.toString(encoding); acc = acc.slice(idx + 1); } } if (acc.length > 0) { yield acc.toString(encoding); } fs.closeSync(fd); } exports.iterateFileByLine = iterateFileByLine; //# sourceMappingURL=fs.js.map