-
Notifications
You must be signed in to change notification settings - Fork 10
/
fs-setup.js
45 lines (35 loc) · 976 Bytes
/
fs-setup.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
const path = require('path');
const fs = require('fs-extra');
const tempy = require('tempy');
module.exports = (fsObject = {}) => {
const cwd = tempy.directory();
Object.keys(fsObject).forEach((filename) => {
fs.outputFileSync(path.join(cwd, filename), fsObject[filename]);
});
const createFileObject = (filename) => {
return new Proxy({}, {
get: (target, prop) => {
const fullPath = path.join(cwd, filename);
switch (prop) {
case 'content':
return fs.readFileSync(fullPath, 'utf8');
case 'exists':
return fs.existsSync(fullPath);
case 'path':
return fullPath;
case 'write':
return content => fs.outputFileSync(fullPath, content);
default:
return undefined;
}
},
});
};
const files = new Proxy({}, {
get: (target, prop) => createFileObject(prop),
});
return {
cwd,
files,
};
};