Skip to content

Commit 9824f65

Browse files
committedJun 18, 2024
Code Cleanup
1 parent 7c2083b commit 9824f65

File tree

1 file changed

+71
-163
lines changed

1 file changed

+71
-163
lines changed
 

‎index.js

+71-163
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ function checkCodename(codename)
7171
core.warning("It looks like you are using `codename` similar to official Voxelite ones, please choose a different prefix");
7272
}
7373
}
74+
function validateNonPrintCharacter(field, value)
75+
{
76+
for(let i = 0; i < value.length; i++)
77+
{
78+
const c = value.charCodeAt(i);
79+
if(c < 32 /* space character */)
80+
{
81+
if(c === 13 /* \r */ || c === 10 /* \n */)
82+
{
83+
core.error(field + " cannot contain a new line");
84+
return;
85+
}
86+
else if(c === 9 /* \t */)
87+
{
88+
core.error(field + " cannot contain tab character");
89+
return;
90+
}
91+
else
92+
{
93+
core.error(field + " cannot contain non-printable character (first 32 ASCII characters) - found character " + c);
94+
return;
95+
}
96+
}
97+
}
98+
}
7499
function checkName(name)
75100
{
76101
if(typeof name !== "string")
@@ -97,28 +122,7 @@ function checkName(name)
97122
if(name.indexOf(" ") !== -1)
98123
core.warning("`name` should not contain multiple consecutive space characters");
99124

100-
for(let i = 0; i < name.length; i++)
101-
{
102-
const c = name.charCodeAt(i);
103-
if(c < 32 /* space character */)
104-
{
105-
if(c === 13 /* \r */ || c === 10 /* \n */)
106-
{
107-
core.error("`name` cannot contain a new line");
108-
return;
109-
}
110-
else if(c === 9 /* \t */)
111-
{
112-
core.error("`name` cannot contain tab character");
113-
return;
114-
}
115-
else
116-
{
117-
core.error("`name` cannot contain non-printable character (first 32 ASCII characters) - found character " + c);
118-
return;
119-
}
120-
}
121-
}
125+
validateNonPrintCharacter('`name`', name);
122126
}
123127
function checkVersion(version)
124128
{
@@ -170,28 +174,7 @@ function checkDescription(description)
170174
if(description.indexOf(" ") !== -1)
171175
core.warning("`description` should not contain multiple consecutive space characters");
172176

173-
for(let i = 0; i < description.length; i++)
174-
{
175-
const c = description.charCodeAt(i);
176-
if(c < 32 /* space character */)
177-
{
178-
if(c === 13 /* \r */ || c === 10 /* \n */)
179-
{
180-
core.error("`description` cannot contain a new line");
181-
return;
182-
}
183-
else if(c === 9 /* \t */)
184-
{
185-
core.error("`description` cannot contain tab character");
186-
return;
187-
}
188-
else
189-
{
190-
core.error("`description` cannot contain non-printable character (first 32 ASCII characters) - found character " + c);
191-
return;
192-
}
193-
}
194-
}
177+
validateNonPrintCharacter('`description`', description);
195178
}
196179
function checkWebsite(website)
197180
{
@@ -225,29 +208,7 @@ function checkWebsite(website)
225208
if(website.startsWith("http://"))
226209
core.warning("Consider using `https://` instead of `http://` for `website`");
227210

228-
for(let i = 0; i < website.length; i++)
229-
{
230-
const c = website.charCodeAt(i);
231-
if(c < 32 /* space character */)
232-
{
233-
if(c === 13 /* \r */ || c === 10 /* \n */)
234-
{
235-
core.error("`website` cannot contain a new line");
236-
return;
237-
}
238-
else if(c === 9 /* \t */)
239-
{
240-
core.error("`website` cannot contain tab character");
241-
return;
242-
}
243-
else
244-
{
245-
core.error("`website` cannot contain non-printable character (first 32 ASCII characters) - found character " + c);
246-
return;
247-
}
248-
}
249-
}
250-
211+
validateNonPrintCharacter('`website`', website);
251212

252213
if(website.includes('voxelite.net'))
253214
{
@@ -282,28 +243,7 @@ function checkAuthor(author)
282243
if(author.indexOf(" ") !== -1)
283244
core.warning("`author` should not contain multiple consecutive space characters");
284245

285-
for(let i = 0; i < author.length; i++)
286-
{
287-
const c = author.charCodeAt(i);
288-
if(c < 32 /* space character */)
289-
{
290-
if(c === 13 /* \r */ || c === 10 /* \n */)
291-
{
292-
core.error("`author` cannot contain a new line");
293-
return;
294-
}
295-
else if(c === 9 /* \t */)
296-
{
297-
core.error("`author` cannot contain tab character");
298-
return;
299-
}
300-
else
301-
{
302-
core.error("`author` cannot contain non-printable character (first 32 ASCII characters) - found character " + c);
303-
return;
304-
}
305-
}
306-
}
246+
validateNonPrintCharacter('`author`', author);
307247

308248
if(author === 'vl' || author === 'voxelite')
309249
{
@@ -337,28 +277,7 @@ function checkPermission(permission)
337277

338278
//TODO Check against list of known permissions
339279

340-
for(let i = 0; i < permission.length; i++)
341-
{
342-
const c = permission.charCodeAt(i);
343-
if(c < 32 /* space character */)
344-
{
345-
if(c === 13 /* \r */ || c === 10 /* \n */)
346-
{
347-
core.error("`permissions` item cannot contain a new line");
348-
return;
349-
}
350-
else if(c === 9 /* \t */)
351-
{
352-
core.error("`permissions` item cannot contain tab character");
353-
return;
354-
}
355-
else
356-
{
357-
core.error("`permissions` item cannot contain non-printable character (first 32 ASCII characters) - found character " + c);
358-
return;
359-
}
360-
}
361-
}
280+
validateNonPrintCharacter('`permission`', permission);
362281

363282
{
364283
const regex = /^([a-z]([_a-z0-9]?[a-z0-9])*)(\.([a-z]([_a-z0-9]?[a-z0-9])*))*$/
@@ -373,6 +292,11 @@ function checkPermission(permission)
373292
try
374293
{
375294
const filename = core.getInput('file');
295+
if(!fs.existsSync(filename))
296+
{
297+
core.error("Input file not found");
298+
return;
299+
}
376300

377301
fs.readFile(
378302
filename,
@@ -386,76 +310,60 @@ try
386310
const json = JSON.parse(data);
387311

388312
// api_version
389-
{
390-
if(!json.hasOwnProperty("api_version"))
391-
core.error('Missing `api_version`');
392-
else
393-
checkApiVersion(json['api_version']);
394-
}
313+
if(!json.hasOwnProperty("api_version"))
314+
core.error('Missing `api_version`');
315+
else
316+
checkApiVersion(json['api_version']);
395317
// codename
318+
if(!json.hasOwnProperty("codename"))
319+
core.error('Missing `codename`');
320+
else
396321
{
397-
if(!json.hasOwnProperty("codename"))
398-
core.error('Missing `codename`');
399-
else
400-
{
401-
checkCodename(json['codename']);
402-
core.setOutput('codename', json['codename']);
403-
}
322+
checkCodename(json['codename']);
323+
core.setOutput('codename', json['codename']);
404324
}
405325
// version
326+
if(!json.hasOwnProperty("version"))
327+
core.error('Missing `version`');
328+
else
406329
{
407-
if(!json.hasOwnProperty("version"))
408-
core.error('Missing `version`');
409-
else
410-
{
411-
checkVersion(json['version']);
412-
core.setOutput('version', json['version']);
413-
}
330+
checkVersion(json['version']);
331+
core.setOutput('version', json['version']);
414332
}
415333
// name
416-
{
417-
if(!json.hasOwnProperty("name"))
418-
core.warning('Missing `name`, `codename` will be used instead');
419-
else
420-
checkName(json['name']);
421-
}
334+
if(!json.hasOwnProperty("name"))
335+
core.warning('Missing `name`, `codename` will be used instead');
336+
else
337+
checkName(json['name']);
422338
// description
423-
{
424-
if(json.hasOwnProperty("description"))
425-
checkDescription(json['description']);
426-
}
339+
if(json.hasOwnProperty("description"))
340+
checkDescription(json['description']);
427341
// website
428-
{
429-
if(json.hasOwnProperty("website"))
430-
checkWebsite(json['website']);
431-
}
342+
if(json.hasOwnProperty("website"))
343+
checkWebsite(json['website']);
432344
// author
345+
if(json.hasOwnProperty("author"))
433346
{
434-
if(json.hasOwnProperty("author"))
347+
const jsonAuthor = json["author"];
348+
if(typeof jsonAuthor === "string")
349+
checkAuthor(jsonAuthor)
350+
else if(Array.isArray(jsonAuthor) && jsonAuthor.every(item => typeof item === "string"))
435351
{
436-
const jsonAuthor = json["author"];
437-
if(typeof jsonAuthor === "string")
438-
checkAuthor(jsonAuthor)
439-
else if(Array.isArray(jsonAuthor) && jsonAuthor.every(item => typeof item === "string"))
440-
{
441-
jsonAuthor.forEach((author) => checkAuthor(author));
442-
}
443-
else
444-
console.error("Unsupported data type for `author` - use a string or an array of strings")
352+
jsonAuthor.forEach((author) => checkAuthor(author));
445353
}
354+
else
355+
console.error("Unsupported data type for `author` - use a string or an array of strings")
446356
}
447357
// permissions
358+
if(json.hasOwnProperty("permissions"))
448359
{
449-
if(json.hasOwnProperty("permissions"))
360+
const jsonPermissions = json["permissions"];
361+
if(Array.isArray(jsonPermissions) && jsonPermissions.every(item => typeof item === "string"))
450362
{
451-
const jsonPermissions = json["permissions"];
452-
if(Array.isArray(jsonPermissions) && jsonPermissions.every(item => typeof item === "string"))
453-
{
454-
jsonPermissions.forEach((permission) => checkPermission(permission));
455-
}
456-
else
457-
console.error("Unsupported data type for `permissions` - use an array of strings")
363+
jsonPermissions.forEach((permission) => checkPermission(permission));
458364
}
365+
else
366+
console.error("Unsupported data type for `permissions` - use an array of strings")
459367
}
460368
}
461369
}

0 commit comments

Comments
 (0)