forked from kamranahmedse/developer-roadmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path-map.js
87 lines (77 loc) · 2.23 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
const path = require('path');
const glob = require('glob');
const guides = require('./storage/guides.json');
const roadmaps = require('./storage/roadmaps');
const PAGES_PATH = path.join(__dirname, 'pages');
/**
* 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 it's respective versions
* @returns {*}
*/
const getRoadmapRoutes = () => {
return roadmaps.reduce((roadmapRoutes, roadmap) => {
const [, slug] = roadmap.url.split('/');
return {
...roadmapRoutes,
// Default roadmap path i.e. `{ '/frontend': { page: '/[roadmap]/index', query: 'frontend' }`
[roadmap.url]: {
page: '/[roadmap]',
query: slug
},
// Route for each of the versions of this roadmap i.e.
// `{ '/frontend/2019': { page: '/[roadmap]/[version]', query: 'frontend/2019' } }`
...((roadmap.versions || []).reduce((versionRoutes, version) => {
return {
...versionRoutes,
[`${roadmap.url}/${version}`]: {
page: '/[roadmap]/[version]',
query: `${slug}/${version}`
}
};
}, {})),
};
}, {});
};
module.exports = {
getPageRoutes,
getGuideRoutes,
getRoadmapRoutes,
};