-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple-fs-routes.js
35 lines (30 loc) · 1.1 KB
/
simple-fs-routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const { basename, relative, resolve } = require('path');
const glob = require('glob');
const { PAGE_DIR } = require('./constants');
// https://nextjs.org/docs/routing/dynamic-routes
const catchAll = /\/\[\.\.\.([A-Za-z0-9_]+)\]/g;
const param = /\/\[([A-Za-z0-9_]+)\]/g;
const convertNextPathToRoute = nextPath => {
return nextPath.replace(catchAll, '/:$1+').replace(param, '/:$1');
};
module.exports = targets => {
const { routes } = targets.of('@magento/venia-ui');
routes.tap(routeArray => {
const routeBase = resolve(process.cwd(), PAGE_DIR);
const pages = glob.sync('**/*.js', { cwd: routeBase, absolute: true });
return routeArray.concat(
pages.map((path, i) => {
const relPath = relative(routeBase, path).replace(
/\.[jt]sx?$/,
''
);
return {
name: `GeneratedPage_${basename(relPath)}${i}_`,
pattern: `/${convertNextPathToRoute(relPath)}`,
path
};
})
);
});
return targets;
};