UNPKG

1.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.cachedProcessFile = void 0;
4function cachedProcessFile(processor, fs, resolvePath) {
5 const cache = {};
6 const postProcessors = [];
7 function process(fullpath, ignoreCache = false, context) {
8 const resolvedPath = resolvePath(fullpath, context);
9 const stat = fs.statSync(resolvedPath);
10 const cached = cache[resolvedPath];
11 if (ignoreCache ||
12 !cached ||
13 (cached && cached.stat.mtime.valueOf() !== stat.mtime.valueOf())) {
14 const content = fs.readFileSync(resolvedPath, 'utf8');
15 const value = processContent(content, resolvedPath);
16 cache[resolvedPath] = { value, stat: { mtime: stat.mtime } };
17 }
18 return cache[resolvedPath].value;
19 }
20 function processContent(content, filePath) {
21 return postProcessors.reduce((value, postProcessor) => {
22 return postProcessor(value, filePath);
23 }, processor(filePath, content));
24 }
25 function add(fullpath, value) {
26 let mtime;
27 try {
28 mtime = fs.statSync(fullpath).mtime;
29 }
30 catch (e) {
31 mtime = new Date();
32 }
33 cache[fullpath] = {
34 value,
35 stat: {
36 mtime,
37 },
38 };
39 }
40 return {
41 processContent,
42 postProcessors,
43 cache,
44 process,
45 add,
46 resolvePath,
47 };
48}
49exports.cachedProcessFile = cachedProcessFile;
50//# sourceMappingURL=cached-process-file.js.map
\No newline at end of file