-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.js
41 lines (33 loc) · 897 Bytes
/
get.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
const fs = require("fs");
const path = require("path");
const get = (target, property) => {
const createFileObject = require("./index");
const getContent = require("./getContent");
const value = Reflect.get(target, property);
// Check if it is existing property and return it
if (value) {
return value;
}
// If it is not existing property treat it as file
try {
const rootPath = Reflect.get(target, "path");
const fullPath = path.join(rootPath, property);
const stat = fs.lstatSync(fullPath);
const isFile = stat.isFile();
const fileObject = {
path: fullPath,
name: property,
...stat,
isFile,
getContent: getContent(fullPath),
};
if (isFile) {
return fileObject;
}
return createFileObject(fileObject);
} catch (error) {
console.log(error);
return undefined;
}
};
module.exports = get;