canihazusername/generate/wordlist.js

39 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2023-07-12 22:37:34 +02:00
import { readFileSync, readdirSync, statSync, writeFileSync } from 'fs'
import { basename, join } from 'path'
2020-02-05 21:21:51 +01:00
const endsWithTxt = /^.*\.txt$/
function walkDir(dir, callback) {
2023-07-12 22:37:34 +02:00
readdirSync(dir).forEach((f) => {
const dirPath = join(dir, f)
const isDirectory = statSync(dirPath).isDirectory()
isDirectory ? walkDir(dirPath, callback) : callback(join(dir, f))
2021-03-15 18:14:19 +01:00
})
2020-02-05 21:21:51 +01:00
}
function convertAndSaveWordlistAsJSON() {
2021-03-15 18:14:19 +01:00
const wordlist = {}
2020-02-05 21:21:51 +01:00
2021-03-15 18:14:19 +01:00
walkDir('./generate/wordlist', (filename) => {
// Not a txt file
if (!endsWithTxt.test(filename)) return
2020-02-05 21:21:51 +01:00
2021-03-15 18:14:19 +01:00
// Read the file
2023-07-12 22:37:34 +02:00
const file = readFileSync(filename, 'utf-8')
2020-02-05 21:21:51 +01:00
2021-03-15 18:14:19 +01:00
// Each line of the file to an array removing the empty lines
const lines = file
.split('\n')
.filter((entry) => entry !== '')
.map((entry) => entry.trim())
2020-02-05 21:21:51 +01:00
2023-07-12 22:47:42 +02:00
const set = new Set(lines) // remove duplicates
const name = basename(filename, 'utf-8').slice(0, -4) // trim .txt
2021-03-15 18:14:19 +01:00
wordlist[name] = [...set]
})
2020-02-05 21:21:51 +01:00
2023-07-12 22:47:42 +02:00
writeFileSync('./src/wordlist.json', JSON.stringify(wordlist), 'utf-8')
2020-02-05 21:21:51 +01:00
}
2021-03-15 18:14:19 +01:00
convertAndSaveWordlistAsJSON()