Skip to content

Commit

Permalink
fix scope
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed May 4, 2023
1 parent 71006d0 commit da9b70d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
15 changes: 9 additions & 6 deletions core/scopes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Page } from "../core.ts";
import type { Entry } from "../core.ts";

/**
* Define independent updates scopes
Expand All @@ -8,7 +8,7 @@ export default class Scopes {
scopes = new Set<ScopeFilter>();

/** Returns a function to filter the pages that must be rebuild */
getFilter(changedFiles: Iterable<string>): (page: Page) => boolean {
getFilter(changedFiles: Iterable<string>): (entry: Entry) => boolean {
// There's no any scope, so rebuild all pages
if (this.scopes.size === 0) {
return () => true;
Expand Down Expand Up @@ -42,18 +42,21 @@ export default class Scopes {
}

// Generate the filter function
return function (page) {
const path = page.src.path + page.src.ext;
return function (entry) {
// Ignore directories
if (entry.type === "directory") {
return false;
}

// It matches with any scope that has changed
for (const scopeFn of changed) {
if (scopeFn(path)) {
if (scopeFn(entry.path)) {
return true;
}
}

// It's not scoped
return noScoped && notChanged.every((scopeFn) => !scopeFn(path));
return noScoped && notChanged.every((scopeFn) => !scopeFn(entry.path));
};
}
}
Expand Down
8 changes: 5 additions & 3 deletions core/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export default class Site {
const [_pages, _staticFiles] = await this.source.build(
this.globalComponents,
[
(page) => !page.data.draft || this.options.dev,
(_, page) => !page?.data.draft || this.options.dev,
],
);

Expand Down Expand Up @@ -558,7 +558,7 @@ export default class Site {
const [_pages, _staticFiles] = await this.source.build(
this.globalComponents,
[
(page) => !page.data.draft || this.options.dev,
(_, page) => !page?.data.draft || this.options.dev,
this.scopes.getFilter(files),
],
);
Expand Down Expand Up @@ -664,7 +664,9 @@ export default class Site {
const [pages] = await this.source.build(
this.globalComponents,
[
(page) => page.src.path + page.src.ext === file,
(entry) =>
(entry.type === "directory" && file.startsWith(entry.path)) ||
entry.path === file,
],
);

Expand Down
23 changes: 19 additions & 4 deletions core/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ export default class Source {

async build(
globalComponents: Components,
pageFilters: ((page: Page) => boolean)[],
buildFilters: BuildFilter[],
): Promise<[Page[], StaticFile[]]> {
const pages: Page[] = [];
const staticFiles: StaticFile[] = [];

await this.#build(
buildFilters,
this.fs.entries.get("/")!,
"/",
globalComponents,
Expand All @@ -125,21 +126,24 @@ export default class Source {
);

return [
pages.filter((
page,
) => pageFilters.every((filter) => filter(page))),
pages,
staticFiles,
];
}

async #build(
buildFilters: BuildFilter[],
dir: Entry,
path: string,
parentComponents: Components,
parentData: Data,
pages: Page[],
staticFiles: StaticFile[],
) {
if (buildFilters.some((filter) => !filter(dir))) {
return;
}

// Parse the date/time in the folder name
const [name, date] = parseDate(dir.name);
path = posix.join(path, name);
Expand Down Expand Up @@ -180,6 +184,10 @@ export default class Source {

// Load the pages and static files
for (const entry of dir.children.values()) {
if (buildFilters.some((filter) => !filter(entry))) {
continue;
}

// Static files
if (this.staticPaths.has(entry.path)) {
const dest = this.staticPaths.get(entry.path);
Expand Down Expand Up @@ -280,6 +288,10 @@ export default class Source {
page.data.date = getDate(page.data.date, entry);
page.data.page = page;

if (buildFilters.some((filter) => !filter(entry, page))) {
continue;
}

pages.push(page);
continue;
}
Expand All @@ -288,6 +300,7 @@ export default class Source {
// Load recursively the directory
if (entry.type === "directory") {
await this.#build(
buildFilters,
entry,
path,
parentComponents,
Expand Down Expand Up @@ -413,6 +426,8 @@ function toProxy(
}) as unknown as ProxyComponents;
}

export type BuildFilter = (entry: Entry, page?: Page) => boolean;

export type ComponentFunction = (props: Record<string, unknown>) => string;

export interface ProxyComponents {
Expand Down
64 changes: 34 additions & 30 deletions tests/core/scopes.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { assertStrictEquals as equals } from "../../deps/assert.ts";
import Scopes from "../../core/scopes.ts";
import { Page } from "../../core/filesystem.ts";
import FS from "../../core/fs.ts";

Deno.test("Scripts", async (t) => {
const scopes = new Scopes();

equals(scopes.scopes.size, 0);

const pages: Page[] = [
new Page({ path: "file1", ext: ".foo" }),
new Page({ path: "file2", ext: ".bar" }),
new Page({ path: "file3", ext: ".css" }),
new Page({ path: "file4", ext: ".html" }),
];
const fs = new FS({
root: "/",
});

fs.addEntry({ path: "/file1.foo", type: "file" });
fs.addEntry({ path: "/file2.bar", type: "file" });
fs.addEntry({ path: "/file3.css", type: "file" });
fs.addEntry({ path: "/file4.html", type: "file" });

const entries = Array.from(fs.entries.values());

await t.step("Add scopes", () => {
scopes.scopes.add((path: string) => path.endsWith(".foo"));
Expand All @@ -24,48 +28,48 @@ Deno.test("Scripts", async (t) => {

await t.step("Check scoped changes", () => {
const filter = scopes.getFilter([
"file1.foo",
"file3.foo",
"/file1.foo",
"/file3.foo",
]);

const filteredPages = pages.filter(filter);
equals(filteredPages.length, 1);
equals(filteredPages[0].src.path, "file1");
const filteredEntries = entries.filter(filter);
equals(filteredEntries.length, 1);
equals(filteredEntries[0].path, "/file1.foo");
});

await t.step("Check 2 scoped changes", () => {
const filter = scopes.getFilter([
"file1.foo",
"file2.bar",
"/file1.foo",
"/file2.bar",
]);

const filteredPages = pages.filter(filter);
equals(filteredPages.length, 2);
equals(filteredPages[0].src.path, "file1");
equals(filteredPages[1].src.path, "file2");
const filteredEntries = entries.filter(filter);
equals(filteredEntries.length, 2);
equals(filteredEntries[0].path, "/file1.foo");
equals(filteredEntries[1].path, "/file2.bar");
});

await t.step("Check unscoped changes", () => {
const filter = scopes.getFilter([
"file3.css",
"/file3.css",
]);

const filteredPages = pages.filter(filter);
equals(filteredPages.length, 2);
equals(filteredPages[0].src.path, "file3");
equals(filteredPages[1].src.path, "file4");
const filteredEntries = entries.filter(filter);
equals(filteredEntries.length, 2);
equals(filteredEntries[0].path, "/file3.css");
equals(filteredEntries[1].path, "/file4.html");
});

await t.step("Check scoped and unscoped changes", () => {
const filter = scopes.getFilter([
"file3.css",
"file1.foo",
"/file3.css",
"/file1.foo",
]);

const filteredPages = pages.filter(filter);
equals(filteredPages.length, 3);
equals(filteredPages[0].src.path, "file1");
equals(filteredPages[1].src.path, "file3");
equals(filteredPages[2].src.path, "file4");
const filteredEntries = entries.filter(filter);
equals(filteredEntries.length, 3);
equals(filteredEntries[0].path, "/file1.foo");
equals(filteredEntries[1].path, "/file3.css");
equals(filteredEntries[2].path, "/file4.html");
});
});

0 comments on commit da9b70d

Please sign in to comment.