forked from roadmapsh/deprecated-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath-map.js
109 lines (98 loc) · 2.66 KB
/
path-map.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const path = require('path');
const glob = require('glob');
const fs = require('fs');
const roadmaps = require('../content/roadmaps.json');
const guides = require('../content/guides.json');
const PAGES_PATH = path.join(__dirname, '../pages');
const ROADMAPS_PATH = path.join(__dirname, '../content/roadmaps');
/**
* Generate the page routes from the page files inside `/pages`
* directory. Gives the format understood by next
* {
* '/slug': { page: '/path/to-file' }
* }
*/
const getPageRoutes = () => {
const files = glob.sync(`${PAGES_PATH}/**/*.js`, {
ignore: [
'**/_*.js', // private non-page files e.g. _document.js
'**/[[]*[]].js', // Ignore dynamic pages i.e. `page/[something].js` files
'**/[[]*[]]/*.js', // Ignore files inside dynamic pages i.e. `[something]/abc.js`
],
});
const pageRoutes = {};
files.forEach(file => {
const pageName = file.replace(PAGES_PATH, '').replace('.js', '');
const pagePath = pageName.replace('/index', '') || '/';
pageRoutes[pagePath] = { page: `${pageName}` }
});
return pageRoutes;
};
/**
* Generates routes for guide pages
* @returns {*}
*/
const getGuideRoutes = () => {
return guides.reduce((acc, guide) => {
const [, , slug] = guide.url.split('/');
return {
...acc,
[guide.url]: {
page: '/guides/[guide]',
query: slug,
},
};
}, {});
};
/**
* Generates routes for each of the roadmap and its respective versions
* @returns {*}
*/
const getRoadmapRoutes = () => {
return roadmaps.reduce((roadmapRoutes, roadmap) => {
const pages = Object.values(roadmap.sidebar || {})
.reduce((acc, menuPages) => {
return [
...acc,
...menuPages
]
}, []);
return {
...roadmapRoutes,
// Default roadmap path i.e. `{ '/frontend': { page: '/[roadmap]', query: 'frontend' }`
[roadmap.url]: {
page: '/[roadmap]',
query: roadmap.url.replace(/\/+/, ''),
},
[`${roadmap.url}/resources`]: {
page: '/[roadmap]/resources',
query: roadmap.url.replace(/\/+/, ''),
},
// Routes for all the pages inside this directory
...pages.reduce((pageRoutes, page) => {
return {
...pageRoutes,
[page.url]: {
page: '/[roadmap]/[page]',
query: page.url.replace(/^\//, '')
}
};
}, {})
};
}, {});
};
/**
* Generates the path-map understood by next.js
* @returns {{}}
*/
const getPathMap = () => () => ({
...getPageRoutes(),
...getGuideRoutes(),
...getRoadmapRoutes(),
});
module.exports = {
getPageRoutes,
getGuideRoutes,
getRoadmapRoutes,
getPathMap,
};