forked from aws-amplify/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePathMap.cjs.js
213 lines (200 loc) · 5.57 KB
/
generatePathMap.cjs.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');
function generatePathMap(
obj,
pathMap = {
'/': {
page: '/'
},
'/404': {
page: '/404'
},
'/start': {
page: '/start'
},
'/start/q/integration/js': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/react': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/angular': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/vue': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/next': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/android': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/react-native': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/ios': {
page: '/start/q/integration/[integration]'
},
'/start/q/integration/flutter': {
page: '/start/q/integration/[integration]'
},
'/lib/q/platform/flutter': {
page: '/lib/q/platform/[platform]'
},
'/lib/q/platform/android': {
page: '/lib/q/platform/[platform]'
},
'/lib/q/platform/ios': {
page: '/lib/q/platform/[platform]'
},
'/lib/q/platform/js': {
page: '/lib/q/platform/[platform]'
},
'/lib/q/platform/react-native': {
page: '/lib/q/platform/[platform]'
},
'/lib-v1/q/platform/android': {
page: '/lib-v1/q/platform/[platform]'
},
'/lib-v1/q/platform/ios': {
page: '/lib-v1/q/platform/[platform]'
},
'/sdk/q/platform/js': {
page: '/sdk/q/platform/[platform]'
},
'/sdk/q/platform/android': {
page: '/sdk/q/platform/[platform]'
},
'/sdk/q/platform/ios': {
page: '/sdk/q/platform/[platform]'
},
'/sdk/q/platform/flutter': {
page: '/sdk/q/platform/[platform]'
},
'/console': {
page: '/console'
},
'/cli': {
page: '/cli'
},
'/cli/function': {
page: '/cli/function'
},
'/flutter-references': {
page: '/flutter-references'
},
'/contribute': {
page: '/contribute'
},
'/contribute/getting-started': {
page: '/contribute/getting-started'
}
},
removeChoosePages = false //this flag if set will generate a pathmap without the choose platform pages
) {
for (const [_, value] of Object.entries(obj)) {
const { items, filters, route, productRoot } = value;
if (productRoot) {
const { route } = productRoot;
let filterKind = '';
if (route.startsWith('/cli') || route.startsWith('/console')) {
filterKind = '';
} else if (route.startsWith('/lib')) {
filterKind = 'platform';
} else if (route.startsWith('/sdk')) {
filterKind = 'platform';
} else if (route.startsWith('/ui')) {
filterKind = 'framework';
} else if (route.startsWith('/guides')) {
filterKind = 'platform';
} else if (route.startsWith('/start')) {
filterKind = 'integration';
}
if (filterKind !== '') {
const aOrAn = 'aeiou'.includes(filterKind[0]) ? 'an' : 'a';
pathMap[route] = {
page: '/ChooseFilterPage',
query: {
address: route,
directoryPath: '/ChooseFilterPage',
filterKind: filterKind,
message: `Choose ${aOrAn} ${filterKind}:`
}
};
}
}
if (items) {
generatePathMap(items, pathMap, removeChoosePages);
}
if (!filters || !filters.length) {
let page = '';
const mdxSrc = `${route}.mdx`;
const tsxSrc = `${route}.tsx`;
const maybeMDXFile = './src/pages' + mdxSrc;
const maybeTSXFile = './src/pages' + tsxSrc;
if (fs.existsSync(maybeTSXFile)) {
page = tsxSrc;
} else if (fs.existsSync(maybeMDXFile)) {
page = mdxSrc;
}
if (page.length) {
pathMap[route] = {
page: route
};
}
continue;
}
let page = '';
let routeType = '';
['platform', 'framework', 'integration'].forEach((type) => {
const src = `${route}/q/${type}/[${type}].mdx`;
const maybeFile = './src/pages' + src;
if (fs.existsSync(maybeFile)) {
page = src;
routeType = type;
}
});
if (!page || !routeType) {
continue;
}
// generate for _all_ filters -- unsupported filters will just generate ChooseFilterPages, which is what we want
// ideally misspellings would also map to a ChooseFilterPage, but this doesn't work with SSG
let allFilters = filters;
if (routeType !== '') {
allFilters = [
'js',
'android',
'ios',
'flutter',
'react',
'react-native',
'angular',
'vue',
'next'
];
}
if (removeChoosePages) {
// reset the allFilters to the filters found in the directory object, this will remove the choose platform pages from the pathmap being generated
allFilters = filters;
}
allFilters.forEach((filter) => {
pathMap[route + '/q/' + routeType + '/' + filter] = {
page: `${route}/q/${routeType}/[${routeType}]`
};
});
const aOrAn = 'aeiou'.includes(routeType[0]) ? 'an' : 'a';
pathMap[route] = {
page: '/ChooseFilterPage',
query: {
address: route,
directoryPath: '/ChooseFilterPage',
filterKind: routeType,
filters: filters,
message: `Choose ${aOrAn} ${routeType}:`
}
};
}
return pathMap;
}
module.exports = generatePathMap;