Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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(/&nbsp;/gm, '')
.replace(/&times;/gm, '')
.replace(/&mdash;/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