123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- const fs = require('fs')
- const path = require('path')
- const XLSX = require('xlsx')
- const {MongoConf} = require('./config')
- const MongoClient = require('mongodb').MongoClient
- const assert = require('assert')
- const dbIns = MongoConf['R1']
- const connStr = `mongodb://${dbIns.host}:${dbIns.port}`
- const connOpt = {
- auth : {
- user: dbIns.username,
- password: dbIns.password,
- },
- authSource: dbIns.dbname,
- authMechanism: "SCRAM-SHA-1",
- useNewUrlParser: true
- }
- const dataPath = "../assets/scripts/data"
- const dataPrefix = "allstar_"
- const getSheet = function (filename) {
- let filePath = path.join(__dirname, "/excel", filename)
-
- let workbook = XLSX.readFileSync(filePath)
- let sheets = workbook.SheetNames
- let sheetsObj = {}
-
- sheets.forEach(n => {
- let ws = workbook.Sheets[n]
- let def = XLSX.utils.sheet_to_json(ws, {range: 0, header: 1})
- let typeMap = {}
- let typeArr = def[0]
- let keyArr = def[2]
- keyArr.forEach((item, index) => {
- typeMap[item] = typeArr[index]
- })
- let res = XLSX.utils.sheet_to_json(ws, {range: 2})
- res.forEach(item => {
- for(let i in item) {
- item[i] = formatType(item[i], typeMap[i])
- }
- })
- sheetsObj[n] = res
- })
- return sheetsObj
-
-
-
- }
- const writeToAssets = function (filename, content) {
- let writeFileName = filename.replace(/\.xlsx|\.xls/, '.js').replace(dataPrefix, "")
- let writePath = path.join(__dirname, dataPath, writeFileName)
- fs.writeFileSync(writePath, 'module.exports=' + JSON.stringify(content))
-
- }
- const writeJson = function (filename, content) {
- let writeFileName = filename.replace(/\.xlsx|\.xls/, '.json').replace(dataPrefix, "")
- let writePath = path.join(__dirname, "/sheet", writeFileName)
- fs.writeFileSync(writePath, JSON.stringify(content))
- }
- const formatType = function(target, type) {
- let _type = type.toLowerCase()
- switch (_type) {
- case 'integer':
- target = parseInt(target, 10)
- break;
- case 'double':
- target = parseFloat(target)
- break;
- case 'string':
- target = target.toString()
- break;
- case 'object':
- target = JSON.parse(target)
- break;
- case 'array':
- target = JSON.parse(target)
- break;
- default:
- break;
- }
- return target
- }
- const init = function () {
- fs.readdir("./excel", (err, file) => {
- file.forEach(n => {
- if(n.match(/\~\$/)) {
- return
- }
- let ws = getSheet(n)
- let sheetName = n.replace(/\.xlsx|\.xls/, "")
- let target
- if(ws.length > 0) {
-
-
-
-
-
-
- target = ws
- } else {
-
- let _array = []
- for(let i in ws) {
- _array = _array.concat(ws[i])
- }
- target = _array
- }
- MongoClient.connect(connStr, connOpt, (err, client) => {
- var db = client.db(dbIns.dbname)
- var collection = db.collection(sheetName)
- collection.drop(null, () => {
- collection.insert(target, () => {
- console.log(`成功更新配置表:${sheetName}`)
- client.close()
- })
- })
- });
-
- let wsKeys = Object.keys(ws)
- if(wsKeys.length == 1) {
- writeToAssets(n, ws[wsKeys[0]])
- writeJson(n, ws[wsKeys[0]])
- } else {
- writeToAssets(n, ws)
- writeJson(n, ws)
- }
- })
-
- })
- }
- init()
|