Skip to content

Commit

Permalink
[fix] Process symlinked routes (#4957)
Browse files Browse the repository at this point in the history
* [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 <mrkishi@users.noreply.github.com>

* reword sym-linked to symlinked

* skip symlinks tests when unsupported

Co-authored-by: Maurício Kishi <mrkishi@users.noreply.github.com>
Co-authored-by: mrkishi <mauriciokishi@gmail.com>
  • Loading branch information
3 people committed May 23, 2022
1 parent e1cff24 commit eb2ab91
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-cobras-switch.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow symlinked directories in the routes folder
12 changes: 7 additions & 5 deletions packages/kit/src/core/sync/create_manifest_data/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ function count_occurrences(needle, haystack) {
* @param {string[]} [files] * @param {string[]} [files]
*/ */
function list_files(dir, path = '', files = []) { function list_files(dir, path = '', files = []) {
fs.readdirSync(dir, { withFileTypes: true }) fs.readdirSync(dir)
.sort(({ name: a }, { name: b }) => { .sort((a, b) => {
// sort each directory in (__layout, __error, everything else) order // sort each directory in (__layout, __error, everything else) order
// so that we can trace layouts/errors immediately // so that we can trace layouts/errors immediately


Expand All @@ -444,10 +444,12 @@ function list_files(dir, path = '', files = []) {
return a < b ? -1 : 1; return a < b ? -1 : 1;
}) })
.forEach((file) => { .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()) { if (stats.isDirectory()) {
list_files(`${dir}/${file.name}`, joined, files); list_files(full, joined, files);
} else { } else {
files.push(joined); files.push(joined);
} }
Expand Down
38 changes: 38 additions & 0 deletions packages/kit/src/core/sync/create_manifest_data/index.spec.js
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import path from 'path'; import path from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { test } from 'uvu'; import { test } from 'uvu';
Expand Down Expand Up @@ -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', () => { test('creates routes with layout', () => {
const { components, routes } = create('samples/basic-layout'); const { components, routes } = create('samples/basic-layout');


Expand Down
Empty file.
Empty file.

0 comments on commit eb2ab91

Please sign in to comment.