-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (126 loc) · 4.01 KB
/
index.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
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
'use strict';
const path = require('path');
const View = require("express/lib/view");
const ExpressHandlebars = require("express-handlebars/lib/express-handlebars");
function ExpressHandlebarsMemoryFs(memoryFs) {
if (process.env.NODE_ENV === 'production') {
return;
}
if (!memoryFs) {
throw new Error('express-handlebars-memory-fs expects 1 arguments, webpack outputFileSystem. such as: `compiler.outputFileSystem`.');
}
const viewResolve = View.prototype.resolve;
View.prototype.resolve = function resolve(dir, file) {
var ext = this.ext;
// <path>.<ext>
var viewPath = path.join(dir, file);
var viewStat = tryStat(viewPath);
if (viewStat && viewStat.isFile()) {
return viewPath;
}
// <path>/index.<ext>
viewPath = path.join(dir, path.basename(file, ext), 'index' + ext);
viewStat = tryStat(viewPath);
if (viewStat && viewStat.isFile()) {
return viewPath;
}
return viewResolve.call(this, dir, file);
};
function tryStat(path) {
try {
return memoryFs.statSync(path);
} catch (e) {
return undefined;
}
}
const ExpressHandlebars_getFile = ExpressHandlebars.prototype._getFile;
ExpressHandlebars.prototype._getFile = function (filePath, options) {
filePath = path.resolve(filePath);
options || (options = {});
let cache = this._fsCache;
let file = options.cache && cache[filePath];
if (file) {
return file;
}
let fileStat = tryStat(filePath);
if (fileStat && fileStat.isFile()) {
file = cache[filePath] = new Promise(function (resolve, reject) {
memoryFs.readFile(filePath, 'utf8', function (err, file) {
if (err) {
reject(err);
} else {
resolve(file);
}
});
});
return file.catch(function (err) {
delete cache[filePath];
throw err;
});
}
return ExpressHandlebars_getFile.call(this, filePath, options);
};
const ExpressHandlebars_getDir = ExpressHandlebars.prototype._getDir;
ExpressHandlebars.prototype._getDir = function (dirPath, options) {
dirPath = path.resolve(dirPath);
options || (options = {});
let cache = this._fsCache;
let dir = options.cache && cache[dirPath];
if (dir) {
return dir.then(function (dir) {
return dir.concat();
});
}
let extname = this.extname;
// Optimistically cache dir promise to reduce file system I/O, but remove
// from cache if there was a problem.
dir = cache[dirPath] = new Promise(function (resolve, reject) {
memoryFs.stat(dirPath, function (err, stat) {
if (err) {
reject(err);
} else {
let relativeFileNams = [];
let files = readdirSync(dirPath, extname);
for (let m = 0; m < files.length; m++) {
let file = files[m];
let fileName = path.relative(dirPath, file);
// home\partial.hbs → home/partial.hbs
fileName = fileName.split(path.sep).join('/');
relativeFileNams.push(fileName);
}
resolve(relativeFileNams);
}
});
});
if (dir) {
return dir.then(function (dir) {
return dir.concat();
});
}
return ExpressHandlebars_getDir.call(this, dirPath, options);
};
function readdirSync(dirPath, ext) {
let filePaths = [];
let childrenFilePaths = memoryFs.readdirSync(dirPath);
for (let i = 0; i < childrenFilePaths.length; i++) {
let tempFile = childrenFilePaths[i];
tempFile = path.join(dirPath, tempFile);
let tempFileStat = memoryFs.statSync(tempFile);
if (!tempFileStat) {
continue;
}
if (tempFileStat.isFile()) {
let fileExt = path.extname(tempFile);
if (fileExt !== ext) {
continue;
}
filePaths.unshift(tempFile);
} else {
let childFilePaths = readdirSync(tempFile, ext);
filePaths.push(...childFilePaths);
}
}
return filePaths;
}
}
module.exports = ExpressHandlebarsMemoryFs;