-
-
Notifications
You must be signed in to change notification settings - Fork 498
/
EleventyFiles.js
412 lines (348 loc) · 10.9 KB
/
EleventyFiles.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const fs = require("fs-extra");
const fastglob = require("fast-glob");
const EleventyExtensionMap = require("./EleventyExtensionMap");
const TemplateData = require("./TemplateData");
const TemplateGlob = require("./TemplateGlob");
const TemplatePath = require("./TemplatePath");
const TemplatePassthroughManager = require("./TemplatePassthroughManager");
const config = require("./Config");
const debug = require("debug")("Eleventy:EleventyFiles");
// const debugDev = require("debug")("Dev:Eleventy:EleventyFiles");
const aggregateBench = require("./BenchmarkManager").get("Aggregate");
class EleventyFiles {
constructor(input, outputDir, formats, passthroughAll) {
this.config = config.getConfig();
this.input = input;
this.inputDir = TemplatePath.getDir(this.input);
this.outputDir = outputDir;
this.initConfig();
this.passthroughAll = !!passthroughAll;
this.formats = formats;
}
initConfig() {
this.includesDir = TemplatePath.join(
this.inputDir,
this.config.dir.includes
);
if ("layouts" in this.config.dir) {
this.layoutsDir = TemplatePath.join(
this.inputDir,
this.config.dir.layouts
);
}
}
init() {
// Input was a directory
if (this.input === this.inputDir) {
this.templateGlobs = this.extensionMap.getGlobs(this.inputDir);
} else {
this.templateGlobs = TemplateGlob.map([this.input]);
}
this.initPassthroughManager();
this.setupGlobs();
}
get validTemplateGlobs() {
if (!this._validTemplateGlobs) {
let globs;
if (this.input === this.inputDir) {
globs = this.extensionMap.getValidGlobs(this.inputDir);
} else {
globs = this.templateGlobs;
}
this._validTemplateGlobs = globs;
}
return this._validTemplateGlobs;
}
get passthroughGlobs() {
let paths = new Set();
// stuff added in addPassthroughCopy()
for (let path of this.passthroughManager.getConfigPathGlobs()) {
paths.add(path);
}
// non-template language extensions
for (let path of this.extensionMap.getPassthroughCopyGlobs(this.inputDir)) {
paths.add(path);
}
return Array.from(paths);
}
restart() {
this.passthroughManager.reset();
this.setupGlobs();
}
/* For testing */
_setConfig(config) {
this.config = config;
this.initConfig();
}
/* Set command root for local project paths */
_setLocalPathRoot(dir) {
this.localPathRoot = dir;
}
set extensionMap(extensionMap) {
this._extensionMap = extensionMap;
}
get extensionMap() {
// for tests
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap(this.formats);
this._extensionMap.config = this.config;
}
return this._extensionMap;
}
setPassthroughAll(passthroughAll) {
this.passthroughAll = !!passthroughAll;
}
initPassthroughManager() {
let mgr = new TemplatePassthroughManager();
mgr.setInputDir(this.inputDir);
mgr.setOutputDir(this.outputDir);
mgr.extensionMap = this.extensionMap;
this.passthroughManager = mgr;
}
getPassthroughManager() {
return this.passthroughManager;
}
setPassthroughManager(mgr) {
mgr.extensionMap = this.extensionMap;
this.passthroughManager = mgr;
}
setTemplateData(templateData) {
this.templateData = templateData;
}
// TODO make this a getter
getTemplateData() {
if (!this.templateData) {
this.templateData = new TemplateData(this.inputDir);
}
return this.templateData;
}
getDataDir() {
let data = this.getTemplateData();
return data.getDataDir();
}
setupGlobs() {
this.fileIgnores = this.getIgnores();
if (this.passthroughAll) {
this.templateGlobsWithIgnoresFromFiles = TemplateGlob.map([
TemplateGlob.normalizePath(this.input, "/**")
]).concat(this.fileIgnores);
} else {
this.templateGlobsWithIgnoresFromFiles = this.templateGlobs.concat(
this.fileIgnores
);
}
this.templateGlobsWithAllIgnores = this.templateGlobsWithIgnoresFromFiles.concat(
this._getIncludesAndDataDirIgnores()
);
}
static getFileIgnores(ignoreFiles, defaultIfFileDoesNotExist) {
if (!Array.isArray(ignoreFiles)) {
ignoreFiles = [ignoreFiles];
}
let ignores = [];
let fileFound = false;
let dirs = [];
for (let ignorePath of ignoreFiles) {
ignorePath = TemplatePath.normalize(ignorePath);
let dir = TemplatePath.getDirFromFilePath(ignorePath);
dirs.push(dir);
if (fs.existsSync(ignorePath) && fs.statSync(ignorePath).size > 0) {
fileFound = true;
let ignoreContent = fs.readFileSync(ignorePath, "utf-8");
// make sure that empty .gitignore with spaces takes default ignore.
if (ignoreContent.trim().length === 0) {
fileFound = false;
} else {
ignores = ignores.concat(
EleventyFiles.normalizeIgnoreContent(dir, ignoreContent)
);
}
}
}
if (!fileFound && defaultIfFileDoesNotExist) {
ignores.push("!" + TemplateGlob.normalizePath(defaultIfFileDoesNotExist));
for (let dir of dirs) {
ignores.push(
"!" + TemplateGlob.normalizePath(dir, defaultIfFileDoesNotExist)
);
}
}
ignores.forEach(function(path) {
debug(`${ignoreFiles} ignoring: ${path}`);
});
return ignores;
}
static normalizeIgnoreContent(dir, ignoreContent) {
let ignores = [];
if (ignoreContent) {
ignores = ignoreContent
.split("\n")
.map(line => {
return line.trim();
})
.filter(line => {
if (line.charAt(0) === "!") {
debug(
">>> When processing .gitignore/.eleventyignore, Eleventy does not currently support negative patterns but encountered one:"
);
debug(">>>", line);
debug(
"Follow along at https://github.com/11ty/eleventy/issues/693 to track support."
);
}
// empty lines or comments get filtered out
return (
line.length > 0 && line.charAt(0) !== "#" && line.charAt(0) !== "!"
);
})
.map(line => {
let path = TemplateGlob.normalizePath(dir, "/", line);
path = TemplatePath.addLeadingDotSlash(
TemplatePath.relativePath(path)
);
try {
// Note these folders must exist to get /** suffix
let stat = fs.statSync(path);
if (stat.isDirectory()) {
return "!" + path + "/**";
}
return "!" + path;
} catch (e) {
return "!" + path;
}
});
}
return ignores;
}
getIgnores() {
let files = [];
let rootDirectory = this.localPathRoot || TemplatePath.getWorkingDir();
let absoluteInputDir = TemplatePath.absolutePath(this.inputDir);
if (this.config.useGitIgnore) {
let gitIgnores = [TemplatePath.join(rootDirectory, ".gitignore")];
if (rootDirectory !== absoluteInputDir) {
gitIgnores.push(TemplatePath.join(this.inputDir, ".gitignore"));
}
files = files.concat(
EleventyFiles.getFileIgnores(gitIgnores, "node_modules/**")
);
}
if (this.config.eleventyignoreOverride !== false) {
let eleventyIgnores = [
TemplatePath.join(rootDirectory, ".eleventyignore")
];
if (rootDirectory !== absoluteInputDir) {
eleventyIgnores.push(
TemplatePath.join(this.inputDir, ".eleventyignore")
);
}
files = files.concat(
this.config.eleventyignoreOverride ||
EleventyFiles.getFileIgnores(eleventyIgnores)
);
}
files = files.concat(TemplateGlob.map("!" + this.outputDir + "/**"));
return files;
}
getIncludesDir() {
return this.includesDir;
}
getLayoutsDir() {
return this.layoutsDir;
}
getFileGlobs() {
return this.templateGlobsWithAllIgnores;
}
getRawFiles() {
return this.templateGlobs;
}
getWatchPathCache() {
return this.pathCache;
}
async getFiles() {
let globs = this.getFileGlobs();
debug("Searching for: %o", globs);
let bench = aggregateBench.get("Searching the file system");
bench.before();
let paths = TemplatePath.addLeadingDotSlashArray(
await fastglob(globs, {
caseSensitiveMatch: false,
dot: true
})
);
bench.after();
if ("extensionMap" in this.config) {
let extensions = this.config.extensionMap;
paths = paths.filter(function(path) {
for (let entry of extensions) {
// TODO `.${extension}` ?
if (path.endsWith(entry.extension) && entry.filter) {
return entry.filter(path);
}
}
return true;
});
}
this.pathCache = paths;
return paths;
}
/* For `eleventy --watch` */
getGlobWatcherFiles() {
// TODO is it better to tie the includes and data to specific file extensions or keep the **?
return this.validTemplateGlobs
.concat(this.passthroughGlobs)
.concat(this._getIncludesAndDataDirs());
}
/* For `eleventy --watch` */
async getGlobWatcherTemplateDataFiles() {
let templateData = this.getTemplateData();
return await templateData.getTemplateDataFileGlob();
}
/* For `eleventy --watch` */
// TODO this isn’t great but reduces complexity avoiding using TemplateData:getLocalDataPaths for each template in the cache
async getWatcherTemplateJavaScriptDataFiles() {
let globs = await this.getTemplateData().getTemplateJavaScriptDataFileGlob();
let bench = aggregateBench.get("Searching the file system");
bench.before();
let results = TemplatePath.addLeadingDotSlashArray(
await fastglob(globs, {
ignore: ["**/node_modules/**"],
caseSensitiveMatch: false,
dot: true
})
);
bench.after();
return results;
}
/* Ignored by `eleventy --watch` */
getGlobWatcherIgnores() {
// convert to format without ! since they are passed in as a separate argument to glob watcher
return this.fileIgnores.map(ignore =>
TemplatePath.stripLeadingDotSlash(ignore.substr(1))
);
}
_getIncludesAndDataDirs() {
let files = [];
// we want this to fail on "" because we don’t want to ignore the
// entire input directory when using ""
if (this.config.dir.includes) {
files = files.concat(TemplateGlob.map(this.includesDir + "/**"));
}
// we want this to fail on "" because we don’t want to ignore the
// entire input directory when using ""
if (this.config.dir.layouts) {
files = files.concat(TemplateGlob.map(this.layoutsDir + "/**"));
}
if (this.config.dir.data && this.config.dir.data !== ".") {
let dataDir = this.getDataDir();
files = files.concat(TemplateGlob.map(dataDir + "/**"));
}
return files;
}
_getIncludesAndDataDirIgnores() {
return this._getIncludesAndDataDirs().map(function(dir) {
return "!" + dir;
});
}
}
module.exports = EleventyFiles;