-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.js
96 lines (76 loc) · 2.7 KB
/
dangerfile.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
const { danger, markdown } = require('danger');
const fse = require('fs-extra');
const path = require('path');
const prettier = require('prettier');
const dangerCommand = process.env.DANGER_COMMAND;
async function reportBundleSize() {
const snapshotPath = path.join(__dirname, './performance-snapshot.json');
const prettierConfigPath = path.join(__dirname, './prettier.config.js');
const snapshot = await fse.readJSON(snapshotPath);
const headers = `
| Test case | Unit | Min | Max | Median | Mean | σ |
| --------- | ---- | --- | --- | ------ | ---- | - |`;
let text = `These are the results for the performance tests:
${headers}\n`;
const formatter = new Intl.NumberFormat('en');
Object.entries(snapshot).forEach(([name, values]) => {
const min = formatter.format(values.min);
const max = formatter.format(values.max);
const mean = formatter.format(values.mean);
const median = formatter.format(values.median);
const stdDev = formatter.format(values.stdDev);
text += `| ${name} | ms | ${min} | ${max} | ${median} | ${mean} | ${stdDev} |\n`;
});
const prettierConfig = prettier.resolveConfig.sync(snapshotPath, {
config: prettierConfigPath,
});
markdown(prettier.format(text, { prettierConfig, parser: 'markdown' }));
}
function addDeployPreviewUrls() {
/**
* The incoming docsPath from danger does not start with `/`
* e.g. ['docs/data/data-grid/editing/editing.md']
*/
function formatFileToLink(docsPath) {
const url = docsPath.replace('docs/data', 'x/').replace(/\/[^/]+\.md$/, '/');
return url
.replace('data-grid/', 'react-data-grid/')
.replace('date-pickers/', 'react-date-pickers/')
.replace(/\/[^/]+\.md$/, '/');
}
const netlifyPreview = `https://deploy-preview-${danger.github.pr.number}--material-ui-x.netlify.app/`;
const files = [...danger.git.created_files, ...danger.git.modified_files];
// limit to the first 5 docs
const docs = files
.filter((file) => file.startsWith('docs/data') && file.endsWith('.md'))
.slice(0, 5);
markdown(`
## Netlify deploy preview
Netlify deploy preview: <a href="${netlifyPreview}">${netlifyPreview}</a>
### Updated pages
${
docs.length
? docs
.map((docsPath) => {
const formattedUrl = formatFileToLink(docsPath);
return `- [${docsPath}](${netlifyPreview}${formattedUrl})`;
})
.join('\n')
: 'No updates.'
}
`);
}
async function run() {
addDeployPreviewUrls();
switch (dangerCommand) {
case 'reportPerformance':
await reportBundleSize();
break;
default:
throw new TypeError(`Unrecognized danger command '${dangerCommand}'`);
}
}
run().catch((error) => {
console.error(error);
process.exit(1);
});