forked from graphql-markdown/graphql-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.js
117 lines (103 loc) · 3.35 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
const { danger, fail, warn, schedule } = require("danger");
const Diff = require("diff");
const filter = require("lodash.filter");
const pico = require("picocolors");
const COLOR = {
ADDED: pico.green,
REMOVED: pico.red,
COMMON: pico.gray,
FAIL: pico.red,
WARN: pico.yellow,
};
const PACKAGE_LOCK = "package-lock.json";
const PACKAGE_JSON = "package.json";
const YARN_LOCK = "yarn.lock";
const LICENSE_FILE = "LICENSE";
const README_FILE = "README.md";
const DANGER_FILE = "dangerfile.js";
const JEST_SNAPSHOT = /^.+\.snap$/;
const packageLock = danger.git.fileMatch(PACKAGE_LOCK);
const packageJson = danger.git.fileMatch(PACKAGE_JSON);
const yarnLock = danger.git.fileMatch(YARN_LOCK);
const licenseFile = danger.git.fileMatch(LICENSE_FILE);
const readmeFile = danger.git.fileMatch(README_FILE);
const dangerFile = danger.git.fileMatch(DANGER_FILE);
const getDiffDependencies = (dependencies) => {
const diff = Diff.diffJson(dependencies.before, dependencies.after);
let colorDiff = ``;
diff.forEach((part) => {
const color = part.added
? COLOR.ADDED
: part.removed
? COLOR.REMOVED
: COLOR.COMMON;
colorDiff = `${colorDiff} ${color(part.value)}`;
});
return colorDiff;
};
// rule-package-lock-detected
if (packageLock.modified || packageLock.created) {
fail(
COLOR.FAIL(
`\`${PACKAGE_LOCK}\` detected, you must used 'yarn' for dependencies.`,
),
);
}
// rule-yarn-lock-deleted
if (yarnLock.deleted) {
fail(COLOR.FAIL(`This PR deleted the \`${YARN_LOCK}\` file.`));
}
// rule-yarn-lock-not-updated
if (packageJson.modified && !(yarnLock.modified || yarnLock.created)) {
schedule(async () => {
const packageDiff = await danger.git.JSONDiffForFile(PACKAGE_JSON);
if (packageDiff.dependencies) {
const description = COLOR.FAIL(
"Dependencies changed with no corresponding lockfile changes:",
);
fail(`${description}\n${getDiffDependencies(packageDiff.dependencies)}`);
}
if (packageDiff.devDependencies) {
const description = COLOR.FAIL(
"Dev dependencies changed with no corresponding lockfile changes:",
);
fail(
`${description}\n${getDiffDependencies(packageDiff.devDependencies)}`,
);
}
});
}
// rule-license-file-modified
if (licenseFile.modified) {
warn(COLOR.WARN(`This PR modified the \`${LICENSE_FILE}\` file.`));
}
// rule-license-file-deleted
if (licenseFile.deleted) {
fail(COLOR.FAIL(`This PR deleted the \`${LICENSE_FILE}\` file.`));
}
// rule-readme-file-deleted
if (readmeFile.deleted) {
fail(COLOR.FAIL(`This PR deleted the \`${README_FILE}\` file.`));
}
const jestSnapshots = {
modified: filter(danger.git.modified_files, (file) =>
file.match(JEST_SNAPSHOT),
),
deleted: filter(danger.git.deleted_files, (file) =>
file.match(JEST_SNAPSHOT),
),
};
// rule-jest-snapshot-modified
if (jestSnapshots.modified.length > 0) {
const description = COLOR.WARN("This PR modified some Jest snapshot file/s:");
warn(`${description}\n - ${jestSnapshots.modified.join("\n - ")}`);
}
// rule-jest-snapshot-deleted
if (jestSnapshots.deleted.length > 0) {
const description = COLOR.WARN("This PR deleted some Jest snapshot file/s:");
warn(`${description}\n - ${jestSnapshots.deleted.join("\n - ")}`);
}
// rule-danger-file-modified
if (dangerFile.modified) {
warn(COLOR.WARN(`This PR modified the \`${DANGER_FILE}\` file.`));
}