'use strict'; // panel/index.js, this filename needs to match the one registered in package.json const packageName = "exceltojson"; const fs = require('fs'); const { join } = require('path'); const path = require('path'); const xlsx = require('../node_modules/node-xlsx'); const Store = require('../node_modules/electron-store'); const Vue = require('../vue'); const store = new Store(); var s_data= {}; let json_name = '.json'; //生成的文件名字 exports.template = fs.readFileSync(join(__dirname, './index.html'), 'utf8') , exports.style = fs.readFileSync(join( __dirname,'./index.css'), 'utf8'), exports.$ = { app: '#app' } exports.ready = function() { new Vue({ el: this.$.app, data () { return { excel_dir:"", json_dir:"", ts_dir:"", msg:"", } }, created(){ //读取缓存的值 console.log('created') let store_excel = store.get('excel_dir'); let store_json = store.get('json_dir'); let ts_dir = store.get('ts_dir'); if(store_excel != undefined) { this.excel_dir = store_excel; } if(store_json != undefined) { this.json_dir = store_json } if(ts_dir != undefined) { this.ts_dir = ts_dir } }, methods: { onBtnClcik(){ // todo console.log('onBtnClcik') }, async btn_excel(){ const config = { type: 'directory', }; const res = await Editor.Dialog.select(config); if (!res.canceled) { let dir = res.filePaths[0]; store.set('excel_dir', dir); this.excel_dir = dir; } }, async btn_json(){ const config = { type: 'directory', }; const res = await Editor.Dialog.select(config); if (!res.canceled) { let dir =res.filePaths[0]; store.set('json_dir',dir); this.json_dir = dir; } }, async btn_ts(){ const config = { type: 'directory', }; const res = await Editor.Dialog.select(config); if (!res.canceled) { let dir =res.filePaths[0]; store.set('ts_dir',dir); this.ts_dir = dir; } }, btn_go(){ console.log('btn_go') if(this.excel_dir == '' || this.excel_dir == undefined) { alert('请输入excel所在目录'); return; } else if(this.json_dir == '' || this.json_dir == undefined) { alert('请输入json输出目录'); return; } if(this.json_dir.indexOf('resources') === -1) { alert('json输出目录必须为resources下目录'); return; } fs.readdir(path.join(this.excel_dir+'/'), (err, files)=>{ for(let i=0;i}\n\t* @memberof ${className}\n\t*/\n\tpublic list: Array<${interfaceName}> = [];\n`; const instanceStr = `\t/**\n\t* 配置单例\n\t*\n\t* @static\n\t* @returns {${className}}\n\t* @memberof ${className}\n\t*/\n\t${getInstanceStr}` const dataLengthStr = this.addDataLength(className); const loadConfigStr = this.loadConfigStr(file_name,className); const initStr = this.initStr(className); const exportStr = `\n\nexport default ${className}.getInstance();`; classStr += staticStr+listStr+instanceStr +loadConfigStr+'\n'+initStr+'\n'+ allKeyStr+'\n'+dataLengthStr + '\n}' ; let resultStr = classStr + exportStr+interfaceStr; console.log('resultStr',resultStr); fs.writeFile(dmUrl,resultStr,function(err){ if (err) { console.log('write is error...',err); } else { } }); }, getInstanceStr(className) { const loadStr = `public static getInstance(): ${className} { if (!${className}.instance) { ${className}.instance = new ${className}(); } return ${className}.instance; } ` return loadStr }, initStr(className) { const configStr = ` /** * 初始化 * * @memberof TableBigWheelConfigList */ public async init(): Promise { if(${className}.instance.list.length === 0) { await this.loadConfig(); } }` return configStr; }, loadConfigStr(file_name,className) { const index = this.json_dir.indexOf('resources'); const jsonDir = this.json_dir.substring(index+10,this.json_dir.length); const jsonPath = `${jsonDir}/${file_name}`; const configStr = ` /** * 加载文件 * * @return {*} {Promise} * @memberof LanguageList */ public async loadConfig(): Promise { const data: any = await new Promise((resolve: any, reject: any): any => { resources.load('${jsonPath}', (error: any, texture: any): any => { if (error) { console.log('loadRes error', '${jsonPath}', JSON.stringify(error)); resolve(null); } else { resolve(texture); } }); }); ${className}.instance.list = data.json.list; }` return configStr; }, addDataLength(className) { const dataLengthStr = ` /** * 数据长度 * * @return {*} {number} * @memberof ${className} */ public dataLength(): number { return this.list.length; }` return dataLengthStr; }, addKeyStr(key,type,className,titleDesc) { const keyStr = ` /** * ${titleDesc} * * @param {number} index * @return {*} {${type}} * @memberof ${className} */ public ${key}(index: number): ${type} { return this.list[index].${key}; }` return keyStr }, getTypeByStr(typeStr) { if(typeStr === 'int' || typeStr === 'float') { return 'number'; } else if(typeStr === 'float') { resultData[keyStr] = parseFloat(curDataList[index]) ; } else if(typeStr=== 'boolean') { return 'boolean'; } else { return 'string'; } } }, }) };