Skip to content

Commit

Permalink
fix: support multiple remix projects in one monorepo (#21)
Browse files Browse the repository at this point in the history
* fix: support multiple remix projects in one monorepo

close #20

* chore: bump version

* chore: bump version
  • Loading branch information
yesmeck committed Oct 16, 2022
1 parent ff48459 commit 9cb47cf
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 150 deletions.
Empty file.
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// __routes__

module.exports.$path = function $path(route, ...paramsOrQuery) {
const routeParams = routes[route];
let path = route;
let query = paramsOrQuery[0];
if (
(routeParams === null || routeParams === void 0
? void 0
: routeParams.length) > 0
) {
const params = paramsOrQuery[0];
query = paramsOrQuery[1];
routeParams.forEach((name) => {
path = path.replace(':' + name, params[name]);
});
}
if (!query) {
return path;
}
const searchParams = new URLSearchParams(query);
return path + '?' + searchParams.toString();
}

module.exports.$params = function $params(_route, params) {
return params;
}
1 change: 0 additions & 1 deletion index.d.ts

This file was deleted.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "remix-routes",
"version": "0.4.0-alpha.1",
"version": "0.6.0-alpha.1",
"description": "",
"main": "lib/index.js",
"main": "index.js",
"types": "index.d.ts",
"bin": {
"remix-routes": "lib/cli.js"
},
"files": [
"lib",
"!lib/__tests__",
"index.d.ts"
"helper.js"
],
"scripts": {
"watch": "tsc --watch",
Expand All @@ -27,6 +27,7 @@
"@remix-run/dev": "^1.6.7",
"@types/cli-table": "^0.3.0",
"@types/jest": "^27.0.3",
"@types/mkdirp": "^1.0.2",
"@types/node": "^16.11.12",
"@types/pluralize": "^0.0.29",
"jest": "^27.4.4",
Expand All @@ -36,7 +37,8 @@
},
"dependencies": {
"chokidar": "^3.5.2",
"meow": "9.0.0"
"meow": "9.0.0",
"mkdirp": "^1.0.4"
},
"repository": {
"type": "git",
Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/__mocks__/.remix-routes.ts

This file was deleted.

121 changes: 82 additions & 39 deletions src/__tests__/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`build 1`] = `
"
const routes = {
\\"/chats/:season/:episode/:slug\\": [
\\"season\\",
\\"episode\\",
\\"slug\\"
],
\\"/chats/:season/:episode\\": [
\\"season\\",
\\"episode\\"
],
\\"/people/:personId\\": [
\\"personId\\"
],
\\"/people/:personId/:planId/remove-plan\\": [
\\"personId\\",
\\"planId\\"
],
\\"/s/:query\\": [
\\"query\\"
],
\\"/admin/episodes/:id\\": [
\\"id\\"
],
\\"/admin/episodes/:id/comments\\": [
\\"id\\"
],
\\"/jokes/:jokeId\\": [
\\"jokeId\\"
]
};
module.exports = { routes }
"
`;

exports[`build 2`] = `"{\\"name\\":\\".remix-routes\\",\\"main\\":\\"index.js\\"}"`;

exports[`build 3`] = `
"export declare function $path(
route: \\"/\\",
query?: string | string[][] | Record<string, string> | URLSearchParams
Expand Down Expand Up @@ -68,6 +29,10 @@ export declare function $path(
route: \\"/blog/rss.xml\\",
query?: string | string[][] | Record<string, string> | URLSearchParams
): string;
export declare function $path(
route: \\"/posts/delete\\",
query?: string | string[][] | Record<string, string> | URLSearchParams
): string;
export declare function $path(
route: \\"/auth\\",
query?: string | string[][] | Record<string, string> | URLSearchParams
Expand All @@ -76,6 +41,15 @@ export declare function $path(
route: \\"/auth/login\\",
query?: string | string[][] | Record<string, string> | URLSearchParams
): string;
export declare function $path(
route: \\"/posts\\",
query?: string | string[][] | Record<string, string> | URLSearchParams
): string;
export declare function $path(
route: \\"/posts/:id\\",
params: { id: string | number },
query?: string | string[][] | Record<string, string> | URLSearchParams
): string;
export declare function $path(
route: \\"/s/:query\\",
params: { query: string | number },
Expand Down Expand Up @@ -145,6 +119,12 @@ export declare function $params(
personId: string,
planId: string
};
export declare function $params(
route: \\"/posts/:id\\",
params: { readonly [key: string]: string | undefined }
): {
id: string
};
export declare function $params(
route: \\"/s/:query\\",
params: { readonly [key: string]: string | undefined }
Expand Down Expand Up @@ -172,3 +152,66 @@ export declare function $params(
"
`;

exports[`build 2`] = `
"const routes = {
\\"/chats/:season/:episode/:slug\\": [
\\"season\\",
\\"episode\\",
\\"slug\\"
],
\\"/chats/:season/:episode\\": [
\\"season\\",
\\"episode\\"
],
\\"/people/:personId\\": [
\\"personId\\"
],
\\"/people/:personId/:planId/remove-plan\\": [
\\"personId\\",
\\"planId\\"
],
\\"/posts/:id\\": [
\\"id\\"
],
\\"/s/:query\\": [
\\"query\\"
],
\\"/admin/episodes/:id\\": [
\\"id\\"
],
\\"/admin/episodes/:id/comments\\": [
\\"id\\"
],
\\"/jokes/:jokeId\\": [
\\"jokeId\\"
]
}
module.exports.$path = function $path(route, ...paramsOrQuery) {
const routeParams = routes[route];
let path = route;
let query = paramsOrQuery[0];
if (
(routeParams === null || routeParams === void 0
? void 0
: routeParams.length) > 0
) {
const params = paramsOrQuery[0];
query = paramsOrQuery[1];
routeParams.forEach((name) => {
path = path.replace(':' + name, params[name]);
});
}
if (!query) {
return path;
}
const searchParams = new URLSearchParams(query);
return path + '?' + searchParams.toString();
}
module.exports.$params = function $params(_route, params) {
return params;
}
"
`;
12 changes: 8 additions & 4 deletions src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import * as fs from 'fs';

test('build', async () => {
await build(path.resolve(__dirname, '../../fixture'));
const outputPath = path.resolve(__dirname, '../../node_modules/.remix-routes');
const outputPath = path.resolve(__dirname, '../../node_modules');
fs.readdirSync(outputPath).forEach((file) => {
expect(fs.readFileSync(path.resolve(outputPath, file), 'utf8')).toMatchSnapshot();
})
})
if (file.startsWith('remix-routes')) {
expect(
fs.readFileSync(path.resolve(outputPath, file), 'utf8'),
).toMatchSnapshot();
}
});
});
43 changes: 43 additions & 0 deletions src/__tests__/helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as path from 'path';
import { build } from '../cli';

describe('helper', () => {
let $path: any, $params: any;

beforeAll(async () => {
await build(path.resolve(__dirname, '../../fixture'));
$path = require('remix-routes').$path;
$params = require('remix-routes').$params;
});

test('$path', () => {
expect($path('/posts')).toBe('/posts');
});

test('$path + params', () => {
expect($path('/posts/:id', { id: 1 })).toBe('/posts/1');
});

test('$path + query', () => {
expect($path('/posts', { order: 'desc' })).toBe('/posts?order=desc');
});

test('$path + array query', () => {
expect(
$path('/posts/delete', [
['id', '1'],
['id', '2'],
]),
).toBe('/posts/delete?id=1&id=2');
});

test('$path + params + query', () => {
expect($path('/posts/:id', { id: 1 }, { raw: 'true' })).toBe(
'/posts/1?raw=true',
);
});

test('$params', () => {
expect($params('/posts/:id', { id: '1' })).toStrictEqual({ id: '1' });
});
});
30 changes: 0 additions & 30 deletions src/__tests__/index.test.ts

This file was deleted.

0 comments on commit 9cb47cf

Please sign in to comment.