-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (48 loc) · 1.41 KB
/
index.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
const core = require('@actions/core');
const glob = require("glob")
const fs = require("fs")
const readline = require("readline")
const gh = require("@actions/github")
const src = core.getInput('path');
const copyrightPath = core.getInput('copyrightPath');
const excludePath = core.getInput('exclude');
glob(src, async (error, res) => {
if (error) {
core.setFailed(err);
} else {
const copyright = await getCopyRightString(copyrightPath);
res.forEach(file => {
if (!file.startsWith(excludePath)) {
getFirstFiveLines(file).then((ln) => {
if (ln != copyright) core.setFailed(`${gh.context.job} failed!: File ${file} does not contain the right copyright information!`);
});
}
});
}
});
const getCopyRightString = async (pathToFile) => {
const readable = fs.createReadStream(pathToFile);
const reader = readline.createInterface({ input: readable });
let fileData = '';
for await (const line of reader) {
fileData += line + '\n';
}
readable.close();
return fileData;
};
const getFirstFiveLines = async (pathToFile) => {
const readable = fs.createReadStream(pathToFile);
const reader = readline.createInterface({ input: readable });
let res = '';
let lineCount = 0;
for await (const line of reader) {
res += line + '\n';
lineCount++;
if (lineCount === 5) {
reader.close();
break;
}
}
readable.close();
return res;
};