Small changes to build.js

archived
peshomir 2024-03-27 21:40:34 +02:00
parent 5fba0336a0
commit 7c787e8799
1 changed files with 8 additions and 5 deletions

View File

@ -8,10 +8,8 @@ fs.writeFileSync("./build/index.html", fs.readFileSync("./build/index.html").toS
let script = fs.readFileSync('./game/latest.js', { encoding: 'utf8' }).replace("\n", "").trim(); let script = fs.readFileSync('./game/latest.js', { encoding: 'utf8' }).replace("\n", "").trim();
const replaceOne = (expression, replaceValue) => { const replaceOne = (expression, replaceValue) => {
const result = expression.exec(script); const result = matchOne(expression);
if (result === null) throw new Error("no match for: ") + expression; // this (below) works correctly because expression.lastIndex gets reset above in matchOne when there is no match
if (expression.exec(script) !== null) throw new Error("more than one match for: " + expression);
// this (below) works correctly because expression.lastIndex gets reset in the line above when there is no match
script = script.replace(expression, replaceValue); script = script.replace(expression, replaceValue);
return result; return result;
} }
@ -36,7 +34,7 @@ const matchDictionaryExpression = expression => {
} }
// Return value example: // Return value example:
// When this function is called with "var1=var2+1;" as the code // When replaceRawCode or matchRawCode are called with "var1=var2+1;" as the code
// and this matches "a=b+1;", the returned value will be the object: { var1: "a", var2: "b" } // and this matches "a=b+1;", the returned value will be the object: { var1: "a", var2: "b" }
const replaceRawCode = (/** @type {string} */ raw, /** @type {string} */ result, nameMappings) => { const replaceRawCode = (/** @type {string} */ raw, /** @type {string} */ result, nameMappings) => {
const { expression, groups } = generateRegularExpression(raw, false, nameMappings); const { expression, groups } = generateRegularExpression(raw, false, nameMappings);
@ -47,6 +45,11 @@ const replaceRawCode = (/** @type {string} */ raw, /** @type {string} */ result,
const expressionMatchResult = replaceOne(expression, replacementString); const expressionMatchResult = replaceOne(expression, replacementString);
return Object.fromEntries(Object.entries(groups).map(([identifier, groupNumber]) => [identifier, expressionMatchResult[groupNumber]])); return Object.fromEntries(Object.entries(groups).map(([identifier, groupNumber]) => [identifier, expressionMatchResult[groupNumber]]));
} }
const matchRawCode = (/** @type {string} */ raw, nameMappings) => {
const { expression, groups } = generateRegularExpression(raw, false, nameMappings);
const expressionMatchResult = matchOne(expression);
return Object.fromEntries(Object.entries(groups).map(([identifier, groupNumber]) => [identifier, expressionMatchResult[groupNumber]]));
}
const generateRegularExpression = (/** @type {string} */ code, /** @type {boolean} */ isForDictionary, nameMappings) => { const generateRegularExpression = (/** @type {string} */ code, /** @type {boolean} */ isForDictionary, nameMappings) => {
const groups = {}; const groups = {};
let groupNumberCounter = 1; let groupNumberCounter = 1;