Skip to content

Commit

Permalink
Implemented #520
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 4, 2023
1 parent 70b9ee3 commit 421c399
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
1 change: 1 addition & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- Use the lib `dom` and `dom.iterable` types instead of `deno-dom`.
- Removed `lume/core/utils.ts` and moved all utilities to different files under
`/lume/core/utils/` folder.
- Allow to copy files/directories inside ignored directories #520

## `search` Plugin

Expand Down
85 changes: 50 additions & 35 deletions core/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,32 +245,26 @@ export default class Source {
if (this.staticPaths.has(entry.path)) {
const { dest, dirOnly } = this.staticPaths.get(entry.path)!;

if (entry.type === "file") {
if (dirOnly) {
continue;
}
staticFiles.push({
entry,
outputPath: getOutputPath(entry, path, dest),
});
continue;
}

staticFiles.push(...this.#getStaticFiles(
entry,
typeof dest === "string" ? dest : posix.join(path, entry.name),
typeof dest === "function" ? dest : undefined,
));
staticFiles.push(...this.#getStaticFiles(path, entry, dest, dirOnly));
continue;
}

// Ignore .filename and _filename
if (entry.name.startsWith(".") || entry.name.startsWith("_")) {
continue;
}

// Check if the file should be ignored
if (this.ignored.has(entry.path)) {
// Check if the entry should be ignored
if (
entry.name.startsWith(".") || entry.name.startsWith("_") ||
this.ignored.has(entry.path)
) {
for (const [staticSrc, { dest, dirOnly }] of this.staticPaths) {
if (staticSrc.startsWith(entry.path)) {
const staticEntry = this.fs.entries.get(staticSrc)!;
const staticPath = posix.dirname(
posix.join(path, staticEntry.path.slice(entry.path.length)),
);
staticFiles.push(
...this.#getStaticFiles(staticPath, staticEntry, dest, dirOnly),
);
}
}
continue;
}

Expand All @@ -288,29 +282,27 @@ export default class Source {
const dest = this.copyRemainingFiles(entry.path);

if (dest) {
staticFiles.push({
entry,
outputPath: getOutputPath(
entry,
staticFiles.push(
...this.#getStaticFiles(
path,
entry,
typeof dest === "string" ? dest : undefined,
),
});
);
}
}
continue;
}

// The file is a static file
if (format.copy) {
staticFiles.push({
entry,
outputPath: getOutputPath(
entry,
staticFiles.push(
...this.#getStaticFiles(
path,
entry,
typeof format.copy === "function" ? format.copy : undefined,
),
});
);
continue;
}

Expand Down Expand Up @@ -404,7 +396,7 @@ export default class Source {
}

/** Scan the static files in a directory */
*#getStaticFiles(
*#scanStaticFiles(
dirEntry: Entry,
destPath: string,
destFn?: (file: string) => string,
Expand All @@ -429,14 +421,37 @@ export default class Source {
}

if (entry.type === "directory") {
yield* this.#getStaticFiles(
yield* this.#scanStaticFiles(
entry,
posix.join(destPath, entry.name),
destFn,
);
}
}
}

#getStaticFiles(
path: string,
entry: Entry,
dest: string | ((path: string) => string) | undefined,
dirOnly = false,
): StaticFile[] {
if (entry.type === "file") {
if (!dirOnly) {
return [{
entry,
outputPath: getOutputPath(entry, path, dest),
}];
}
return [];
}

return Array.from(this.#scanStaticFiles(
entry,
typeof dest === "string" ? dest : posix.join(path, entry.name),
typeof dest === "function" ? dest : undefined,
));
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/__snapshots__/static_files.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ snapshot[`Copy static files 1`] = `
src: [
"/",
"/_headers",
"/_static",
"/_static/inner",
"/_static/inner/yes.txt",
"/four.no",
"/one.yes",
"/other",
Expand Down Expand Up @@ -170,6 +173,11 @@ snapshot[`Copy static files 2`] = `
flags: [],
outputPath: "/_headers",
},
{
entry: "/_static/inner/yes.txt",
flags: [],
outputPath: "/inner/yes.txt",
},
{
entry: "/other/one",
flags: [],
Expand Down
1 change: 1 addition & 0 deletions tests/assets/static_files/_static/inner/yes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yes
3 changes: 3 additions & 0 deletions tests/static_files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Deno.test("Copy static files", async (t) => {
// not copied because of the trailing slash
site.copy("three.no/");

// Copy a directory inside a ignored directory
site.copy("_static/inner", "inner");

await build(site);
await assertSiteSnapshot(t, site);
});

0 comments on commit 421c399

Please sign in to comment.