This repository was archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdangerfile.js
91 lines (83 loc) · 2.96 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 } = 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']);
const coverageLink = linkToCircleAsset(
'coverage/lcov-report/index.html'
);
markdown(
`All tests passed! [View Coverage Report](${coverageLink})`
);
} 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
);
}
},
function storybook() {
const storybookURI = linkToCircleAsset('storybook-dist/index.html');
markdown(
`[A Storybook for this PR has been deployed!](${storybookURI}). ` +
'It will be accessible as soon as the current build completes.'
);
}
];
for (const task of tasks) task();
function linkToCircleAsset(pathFromProjectRoot) {
const org = process.env.CIRCLE_PROJECT_USERNAME;
const repo = process.env.CIRCLE_PROJECT_REPONAME;
const buildNum = process.env.CIRCLE_BUILD_NUM;
const idx = process.env.CIRCLE_NODE_INDEX;
return [
'https://circleci.com/api/v1/project',
`/${org}/${repo}/${buildNum}`,
`/artifacts/${idx}/home/ubuntu`,
`/${repo}/${pathFromProjectRoot}`
].join('');
}