保誠-保戶業務員媒合平台
Tomas
2022-05-19 957a1f10a06fdbb76f1a0ba94fe44126c613fee3
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"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 = require("path");
const fs_extra_1 = __importDefault(require("fs-extra"));
/**
 * It's an implementation of the FileSystem interface which reads and writes directly to the real file system.
 *
 * @param caseSensitive
 */
function createRealFileSystem(caseSensitive = false) {
    // read cache
    const existsCache = new Map();
    const readStatsCache = new Map();
    const readFileCache = new Map();
    const readDirCache = new Map();
    const realPathCache = new Map();
    function normalizePath(path) {
        return caseSensitive ? path_1.normalize(path) : path_1.normalize(path).toLowerCase();
    }
    // read methods
    function exists(path) {
        const normalizedPath = normalizePath(path);
        if (!existsCache.has(normalizedPath)) {
            existsCache.set(normalizedPath, fs_extra_1.default.existsSync(normalizedPath));
        }
        return !!existsCache.get(normalizedPath);
    }
    function readStats(path) {
        const normalizedPath = normalizePath(path);
        if (!readStatsCache.has(normalizedPath)) {
            if (exists(normalizedPath)) {
                readStatsCache.set(normalizedPath, fs_extra_1.default.statSync(normalizedPath));
            }
        }
        return readStatsCache.get(normalizedPath);
    }
    function readFile(path, encoding) {
        const normalizedPath = normalizePath(path);
        if (!readFileCache.has(normalizedPath)) {
            const stats = readStats(normalizedPath);
            if (stats && stats.isFile()) {
                readFileCache.set(normalizedPath, fs_extra_1.default.readFileSync(normalizedPath, { encoding: encoding }).toString());
            }
            else {
                readFileCache.set(normalizedPath, undefined);
            }
        }
        return readFileCache.get(normalizedPath);
    }
    function readDir(path) {
        const normalizedPath = normalizePath(path);
        if (!readDirCache.has(normalizedPath)) {
            const stats = readStats(normalizedPath);
            if (stats && stats.isDirectory()) {
                readDirCache.set(normalizedPath, fs_extra_1.default.readdirSync(normalizedPath, { withFileTypes: true }));
            }
            else {
                readDirCache.set(normalizedPath, []);
            }
        }
        return readDirCache.get(normalizedPath) || [];
    }
    function getRealPath(path) {
        const normalizedPath = normalizePath(path);
        if (!realPathCache.has(normalizedPath)) {
            let base = normalizedPath;
            let nested = '';
            while (base !== path_1.dirname(base)) {
                if (exists(base)) {
                    realPathCache.set(normalizedPath, normalizePath(path_1.join(fs_extra_1.default.realpathSync(base), nested)));
                    break;
                }
                nested = path_1.join(path_1.basename(base), nested);
                base = path_1.dirname(base);
            }
        }
        return realPathCache.get(normalizedPath) || normalizedPath;
    }
    function createDir(path) {
        const normalizedPath = normalizePath(path);
        fs_extra_1.default.mkdirSync(normalizedPath, { recursive: true });
        // update cache
        existsCache.set(normalizedPath, true);
        if (readDirCache.has(path_1.dirname(normalizedPath))) {
            readDirCache.delete(path_1.dirname(normalizedPath));
        }
        if (readStatsCache.has(normalizedPath)) {
            readStatsCache.delete(normalizedPath);
        }
    }
    function writeFile(path, data) {
        const normalizedPath = normalizePath(path);
        if (!exists(path_1.dirname(normalizedPath))) {
            createDir(path_1.dirname(normalizedPath));
        }
        fs_extra_1.default.writeFileSync(normalizedPath, data);
        // update cache
        existsCache.set(normalizedPath, true);
        if (readDirCache.has(path_1.dirname(normalizedPath))) {
            readDirCache.delete(path_1.dirname(normalizedPath));
        }
        if (readStatsCache.has(normalizedPath)) {
            readStatsCache.delete(normalizedPath);
        }
        if (readFileCache.has(normalizedPath)) {
            readFileCache.delete(normalizedPath);
        }
    }
    function deleteFile(path) {
        if (exists(path)) {
            const normalizedPath = normalizePath(path);
            fs_extra_1.default.unlinkSync(normalizedPath);
            // update cache
            existsCache.set(normalizedPath, false);
            if (readDirCache.has(path_1.dirname(normalizedPath))) {
                readDirCache.delete(path_1.dirname(normalizedPath));
            }
            if (readStatsCache.has(normalizedPath)) {
                readStatsCache.delete(normalizedPath);
            }
            if (readFileCache.has(normalizedPath)) {
                readFileCache.delete(normalizedPath);
            }
        }
    }
    function updateTimes(path, atime, mtime) {
        if (exists(path)) {
            const normalizedPath = normalizePath(path);
            fs_extra_1.default.utimesSync(normalizePath(path), atime, mtime);
            // update cache
            if (readStatsCache.has(normalizedPath)) {
                readStatsCache.delete(normalizedPath);
            }
        }
    }
    return {
        exists(path) {
            return exists(getRealPath(path));
        },
        readFile(path, encoding) {
            return readFile(getRealPath(path), encoding);
        },
        readDir(path) {
            return readDir(getRealPath(path));
        },
        readStats(path) {
            return readStats(getRealPath(path));
        },
        realPath(path) {
            return getRealPath(path);
        },
        normalizePath(path) {
            return normalizePath(path);
        },
        writeFile(path, data) {
            writeFile(getRealPath(path), data);
        },
        deleteFile(path) {
            deleteFile(getRealPath(path));
        },
        createDir(path) {
            createDir(getRealPath(path));
        },
        updateTimes(path, atime, mtime) {
            updateTimes(getRealPath(path), atime, mtime);
        },
        clearCache() {
            existsCache.clear();
            readStatsCache.clear();
            readFileCache.clear();
            readDirCache.clear();
            realPathCache.clear();
        },
    };
}
exports.createRealFileSystem = createRealFileSystem;