89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const path_1 = __importDefault(require("path"));
|
|
const is_1 = __importDefault(require("is"));
|
|
const buffer_1 = require("buffer");
|
|
const immutable_1 = __importDefault(require("immutable"));
|
|
const fs_1 = __importDefault(require("../models/fs"));
|
|
const error_1 = __importDefault(require("../utils/error"));
|
|
/**
|
|
Create a fake filesystem for unit testing HonKit.
|
|
|
|
@param {Map<String:String|Map>}
|
|
*/
|
|
function createMockFS(files) {
|
|
files = immutable_1.default.fromJS(files);
|
|
const mtime = new Date();
|
|
function getFile(filePath) {
|
|
const parts = path_1.default.normalize(filePath).split(path_1.default.sep);
|
|
return parts.reduce((list, part, i) => {
|
|
if (!list)
|
|
return null;
|
|
let file;
|
|
if (!part || part === ".")
|
|
file = list;
|
|
else
|
|
file = list.get(part);
|
|
if (!file)
|
|
return null;
|
|
if (is_1.default.string(file)) {
|
|
if (i === parts.length - 1)
|
|
return file;
|
|
else
|
|
return null;
|
|
}
|
|
return file;
|
|
}, files);
|
|
}
|
|
function fsExists(filePath) {
|
|
return Boolean(getFile(filePath) !== null);
|
|
}
|
|
function fsReadFile(filePath) {
|
|
const file = getFile(filePath);
|
|
if (!is_1.default.string(file)) {
|
|
throw error_1.default.FileNotFoundError({
|
|
filename: filePath
|
|
});
|
|
}
|
|
return buffer_1.Buffer.from(file, "utf8");
|
|
}
|
|
function fsStatFile(filePath) {
|
|
const file = getFile(filePath);
|
|
if (!file) {
|
|
throw error_1.default.FileNotFoundError({
|
|
filename: filePath
|
|
});
|
|
}
|
|
return {
|
|
mtime: mtime
|
|
};
|
|
}
|
|
function fsReadDir(filePath) {
|
|
const dir = getFile(filePath);
|
|
if (!dir || is_1.default.string(dir)) {
|
|
throw error_1.default.FileNotFoundError({
|
|
filename: filePath
|
|
});
|
|
}
|
|
return dir
|
|
.map((content, name) => {
|
|
if (!is_1.default.string(content)) {
|
|
name = `${name}/`;
|
|
}
|
|
return name;
|
|
})
|
|
.valueSeq();
|
|
}
|
|
return fs_1.default.create({
|
|
root: "",
|
|
fsExists: fsExists,
|
|
fsReadFile: fsReadFile,
|
|
fsStatFile: fsStatFile,
|
|
fsReadDir: fsReadDir
|
|
});
|
|
}
|
|
exports.default = createMockFS;
|