silvie
Version:
Typescript Back-end Framework
253 lines (235 loc) • 6.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = require("fs");
var _promises = _interopRequireDefault(require("fs/promises"));
var _path = require("path");
var _ncp = _interopRequireDefault(require("ncp"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
class Disk {
constructor(basePath) {
_defineProperty(this, "basePath", null);
/**
* Resolve a filename in the current disk
* @param filename
*/
_defineProperty(this, "resolve", filename => {
return (0, _path.resolve)(this.basePath, filename);
});
this.basePath = basePath;
if (this.missing(basePath)) {
this.makeDirectory(basePath, true);
}
}
/**
* Get file stats
* @param filename
*/
stat(filename) {
return _promises.default.stat(this.resolve(filename));
}
/**
* Reads a file with the given options
* @param filename
* @param options
*/
get(filename, options) {
return _promises.default.readFile(this.resolve(filename), options);
}
/**
* Writes a content to a file with the given options
* @param filename
* @param contents
* @param options
*/
async put(filename, contents, options) {
await _promises.default.writeFile(this.resolve(filename), contents, options);
return true;
}
/**
* Checks to see if a file exists
* @param filename
*/
async exists(filename) {
try {
await _promises.default.stat(this.resolve(filename));
return true;
} catch (_unused) {
return false;
}
}
/**
* Checks to see if a file not exists
* @param filename
*/
async missing(filename) {
return !(await this.exists(filename));
}
/**
* Check to see if a path is a directory
* @param path
*/
async isDirectory(path) {
return (await this.stat(path)).isDirectory();
}
/**
* Rename a file
* @param oldPath
* @param newPath
*/
async rename(oldPath, newPath) {
if (await this.exists(oldPath)) {
if (!(await this.exists(newPath))) {
if ((0, _path.dirname)(oldPath) === (0, _path.dirname)(newPath)) {
await _promises.default.rename(this.resolve(oldPath), this.resolve(newPath));
return true;
}
}
}
return false;
}
/**
* Move a file
* @param oldPath
* @param newPath
* @param overwrite
*/
async move(oldPath, newPath, overwrite = false) {
if (await this.exists(oldPath)) {
let dstPath = newPath;
if (await this.isDirectory(newPath)) {
dstPath = (0, _path.join)(newPath, (0, _path.basename)(oldPath));
}
if (overwrite || !(await this.exists(dstPath))) {
await _promises.default.rename(this.resolve(oldPath), this.resolve(dstPath));
return true;
}
}
return false;
}
/**
* Copy a file or directory
* @param source
* @param destination
* @param overwrite
*/
async copy(source, destination, overwrite = false) {
if (await this.isDirectory(source)) {
return this.copyDirectory(source, destination, overwrite);
}
return this.copyFile(source, destination, overwrite);
}
/**
* Copy a file
* @param filename
* @param destination
* @param overwrite
*/
async copyFile(filename, destination, overwrite = false) {
if (await this.exists(filename)) {
let dest = destination;
if (await this.isDirectory(destination)) {
dest = (0, _path.join)(destination, (0, _path.basename)(filename));
}
if (overwrite || !(await this.exists(dest))) {
await _promises.default.copyFile(this.resolve(filename), this.resolve(dest));
return true;
}
}
return false;
}
/**
* Copy a directory
* @param source
* @param destination
* @param overwrite
*/
async copyDirectory(source, destination, overwrite = false) {
if (await this.isDirectory(source)) {
let dest = destination;
if (await this.isDirectory(destination)) {
dest = (0, _path.join)(destination, (0, _path.basename)(source));
}
if (overwrite || !(await this.exists(dest))) {
return new Promise((resolveFn, rejectFn) => {
try {
(0, _ncp.default)(this.resolve(source), this.resolve(dest), error => {
if (error) {
rejectFn(error);
}
resolveFn(true);
});
} catch (e) {
resolveFn(e);
}
});
}
}
return false;
}
/**
* Delete a file or directory
* @param path
* @param recursive
*/
async delete(path, recursive = false) {
if (await this.isDirectory(path)) {
return this.deleteDirectory(path, recursive);
}
return this.deleteFile(path);
}
/**
* Delete a file
* @param filename
*/
async deleteFile(filename) {
await _promises.default.unlink(this.resolve(filename));
return true;
}
/**
* Delete a directory
* @param path
* @param recursive
*/
async deleteDirectory(path, recursive = false) {
await _promises.default.rmdir(this.resolve(path), {
recursive
});
return true;
}
/**
* Create a directory
* @param path
* @param recursive
* @param mode
*/
async makeDirectory(path, recursive = false, mode = 0o777) {
await _promises.default.mkdir(this.resolve(path), {
recursive,
mode
});
return true;
}
/**
* Create a read stream from a path
* @param path
* @param options
*/
readStreamFrom(path, options) {
return (0, _fs.createReadStream)(this.resolve(path), options);
}
/**
* Create a write stream to a path
* @param path
* @param options
*/
writeStreamTo(path, options) {
return (0, _fs.createWriteStream)(this.resolve(path), options);
}
}
exports.default = Disk;