diff --git a/CHANGELOG.md b/CHANGELOG.md index ad8fa2fa..83bbc211 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. @@ -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 diff --git a/core/source.ts b/core/source.ts index aaeeebb3..864e615d 100644 --- a/core/source.ts +++ b/core/source.ts @@ -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 */ @@ -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("/"), + }, ); } @@ -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), diff --git a/tests/__snapshots__/static_files.test.ts.snap b/tests/__snapshots__/static_files.test.ts.snap index bfa9e483..19db3953 100644 --- a/tests/__snapshots__/static_files.test.ts.snap +++ b/tests/__snapshots__/static_files.test.ts.snap @@ -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: [], diff --git a/tests/assets/static_files/other2/one b/tests/assets/static_files/other2/one new file mode 100644 index 00000000..6b584e8e --- /dev/null +++ b/tests/assets/static_files/other2/one @@ -0,0 +1 @@ +content \ No newline at end of file diff --git a/tests/assets/static_files/other2/two b/tests/assets/static_files/other2/two new file mode 100644 index 00000000..6b584e8e --- /dev/null +++ b/tests/assets/static_files/other2/two @@ -0,0 +1 @@ +content \ No newline at end of file diff --git a/tests/config.test.ts b/tests/config.test.ts index 1f17ef00..f9600c79 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -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", () => { diff --git a/tests/static_files.test.ts b/tests/static_files.test.ts index 27f653f5..cebec113 100644 --- a/tests/static_files.test.ts +++ b/tests/static_files.test.ts @@ -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); });