-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (94 loc) · 3.45 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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
function processFiles(inFile, inDirectory, outFile)
{
//core.info("inFile: " + inFile);
//core.info("inDirectory: " + inDirectory);
//core.info("outFile: " + outFile);
let outputJson = JSON.constructor();
if(typeof inFile === "string" && inFile !== '')
{
const fileData = fs.readFileSync(inFile, 'utf8');
outputJson = JSON.parse(fileData);
}
if(typeof inDirectory === "string" && inDirectory !== '')
{
fs.readdirSync(inDirectory).forEach(filename => {
if(!filename.endsWith(".json"))
return;
const fileData = fs.readFileSync(inDirectory + '/' + filename, { encoding: 'utf8', flag: 'r' });
const json = JSON.parse(fileData);
Object.keys(json).forEach(key => {
if(outputJson.hasOwnProperty(key))
core.error("Duplicate key: " + key);
else
outputJson[key] = json[key]
});
});
}
if(Object.keys(outputJson).length !== 0)
{
fs.writeFileSync(
outFile,
JSON.stringify(outputJson, null, null),
{ encoding: 'utf8', flag: 'w' }
);
return outputJson;
}
else
return null;
}
const inDirectory = core.getInput('in-directory');
const outDirectory = core.getInput('out-directory');
const languageNameRegex = /^[a-z]{2}(-[a-zA-Z]{2})*$/
try
{
if(!fs.existsSync(outDirectory))
fs.mkdirSync(outDirectory, { recursive: true });
let languages = new Set;
fs.readdirSync(inDirectory, { withFileTypes: true }).forEach(fileInfo => {
if(fileInfo.isDirectory())
{
if(languageNameRegex.test(fileInfo.name))
languages.add(fileInfo.name);
else
core.warning("Directory " + fileInfo.name + " was skipped - incorrect language format");
}
else if(fileInfo.isFile())
{
if(!fileInfo.name.endsWith(".json"))
{
core.warning("File " + fileInfo.name + " was skipped - unsupported extension");
return;
}
const language = fileInfo.name.substring(0, fileInfo.name.length - ".json".length);
// Validate name usability
if(!languageNameRegex.test(language))
{
core.warning("File " + fileInfo.name + " was skipped - invalid name for an object");
return;
}
languages.add(language);
}
});
core.info("languages: " + Array.from(languages).join(','));
let usedLanguages = new Set;
languages.forEach(language => {
let argFile = null;
if(fs.existsSync(inDirectory + '/' + language + ".json")) //TODO and is file
argFile = inDirectory + '/' + language + ".json";
let argDirectory = null;
if(fs.existsSync(inDirectory + '/' + language)) //TODO and is directory
argDirectory = inDirectory + '/' + language;
const outputJson = processFiles(argFile, argDirectory, outDirectory + '/' + language + '.json');
if(outputJson !== null)
usedLanguages.add(language);
});
core.info("usedLanguages: " + Array.from(usedLanguages).join(','));
core.setOutput("languages", Array.from(usedLanguages).join(','));
}
catch(error)
{
core.setFailed(error.message);
}