ooafs
Version:
Type-safe Object-Oriented Async FileSystem
190 lines (189 loc) • 6.61 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Path = __importStar(require("path"));
const EntryFilter_1 = __importDefault(require("./EntryFilter"));
const EntryType_1 = __importDefault(require("./EntryType"));
const fs_extra_1 = __importDefault(require("fs-extra"));
/**
* This class represents a generic filesystem entry. If you use TypeScript, you
* can use the `File` and `Directory` interfaces which hide some methods for a
* better type-safety
*/
class Entry {
constructor(path) {
// Remove trailing slash
this.path = path.replace(/[\\/]+$/, '');
}
get absolutePath() {
return Path.resolve(this.path);
}
get parsedPath() {
return Path.parse(this.path);
}
get name() {
return this.parsedPath.base;
}
get extension() {
return this.parsedPath.ext;
}
// Common methods
asEntry() {
return this;
}
child(relativePath) {
return new Entry(Path.join(this.path, relativePath));
}
copy(dest) {
return __awaiter(this, void 0, void 0, function* () {
const dstPath = (typeof dest === 'string')
? Path.resolve(this.parent().path, dest)
: dest.path;
yield fs_extra_1.default.copy(this.path, dstPath);
return new Entry(dstPath);
});
}
createDirectory(subpath) {
return __awaiter(this, void 0, void 0, function* () {
if (subpath) {
const child = this.child(subpath);
yield fs_extra_1.default.mkdirs(child.path);
return child;
}
else {
yield fs_extra_1.default.mkdirs(this.path);
return this;
}
});
}
createFile(subpath) {
return __awaiter(this, void 0, void 0, function* () {
if (subpath) {
const child = this.child(subpath);
yield fs_extra_1.default.createFile(child.path);
return child;
}
else {
yield fs_extra_1.default.createFile(this.path);
return this;
}
});
}
createReadStream(options) {
return fs_extra_1.default.createReadStream(this.path, options);
}
equals(other) {
return this.absolutePath === other.absolutePath;
}
exists() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield fs_extra_1.default.stat(this.path);
return true;
}
catch (_) {
return false;
}
});
}
type() {
return __awaiter(this, void 0, void 0, function* () {
const stat = yield this.stat();
if (stat.isDirectory())
return EntryType_1.default.DIRECTORY;
if (stat.isFile())
return EntryType_1.default.FILE;
return EntryType_1.default.UNKNOWN;
});
}
isDirectory() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.type()) === EntryType_1.default.DIRECTORY;
});
}
isFile() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.type()) === EntryType_1.default.FILE;
});
}
list(filter) {
return __awaiter(this, void 0, void 0, function* () {
const list = [];
for (let subpath of yield fs_extra_1.default.readdir(this.path)) {
const subentry = new Entry(Path.resolve(this.path, subpath));
if (yield EntryFilter_1.default.matches(subentry, filter)) {
list.push(subentry);
}
}
return list;
});
}
listRecursively(filter) {
return __awaiter(this, void 0, void 0, function* () {
const list = [];
function addEntry(entry, includeThis = true) {
return __awaiter(this, void 0, void 0, function* () {
if (includeThis && (yield EntryFilter_1.default.matches(entry, filter))) {
list.push(entry);
}
if (yield entry.isDirectory()) {
for (let subentry of yield entry.list()) {
yield addEntry(subentry);
}
}
});
}
yield addEntry(this, false);
return list;
});
}
move(dest) {
return __awaiter(this, void 0, void 0, function* () {
const dstPath = (typeof dest === 'string')
? Path.resolve(this.parent().path, dest)
: dest.path;
yield fs_extra_1.default.move(this.path, dstPath);
return new Entry(dstPath);
});
}
parent() {
return new Entry(Path.dirname(this.path));
}
read(options) {
return fs_extra_1.default.readFile(this.path, options);
}
remove() {
return fs_extra_1.default.remove(this.path);
}
size() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.stat()).size;
});
}
stat() {
return fs_extra_1.default.stat(this.path);
}
write(data, options) {
return __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.writeFile(this.path, data, options);
});
}
}
exports.default = Entry;