-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (123 loc) · 3.66 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const core = require('@actions/core');
const github = require('@actions/github');
const path = require('path')
const fs = require('fs');
const readline = require('readline');
const FILE_IGNORE_REGEX = [
'^\.git'
]
const CONFIGURED_IGNORE_REGEX = process.env.IGNORE
/**
* Only checks that the input is non-empty string
*/
const isValidConfigRegex = (input) => {
return input !== undefined
&& input !== null
&& input.trim().length > 0
}
const ignorePath = (inputFile) => {
const fullPath = path.resolve(process.env.GITHUB_WORKSPACE);
const inputPath = path.resolve(inputFile);
const relativePath = path.relative(fullPath, inputPath);
const ignoreExpressions = isValidConfigRegex(CONFIGURED_IGNORE_REGEX)
? FILE_IGNORE_REGEX.concat([CONFIGURED_IGNORE_REGEX])
: FILE_IGNORE_REGEX
for (regex of ignoreExpressions) {
if (new RegExp(regex).test(relativePath)) {
return true;
}
}
return false;
}
const findBidiCharactersInDirectory = async (inputDirectory) => {
let failedFiles = 0;
const files = fs.readdirSync(inputDirectory);
if (!fs.lstatSync(inputDirectory).isDirectory()) {
throw new Error('Input is not a directory');
}
for (const file of files) {
const filePath = inputDirectory + path.sep + file
if (ignorePath(filePath)) {
console.log(`Ignoring path: ${filePath}`);
}
else
{
const lstat = fs.lstatSync(filePath);
if (lstat.isDirectory()) {
failedFiles += await findBidiCharactersInDirectory(filePath);
} else if (lstat.isFile()) {
const fileResult = await findBidiCharactersInFile(filePath, 'utf-8')
if (fileResult.length > 0) {
console.log(`${filePath} contains bidi characters.`);
for (const resultLine of fileResult) {
console.log(` ${resultLine.line}:${resultLine.col}`);
}
failedFiles++;
}
}
}
}
return failedFiles;
}
const BIDI_CHARS = [
'\u202a',
'\u202b',
'\u202c',
'\u202d',
'\u202e',
'\u2066',
'\u2067',
'\u2068',
'\u2069',
'\u200E',
'\u200F',
'\u061C'
];
const bidiRegex = new RegExp(`[${BIDI_CHARS.join("")}]`);
const findBidiCharactersInFile = async (inputFile) => {
if (!fs.lstatSync(inputFile).isFile()) {
throw new Error('Input is not a file');
}
const output = [];
const fileStream = fs.createReadStream(inputFile);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
let lineNumber = 0;
for await (const line of rl) {
lineNumber++;
const match = bidiRegex.exec(line);
if (match) {
output.push({ line: lineNumber, col: match.index });
}
}
return output;
}
// most @actions toolkit packages have async methods
async function run() {
try {
const startTime = (new Date()).getTime();
const workspacePath = path.resolve(process.env.GITHUB_WORKSPACE);
console.log('Executing in ' + workspacePath);
if (fs.readdirSync(workspacePath).length == 0) {
throw new Error('GITHUB_WORKSPACE is empty. Please include an actions/checkout action in your steps to populate this directory.');
}
const success = findBidiCharactersInDirectory(workspacePath);
success.then(failures => {
const endTime = (new Date()).getTime();
const runTime = endTime - startTime;
core.setOutput("time", `${runTime}ms` );
if (failures == 0) {
console.log('CHECK PASSED');
} else {
const error = `CHECK FAILED: ${failures} files found with bidi characters.`;
console.log(error);
core.setFailed(error + ' Check log for details.');
}
})
} catch (error) {
core.setFailed(error.message);
}
}
run();