This repository has been archived by the owner on Aug 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdangerfile.ts
123 lines (108 loc) · 3.29 KB
/
dangerfile.ts
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
require("colors");
import * as Diff from "diff";
import { danger, fail, schedule, warn } from "danger";
const COLOR = {
ADDED: "green",
COMMON: "grey",
FAIL: "red",
REMOVED: "red",
WARN: "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} ${part.value[color]}`;
});
return colorDiff;
};
// Rule-yarn-lock-detected
if (yarnLock.modified || yarnLock.created) {
fail(
`\`${YARN_LOCK}\` detected, you must used 'npm' for dependencies.`[
COLOR.FAIL
]
);
}
// Rule-yarn-lock-deleted
if (packageLock.deleted) {
fail(`This PR deleted the \`${PACKAGE_LOCK}\` file.`[COLOR.FAIL]);
}
// Rule-yarn-lock-not-updated
if (packageJson.modified) {
schedule(async () => {
const packageDiff = await danger.git.JSONDiffForFile(PACKAGE_JSON);
if (packageDiff.dependencies) {
const description =
"Dependencies changed with no corresponding lockfile changes:"[
COLOR.FAIL
];
fail(`${description}\n${getDiffDependencies(packageDiff.dependencies)}`);
}
if (packageDiff.devDependencies) {
const description =
"Dev dependencies changed with no corresponding lockfile changes:"[
COLOR.FAIL
];
fail(
`${description}\n${getDiffDependencies(packageDiff.devDependencies)}`
);
}
});
}
// Rule-license-file-modified
if (licenseFile.modified) {
warn(`This PR modified the \`${LICENSE_FILE}\` file.`[COLOR.WARN]);
}
// Rule-license-file-deleted
if (licenseFile.deleted) {
fail(`This PR deleted the \`${LICENSE_FILE}\` file.`[COLOR.FAIL]);
}
// Rule-readme-file-deleted
if (readmeFile.deleted) {
fail(`This PR deleted the \`${README_FILE}\` file.`[COLOR.FAIL]);
}
const jestSnapshots = {
deleted: danger.git.deleted_files.filter(file => {
{
return file.match(JEST_SNAPSHOT);
}
}),
modified: danger.git.modified_files.filter(file => {
{
return file.match(JEST_SNAPSHOT);
}
}),
};
// Rule-jest-snapshot-modified
if (jestSnapshots.modified.length > 0) {
const description = "This PR modified some Jest snapshot file/s:"[COLOR.WARN];
warn(`${description}\n - ${jestSnapshots.modified.join("\n - ")}`);
}
// Rule-jest-snapshot-deleted
if (jestSnapshots.deleted.length > 0) {
const description = "This PR deleted some Jest snapshot file/s:"[COLOR.WARN];
warn(`${description}\n - ${jestSnapshots.deleted.join("\n - ")}`);
}
// Rule-danger-file-modified
if (dangerFile.modified) {
warn(`This PR modified the \`${DANGER_FILE}\` file.`[COLOR.WARN]);
}