forked from magento/pwa-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.js
91 lines (82 loc) · 3 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
const execa = require('execa');
const { fail, markdown, danger } = require('danger');
const fromRoot = path => path.replace(`${process.cwd()}/`, '');
const fence = '```';
const codeFence = str => `${fence}\n${str.trim()}\n${fence}`;
const tasks = [
function prettierCheck() {
try {
execa.sync('npm', ['run', '--silent', 'prettier:check']);
} catch (err) {
const { stdout } = err;
fail(
'The following file(s) were not ' +
'formatted with **prettier**. Make sure to execute `npm run prettier` ' +
`locally prior to committing.\n${codeFence(stdout)}`
);
}
},
function eslintCheck() {
try {
execa.sync('npm', ['run', '--silent', 'lint', '--', '-f', 'json']);
} catch (err) {
const { stdout } = err;
const results = JSON.parse(stdout);
const errFiles = results
.filter(r => r.errorCount)
.map(r => fromRoot(r.filePath));
fail(
'The following file(s) did not pass **ESLint**. Execute ' +
'`npm run lint` locally for more details\n' +
codeFence(errFiles.join('\n'))
);
}
},
function unitTests() {
try {
execa.sync('jest', ['--json', '--coverage']);
} catch (err) {
const summary = JSON.parse(err.stdout);
const failedTests = summary.testResults.filter(
t => t.status !== 'passed'
);
// prettier-ignore
const failSummary = failedTests.map(t =>
`<details>
<summary>${fromRoot(t.name)}</summary>
<pre>${t.message}</pre>
</details>`
).join('\n');
fail(
'The following unit tests did _not_ pass 😔. ' +
'All tests must pass before this PR can be merged\n\n\n' +
failSummary
);
return;
}
execa.sync('npm', ['run', 'coveralls']);
}
// Disabled for now, but leaving in for future implementation.
// Can't use right now due to the lack of permissions granularity
// in GitHub
// async function addProjectLabels() {
// const allChangedFiles = [
// ...danger.git.created_files,
// ...danger.git.deleted_files,
// ...danger.git.modified_files
// ];
// const touchedPackages = allChangedFiles.reduce((touched, path) => {
// const matches = path.match(/packages\/([\w-]+)\//);
// return matches ? touched.add(matches[1]) : touched;
// }, new Set());
// if (!touchedPackages.size) return;
// await danger.github.api.issues.addLabels(
// Object.assign({}, danger.github.thisPR, {
// labels: Array.from(touchedPackages).map(s => `pkg:${s}`)
// })
// );
// }
];
(async () => {
for (const task of tasks) await task();
})();