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

feat: --[no]-trailing-slash CLI flags #806

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions source/utilities/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions source/utilities/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions tests/__fixtures__/config/custom/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"public": "app/",
"trailingSlash": true,
"renderSingle": true
}
2 changes: 2 additions & 0 deletions tests/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
`;

Expand Down
34 changes: 34 additions & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down