Created
November 7, 2019 15:15
-
-
Save waghcwb/3b8dc2f0c1cff6ab964c6b2b42b3f62d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { readFile: originalReadFile, writeFile: originalWriteFile } = require('fs') | |
const { promisify } = require('util') | |
const { resolve, join } = require('path') | |
const originalGlob = require('glob') | |
const glob = promisify(originalGlob) | |
const readFile = promisify(originalReadFile) | |
const writeFile = promisify(originalWriteFile) | |
class Storefront { | |
static async getFiles() { | |
return glob( | |
join( | |
resolve('../../'), | |
'**/*.{tag,jsp}' | |
) | |
) | |
} | |
static async getHardcodedStrings(content, filename = '') { | |
const getTextInHtmlTag = | |
content | |
.replace(/(?=<%--)([\s\S]*?)--%>/gm, '') | |
.replace(/".*?"/gm, '""') | |
.replace(/<c:if\b[^>]*>([\s\S]*?)<\/c:if>/gm, '') | |
.replace(/(?=<!--)([\s\S]*?)-->/gm, '') | |
.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/gm, '') // remove script tags | |
.replace(/(?=\${)([\s\S]*?)}/gm, '') // replace jsp variables | |
.replace(/\s\s+/gm, ' ') // remove repeated spaces | |
.replace(/\n/gm, '') // remove line breaks | |
.replace(/(?=<)([\s\S]*?)>/gm, '') | |
.replace(/(?={)([\s\S]*?)}/gm, '') | |
.replace(/ /gm, '') | |
.replace(/×/gm, '') | |
.replace(/—/gm, '') | |
.replace(/[\W]/gm, '') | |
.replace(/\s\s+/gm, ' ') // remove repeated spaces | |
.replace(/^\s/gm, ' ') // remove repeated spaces | |
if (content.indexOf('contentType="application/json"') > 0) { | |
return '' | |
} | |
if (filename.includes('tomcat-8.5.32')) { | |
return '' | |
} | |
if (filename.includes('WEB-INF/jsp/')) { | |
return '' | |
} | |
return getTextInHtmlTag.trim() | |
} | |
} | |
async function main() { | |
const filesWithHardcodedStrings = [] | |
for (let file of await Storefront.getFiles()) { | |
const content = (await readFile(file)).toString() | |
if ((await Storefront.getHardcodedStrings(content, file)).length) { | |
const item = file.split('webroot/').pop() | |
if (!filesWithHardcodedStrings.includes(item)) { | |
filesWithHardcodedStrings.push(item) | |
} | |
} | |
} | |
return filesWithHardcodedStrings | |
} | |
main() | |
.then(async (files) => { | |
console.log(files.join('\n')) | |
console.log('Total:', files.length) | |
await writeFile('files.txt', files.join('\n')) | |
}) | |
.catch(err => console.error(err.message)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment