添加 YAML 导器
This commit is contained in:
@@ -5,6 +5,8 @@ import { JSONExporter } from "./exporters/JSONExporter";
|
||||
import { CSharpExporter } from "./exporters/CSharpExporter";
|
||||
import * as colors from "colors";
|
||||
import { TypeScriptExporter } from "./exporters/TypeScriptExporter";
|
||||
import * as yaml from "js-yaml";
|
||||
import { YAMLExporter } from "./exporters/YAMLExporter";
|
||||
|
||||
export interface Configurations {
|
||||
/** 解析配置 */
|
||||
@@ -20,6 +22,7 @@ const exporters: {[key:string]: new(config: ExporterConfigs) => TableExporter }
|
||||
json: JSONExporter,
|
||||
csharp: CSharpExporter,
|
||||
typescript: TypeScriptExporter,
|
||||
yaml: YAMLExporter,
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +35,7 @@ export class ExcelExporterApplication {
|
||||
|
||||
constructor(config_file: string) {
|
||||
let file = FileAccess.open(config_file, ModeFlags.READ);
|
||||
this.configs = JSON.parse(file.get_as_utf8_string()) as Configurations;
|
||||
this.configs = yaml.load(file.get_as_utf8_string()) as Configurations;
|
||||
file.close();
|
||||
this.parser = new TableParser(this.configs.parser);
|
||||
|
||||
@@ -47,9 +50,9 @@ export class ExcelExporterApplication {
|
||||
}
|
||||
|
||||
parse() {
|
||||
for (const item of this.configs.input) {
|
||||
console.log(colors.grey(`解析配表文件: ${item.file}`));
|
||||
let sheets = this.parser.parse_xlsl(item.file);
|
||||
for (const file of this.configs.input) {
|
||||
console.log(colors.grey(`解析配表文件: ${file}`));
|
||||
let sheets = this.parser.parse_xlsl(file);
|
||||
for (const name in sheets) {
|
||||
this.tables[name] = sheets[name];
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ export class TableExporter {
|
||||
this.configs = configs;
|
||||
}
|
||||
|
||||
get extension(): string { return ''}
|
||||
|
||||
protected line(text = "", indent = 0) {
|
||||
let line = "";
|
||||
for (let i = 0; i < indent; i++) {
|
||||
|
||||
@@ -13,6 +13,7 @@ interface CSharpExporterConfigs extends ExporterConfigs {
|
||||
export class CSharpExporter extends TableExporter {
|
||||
protected declear_content = "";
|
||||
protected classes: string[] = [];
|
||||
get extension(): string { return 'cs' }
|
||||
|
||||
constructor(configs: ExporterConfigs) {
|
||||
super(configs);
|
||||
@@ -34,7 +35,6 @@ export class CSharpExporter extends TableExporter {
|
||||
this.declear_content += this.line("}");
|
||||
}
|
||||
|
||||
|
||||
export(name: string, table: TableData) {
|
||||
const base_type = (this.configs as CSharpExporterConfigs).base_type;
|
||||
let body = "";
|
||||
@@ -77,7 +77,7 @@ export class CSharpExporter extends TableExporter {
|
||||
|
||||
let file = path.join(this.configs.directory, (this.configs as CSharpExporterConfigs).file_name);
|
||||
if (!file.endsWith(".cs")) {
|
||||
file += ".cs";
|
||||
file += "." + this.extension;
|
||||
}
|
||||
this.save_text(file, this.declear_content.replace("%CLASSES%", class_text));
|
||||
console.log(colors.green(`\t${file}`));
|
||||
|
||||
@@ -17,6 +17,8 @@ export class JSONExporter extends TableExporter {
|
||||
}
|
||||
}
|
||||
|
||||
get extension(): string { return 'json'}
|
||||
|
||||
protected recursively_order_keys(unordered: object | Array<object>) {
|
||||
// If it's an array - recursively order any
|
||||
// dictionary items within the array
|
||||
@@ -37,18 +39,7 @@ export class JSONExporter extends TableExporter {
|
||||
return unordered;
|
||||
}
|
||||
|
||||
export(name: string, table: TableData) {
|
||||
const file = path.join(this.configs.directory, name + ".json");
|
||||
let headers = table.headers;
|
||||
let values = [];
|
||||
for (const row of table.values) {
|
||||
let new_row = {};
|
||||
for (let i = 0; i < headers.length; i++) {
|
||||
const field = headers[i];
|
||||
new_row[field.name] = row[i];
|
||||
}
|
||||
values.push(new_row);
|
||||
}
|
||||
protected get indent(): string {
|
||||
let indent = "";
|
||||
const configs = (this.configs as JSONExporterConfigs);
|
||||
if (configs.indent) {
|
||||
@@ -60,7 +51,26 @@ export class JSONExporter extends TableExporter {
|
||||
indent = configs.indent;
|
||||
}
|
||||
}
|
||||
const text = JSON.stringify(this.recursively_order_keys(values), null, indent);
|
||||
return indent;
|
||||
}
|
||||
|
||||
export_json_object(name: string, table: TableData) {
|
||||
let headers = table.headers;
|
||||
let values = [];
|
||||
for (const row of table.values) {
|
||||
let new_row = {};
|
||||
for (let i = 0; i < headers.length; i++) {
|
||||
const field = headers[i];
|
||||
new_row[field.name] = row[i];
|
||||
}
|
||||
values.push(new_row);
|
||||
}
|
||||
return this.recursively_order_keys(values);
|
||||
}
|
||||
|
||||
export(name: string, table: TableData) {
|
||||
const file = path.join(this.configs.directory, `${name}.${this.extension}`);
|
||||
const text = JSON.stringify(this.export_json_object(name, table), null, this.indent);
|
||||
this.save_text(file, text);
|
||||
console.log(colors.green(`\t ${name} ==> ${file}`));
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ interface TypeScriptExporterConfigs extends ExporterConfigs {
|
||||
export class TypeScriptExporter extends TableExporter {
|
||||
|
||||
classes: string[] = [];
|
||||
get extension(): string { return 'ts'}
|
||||
|
||||
constructor(configs: TypeScriptExporterConfigs) {
|
||||
super(configs);
|
||||
|
||||
36
src/excel-exporter/exporters/YAMLExporter.ts
Normal file
36
src/excel-exporter/exporters/YAMLExporter.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { ExporterConfigs } from "excel-exporter/TableExporter";
|
||||
import { TableData } from "excel-exporter/TableParser";
|
||||
import { path } from "tiny/path";
|
||||
import * as colors from "colors";
|
||||
import { JSONExporter } from "./JSONExporter";
|
||||
import * as yaml from "js-yaml";
|
||||
|
||||
interface YAMLExporterConfigs extends ExporterConfigs {
|
||||
/** 缩进字符 */
|
||||
indent: number;
|
||||
}
|
||||
|
||||
export class YAMLExporter extends JSONExporter {
|
||||
|
||||
get extension(): string { return 'yaml'}
|
||||
|
||||
constructor(configs: ExporterConfigs) {
|
||||
super(configs);
|
||||
if ( typeof ((this.configs as YAMLExporterConfigs).indent) != 'number') {
|
||||
(this.configs as YAMLExporterConfigs).indent = 2;
|
||||
}
|
||||
}
|
||||
|
||||
export(name: string, table: TableData) {
|
||||
const file = path.join(this.configs.directory, `${name}.${this.extension}`);
|
||||
const text = yaml.dump(
|
||||
this.export_json_object(name, table),
|
||||
{
|
||||
indent: this.indent.length,
|
||||
sortKeys: true,
|
||||
}
|
||||
);
|
||||
this.save_text(file, text);
|
||||
console.log(colors.green(`\t ${name} ==> ${file}`));
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import * as colors from "colors";
|
||||
(async function main(argv: string[]) {
|
||||
|
||||
let config_file = argv[argv.length - 1];
|
||||
if (config_file.endsWith(".json") && FileAccess.exists(config_file)) {
|
||||
if (config_file.endsWith(".yaml") && FileAccess.exists(config_file)) {
|
||||
let app = new ExcelExporterApplication(config_file);
|
||||
app.parse();
|
||||
app.export();
|
||||
|
||||
Reference in New Issue
Block a user