forked from rsuite/rsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindPages.js
47 lines (42 loc) · 1.05 KB
/
findPages.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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
function findPages(
options = {},
directory = path.resolve(__dirname, '../pages'),
pages = [
{
pathname: '/',
children: []
}
]
) {
fs.readdirSync(directory).forEach(item => {
const itemPath = path.resolve(directory, item);
const pathname = itemPath
.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
.replace(/^.*\/pages/, '')
.replace('.js', '')
.replace(/^\/index$/, '/') // Replace `index` by `/`.
.replace(/\/index$/, '');
if (
pathname.indexOf('/en-US') !== -1 ||
pathname.indexOf('/zh-CN') !== -1 ||
pathname.indexOf('/fragments') >= 0 ||
pathname.indexOf('/_common') >= 0
) {
return;
}
if (fs.statSync(itemPath).isDirectory()) {
const children = [];
pages.push({
pathname,
children
});
findPages(options, itemPath, children);
return;
}
});
return pages;
}
module.exports = findPages;