From dfc3e0f7275002f514629f52665013631bfb17a5 Mon Sep 17 00:00:00 2001 From: paf <paf@titelfrei.de> Date: Tue, 16 Mar 2021 08:55:12 +0100 Subject: [PATCH] makes the script show defaults as well to make it easier to update custom-texts.json after adding some new custom texts token. --- src/scripts/findCustomTexts.js | 48 ++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/scripts/findCustomTexts.js b/src/scripts/findCustomTexts.js index e63efb6c..fbdc6aa6 100644 --- a/src/scripts/findCustomTexts.js +++ b/src/scripts/findCustomTexts.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console,no-restricted-syntax */ // analysis of source code (html, ts): // listing of all used keys for customText @@ -8,31 +9,36 @@ const startFolder = '../app'; const mdSourceFilename = '../app/config/custom-texts.md'; const mdTargetFilename = '../../docs/custom-texts.md'; -let foundKeys = {}; -let foundSourceFiles = []; +const foundKeys = {}; +const foundSourceFiles = []; +const foundDefaults = {}; let foundError = false; -function analyse ( fileName, isHtml ) { +function analyse(fileName, isHtml) { const fileContent = fs.readFileSync(fileName, 'utf8').toString(); - const searchPattern = isHtml ? /\|\s*customtext:\s*'\w+'/g : /\.getCustomText\s*\(\s*'\w+'\s*\)/g; - const matches = fileContent.match(searchPattern); - if (matches) { - foundSourceFiles.push(fileName); - for (let i = 0; i < matches.length; i++) { - const posStart = matches[i].indexOf("'"); - const posEnd = matches[i].lastIndexOf("'"); - const foundKey = matches[i].substr(posStart + 1, posEnd - posStart - 1); - if (foundKeys[foundKey]) { - foundKeys[foundKey] += 1; - } else { - foundKeys[foundKey] = 1; - } + const searchPattern = isHtml ? + /["'`]([^"'`]+)["'`]\s*\|\s*customtext:\s*["'`](\w+)["'`]/g : + /(getCustomText)\(\s*["'`](\w+)["'`]/g; + let matches = fileContent.matchAll(searchPattern); + + for (const match of matches) { + if (foundSourceFiles.indexOf(fileName) === -1) { + foundSourceFiles.push(fileName); + } + const foundKey = match[2]; + const foundDefault = isHtml ? match[1] : '(none)'; + if (foundKeys[foundKey]) { + foundKeys[foundKey] += 1; + foundDefaults[foundKey].push(foundDefault); + } else { + foundKeys[foundKey] = 1; + foundDefaults[foundKey] = [foundDefault]; } } } -function takeFolder(sourceFolder ) { +function takeFolder(sourceFolder) { const dirEntries = fs.readdirSync(sourceFolder); for (let i = 0; i < dirEntries.length; i++) { const fullObjectName = sourceFolder + '/' + dirEntries[i]; @@ -61,11 +67,13 @@ console.log(); console.log('\x1b[33m%s\x1b[0m', 'used keys:'); for (const k of Object.keys(foundKeys)) { if (defaults[k]) { - console.log(` ${k}: ${foundKeys[k]}`); + console.log(`${k}: ${foundKeys[k]}`); + console.log(` - [${defaults[k].defaultvalue}]`); } else { foundError = true; - console.log('\x1b[31m%s\x1b[0m', ` ${k}: ${foundKeys[k]}`) + console.log('\x1b[31m%s\x1b[0m', `${k}: ${foundKeys[k]}`); } + console.log(` - ${foundDefaults[k].join('\n - ')}`); } console.log(); @@ -78,7 +86,7 @@ console.log(); console.log('\x1b[33m%s\x1b[0m', 'not used:'); for (const k of Object.keys(defaults).sort()) { if (!foundKeys[k]) { - console.log(` ${k}`); + console.log(`${k}`); } } -- GitLab