const fs = require('fs')
module.exports = {
    setData(filePath, updateData = [], savePath = '') {
        let fileData = this.read(filePath)
        for (let i in updateData) {
            fileData = this.replaceFile(fileData, i, updateData[i])
        }
        this.save(savePath ? savePath : filePath, fileData)
    },
    read(filePath) {
        let file = fs.readFileSync(filePath, {
            encoding: 'utf-8'
        })
        return file
    },
    replaceFile(fileData, path, value) {
        let fileArr = fileData.split(/;\n/)
        for (let index = 0; index < fileArr.length; index++) {
            const item = fileArr[index]
            const itemArr = item.split(':')
            if (path === itemArr[0]) {
                fileArr[index] = `${itemArr[0]}: ${value};`
                break;
            }
        }
        return fileArr.join('\n')
    },
    save(filePath, fileData) {
        fs.writeFileSync(filePath, fileData, {
            "flag": "w"
        })
    }
}
