Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Site.copy doing nothing for trailing slashes #426

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project try to adheres to [Semantic Versioning](https://semver.org/),
but not always is possible (due the use of unstable features from Deno).
Any BREAKING CHANGE between minor versions will be documented here in upper case.

## [Unreleased]

### Fixed

- `Site.copy` now works as expected when given a path with a trailing slash. [#426]

## [1.17.4] - 2023-05-25
### Added
- The env variable `LUME_ENV=development` is created when `deno task lume --dev`.
Expand Down Expand Up @@ -2254,6 +2260,7 @@ The first version.
[#417]: https://github.com/lumeland/lume/issues/417
[#418]: https://github.com/lumeland/lume/issues/418
[#419]: https://github.com/lumeland/lume/issues/419
[#426]: https://github.com/lumeland/lume/pull/426

[1.17.4]: https://github.com/lumeland/lume/compare/v1.17.3...v1.17.4
[1.17.3]: https://github.com/lumeland/lume/compare/v1.17.2...v1.17.3
Expand Down
14 changes: 10 additions & 4 deletions core/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class Source {
/** List of static files and folders to copy */
staticPaths = new Map<
string,
string | ((path: string) => string) | undefined
{ dest: string | ((path: string) => string) | undefined; dirOnly: boolean }
>();

/** List of static files and folders to copy */
Expand Down Expand Up @@ -103,8 +103,11 @@ export default class Source {

addStaticPath(from: string, to?: string | ((path: string) => string)) {
this.staticPaths.set(
normalizePath(from),
typeof to === "string" ? normalizePath(to) : to,
normalizePath(from.replace(/\/$/, "")),
{
dest: typeof to === "string" ? normalizePath(to) : to,
dirOnly: from.endsWith("/"),
},
);
}

Expand Down Expand Up @@ -190,9 +193,12 @@ export default class Source {

// Static files
if (this.staticPaths.has(entry.path)) {
const dest = this.staticPaths.get(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),
Expand Down
10 changes: 10 additions & 0 deletions tests/__snapshots__/static_files.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ snapshot[`Copy static files 2`] = `
flags: [],
outputPath: "/other/two",
},
{
entry: "/other2/one",
flags: [],
outputPath: "/other2/one",
},
{
entry: "/other2/two",
flags: [],
outputPath: "/other2/two",
},
{
entry: "/posts/2022-01-01_first-post/assets/inner/inner2/styles.scss",
flags: [],
Expand Down
1 change: 1 addition & 0 deletions tests/assets/static_files/other2/one
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content
1 change: 1 addition & 0 deletions tests/assets/static_files/other2/two
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content
6 changes: 3 additions & 3 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ Deno.test("static files configuration", () => {
site.copy("img");
equals(staticPaths.size, 1);
equals(staticPaths.has("/img"), true);
equals(staticPaths.get("/img"), undefined);
equals(staticPaths.get("/img")!.dest, undefined);

site.copy("statics/favicon.ico", "favicon.ico");
equals(staticPaths.size, 2);
equals(
staticPaths.get("/statics/favicon.ico"),
staticPaths.get("/statics/favicon.ico")!.dest,
"/favicon.ico",
);

site.copy("css", ".");
equals(staticPaths.size, 3);
equals(staticPaths.get("/css"), "/");
equals(staticPaths.get("/css")!.dest, "/");
});

Deno.test("ignored files configuration", () => {
Expand Down
6 changes: 6 additions & 0 deletions tests/static_files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Deno.test("Copy static files", async (t) => {
(file) => "/subdir" + file.replace(/\.copy2/, ".copy3"),
);

// copied with the trailing slash
site.copy("other2/");

// not copied because of the trailing slash
site.copy("three.no/");

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