Repository navigation
Expand file tree
/
Copy pathFiles.js
More file actions
471 lines (382 loc) · 11.4 KB
/
Copy pathFiles.js
File metadata and controls
471 lines (382 loc) · 11.4 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import { readFileSync } from "node:fs";
import { TemplatePath } from "@11ty/eleventy-utils";
import { createDebug } from "./Util/DebugLogUtil.js";
import DirContains from "./Util/DirContains.js";
import TemplateGlob from "./TemplateGlob.js";
import checkPassthroughCopyBehavior from "./Util/PassthroughCopyBehaviorCheck.js";
const debug = createDebug("Files");
export class Files {
#extensionMap;
#watcherGlobs;
constructor(formats, templateConfig) {
if (!templateConfig) {
throw new Error("Internal error: Missing `templateConfig`` argument.");
}
this.templateConfig = templateConfig;
this.config = templateConfig.getConfig();
this.aggregateBench = this.config.benchmarkManager.get("Aggregate");
this.formats = formats;
this.eleventyIgnoreContent = false;
}
get dirs() {
return this.templateConfig.directories;
}
get inputDir() {
return this.dirs.input;
}
get outputDir() {
return this.dirs.output;
}
get includesDir() {
return this.dirs.includes;
}
get layoutsDir() {
return this.dirs.layouts;
}
get dataDir() {
return this.dirs.data;
}
// Backwards compat
getDataDir() {
return this.dataDir;
}
setFileSystemSearch(fileSystemSearch) {
this.fileSystemSearch = fileSystemSearch;
}
init() {
if (this.dirs.inputFile || this.dirs.inputGlob) {
this.templateGlobs = TemplateGlob.map([this.dirs.inputFile || this.dirs.inputGlob]);
} else {
// Input is a directory
this.templateGlobs = this.extensionMap.getGlobs(this.inputDir);
}
this.setupGlobs();
}
#getWatcherGlobs() {
if (!this.#watcherGlobs) {
let globs;
// Input is a file
if (this.inputFile) {
globs = this.templateGlobs;
} else {
// input is a directory
globs = this.extensionMap.getValidGlobs(this.inputDir);
}
this.#watcherGlobs = globs;
}
return this.#watcherGlobs;
}
get passthroughGlobs() {
return this.#getPassthroughGlobs();
}
// `isEmulatable` filters `addPassthroughCopy()` entries by whether the dev server can emulate them
#getPassthroughGlobs(isEmulatable) {
let paths = new Set();
// stuff added in addPassthroughCopy()
for (let path of this.passthroughManager.getConfigPathGlobs(isEmulatable)) {
paths.add(path);
}
// non-template language extensions
if (isEmulatable !== false) {
for (let path of this.extensionMap.getPassthroughCopyGlobs(this.inputDir)) {
paths.add(path);
}
}
return Array.from(paths);
}
restart() {
this.setupGlobs();
this._glob = null;
}
/* For testing */
_setConfig(config) {
if (!config.ignores) {
config.ignores = new Set();
config.ignores.add("**/node_modules/**");
}
this.config = config;
this.init();
}
/* Set command root for local project paths */
// This is only used by tests
_setLocalPathRoot(dir) {
this.localPathRoot = dir;
}
set extensionMap(extensionMap) {
this.#extensionMap = extensionMap;
}
get extensionMap() {
// for tests
if (!this.#extensionMap) {
throw new Error("Internal error: missing `extensionMap` in EleventyFiles.");
}
return this.#extensionMap;
}
setRunMode(runMode) {
this.runMode = runMode;
}
setPassthroughManager(mgr) {
this.passthroughManager = mgr;
}
set templateData(templateData) {
this._templateData = templateData;
}
get templateData() {
return this._templateData;
}
setupGlobs() {
this.fileIgnores = this.getIgnores();
this.extraIgnores = this.getIncludesAndDataDirs();
this.uniqueIgnores = this.getIgnoreGlobs();
// Conditional added for tests that don’t have a config
if (this.config?.events) {
this.config.events.emit("buildawesome.ignores", this.uniqueIgnores);
}
this.normalizedTemplateGlobs = this.templateGlobs;
}
normalizeIgnoreEntry(entry) {
if (!entry.startsWith("**/")) {
return TemplateGlob.normalizePath(this.localPathRoot || ".", entry);
}
return entry;
}
getIgnoreGlobs() {
let uniqueIgnores = new Set();
for (let ignore of this.fileIgnores) {
uniqueIgnores.add(ignore);
}
for (let ignore of this.extraIgnores) {
uniqueIgnores.add(ignore);
}
// Placing the config ignores last here is important to the tests
for (let ignore of this.config.ignores) {
uniqueIgnores.add(this.normalizeIgnoreEntry(ignore));
}
return Array.from(uniqueIgnores);
}
// v4.0.0-alpha.8 swapped from static to use existsCache
getFileIgnores(ignoreFiles) {
if (!Array.isArray(ignoreFiles)) {
ignoreFiles = [ignoreFiles];
}
let ignores = [];
for (let ignorePath of ignoreFiles) {
ignorePath = TemplatePath.normalize(ignorePath);
if (this.templateConfig.existsCache.exists(ignorePath)) {
let ignoreContent = readFileSync(ignorePath, "utf8");
if (ignoreContent) {
let dir = TemplatePath.getDirFromFilePath(ignorePath);
ignores = ignores.concat(this.normalizeIgnoreContent(dir, ignoreContent));
}
}
}
ignores.forEach((path) => debug(`${ignoreFiles} ignoring: ${path}`));
return ignores;
}
// v4.0.0-alpha.8 swapped from static to use existsCache
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/buildawesome/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));
if (this.templateConfig.existsCache.isDirectory(path)) {
return path + "/**";
}
return path;
});
}
return ignores;
}
/* Tests only */
_setEleventyIgnoreContent(content) {
this.eleventyIgnoreContent = content;
}
getIgnores() {
let files = new Set();
for (let ignore of this.getFileIgnores(this.getIgnoreFiles())) {
files.add(ignore);
}
// testing API
if (this.eleventyIgnoreContent !== false) {
files.add(this.eleventyIgnoreContent);
}
// Make sure output dir isn’t in the input dir (or it will ignore all input!)
// input: . and output: . (skip ignore)
// input: ./content and output . (skip ignore)
// input: . and output: ./_site (add ignore)
let outputContainsInputDir = DirContains(this.outputDir, this.inputDir);
if (!outputContainsInputDir) {
// both are already normalized in 3.0
files.add(TemplateGlob.map(this.outputDir + "/**"));
}
return Array.from(files);
}
getIgnoreFiles() {
let ignoreFiles = new Set();
let rootDirectory = this.localPathRoot || ".";
if (this.config.useGitIgnore) {
ignoreFiles.add(TemplatePath.join(rootDirectory, ".gitignore"));
}
if (this.eleventyIgnoreContent === false) {
let absoluteInputDir = TemplatePath.absolutePath(this.inputDir);
ignoreFiles.add(TemplatePath.join(rootDirectory, ".eleventyignore"));
if (rootDirectory !== absoluteInputDir) {
ignoreFiles.add(TemplatePath.join(this.inputDir, ".eleventyignore"));
}
}
return Array.from(ignoreFiles);
}
/* Backwards compat */
getIncludesDir() {
return this.includesDir;
}
/* Backwards compat */
getLayoutsDir() {
return this.layoutsDir;
}
getFileGlobs() {
return this.normalizedTemplateGlobs;
}
getRawFiles() {
return this.templateGlobs;
}
async getWatchPathCache() {
// Issue #1325: make sure passthrough copy files are not included here
if (!this.pathCache) {
throw new Error("Watching requires `.getFiles()` to be called first in EleventyFiles");
}
let ret = [];
// Filter out the passthrough copy paths.
for (let path of this.pathCache) {
if (
this.extensionMap.isFullTemplateFilePath(path) &&
(await this.extensionMap.shouldSpiderJavaScriptDependencies(path))
) {
ret.push(path);
}
}
return ret;
}
_globSearch() {
let globs = this.getFileGlobs();
// returns a promise
debug("Searching for: %o", globs);
return this.fileSystemSearch.search("templates", globs, {
ignore: this.uniqueIgnores,
});
}
async getFiles() {
let bench = this.aggregateBench.get("Searching the file system (templates)");
bench.before();
let globResults = await this._globSearch();
let paths = TemplatePath.addLeadingDotSlashArray(globResults);
bench.after();
// Note 2.0.0-canary.19 removed a `filter` option for custom template syntax here that was unpublished and unused.
this.pathCache = paths;
return paths;
}
getIncrementalFileShapes(paths, incrementalFilePaths) {
if (!incrementalFilePaths || incrementalFilePaths.length === 0) {
return {};
}
let passthroughIncrementalPaths = this.passthroughManager.filterToPassthroughCopyFilesOnly(
paths,
incrementalFilePaths,
);
let shapes = {};
for (let incrementalFilePath of incrementalFilePaths) {
if (passthroughIncrementalPaths.includes(incrementalFilePath)) {
shapes[incrementalFilePath] = "copy";
} else if (paths.includes(incrementalFilePath)) {
shapes[incrementalFilePath] = "template";
}
// otherwise: include/layout/unknown
}
return shapes;
}
/* For `eleventy --watch` */
getGlobWatcherFiles() {
let directoryGlobs = this.getIncludesAndDataDirs();
let globs = this.#getWatcherGlobs();
// Emulated passthrough copy files are watched by the dev server, others are copied on rebuild
if (checkPassthroughCopyBehavior(this.config, this.runMode)) {
return globs.concat(this.#getPassthroughGlobs(false)).concat(directoryGlobs);
}
// Revert to old passthroughcopy copy files behavior
return globs.concat(this.passthroughGlobs).concat(directoryGlobs);
}
/* For `eleventy --watch` */
getGlobWatcherFilesForPassthroughCopy() {
return this.#getPassthroughGlobs(true);
}
/* For `eleventy --watch` */
getGlobWatcherTemplateDataFiles() {
return this.templateData.getTemplateDataFileGlob();
}
/* For `eleventy --watch` */
// Improvement: Move this up the pipeline and feed into the ExistsCache to alleviate TemplateData checks
async getWatcherTemplateJavaScriptDataFiles() {
let globs = this.templateData.getTemplateJavaScriptDataFileGlob();
let bench = this.aggregateBench.get("Searching the file system (watching)");
bench.before();
let results = TemplatePath.addLeadingDotSlashArray(
await this.fileSystemSearch.search("js-dependencies", globs, {
ignore: [
"**/node_modules/**",
".git/**",
// TODO outputDir
// this.outputDir,
],
}),
);
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
let entries = new Set(
this.fileIgnores.map((ignore) => TemplatePath.stripLeadingDotSlash(ignore)),
);
for (let ignore of this.config.watchIgnores) {
entries.add(this.normalizeIgnoreEntry(ignore));
}
// de-duplicated
return Array.from(entries);
}
getIncludesAndDataDirs() {
let rawPaths = new Set();
rawPaths.add(this.includesDir);
if (this.layoutsDir) {
rawPaths.add(this.layoutsDir);
}
rawPaths.add(this.dataDir);
return Array.from(rawPaths)
.filter((entry) => {
// never ignore the input directory (even if config file returns "" for these)
return entry && entry !== this.inputDir;
})
.map((entry) => {
return TemplateGlob.map(entry + "**");
});
}
}