-
-
Notifications
You must be signed in to change notification settings - Fork 498
/
TemplateData.js
353 lines (297 loc) · 9.98 KB
/
TemplateData.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const fs = require("fs-extra");
const fastglob = require("fast-glob");
const parsePath = require("parse-filepath");
const lodashset = require("lodash/set");
const lodashUniq = require("lodash/uniq");
const merge = require("./Util/Merge");
const TemplateRender = require("./TemplateRender");
const TemplatePath = require("./TemplatePath");
const TemplateGlob = require("./TemplateGlob");
const EleventyExtensionMap = require("./EleventyExtensionMap");
const EleventyBaseError = require("./EleventyBaseError");
const bench = require("./BenchmarkManager").get("Data");
const config = require("./Config");
const debugWarn = require("debug")("Eleventy:Warnings");
const debug = require("debug")("Eleventy:TemplateData");
const debugDev = require("debug")("Dev:Eleventy:TemplateData");
const deleteRequireCache = require("./Util/DeleteRequireCache");
class TemplateDataParseError extends EleventyBaseError {}
class TemplateData {
constructor(inputDir) {
this.config = config.getConfig();
this.dataTemplateEngine = this.config.dataTemplateEngine;
this.inputDirNeedsCheck = false;
this.setInputDir(inputDir);
this.rawImports = {};
this.globalData = null;
}
/* Used by tests */
_setConfig(config) {
this.config = config;
this.dataTemplateEngine = this.config.dataTemplateEngine;
}
setInputDir(inputDir) {
this.inputDirNeedsCheck = true;
this.inputDir = inputDir;
this.dataDir = this.config.dir.data
? TemplatePath.join(inputDir, this.config.dir.data)
: inputDir;
}
setDataTemplateEngine(engineName) {
this.dataTemplateEngine = engineName;
}
getRawImports() {
let pkgPath = TemplatePath.absolutePath("package.json");
try {
this.rawImports[this.config.keys.package] = require(pkgPath);
} catch (e) {
debug(
"Could not find and/or require package.json for data preprocessing at %o",
pkgPath
);
}
return this.rawImports;
}
getDataDir() {
return this.dataDir;
}
clearData() {
this.globalData = null;
}
async cacheData() {
this.clearData();
return this.getData();
}
_getGlobalDataGlobByExtension(dir, extension) {
return TemplateGlob.normalizePath(
dir,
"/",
this.config.dir.data !== "." ? this.config.dir.data : "",
`/**/*.${extension}`
);
}
async _checkInputDir() {
if (this.inputDirNeedsCheck) {
let globalPathStat = await fs.stat(this.inputDir);
if (!globalPathStat.isDirectory()) {
throw new Error("Could not find data path directory: " + this.inputDir);
}
this.inputDirNeedsCheck = false;
}
}
async getInputDir() {
let dir = ".";
if (this.inputDir) {
await this._checkInputDir();
dir = this.inputDir;
}
return dir;
}
async getTemplateDataFileGlob() {
let dir = await this.getInputDir();
return TemplatePath.addLeadingDotSlashArray([
`${dir}/**/*.json`, // covers .11tydata.json too
`${dir}/**/*${this.config.jsDataFileSuffix}.js`
]);
}
async getTemplateJavaScriptDataFileGlob() {
let dir = await this.getInputDir();
return TemplatePath.addLeadingDotSlashArray([
`${dir}/**/*${this.config.jsDataFileSuffix}.js`
]);
}
async getGlobalDataGlob() {
let dir = await this.getInputDir();
return [this._getGlobalDataGlobByExtension(dir, "(json|js)")];
}
getWatchPathCache() {
return this.pathCache;
}
async getGlobalDataFiles() {
let paths = await fastglob(await this.getGlobalDataGlob(), {
caseSensitiveMatch: false,
dot: true
});
this.pathCache = paths;
return paths;
}
getObjectPathForDataFile(path) {
let reducedPath = TemplatePath.stripLeadingSubPath(path, this.dataDir);
let parsed = parsePath(reducedPath);
let folders = parsed.dir ? parsed.dir.split("/") : [];
folders.push(parsed.name);
return folders.join(".");
}
async getAllGlobalData() {
let rawImports = this.getRawImports();
let globalData = {};
let files = TemplatePath.addLeadingDotSlashArray(
await this.getGlobalDataFiles()
);
let dataFileConflicts = {};
for (let j = 0, k = files.length; j < k; j++) {
let folders = await this.getObjectPathForDataFile(files[j]);
// TODO maybe merge these two? (if both valid objects)
if (dataFileConflicts[folders]) {
debugWarn(
`Warning: the key for a global data file (${files[j]}) will overwrite data from an already existing global data file (${dataFileConflicts[folders]})`
);
}
dataFileConflicts[folders] = files[j];
debug(`Found global data file ${files[j]} and adding as: ${folders}`);
let data = await this.getDataValue(files[j], rawImports);
lodashset(globalData, folders, data);
}
return globalData;
}
async getData() {
let rawImports = this.getRawImports();
if (!this.globalData) {
let globalJson = await this.getAllGlobalData();
// OK: Shallow merge when combining rawImports (pkg) with global data files
this.globalData = Object.assign({}, globalJson, rawImports);
}
return this.globalData;
}
/* Template and Directory data files */
async combineLocalData(localDataPaths) {
let localData = {};
if (!Array.isArray(localDataPaths)) {
localDataPaths = [localDataPaths];
}
for (let path of localDataPaths) {
// clean up data for template/directory data files only.
let dataForPath = await this.getDataValue(path, null, true);
let cleanedDataForPath = TemplateData.cleanupData(dataForPath);
TemplateData.mergeDeep(this.config, localData, cleanedDataForPath);
// debug("`combineLocalData` (iterating) for %o: %O", path, localData);
}
return localData;
}
async getLocalData(templatePath) {
let localDataPaths = await this.getLocalDataPaths(templatePath);
let importedData = await this.combineLocalData(localDataPaths);
let globalData = await this.getData();
// OK-ish: shallow merge when combining template/data dir files with global data files
let localData = Object.assign({}, globalData, importedData);
// debug("`getLocalData` for %o: %O", templatePath, localData);
return localData;
}
async _getLocalJsonString(path) {
let rawInput;
try {
rawInput = await fs.readFile(path, "utf-8");
} catch (e) {
// if file does not exist, return nothing
}
return rawInput;
}
async getDataValue(path, rawImports, ignoreProcessing) {
if (ignoreProcessing || TemplatePath.getExtension(path) === "js") {
let localPath = TemplatePath.absolutePath(path);
if (await fs.pathExists(localPath)) {
let dataBench = bench.get(`\`${path}\``);
dataBench.before();
deleteRequireCache(localPath);
let returnValue = require(localPath);
if (typeof returnValue === "function") {
returnValue = await returnValue();
}
dataBench.after();
return returnValue;
} else {
return {};
}
} else {
let rawInput = await this._getLocalJsonString(path);
let engineName = this.dataTemplateEngine;
if (rawInput) {
if (ignoreProcessing || engineName === false) {
try {
return JSON.parse(rawInput);
} catch (e) {
throw new TemplateDataParseError(
`Having trouble parsing data file ${path}`,
e
);
}
} else {
let fn = await new TemplateRender(engineName).getCompiledTemplate(
rawInput
);
try {
// pass in rawImports, don’t pass in global data, that’s what we’re parsing
return JSON.parse(await fn(rawImports));
} catch (e) {
throw new TemplateDataParseError(
`Having trouble parsing data file ${path}`,
e
);
}
}
}
}
return {};
}
async getLocalDataPaths(templatePath) {
let paths = [];
let parsed = parsePath(templatePath);
let inputDir = TemplatePath.addLeadingDotSlash(
TemplatePath.normalize(this.inputDir)
);
debugDev("getLocalDataPaths(%o)", templatePath);
debugDev("parsed.dir: %o", parsed.dir);
if (parsed.dir) {
let fileNameNoExt = EleventyExtensionMap.removeTemplateExtension(
parsed.base
);
let filePathNoExt = parsed.dir + "/" + fileNameNoExt;
let dataSuffix = this.config.jsDataFileSuffix;
debug("Using %o to find data files.", dataSuffix);
paths.push(filePathNoExt + dataSuffix + ".js");
paths.push(filePathNoExt + dataSuffix + ".json");
paths.push(filePathNoExt + ".json");
let allDirs = TemplatePath.getAllDirs(parsed.dir);
debugDev("allDirs: %o", allDirs);
for (let dir of allDirs) {
let lastDir = TemplatePath.getLastPathSegment(dir);
let dirPathNoExt = dir + "/" + lastDir;
if (!inputDir) {
paths.push(dirPathNoExt + dataSuffix + ".js");
paths.push(dirPathNoExt + dataSuffix + ".json");
paths.push(dirPathNoExt + ".json");
} else {
debugDev("dirStr: %o; inputDir: %o", dir, inputDir);
if (dir.indexOf(inputDir) === 0 && dir !== inputDir) {
paths.push(dirPathNoExt + dataSuffix + ".js");
paths.push(dirPathNoExt + dataSuffix + ".json");
paths.push(dirPathNoExt + ".json");
}
}
}
}
debug("getLocalDataPaths(%o): %o", templatePath, paths);
return lodashUniq(paths).reverse();
}
static mergeDeep(config, target, ...source) {
if (config.dataDeepMerge) {
return TemplateData.merge(target, ...source);
} else {
return Object.assign(target, ...source);
}
}
static merge(target, ...source) {
return merge(target, ...source);
}
static cleanupData(data) {
if ("tags" in data) {
if (typeof data.tags === "string") {
data.tags = data.tags ? [data.tags] : [];
} else if (data.tags === null) {
data.tags = [];
}
}
return data;
}
}
module.exports = TemplateData;