const fs = require('fs');
const root = "./src"
const savePath = "./src/i18n/languages/en_US.json"
let allFiles = []

function getFiles(dir){
    let files = fs.readdirSync(dir)
    files.forEach(file => {
        let path = dir + '/' + file
        if(fs.lstatSync(path).isDirectory()){
            getFiles(path)
        }else allFiles.push(path)
    })
}

getFiles(root)
/* allFiles = [
    'src/views/ecw/box/shippingSea/utils.js',
    'src/components/bpmnProcessDesigner/package/designer/plugins/content-pad/contentPadProvider.js'
]
console.log(allFiles) */
let messages = new Set()
allFiles.forEach(file => {
    let data = fs.readFileSync(file, {encoding: 'utf-8'})
    let matched = data.matchAll(/\$t\([\'\"]{1}([^\"\']+)[\'\"]{1}/g)
    let i = 0
    for(let item of matched){
        i ++
        messages.add(item[1])
    }
})


let obj = require(savePath)
Array.from(messages).forEach(word => {
    if(!obj[word])obj[word] = ""
})
fs.writeFileSync(savePath, JSON.stringify(obj, null, 4))
console.log(messages)
console.warn(`本次共提取 ${messages.size} 个待翻译内容，保存于${savePath}`)