From eb2ab91338e10d081191f493e3e75abd6ad760d2 Mon Sep 17 00:00:00 2001 From: Mike Nikles <788827+mikenikles@users.noreply.github.com> Date: Mon, 23 May 2022 08:00:34 -0700 Subject: [PATCH] [fix] Process symlinked routes (#4957) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [fix] Process sym-linked routes * Revert to fs.statSync * Update packages/kit/src/core/sync/create_manifest_data/index.js Co-authored-by: MaurĂ­cio Kishi * reword sym-linked to symlinked * skip symlinks tests when unsupported Co-authored-by: MaurĂ­cio Kishi Co-authored-by: mrkishi --- .changeset/few-cobras-switch.md | 5 +++ .../core/sync/create_manifest_data/index.js | 12 +++--- .../sync/create_manifest_data/index.spec.js | 38 +++++++++++++++++++ .../test/samples/symlinks/bar/index.svelte | 0 .../test/samples/symlinks/routes/foo | 1 + .../test/samples/symlinks/routes/index.svelte | 0 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 .changeset/few-cobras-switch.md create mode 100644 packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/bar/index.svelte create mode 120000 packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/foo create mode 100644 packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/index.svelte diff --git a/.changeset/few-cobras-switch.md b/.changeset/few-cobras-switch.md new file mode 100644 index 000000000000..c619a06410cf --- /dev/null +++ b/.changeset/few-cobras-switch.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Allow symlinked directories in the routes folder diff --git a/packages/kit/src/core/sync/create_manifest_data/index.js b/packages/kit/src/core/sync/create_manifest_data/index.js index bd4f3c296a98..76d555072a67 100644 --- a/packages/kit/src/core/sync/create_manifest_data/index.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.js @@ -426,8 +426,8 @@ function count_occurrences(needle, haystack) { * @param {string[]} [files] */ function list_files(dir, path = '', files = []) { - fs.readdirSync(dir, { withFileTypes: true }) - .sort(({ name: a }, { name: b }) => { + fs.readdirSync(dir) + .sort((a, b) => { // sort each directory in (__layout, __error, everything else) order // so that we can trace layouts/errors immediately @@ -444,10 +444,12 @@ function list_files(dir, path = '', files = []) { return a < b ? -1 : 1; }) .forEach((file) => { - const joined = path ? `${path}/${file.name}` : file.name; + const full = `${dir}/${file}`; + const stats = fs.statSync(full); + const joined = path ? `${path}/${file}` : file; - if (file.isDirectory()) { - list_files(`${dir}/${file.name}`, joined, files); + if (stats.isDirectory()) { + list_files(full, joined, files); } else { files.push(joined); } diff --git a/packages/kit/src/core/sync/create_manifest_data/index.spec.js b/packages/kit/src/core/sync/create_manifest_data/index.spec.js index ac197bee9e3a..d7b06bf71fe9 100644 --- a/packages/kit/src/core/sync/create_manifest_data/index.spec.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.spec.js @@ -1,3 +1,4 @@ +import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { test } from 'uvu'; @@ -95,6 +96,43 @@ test('creates routes', () => { ]); }); +const symlink_survived_git = fs + .statSync(path.join(cwd, 'samples/symlinks/routes/foo')) + .isSymbolicLink(); + +const test_symlinks = symlink_survived_git ? test : test.skip; + +test_symlinks('creates symlinked routes', () => { + const { components, routes } = create('samples/symlinks/routes'); + + const index = 'samples/symlinks/routes/index.svelte'; + const symlinked_index = 'samples/symlinks/routes/foo/index.svelte'; + + assert.equal(components, [default_layout, default_error, symlinked_index, index]); + + assert.equal(routes, [ + { + type: 'page', + id: '', + pattern: /^\/$/, + path: '/', + shadow: null, + a: [default_layout, index], + b: [default_error] + }, + + { + type: 'page', + id: 'foo', + pattern: /^\/foo\/?$/, + path: '/foo', + shadow: null, + a: [default_layout, symlinked_index], + b: [default_error] + } + ]); +}); + test('creates routes with layout', () => { const { components, routes } = create('samples/basic-layout'); diff --git a/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/bar/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/bar/index.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/foo b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/foo new file mode 120000 index 000000000000..79264abecd10 --- /dev/null +++ b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/foo @@ -0,0 +1 @@ +../bar \ No newline at end of file diff --git a/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/index.svelte new file mode 100644 index 000000000000..e69de29bb2d1