Skip to content

Commit 81692ab

Browse files
committed
feat(plop): create routing files with plop
1 parent 83645af commit 81692ab

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

config/plop/plopfile.mjs

+29-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @param {import('plop').NodePlopAPI} plop
66
*/
77
export default function (plop) {
8+
plop.setHelper('eq', (a, b) => a === b);
89
plop.setHelper('hasRouteLocale', (value) => value.includes('[locale]/'));
910
plop.setHelper('routeParams', (value) => {
1011
// return all params in between [] in value:
@@ -14,9 +15,17 @@ export default function (plop) {
1415
.map((param) => param.slice(1, -1)) // remove brackets
1516
.filter((param) => param !== 'locale'); // remove locale param
1617
});
18+
plop.setHelper('trailingParam', (value) =>
19+
// Return the trailing param in a route
20+
// If the last slug is not a param, this is undefined
21+
value.split('/').pop()?.match(/\[(.*?)\]/)?.[1]
22+
);
1723
plop.setHelper('trailingSlash', (value) =>
1824
value.endsWith('/') ? value : `${value}/`
1925
);
26+
plop.setHelper('toTemplateString', (value) =>
27+
`${value.replace(/\[(.*?)\]/g, '${$1}')}`
28+
);
2029

2130
plop.setGenerator('api', {
2231
description: 'create a new API route',
@@ -149,13 +158,20 @@ export default function (plop) {
149158
{
150159
type: 'input',
151160
name: 'route',
152-
message: 'Page route (e.g. [locale]/search/ )?',
161+
message: 'Page route (e.g. `[locale]/search/`)?',
153162
},
154163
{
155164
type: 'input',
156165
name: 'name',
157166
message: 'Model name in DatoCMS? (leave empty if none)',
158167
},
168+
{
169+
when: (data) => !!data.name,
170+
type: 'confirm',
171+
name: 'separateRouting',
172+
message: (data) => `Create separate file in routing for \`${plop.getHelper('pascalCase')(data.name)}\`?`,
173+
default: false,
174+
},
159175
],
160176
actions: [
161177
{
@@ -169,6 +185,18 @@ export default function (plop) {
169185
templateFile: 'templates/page/route.query.graphql.hbs',
170186
skip: (data) => !data.name && 'No DatoCMS model',
171187
},
188+
{
189+
type: 'add',
190+
path: '../../src/lib/routing/{{ pascalCase name }}Route.fragment.graphql',
191+
templateFile: 'templates/page/page.fragment.graphql.hbs',
192+
skip: (data) => !data.name && 'No RouteFragment',
193+
},
194+
{
195+
type: 'add',
196+
path: '../../src/lib/routing/{{ camelCase name }}.ts',
197+
templateFile: 'templates/page/page.ts.hbs',
198+
skip: (data) => !data.separateRouting && 'No separate route',
199+
},
172200
],
173201
});
174202
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
fragment {{ pascalCase name }}Route on {{ pascalCase name }}Record {
2+
__typename
3+
id
4+
title
5+
{{#each (routeParams route)}}
6+
{{#if (eq (trailingParam ../route) this)}}
7+
slug
8+
{{#if (hasRouteLocale ../route)}}
9+
_allSlugLocales {
10+
locale
11+
value
12+
}
13+
{{/if}}
14+
{{else}}
15+
{{this}} {
16+
__typename
17+
id
18+
title
19+
slug
20+
{{#if (hasRouteLocale ../route)}}
21+
_allSlugLocales {
22+
locale
23+
value
24+
}
25+
{{/if}}
26+
}
27+
{{/if}}
28+
{{/each}}
29+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{#if (hasRouteLocale route) }}
2+
import type { {{ pascalCase name }}RouteFragment, SiteLocale } from '@lib/datocms/types';
3+
import { getLocalizedSlug } from './lib/slug';
4+
5+
export const get{{ pascalCase name }}Path = ({ locale, record }: { locale?: SiteLocale, record: {{ pascalCase name }}RouteFragment }) => {
6+
{{#each (routeParams route) }}
7+
{{#if (eq (trailingParam ../route) this) }}
8+
const {{this}} = getLocalizedSlug({ locale, record: record })
9+
{{else}}
10+
const {{this}} = getLocalizedSlug({ locale, record: record.{{this}} });
11+
{{/if}}
12+
{{/each}}
13+
return `{{{ toTemplateString route }}}`;
14+
};
15+
{{else}}
16+
import type { {{ pascalCase name }}RouteFragment } from '@lib/datocms/types';
17+
18+
export const get{{ pascalCase name }}Path = (record: {{ pascalCase name }}RouteFragment) => {
19+
{{#each (routeParams route) }}
20+
{{#if (eq (trailingParam ../route) this) }}
21+
const {{this}} = record.slug;
22+
{{else}}
23+
const {{this}} = record.{{this}}.slug;
24+
{{/if}}
25+
{{/each}}
26+
return `{{{ toTemplateString route }}}`;
27+
};
28+
{{/if}}

0 commit comments

Comments
 (0)