-
Notifications
You must be signed in to change notification settings - Fork 26
/
testurls.mjs
executable file
·70 lines (62 loc) · 1.83 KB
/
testurls.mjs
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
import Axios from 'axios';
import chalk from 'chalk';
import path from 'path';
import fs from 'fs/promises';
const checkingUrls = [
// blogs
'/blog/682535b5',
'/blog?latest=true',
'/blog/c/management-and-operation',
'/blog/c/management-and-operation?latest=true',
'/blog/tag/practical-case',
'/blog/tag/practical-case?latest=true',
// user home page
'/u/orgadmin/post/all',
'/u/orgadmin/answer',
'/u/orgadmin/post',
'/u/orgadmin/question',
];
const urls = [
...checkingUrls,
...await getPages(),
];
const axios = Axios.create({
baseURL: process.env.NEXT_PUBLIC_HOME_URL,
});
async function test (url) {
try {
await axios.get(url);
console.log(url, chalk.gray('...'), chalk.green('[OK]'));
} catch (e) {
if (!e.response) {
console.error(url, chalk.gray('...'), chalk.red(`[ERROR]`), e);
} else {
console.log(url, chalk.gray('...'), chalk.red(`[${e?.response.status} ${e?.response.statusText}]`));
}
throw e;
}
}
const res = await Promise.allSettled(urls.map(test));
const failed = res.filter(res => res.status === 'rejected');
if (failed.length > 0) {
console.error(`${chalk.red('[FAILED]')} ${failed.length}/${urls.length} failed`);
process.exit(1)
} else {
console.log(`${chalk.green('[SUCCESS]')} all tests passed`);
}
async function getPages (base = './src/pages') {
const urls = [];
const files = await fs.readdir(base, { withFileTypes: true });
for (let file of files) {
if (file.isFile() && /^index\.page\.[jt]sx?$/.test(file.name)) {
urls.push('/' + path.relative('./src/pages', base));
} else if (file.isDirectory()) {
if (!/^\[.+]$/.test(file.name)) {
urls.push(...await getPages(path.join(base, file.name)));
} else {
console.log(chalk.gray('ignore', await getPages(path.join(base, file.name))));
}
}
}
return urls;
}