UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

77 lines 2.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var debug_1 = __importDefault(require("debug")); var path_1 = __importDefault(require("path")); var fs_1 = require("fs"); var d = debug_1.default('remoteit:File'); var File = /** @class */ (function () { function File(location) { this.location = location; this.createIfMissing(); } /** * Write the given contents to the file. */ File.prototype.write = function (data) { this.content = data; }; /** * Reads the content of the file as a string. */ File.prototype.read = function () { return this.content; }; /** * Remove the file from the filesystem, if it exists. */ File.prototype.remove = function () { if (!this.exists) return; d('remove file:', this.location); fs_1.unlinkSync(this.location); }; Object.defineProperty(File.prototype, "exists", { /** * Returns a boolean whether the file exists or not */ get: function () { return fs_1.existsSync(this.location); }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "content", { /** * Internal getter to retrieve the content of the file. */ get: function () { return fs_1.readFileSync(this.location, 'utf8'); }, /** * Internal setter to set the content of the file. */ set: function (data) { // Make sure parent directory exists before writing. this.createIfMissing(); fs_1.writeFileSync(this.location, data); }, enumerable: true, configurable: true }); /** * Make sure file exists */ File.prototype.createIfMissing = function () { var dir = path_1.default.parse(this.location).dir; if (!fs_1.existsSync(dir)) fs_1.mkdirSync(dir, { recursive: true }); if (!this.exists) fs_1.writeFileSync(this.location, ''); }; return File; }()); exports.File = File; //# sourceMappingURL=File.js.map