forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaderChecks.ts
More file actions
125 lines (104 loc) · 5.24 KB
/
headerChecks.ts
File metadata and controls
125 lines (104 loc) · 5.24 KB
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
import { renderParseError, validate } from "./header-parser";
import * as path from "path";
import * as fs from "fs";
import { scripts, scriptsPath, editorExtensions, editorExtensionsPath, scriptFileExtensions, mainFilenames, printFilePath } from "./utils";
function checkScripts () {
const errorMessages: string[] = [];
let packagesHaveError = 0;
for (const script of scripts) {
const scriptPath = path.resolve(scriptsPath, script);
const scriptFiles = fs.readdirSync(scriptPath).filter(name => scriptFileExtensions.includes(path.extname(name)));
const mainFiles = scriptFiles.filter(x => mainFilenames.includes(x));
let hasError = false;
if (mainFiles.length < 1) {
errorMessages.push(`${printFilePath(scriptPath)} does not have one of the following files: ${mainFilenames.join(', ')}`);
hasError = true;
packagesHaveError++;
continue;
};
for (const file of mainFiles) {
const fullpath = path.resolve(scriptPath, file);
const content = fs.readFileSync(fullpath, 'utf8');
const parseError = validate(content);
const tsSourceFilename = 'index.ts';
const isEmittedFile = path.extname(fullpath) === '.js' && fs.existsSync(path.resolve(scriptPath, tsSourceFilename));
if (!!parseError && !isEmittedFile) {
errorMessages.push(`${printFilePath(fullpath)} ${renderParseError(parseError)}`);
hasError = true;
}
else if (!!parseError && isEmittedFile) {
errorMessages.push(`${printFilePath(fullpath)} ${renderParseError(parseError)}. Adding header from index.ts to ${file}`);
const fullTspath = path.resolve(scriptPath, tsSourceFilename);
const tsFile = fs.readFileSync(fullTspath, 'utf8');
const lines = tsFile.split(/\n|\r\n/);
const jsEmittedFileLines = content.split(/\n|\r\n/);
const startOfHeader = lines.findIndex((value) => /\/\/ Script example for ScriptAPI/.test(value));
const endOfHeader = lines.findIndex((value) => /\r?\n\/\/ Project: https:\/\/[^\r\n]+/.test('\n' + value));
const header = lines.slice(startOfHeader, endOfHeader - startOfHeader + 1);
jsEmittedFileLines.splice(0, 0, ...header);
fs.writeFileSync(fullpath, jsEmittedFileLines.join('\n'));
hasError = false;
};
};
hasError ? packagesHaveError++ : null;
};
return { errorMessages, packagesHaveError };
};
function checkEditorExtensions () {
const errorMessages: string[] = [];
let packagesHaveError = 0;
for (const editorExtension of editorExtensions) {
const editorExtensionPath = path.resolve(editorExtensionsPath, editorExtension);
const editorExtensionFiles = fs.readdirSync(editorExtensionPath).filter(name => scriptFileExtensions.includes(path.extname(name)));
const mainFiles = editorExtensionFiles.filter(x => mainFilenames.includes(x));
let hasError = false;
if (mainFiles.length < 1) {
errorMessages.push(`${printFilePath(editorExtensionPath)} does not have one of the following files: ${mainFilenames.join(', ')}`);
hasError = true;
packagesHaveError++;
continue;
};
for (const file of mainFiles) {
const fullpath = path.resolve(editorExtensionPath, file);
const content = fs.readFileSync(fullpath, 'utf8');
const parseError = validate(content);
const tsSourceFilename = 'index.ts';
const isEmittedFile = path.extname(fullpath) === '.js' && fs.existsSync(path.resolve(editorExtensionPath, tsSourceFilename));
if (!!parseError && !isEmittedFile) {
errorMessages.push(`${printFilePath(fullpath)} ${renderParseError(parseError)}`);
hasError = true;
}
else if (!!parseError && isEmittedFile) {
errorMessages.push(`${printFilePath(fullpath)} ${renderParseError(parseError)}. Adding header from index.ts to ${file}`);
const fullTspath = path.resolve(editorExtensionPath, tsSourceFilename);
const tsFile = fs.readFileSync(fullTspath, 'utf8');
const lines = tsFile.split(/\n|\r\n/);
const jsEmittedFileLines = content.split(/\n|\r\n/);
const startOfHeader = lines.findIndex((value) => /\/\/ editorExtension example for editorExtensionAPI/.test(value));
const endOfHeader = lines.findIndex((value) => /\r?\n\/\/ Project: https:\/\/[^\r\n]+/.test('\n' + value));
const header = lines.slice(startOfHeader, endOfHeader - startOfHeader + 1);
jsEmittedFileLines.splice(0, 0, ...header);
fs.writeFileSync(fullpath, jsEmittedFileLines.join('\n'));
hasError = false;
};
};
hasError ? packagesHaveError++ : null;
};
return { errorMessages, packagesHaveError };
};
function printMessages (messages: string[], packagesHaveError: number) {
console.log(`Found ${messages.length} errors in ${packagesHaveError} scripts.`);
for (const message of messages) {
console.error(message);
}
};
// main entry
export function execute () {
const results = checkScripts();
const results2 = checkEditorExtensions();
const statusCode = results.packagesHaveError + results2.packagesHaveError > 0 ? 1 : 0;
printMessages(results.errorMessages, results.packagesHaveError);
printMessages(results2.errorMessages, results2.packagesHaveError);
console.log('Exit task with code', statusCode);
return statusCode;
};