-
Notifications
You must be signed in to change notification settings - Fork 1
/
dangerfile.js
128 lines (106 loc) · 4.02 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
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
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('charts/', 'react-charts/')
.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.'
}
`);
}
function addL10nHelpMessage() {
const isAddingLocale = danger.git.created_files.some((file) => file.includes('/src/locales/'));
const isUpdatingLocale = danger.git.modified_files.some((file) => file.includes('/src/locales/'));
if (!isAddingLocale && !isUpdatingLocale) {
return;
}
markdown(
[
'## Localization writing tips :writing_hand:',
'',
'Seems you are updating localization :earth_africa: files.',
'',
'Thank you for contributing to the localization! :tada: To make your PR perfect, here is a list of elements to check: :heavy_check_mark:',
'- [ ] Verify if the PR title respects the release format. Here are two examples (depending if you update or add a locale file)',
' > [l10n] Improve Swedish (sv-SE) locale',
' > [l10n] Add Danish (da-DK) locale',
'- [ ] Update the documentation of supported locales by running `yarn l10n`',
...(isAddingLocale
? [
'- [ ] Verify that you have added an export line in `src/locales/index.ts` for the new locale.',
'- [ ] Run `yarn docs:api` which should add your new translation to the list of exported interfaces.',
]
: []),
'- [ ] Clean files with `yarn prettier`.',
'',
].join('\n'),
);
}
async function run() {
addL10nHelpMessage();
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);
});