Skip to content

Commit

Permalink
excel sync
Browse files Browse the repository at this point in the history
  • Loading branch information
linyngfly committed Jan 4, 2022
1 parent 25e57aa commit 0839c23
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 6 deletions.
5 changes: 2 additions & 3 deletions tools/omelox-excel/bin/omelox-excel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
require('cliff');
import { program } from 'commander';
import * as fs from 'fs';
import { isFunction } from 'util';
require('cliff');
const version = require('../../package.json').version;
const COMMAND_ERROR = ('Illegal command format. Use `omelox-excel --help` to get more info.\n' as any).red;

Expand All @@ -17,7 +16,7 @@ fs.readdirSync(__dirname + '/commands').forEach(function (filename) {
if (/\.js$/.test(filename)) {
let name = filename.substr(0, filename.lastIndexOf('.'));
let _command = require('./commands/' + name).default;
if (isFunction(_command)) {
if (typeof _command === 'function') {
_command(program);
}
}
Expand Down
8 changes: 7 additions & 1 deletion tools/omelox-excel/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const FIELD_TYPE = {
export const FIELD_RULE = {
/** 创建字段映射索引 */
INDEX: 'index',
/** KEY索引,用户常量data配置 */
KEY: 'key',
/** KEY索引,用户常量data配置 */
KEY_DESC: 'keydesc',
/** 创建多字段联合索引 */
INDEXS: 'indexs',
/** 检测字段唯一性 */
Expand All @@ -41,7 +45,9 @@ export const CONFIG_TYPE = {
/** 语言配置 */
LANG: 'lang',
/** 错误码配置 */
ERROR: 'error'
ERROR: 'error',
/** Data模板+常量配置 */
CONST_MODEL: 'const_model',
}

export enum DocConfigKey {
Expand Down
36 changes: 34 additions & 2 deletions tools/omelox-excel/lib/exportBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ export abstract class ExportBase {
classname: string,
}): void;

/**
* 生成数据配置模型
* @param filename 文件名
* @param content 文件功能描述
* @param datas 数据
*/
protected abstract genDataConstModel(filename: string, content: string, fields: string[], types: string[], descs: string[], isPublic: boolean, datas: any[]): void;

/**
* 生成常量模型
* @param filename 文件名
Expand Down Expand Up @@ -342,8 +350,32 @@ export abstract class ExportBase {
break;
}
}

this.genDataModel(filename, content, fields, types, descs, isPublic, isAppendData, parent_class);
} else if (config_type === CONFIG_TYPE.CONST_MODEL) {
let category = this.docConfigKey.get(DocConfigKey.category);
let filename = `${this.getHandlerOutDir()}/${category}`;

// 根据模型文件生成数据配置模型
let modelData = workBook.Sheets['default'];
if (!modelData) {
console.log(`${filePath} 无default表单`);
return;
}

let sheetJson = XLSX.utils.sheet_to_json<any>(modelData, { header: 1, raw: true, blankrows: false });

// 字段名
let fields = sheetJson[0] || [];
// 数据类型:unexport(不导出)、float(小数)、int(整数)、string(字符串)、table(对象)、key,cst第一列类型可以取key(map)、cst(常量)
// 字段类型约束:index->创建字段映射索引(id,index),indexs->创建多字段映射索引(类型,indexs,level|scene_id),
// unique->检测字段唯一性(name_key,unique),oc->客戶端专用(res_name,oc),os->服务端专用(baseRate,os)
let types = sheetJson[1] || [];
// 字段描述
let descs = sheetJson[2] || [];

const pathInfo = path.parse(filename);
this.mkdirsSync(pathInfo.dir);
this.genDataConstModel(filename, content, fields, types, descs, isPublic, sheetJson.slice(consts.CONFIG_SKIP_ROW));
}
}

Expand Down Expand Up @@ -397,7 +429,7 @@ export abstract class ExportBase {

if (config_type === CONFIG_TYPE.CONST) {
// 生成CONST模型
let modelData = workBook.Sheets['model'];
let modelData = workBook.Sheets['default'];
if (!modelData) {
console.log(`${filePath} 无template表单`);
return;
Expand Down
89 changes: 89 additions & 0 deletions tools/omelox-excel/lib/exportServerTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,95 @@ export class ${modelrName} extends ${parentClassname} {\r\n`
fs.writeFileSync(`${path.parse(filename).dir}/${modelrName}.ts`, str);
}

/**
* 生成常量DATA配置字段定义
* @param oriFilename 文件名
* @param datas 行数据
*/
protected _genConstDataFieldDefine(oriFilename: string, types: string[], datas: any[], TYPE: string) {
// 查找常量定义key,desc
let keyCol = null;
let keyDescCol = null;
for (let i = 0; i < types.length; i++) {
let typeRules = types[i].split(',');
if (typeRules.indexOf(FIELD_RULE.KEY) !== -1) {
keyCol = i;
}
if (typeRules.indexOf(FIELD_RULE.KEY_DESC) !== -1) {
keyDescCol = i;
}
}

if (null == keyCol || null == keyDescCol) {
throw `${oriFilename} 常量Data配置异常,未配置字段约束${FIELD_RULE.KEY}或者${FIELD_RULE.KEY_DESC}`;
}

let str = '';
for (let i = 0; i < datas.length; i++) {
let rowArray = datas[i];
const keyCst = rowArray[keyCol].toString().trim();
str += `\t/** ${rowArray[keyDescCol]} */\r\n`
str += `\t${keyCst}: ${TYPE};\r\n`
}

return str;
}

protected genDataConstModel(filename: string, content: string, fields: string[], types: string[], descs: string[], isPublic: boolean, datas: any[]): void {
const oriFilename = path.parse(filename).name;
let modelrName = `${oriFilename}_model`;
let str = '';
str += `import { config_data_file_name } from './config_data_file_name';\r\n`

str += `import { config_model_base } from './config_model';`

// 加入data item定义
str += `
export type ${modelrName}_data_item = {
${this._genFieldDefine(fields, types, descs)}
}`

str += `
/**
* ${content}
*/
export class ${modelrName} extends config_model_base {\r\n`
if (this.getModelGetUrl()) {
str += this.getModelGetUrl();
str += `\r\n`
}
// 字段定义

str += `\r\n`

str += this._genConstDataFieldDefine(filename, types, datas, `${modelrName}_data_item`);

str += `\r\n`
str += `\tpublic static getClassName(): string {
return \'${modelrName}\'
}
`

str += `
public static getConfigName(filename?: string): string {
return filename || config_data_file_name.${oriFilename};
}
`

if (isPublic) {
str += `
public static isPublic(): boolean {
return true;
}
`
}

str += `
}`;

fs.writeFileSync(`${path.parse(filename).dir}/${modelrName}.ts`, str);
}

protected genConstModel(filename: string, content: string, datas: any[]): void {
const oriFilename = path.parse(filename).name;
let modelrName = `${oriFilename}_model`;
Expand Down

0 comments on commit 0839c23

Please sign in to comment.