Skip to content

Commit

Permalink
Adds afterBuild/beforeBuild events, adds argument to beforeWatch even…
Browse files Browse the repository at this point in the history
…t to pass in the files that changed.
  • Loading branch information
zachleat committed Oct 22, 2020
1 parent 36c5022 commit e9181d3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
},
"dependencies": {
"@11ty/dependency-tree": "^1.0.0",
"browser-sync": "^2.26.7",
"browser-sync": "^2.26.13",
"chalk": "^3.0.0",
"chokidar": "^3.4.0",
"debug": "^4.1.1",
Expand Down
7 changes: 5 additions & 2 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,10 @@ Arguments:
return;
}

this.config.events.emit("beforeWatch");

this.watchManager.setBuildRunning();

this.config.events.emit("beforeWatch", this.watchManager.getActiveQueue());

// reset and reload global configuration :O
if (this.watchManager.hasQueuedFile(config.getLocalProjectConfigFile())) {
this.resetConfig();
Expand Down Expand Up @@ -735,10 +735,13 @@ Arguments:
EleventyErrorHandler.logger = this.logger;
}

this.config.events.emit("beforeBuild");

try {
let promise = this.writer.write();

ret = await promise;
this.config.events.emit("afterBuild");
} catch (e) {
EleventyErrorHandler.initialMessage(
"Problem writing Eleventy templates",
Expand Down
19 changes: 18 additions & 1 deletion src/EleventyWatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ class EleventyWatch {
return this.activeQueue[0];
}

/* Returns the changed files currently being operated on in the current `watch` build
* Works with or without incremental (though in incremental only one file per time will be processed)
*/
getActiveQueue() {
if (!this.isActive) {
return [];
} else if (this.incremental) {
return [this.activeQueue[0]];
}

return this.activeQueue;
}

_queueMatches(file) {
let filterCallback;
if (typeof file === "function") {
filterCallback = file;
} else {
filterCallback = path => path === file;
filterCallback = (path) => path === file;
}

return this.activeQueue.filter(filterCallback);
Expand Down Expand Up @@ -80,6 +93,10 @@ class EleventyWatch {
return this.pendingQueue.length;
}

getPendingQueue() {
return this.pendingQueue;
}

getActiveQueueSize() {
return this.activeQueue.length;
}
Expand Down
10 changes: 7 additions & 3 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class UserConfig {
}
}

// Duplicate event bindings are avoided with the `reset` method above.
// A new EventEmitter instance is created when the config is reset.
on(eventName, callback) {
return this.events.on(eventName, callback);
}
Expand Down Expand Up @@ -325,7 +327,9 @@ class UserConfig {

_normalizeTemplateFormats(templateFormats) {
if (typeof templateFormats === "string") {
templateFormats = templateFormats.split(",").map(format => format.trim());
templateFormats = templateFormats
.split(",")
.map((format) => format.trim());
}
return templateFormats;
}
Expand Down Expand Up @@ -629,7 +633,7 @@ class UserConfig {
Object.assign(
{
key: fileExtension,
extension: fileExtension
extension: fileExtension,
},
options
)
Expand Down Expand Up @@ -680,7 +684,7 @@ class UserConfig {
dataExtensions: this.dataExtensions,
extensionMap: this.extensionMap,
quietMode: this.quietMode,
events: this.events
events: this.events,
};
}
}
Expand Down
62 changes: 46 additions & 16 deletions test/EleventyWatchTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from "ava";
import EleventyWatch from "../src/EleventyWatch";

test("Standard", t => {
test("Standard", (t) => {
let watch = new EleventyWatch();
t.is(watch.isBuildRunning(), false);

Expand All @@ -12,132 +12,157 @@ test("Standard", t => {
t.is(watch.isBuildRunning(), false);
});

test("Incremental", t => {
test("Incremental", (t) => {
let watch = new EleventyWatch();
t.is(watch.getIncrementalFile(), false);

watch.incremental = true;
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.addToPendingQueue("test.md");
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.setBuildRunning();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 1);
t.is(watch.getIncrementalFile(), "./test.md");
t.deepEqual(watch.getActiveQueue(), ["./test.md"]);

watch.setBuildFinished();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);
t.deepEqual(watch.getPendingQueue(), []);
});

test("Incremental queue 2", t => {
test("Incremental queue 2", (t) => {
let watch = new EleventyWatch();
t.is(watch.getIncrementalFile(), false);

watch.incremental = true;
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.addToPendingQueue("test.md");
watch.addToPendingQueue("test2.md");
t.is(watch.getPendingQueueSize(), 2);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.setBuildRunning();
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 1);
t.is(watch.getIncrementalFile(), "./test.md");
t.deepEqual(watch.getActiveQueue(), ["./test.md"]);

watch.setBuildFinished();
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getPendingQueue(), ["./test2.md"]);
t.deepEqual(watch.getActiveQueue(), []);
});

test("Incremental add while active", t => {
test("Incremental add while active", (t) => {
let watch = new EleventyWatch();
t.is(watch.getIncrementalFile(), false);

watch.incremental = true;
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.addToPendingQueue("test.md");
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.setBuildRunning();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 1);
t.is(watch.getIncrementalFile(), "./test.md");
t.deepEqual(watch.getActiveQueue(), ["./test.md"]);

watch.addToPendingQueue("test2.md");
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 1);
t.is(watch.getIncrementalFile(), "./test.md");
t.deepEqual(watch.getActiveQueue(), ["./test.md"]);

watch.setBuildFinished();
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getPendingQueue(), ["./test2.md"]);
t.deepEqual(watch.getActiveQueue(), []);
});

test("Non-incremental", t => {
test("Non-incremental", (t) => {
let watch = new EleventyWatch();
t.is(watch.getIncrementalFile(), false);

t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.addToPendingQueue("test.md");
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.setBuildRunning();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 1);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), ["./test.md"]);

watch.setBuildFinished();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);
});

test("Non-incremental queue 2", t => {
test("Non-incremental queue 2", (t) => {
let watch = new EleventyWatch();
t.is(watch.getIncrementalFile(), false);

t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.addToPendingQueue("test.md");
watch.addToPendingQueue("test2.md");
t.is(watch.getPendingQueueSize(), 2);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.setBuildRunning();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 2);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), ["./test.md", "./test2.md"]);

watch.setBuildFinished();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getPendingQueue(), []);
t.deepEqual(watch.getActiveQueue(), []);
});

test("Non-incremental add while active", t => {
test("Non-incremental add while active", (t) => {
let watch = new EleventyWatch();
t.is(watch.getIncrementalFile(), false);

Expand All @@ -148,42 +173,47 @@ test("Non-incremental add while active", t => {
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), []);

watch.setBuildRunning();
t.is(watch.getPendingQueueSize(), 0);
t.is(watch.getActiveQueueSize(), 1);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), ["./test.md"]);

watch.addToPendingQueue("test.md");
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 1);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getActiveQueue(), ["./test.md"]);

watch.setBuildFinished();
t.is(watch.getPendingQueueSize(), 1);
t.is(watch.getActiveQueueSize(), 0);
t.is(watch.getIncrementalFile(), false);
t.deepEqual(watch.getPendingQueue(), ["./test.md"]);
t.deepEqual(watch.getActiveQueue(), []);
});

test("Active queue tests", t => {
test("Active queue tests", (t) => {
let watch = new EleventyWatch();
watch.addToPendingQueue("test.md");
watch.addToPendingQueue("test2.md");
watch.addToPendingQueue("test.css");

t.is(
watch.hasAllQueueFiles(path => path.startsWith("./test")),
watch.hasAllQueueFiles((path) => path.startsWith("./test")),
false
);

watch.setBuildRunning();
t.is(watch.hasAllQueueFiles("slkdjflkjsdlkfj"), false);
t.is(
watch.hasAllQueueFiles(path => path.startsWith("./test")),
watch.hasAllQueueFiles((path) => path.startsWith("./test")),
true
);
t.is(
watch.hasAllQueueFiles(path => path.endsWith(".css")),
watch.hasAllQueueFiles((path) => path.endsWith(".css")),
false
);

Expand All @@ -192,31 +222,31 @@ test("Active queue tests", t => {
watch.setBuildFinished();

t.is(
watch.hasAllQueueFiles(path => path.startsWith("./test")),
watch.hasAllQueueFiles((path) => path.startsWith("./test")),
false
);
});

test("Active queue tests, all CSS files", t => {
test("Active queue tests, all CSS files", (t) => {
let watch = new EleventyWatch();
watch.addToPendingQueue("test.css");
watch.addToPendingQueue("test2.css");
watch.addToPendingQueue("test3.css");

t.is(
watch.hasAllQueueFiles(path => path.endsWith(".css")),
watch.hasAllQueueFiles((path) => path.endsWith(".css")),
false
);

watch.setBuildRunning();
t.is(
watch.hasAllQueueFiles(path => path.endsWith(".css")),
watch.hasAllQueueFiles((path) => path.endsWith(".css")),
true
);
watch.setBuildFinished();

t.is(
watch.hasAllQueueFiles(path => path.endsWith(".css")),
watch.hasAllQueueFiles((path) => path.endsWith(".css")),
false
);
});

0 comments on commit e9181d3

Please sign in to comment.