generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-existence.js
40 lines (37 loc) · 1.36 KB
/
check-existence.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
const fs = require('fs');
const checkCodeowners = require('./check-contents');
async function checkPath(path) {
try {
if (fs.existsSync(path)) {return path}
else if (fs.existsSync(`${path}.md`)) {return `${path}.md`}
else if (fs.existsSync(`${path}.txt`)) {return `${path}.txt`}
else if (fs.existsSync(`${path}.rst`)) {return `${path}.rst`}
else if (fs.existsSync(`${path}-MIT`)) {return `${path}-MIT`}
else if (fs.existsSync(`${path}-APACHE`)) {return `${path}-APACHE`}
else {throw Error}
} catch (error) {
return error;
}
}
async function checkExistence(path) {
try {
const truePath = await checkPath(path);
const data = fs.readFileSync(truePath,'utf8')
if (data && data.length > 0) {
if (truePath.includes('CODEOWNERS')) {
const contentCheck = await checkCodeowners(data);
if (!contentCheck) {
return {isPresent:false,message:`There is no global owner in ${path}. Please add one before proceeding.`};
} else {
return {isPresent:true,message:`${path} exists`};
}
}
return {isPresent:true,message:`${path} exists`};
} else {
return {isPresent:false,message:`${path} exists with no content. Please finish file contents before proceeding.`};
}
} catch (error) {
return {isPresent:false,message:`Error finding ${path}. Please ensure file exists and is in the correct location.`};
}
}
module.exports = checkExistence;