forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackageCheck.ts
More file actions
124 lines (103 loc) · 3.55 KB
/
packageCheck.ts
File metadata and controls
124 lines (103 loc) · 3.55 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
// Using validate-npm-package-name
import path from "path";
import { printFilePath, scripts, scriptsPath } from "./utils";
const regexValidate = /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/gm;
const builtinModules = [
"@minecraft/server",
"@minecraft/server-ui",
"@minecraft/server-net",
"@minecraft/server-gametest",
"@minecraft/server-admin",
"@minecraft/server-editor",
"mojang-minecraft",
"mojang-minecraft-ui"
];
var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$');
interface ErrorPack {
warnings: string[],
errors: string[],
path: string;
};
const errorPacks: ErrorPack[] = [];
var blacklist = [
'node_modules',
'favicon.ico',
];
function validate (name: string) {
var warnings: string[] = []
var errors: string[] = []
if (name === null) {
errors.push('name cannot be null');
return { warnings, errors };
};
if (name === undefined) {
errors.push('name cannot be undefined');
return { warnings, errors };
};
if (typeof name !== 'string') {
errors.push('name must be a string')
return { warnings, errors };
};
if (!name.length) errors.push('name length must be greater than zero')
if (name.match(/^\./)) errors.push('name cannot start with a period')
if (name.match(/^_/)) errors.push('name cannot start with an underscore')
if (name.trim() !== name) errors.push('name cannot contain leading or trailing spaces')
// No funny business
blacklist.forEach(function (blacklistedName) {
if (name.toLowerCase() === blacklistedName) {
errors.push(blacklistedName + ' is a blacklisted name')
}
})
// Generate warnings for stuff that used to be allowed
// core module names like http, events, util, etc
builtinModules.forEach(function (builtin) {
if (name.toLowerCase() === builtin) {
warnings.push(builtin + ' is a core module name')
}
})
if (name.length > 214) warnings.push('name can no longer contain more than 214 characters');
// mIxeD CaSe nAMEs
if (name.toLowerCase() !== name) warnings.push('name can no longer contain capital letters')
if (/[~'!()*]/.test(name.split('/').slice(-1)[0])) warnings.push('name can no longer contain special characters ("~\'!()*")')
if (encodeURIComponent(name) !== name) {
// Maybe it's a scoped package name, like @user/package
var nameMatch = name.match(scopedPackagePattern)
if (nameMatch) {
var user = nameMatch[1]
var pkg = nameMatch[2]
if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
return { warnings, errors };
}
}
errors.push('name can only contain URL-friendly characters')
}
return { warnings, errors };
}
export function execute () {
let errorsLength = 0;
let warningsLength = 0;
for (const scriptName of scripts) {
const scriptPath = path.resolve(scriptsPath, scriptName);
const { warnings, errors } = validate(scriptName);
if (warnings.length <= 0 && errors.length <= 0) continue;
errorPacks.push({
warnings,
errors,
path: scriptPath
});
errorsLength += errors.length;
warningsLength += warnings.length;
};
console.log(`Found ${errorsLength} errors and ${warningsLength} warnings.`);
for (const errorPack of errorPacks) {
const filepathDisplay = printFilePath(errorPack.path);
for (const error of errorPack.errors) {
console.error(filepathDisplay + ':', error);
};
for (const warn of errorPack.warnings) {
console.warn(filepathDisplay + ':', warn);
};
}
const statusCode = errorPacks.length > 0 ? 1 : 0;
return statusCode;
}