UNPKG

@wocker/core

Version:
149 lines (148 loc) 5.08 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.FileSystem = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const yaml_1 = __importDefault(require("yaml")); const File_1 = require("./File"); class FileSystem { constructor(source, driver = fs_1.default) { this.source = source; this.driver = driver; } path(...parts) { return path_1.default.join(this.source, ...parts); } cd(path) { return new FileSystem(this.path(path), this.driver); } readBytes(path, position, size) { const file = this.open(path, "r"); try { return file.readBytes(position, size); } finally { file.close(); } } open(path, flags, mode) { return new File_1.File(this.path(path), flags, mode, this.driver); } basename(...parts) { return path_1.default.basename(this.path(...parts)); } exists(...parts) { const fullPath = this.path(...parts); return this.driver.existsSync(fullPath); } stat(...parts) { const fullPath = this.path(...parts); return this.driver.statSync(fullPath); } mkdir(path = "", options) { const fullPath = this.path(path); this.driver.mkdirSync(fullPath, options); } readdir(path = "/", options) { const fullPath = this.path(path); return this.driver.readdirSync(fullPath, options); } /** * @deprecated */ readdirFiles() { return __awaiter(this, arguments, void 0, function* (path = "", options) { const fullPath = this.path(path); return new Promise((resolve, reject) => { this.driver.readdir(fullPath, options, (err, files) => { if (err) { reject(err); return; } files = files.filter((path) => { const stat = this.stat(path); return stat.isFile(); }); resolve(files); }); }); }); } readFile(path) { const filePath = this.path(path); return this.driver.readFileSync(filePath); } readJSON(...paths) { const filePath = this.path(...paths); const res = this.driver.readFileSync(filePath); return JSON.parse(res.toString()); } readYAML(path) { const content = this.readFile(path).toString(); return yaml_1.default.parse(content); } writeFile(path, data, options) { const fullPath = this.path(path); this.driver.writeFileSync(fullPath, data, options); } writeJSON(path, data, options) { const fullPath = this.path(path); const json = JSON.stringify(data, null, 4); this.driver.writeFileSync(fullPath, json, options); } appendFile(path, data, options) { const fullPath = this.path(path); this.driver.appendFileSync(fullPath, data, options); } rm(path, options) { const fullPath = this.path(path); this.driver.rmSync(fullPath, options); } createWriteStream(path, options) { return this.driver.createWriteStream(this.path(path), options); } createReadStream(path, options) { return this.driver.createReadStream(this.path(path), options); } createReadlineStream(path, options) { let { encoding, start, end } = options || {}; const file = this.open(path, "r"); const stream = file.createReadlineStream({ encoding, start, end }); stream.on("end", () => { file.close(); }); stream.on("error", () => { file.close(); }); return stream; } getLinePosition(path, line) { const file = this.open(path, "r"); try { return file.getLinePosition(line); } finally { file.close(); } } watch(path, options = {}) { return this.driver.watch(this.path(path), options); } } exports.FileSystem = FileSystem;