makit
Version:
Make in JavaScript done right!
78 lines (77 loc) • 2.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryFileSystem = void 0;
const path_1 = require("path");
const memory_fs_1 = __importDefault(require("memory-fs"));
/**
* memory-fs 对 FileSystem 接口的封装。目前这个类只用于跑测试。
*/
class MemoryFileSystem {
constructor() {
this.mtimes = new Map();
this.fs = new memory_fs_1.default();
this.now = 10000;
}
async mkdir(path, options = {}) {
this.mkdirSync(path, options);
}
mkdirSync(path, options = {}) {
const fullpath = path_1.resolve(path);
options.recursive ? this.fs.mkdirpSync(fullpath) : this.fs.mkdirSync(fullpath);
}
readFile(path, encoding) {
return this.readFileSync(path, encoding);
}
readFileSync(path, encoding) {
const fullpath = path_1.resolve(path);
return this.fs.readFileSync(fullpath, encoding);
}
async writeFile(path, data) {
this.writeFileSync(path, data);
}
writeFileSync(path, data) {
const fullpath = path_1.resolve(path);
this.mtimes.set(fullpath, this.now++);
return this.fs.writeFileSync(fullpath, data);
}
async stat(path) {
return this.statSync(path);
}
statSync(path) {
const fullpath = path_1.resolve(path);
if (!this.mtimes.has(fullpath)) {
const err = new Error(`file ${fullpath} not found`);
err.code = 'ENOENT';
throw err;
}
return {
mtime: new Date(this.mtimes.get(fullpath)),
mtimeMs: this.mtimes.get(fullpath)
};
}
async unlink(path) {
this.unlinkSync(path);
}
unlinkSync(path) {
const fullpath = path_1.resolve(path);
this.fs.unlinkSync(fullpath);
}
async exists(path) {
return this.existsSync(path);
}
existsSync(path) {
const fullpath = path_1.resolve(path);
return this.fs.existsSync(fullpath);
}
async utimes(path, atime, mtime) {
this.utimesSync(path, atime, mtime);
}
utimesSync(path, atime, mtime) {
const fullpath = path_1.resolve(path);
this.mtimes.set(fullpath, mtime);
}
}
exports.MemoryFileSystem = MemoryFileSystem;