From f8fbe84817e95347061cf1dc3f900752329ce64e Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Mon, 17 Jun 2024 17:05:21 -0700 Subject: [PATCH 1/2] add flag and fix existing tests --- source/types.ts | 2 ++ source/utilities/cli.ts | 4 ++++ source/utilities/config.ts | 7 +++++++ tests/__snapshots__/cli.test.ts.snap | 2 ++ 4 files changed, 15 insertions(+) diff --git a/source/types.ts b/source/types.ts index 9294d7c3..13a1d913 100644 --- a/source/types.ts +++ b/source/types.ts @@ -79,6 +79,8 @@ export declare interface Options { '--no-clipboard': boolean; '--no-compression': boolean; '--no-etag': boolean; + '--trailing-slash': boolean; + '--no-trailing-slash': boolean; '--symlinks': boolean; '--cors': boolean; '--no-port-switching': boolean; diff --git a/source/utilities/cli.ts b/source/utilities/cli.ts index 726c5268..d9ae3750 100644 --- a/source/utilities/cli.ts +++ b/source/utilities/cli.ts @@ -54,6 +54,8 @@ const helpText = chalkTemplate` --no-etag Send \`Last-Modified\` header instead of \`ETag\` + --[no]-trailing-slash Remove or add trailing slashes to all paths + -S, --symlinks Resolve symlinks instead of showing 404 errors --ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS @@ -153,6 +155,8 @@ const options = { '--no-clipboard': Boolean, '--no-compression': Boolean, '--no-etag': Boolean, + '--trailing-slash': Boolean, + '--no-trailing-slash': Boolean, '--symlinks': Boolean, '--cors': Boolean, '--no-port-switching': Boolean, diff --git a/source/utilities/config.ts b/source/utilities/config.ts index db2e3c85..d74a55ab 100644 --- a/source/utilities/config.ts +++ b/source/utilities/config.ts @@ -139,6 +139,13 @@ export const loadConfiguration = async ( // Configure defaults based on the options the user has passed. config.etag = !args['--no-etag']; config.symlinks = args['--symlinks'] || config.symlinks; + const trailingSlash = args['--trailing-slash']; + const noTrailingSlash = args['--no-trailing-slash']; + if (trailingSlash !== undefined) { + config.trailingSlash = trailingSlash; + } else if (noTrailingSlash !== undefined) { + config.trailingSlash = !noTrailingSlash; + } return config; }; diff --git a/tests/__snapshots__/cli.test.ts.snap b/tests/__snapshots__/cli.test.ts.snap index 87996f27..1b30bc85 100644 --- a/tests/__snapshots__/cli.test.ts.snap +++ b/tests/__snapshots__/cli.test.ts.snap @@ -43,6 +43,8 @@ exports[`utilities/cli > render help text 1`] = ` --no-etag Send \`Last-Modified\` header instead of \`ETag\` + --[no]-trailing-slash Remove or add trailing slashes to all paths + -S, --symlinks Resolve symlinks instead of showing 404 errors --ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS From 929af801a1a19004e557e207df48cbc017a9a2ff Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Mon, 17 Jun 2024 17:23:01 -0700 Subject: [PATCH 2/2] add new tests --- tests/__fixtures__/config/custom/config.json | 1 + tests/__snapshots__/config.test.ts.snap | 1 + tests/config.test.ts | 34 ++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/tests/__fixtures__/config/custom/config.json b/tests/__fixtures__/config/custom/config.json index 5df3e5ec..7a053db8 100644 --- a/tests/__fixtures__/config/custom/config.json +++ b/tests/__fixtures__/config/custom/config.json @@ -1,4 +1,5 @@ { "public": "app/", + "trailingSlash": true, "renderSingle": true } diff --git a/tests/__snapshots__/config.test.ts.snap b/tests/__snapshots__/config.test.ts.snap index d1972f53..5a960d9a 100644 --- a/tests/__snapshots__/config.test.ts.snap +++ b/tests/__snapshots__/config.test.ts.snap @@ -15,6 +15,7 @@ exports[`utilities/config > parse valid config at custom location 1`] = ` "public": "tests/__fixtures__/config/custom/app", "renderSingle": true, "symlinks": undefined, + "trailingSlash": true, } `; diff --git a/tests/config.test.ts b/tests/config.test.ts index 3240d1f0..31df145e 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -52,6 +52,40 @@ describe('utilities/config', () => { expect(configuration).toMatchSnapshot(); }); + // The `--trailing-slash` flag should set `trailingSlash` to `true`. + test('sets `trailingSlash` to `true` when --trailing-slash flag is set', async () => { + const configuration = await loadConfiguration( + process.cwd(), + process.cwd(), + { + '--trailing-slash': true, + }, + ); + expect(configuration.trailingSlash).toBe(true); + }); + + // The `--no-trailing-slash` flag should set `trailingSlash` to `false`. + test('sets `trailingSlash` to `false` when --no-trailing-slash flag is set', async () => { + const configuration = await loadConfiguration( + process.cwd(), + process.cwd(), + { + '--no-trailing-slash': true, + }, + ); + expect(configuration.trailingSlash).toBe(false); + }); + + // When `--[no]-trailing-slash` is not specified, `trailingSlash` should be `undefined`. + test('sets `trailingSlash` to `undefined` when trailing slash flags are omitted', async () => { + const configuration = await loadConfiguration( + process.cwd(), + process.cwd(), + {}, + ); + expect(configuration.trailingSlash).toBeUndefined(); + }); + // When the configuration source is deprecated, i.e., the configuration lives // in `now.json` or `package.json`, a warning should be printed. test('warn when configuration comes from a deprecated source', async () => {