I need to be able to loop through an array of file paths and filter out files that do not contain a particular string within them. The target array returns an empty array, but instead I want the target array to include the list of files that do not contain the string, with results parsed to remove a substring up to the last instance of the delimiter.
const fs = require('fs')
const fileList = ['/L1/L2/file1', '/L1/L2/file2', '/L1/L2/file3', '/L1/L2/file4']
const filterText = 'filter files that contain me'
let targetArray = []
async function parseIncompleteDocumentation() {
try {
const delimiter = '/'
for (let componentFile of coveredComponentsDirectoryList)
return new Promise((resolve, reject) => {
fs.readFile(componentFile, 'utf8', function(err, content) {
if (err) {
reject(err)
} else if (!content.includes(incompleteDocumentationFilter)) {
resolve(
existingIncompleteComponentDocumentationList.push(
componentFile.substring(
componentFile.lastIndexOf(delimiter) + 1,
componentFile.length
)
)
)
}
})
}
)
} catch (err) {
return console.log('error message', err) // eslint-disable-line no-console
}
}
const finalArray = await parseFiles()
console.log(finalArray) // returns empty array
If file1 and file3 contain filterText, desired output would be
['file2', 'file4']