Skip to content

Commit

Permalink
feat: introduce strict mode (#66)
Browse files Browse the repository at this point in the history
address #57
  • Loading branch information
yesmeck authored Oct 20, 2023
1 parent 40cd22e commit 3324bfb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-ears-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"remix-routes": minor
---

feat: introduce strict mode
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export default function Component() {
## Command Line Options

- `-w`: Watch for changes and automatically rebuild.
- `-s`: Enale strict mode. In strict mode only routes that define `SearchParams` type are allowed to have query string.
- `-o`: Specify the output path for `remix-routes.d.ts`. Defaults to `./node_modules` if arg is not given.

## TypeScript Integration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ exports[`build v1 routes 1`] = `
type URLSearchParamsInit = string | string[][] | Record<string, string> | URLSearchParams;
// symbol won't be a key of SearchParams
type IsSearchParams<T> = symbol extends keyof T ? false : true;
type ExportedQuery<T> = IsSearchParams<T> extends true ? T : URLSearchParamsInit;
type ExportedQuery<T> = IsSearchParams<T> extends true ? T : URLSearchParamsInit;
export interface Routes {
Expand Down Expand Up @@ -229,7 +231,9 @@ exports[`build v2 routes 1`] = `
type URLSearchParamsInit = string | string[][] | Record<string, string> | URLSearchParams;
// symbol won't be a key of SearchParams
type IsSearchParams<T> = symbol extends keyof T ? false : true;
type ExportedQuery<T> = IsSearchParams<T> extends true ? T : URLSearchParamsInit;
type ExportedQuery<T> = IsSearchParams<T> extends true ? T : URLSearchParamsInit;
export interface Routes {
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-routes/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import * as path from 'path';
import { build } from '../cli';

test('build v1 routes', async () => {
await build(path.resolve(__dirname, '../../fixture/v1'), './node_modules');
await build(path.resolve(__dirname, '../../fixture/v1'), { outputDirPath: './node_modules', watch: false, strict: false });
expect(
fs.readFileSync(path.resolve(__dirname, '../../fixture/v1/node_modules/remix-routes.d.ts'), 'utf8'),
).toMatchSnapshot();
});

test('build v2 routes', async () => {
await build(path.resolve(__dirname, '../../fixture/v2'), './node_modules');
await build(path.resolve(__dirname, '../../fixture/v2'), { outputDirPath: './node_modules', watch: false, strict: false });
expect(
fs.readFileSync(path.resolve(__dirname, '../../fixture/v2/node_modules/remix-routes.d.ts'), 'utf8'),
).toMatchSnapshot();
Expand Down
26 changes: 17 additions & 9 deletions packages/remix-routes/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $ remix-routes
Options
--watch, -w Watch for routes changes
--strict, -s Enable strict mode
--outputDirPath, -o Specify the output path for "remix-routes.d.ts". Defaults to "./node_modules" if arg is not given.
`;

Expand All @@ -34,6 +35,10 @@ const cli = meow(helpText, {
type: 'boolean',
alias: 'w',
},
strict: {
type: 'boolean',
alias: 's',
},
outputDirPath: {
type: 'string',
alias: 'o',
Expand Down Expand Up @@ -83,26 +88,27 @@ async function buildHelpers(remixRoot: string): Promise<RoutesInfo> {
return routesInfo;
}

export async function build(remixRoot: string, outputDirPath: string) {
export async function build(remixRoot: string, flags: typeof cli.flags) {
const routesInfo = await buildHelpers(remixRoot);
generate(routesInfo, remixRoot, outputDirPath);
generate(routesInfo, remixRoot, flags);
}

function watch(remixRoot: string, outputDirPath: string) {
build(remixRoot, outputDirPath);
function watch(remixRoot: string, flags: typeof cli.flags) {
build(remixRoot, flags);
chokidar
.watch([
path.join(remixRoot, 'app/routes/**/*.{ts,tsx}'),
path.join(remixRoot, 'remix.config.js'),
])
.on('change', () => {
build(remixRoot, outputDirPath);
build(remixRoot, flags);
});
console.log('Watching for routes changes...');
}

function generate(routesInfo: RoutesInfo, remixRoot: string, outputDirPath: string) {
function generate(routesInfo: RoutesInfo, remixRoot: string, flags: typeof cli.flags) {
const tsCode = ejs.render(template, {
strictMode: flags.strict,
routes: Object.entries(routesInfo).map(([route, { fileName, params }]) => ({
route,
params,
Expand All @@ -112,7 +118,7 @@ function generate(routesInfo: RoutesInfo, remixRoot: string, outputDirPath: stri

const outputPath = path.join(
remixRoot,
outputDirPath,
flags.outputDirPath,
);

if (!fs.existsSync(outputPath)) {
Expand Down Expand Up @@ -141,10 +147,12 @@ if (require.main === module) {
(async function () {
const remixRoot = process.env.REMIX_ROOT || process.cwd();

console.log(cli.flags);

if (cli.flags.watch) {
watch(remixRoot, cli.flags.outputDirPath);
watch(remixRoot, cli.flags);
} else {
build(remixRoot, cli.flags.outputDirPath);
build(remixRoot, cli.flags);
}
})();
}
Expand Down
6 changes: 5 additions & 1 deletion packages/remix-routes/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ export const template = `declare module "remix-routes" {
type URLSearchParamsInit = string | string[][] | Record<string, string> | URLSearchParams;
// symbol won't be a key of SearchParams
type IsSearchParams<T> = symbol extends keyof T ? false : true;
type ExportedQuery<T> = IsSearchParams<T> extends true ? T : URLSearchParamsInit;
<% if (strictMode) { %>
type ExportedQuery<T> = IsSearchParams<T> extends true ? T : never;
<% } else { %>
type ExportedQuery<T> = IsSearchParams<T> extends true ? T : URLSearchParamsInit;
<% } %>
export interface Routes {
<% routes.forEach(({ route, params, fileName }) => { %>
Expand Down

0 comments on commit 3324bfb

Please sign in to comment.