/
in-memory-fs.js
69 lines (58 loc) · 2.17 KB
/
in-memory-fs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @fileoverview in-memory file system.
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";
//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------
const path = require("path");
const { Volume, createFsFromVolume } = require("memfs");
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
/**
* Define in-memory file system.
* @param {Object} options The options.
* @param {() => string} [options.cwd] The current working directory.
* @param {Object} [options.files] The initial files definition in the in-memory file system.
* @returns {import("fs")} The stubbed `ConfigArrayFactory` class.
*/
function defineInMemoryFs({
cwd = process.cwd,
files = {}
} = {}) {
/**
* The in-memory file system for this mock.
* @type {import("fs")}
*/
const fs = createFsFromVolume(new Volume());
fs.mkdirSync(cwd(), { recursive: true });
/*
* Write all files to the in-memory file system and compile all JavaScript
* files then set to `stubs`.
*/
(function initFiles(directoryPath, definition) {
for (const [filename, content] of Object.entries(definition)) {
const filePath = path.resolve(directoryPath, filename);
const parentPath = path.dirname(filePath);
if (typeof content === "object") {
initFiles(filePath, content);
} else if (typeof content === "string") {
if (!fs.existsSync(parentPath)) {
fs.mkdirSync(parentPath, { recursive: true });
}
fs.writeFileSync(filePath, content);
} else {
throw new Error(`Invalid content: ${typeof content}`);
}
}
}(cwd(), files));
return fs;
}
//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------
module.exports = {
defineInMemoryFs
};