-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate.js
More file actions
122 lines (110 loc) · 3.2 KB
/
validate.js
File metadata and controls
122 lines (110 loc) · 3.2 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
import config from "./src/pluginConfig.js";
import { exit } from "process";
/**
*
* @param {*} name
* @param {*} obj
* @param {'action' | 'condition' | 'expression'} aceType
* @returns
*/
function validateDisplayText(name, obj, aceType) {
// check if obj has displayText property
if (obj.hasOwnProperty("displayText")) {
const paramCount = (obj.params || []).length;
// use regex to find the number of {/d} in displayText
const regex = /{\d+}/g;
const matches = obj.displayText.match(regex);
const matchCount = matches ? matches.length : 0;
// check if the number of {/d} matches the number of params
if (paramCount !== matchCount) {
console.log(
`Error: ${name} has ${matchCount} {x} in displayText but has ${paramCount} params`
);
return false; // validation failed
}
}
return true; // validation passed
}
/**
*
* @param {*} name
* @param {*} obj
* @param {'action' | 'condition' | 'expression'} aceType
* @returns
*/
async function ensureForwardScript(name, obj, aceType) {
const file = await import("./src/instance.js");
const fileClass = file.getInstanceJs(
class {
_getInitProperties() {
return {
// data to be saved for savegames
};
}
runtime = {
sdk: {
addLoadPromise: () => {},
},
};
_postToDOMAsync = () => new Promise(() => {});
_addDOMMessageHandler = () => {};
},
undefined,
undefined
);
globalThis.sdk = "v2";
const instance = new fileClass();
if (obj.handler) {
return true;
}
if (!(obj.forward in instance)) {
console.error(
`Missing ${obj.forward} ${aceType} in "instance.js" file for ${name}`
);
return false;
}
return true;
}
const validationPipeline = [validateDisplayText, ensureForwardScript];
async function valiatePlugin(pluginConfig) {
//assume file is valid
/** @type {Promise<() => boolean>[]} */
const validations = [];
// iterate over all the actions
Object.keys(pluginConfig.Acts).forEach((key) => {
const action = pluginConfig.Acts[key];
validationPipeline.forEach((validationFunction) => {
validations.push(() => validationFunction(key, action, "action"));
});
});
// iterate over all the conditions
Object.keys(pluginConfig.Cnds).forEach((key) => {
const condition = pluginConfig.Cnds[key];
validationPipeline.forEach((validationFunction) => {
validations.push(() => validationFunction(key, condition, "condition"));
});
});
// iterate over all the expressions
Object.keys(pluginConfig.Exps).forEach((key) => {
const expression = pluginConfig.Exps[key];
validationPipeline.forEach((validationFunction) => {
validations.push(() => validationFunction(key, expression, "expression"));
});
});
// check if any validation failed
let isValid = [];
for (const validation of validations) {
const result = await validation();
isValid.push(result);
}
return isValid.every((value) => value === true);
}
const validationResult = await valiatePlugin(config);
// check if the file is valid
if (validationResult) {
console.info("Validation Passed");
exit(0);
} else {
console.error("Validation Failed");
exit(1);
}