diff --git a/abap-api-tools/src/ts/frontend.ts b/abap-api-tools/src/ts/frontend.ts index 996a5c0f..2a8e7f2a 100644 --- a/abap-api-tools/src/ts/frontend.ts +++ b/abap-api-tools/src/ts/frontend.ts @@ -88,7 +88,9 @@ export type UiConfigTableType = { export type UiConfigType = Record; -export type FrontendResultType = Record; +export type JsHtmlType = { js: string; html?: string }; + +export type FrontendResultType = Record; export class Frontend { private api_name: string; @@ -332,6 +334,11 @@ export class Frontend { const result: FrontendResultType = {}; for (const rfm_name of this.apilist) { + // + // flow: rfm + // + log.info(chalk(rfm_name)); + // check local annotations if (!this.abap.parameters[rfm_name]) { log.info(chalk.red(`${rfm_name} annotations not found`)); @@ -345,31 +352,22 @@ export class Frontend { if (Param.paramName.length > paramNameLen) paramNameLen = Param.paramName.length; } - paramNameLen += 4; // for leading comment of optional parameters - - // - // rfm header - // - - log.info(chalk(rfm_name)); - - // writers + paramNameLen += 4; // "buffer" for leading comment of optional parameters log.debug( - "output:", - this.argv.output, - "api:", - this.api_name, - "rfm:", - rfm_name + `output: ${this.argv.output} api: ${this.api_name} rfm: ${rfm_name}` ); + // writers const fileName = path.join( this.argv.output || "", this.api_name, rfm_name.replace(/\//g, "_").toLowerCase() ); + // + // javascript header + // const jsWriter = new Writer({ fileName: this.argv.cmd === Command.call && !this.argv.save @@ -378,12 +376,6 @@ export class Frontend { echoOnSave: this.argv.cmd === Command.call && !this.argv.save, }); - let htmlWriter: Writer = {} as Writer; - - // - // javascript header - // - jsWriter.write( `//\n// ${rfm_name} ${JSON.stringify(this.abap.stat[rfm_name]) .replace(/"|{|}/g, "") @@ -393,39 +385,32 @@ export class Frontend { jsWriter.write("// prettier-ignore"); jsWriter.write(`const parameters = {`); + let htmlWriter = {} as Writer; if (this.argv.ui) { htmlWriter = new Writer({ fileName: `${fileName}.html` }); // , this.argv.save as boolean); // // html header // - htmlWriter.write( - `` + .replace(/:/g, ": ")}\n\n${Signature} -->\n` ); } // - // parameters + // flow: parameters // - let paramClass = ""; for (const [, Param] of Object.entries(rfm)) { - // // param class header - // - if (paramClass !== Param.PARAMCLASS) { paramClass = Param.PARAMCLASS; jsWriter.write(`\n// ${ParamClassDesc[paramClass]} PARAMETERS\n`); } - // - // parameter init - // - + // parameter init, rfm call let paramText = Param.PARAMTEXT ? Param.PARAMTEXT : `no text (${this.argv.lang})`; @@ -472,7 +457,6 @@ export class Frontend { if (Param.paramType === ParamType.exception) { continue; } - // // param class header // @@ -483,9 +467,9 @@ export class Frontend { jsWriter.write("//\n"); if (htmlWriter instanceof Writer) { - htmlWriter.write("\n\n"); + htmlWriter.write( + `\n` + ); } } @@ -530,28 +514,29 @@ export class Frontend { } if (Param.paramType === ParamType.struct) { - this.structure_init({ + const struct = this.structure_init({ Param: Param, Field: Field as StructureType, - jsWriter: jsWriter, - htmlWriter: htmlWriter, }); + jsWriter.write(struct.js); + if (htmlWriter instanceof Writer) htmlWriter.write(struct.html); } if (Param.paramType === ParamType.table) { - this.table_init({ + const table = this.table_init({ Param: Param, Field: Field as StructureType, - jsWriter: jsWriter, - htmlWriter: htmlWriter, }); - this.structure_init({ + jsWriter.write(table.js); + if (htmlWriter instanceof Writer) htmlWriter.write(table.html); + + const struct = this.structure_init({ Param: Param, Field: Field as StructureType, - jsWriter: jsWriter, - htmlWriter: htmlWriter, altParamName: `${Param.paramName}_line`, }); + jsWriter.write(struct.js); + if (htmlWriter instanceof Writer) htmlWriter.write(struct.html); } } @@ -560,6 +545,9 @@ export class Frontend { result[rfm_name].html = htmlWriter.save(); } + // + // flow: Value Helps + // if (this.argv.ui && this.argv.helps) { if (!this.abap.helps || !this.abap.descriptors) { log.error("\nHelps or Descriptors annotations missing"); @@ -580,18 +568,15 @@ export class Frontend { return result; } - table_init(arg: { - Param: ParameterType; - Field: StructureType; - jsWriter?: Writer; - htmlWriter?: Writer; - }): { js: string; html: string } | void { - const jsWriter = arg.jsWriter ? arg.jsWriter : new Writer(), - htmlWriter = arg.htmlWriter ? arg.htmlWriter : new Writer(); - + table_init(arg: { Param: ParameterType; Field: StructureType }): JsHtmlType { + // js + const jsWriter = new Writer({ indent: 2 }); jsWriter.write(`const ${arg.Param.paramName} = [];`); + const result: JsHtmlType = { js: jsWriter.text }; + if (!this.argv.ui || !this.uiConfig.table) return result; - if (!htmlWriter || !this.uiConfig.table) return; + // html + const htmlWriter = new Writer({ indent: 4 }); const element_template = { ...(this.uiConfig.table as UiConfigTableType), @@ -602,7 +587,8 @@ export class Frontend { htmlWriter.write( `` ); - return; + result.html = htmlWriter.text; + return result; } // header @@ -662,18 +648,17 @@ export class Frontend { htmlWriter.write(element_template.footer); } - return { js: jsWriter.save(), html: htmlWriter.save() }; + result.html = htmlWriter.text; + return result; } structure_init(arg: { Param: ParameterType; Field: StructureType; - jsWriter: Writer; - htmlWriter?: Writer; altParamName?: string; - }): void { - const jsWriter = arg.jsWriter, - htmlWriter = arg.htmlWriter, + }): JsHtmlType { + const jsWriter = new Writer({ indent: 2 }), + htmlWriter = this.argv.ui ? new Writer({ indent: 4 }) : undefined, paramName = arg.altParamName ? arg.altParamName : arg.Param.paramName; jsWriter.write("\n// prettier-ignore"); jsWriter.write(`const ${paramName} = {`); @@ -716,6 +701,11 @@ export class Frontend { jsWriter.write("};"); jsWriter.write(); if (htmlWriter instanceof Writer) htmlWriter.write(); + + // result + const result: JsHtmlType = { js: jsWriter.text }; + if (htmlWriter) result.html = htmlWriter.text; + return result; } field_length(Field: FieldType): string { diff --git a/abap-api-tools/src/ts/utils.ts b/abap-api-tools/src/ts/utils.ts index bbea721e..497d3c83 100644 --- a/abap-api-tools/src/ts/utils.ts +++ b/abap-api-tools/src/ts/utils.ts @@ -110,10 +110,11 @@ export class Writer { if (!context) context = {} as IWriter; this.fileName = context.fileName || ""; this.echoOnSave = context.echoOnSave || false; - this.indent_step = context.indent || 2; - - this.indent_step = FileType.js === this.fileName.slice(-2) ? 2 : 4; - this.indent_step = 2; + if (context.indent) { + this.indent_step = context.indent; + } else { + this.indent_step = FileType.html === this.fileName.slice(-2) ? 4 : 2; + } this.indent_count = 0; this.indent = ""; this.output = []; @@ -152,10 +153,11 @@ export class Writer { this.output.push(this.NEWLINE); } - save(): string { + save(fileName?: string): string { const ms: string = this.output.join(this.NEWLINE); - if (this.fileName) { - const stream = fs.createWriteStream(this.fileName); + fileName = fileName || this.fileName; + if (fileName) { + const stream = fs.createWriteStream(fileName); stream.once("open", () => { stream.write(ms); stream.end(); diff --git a/abap-api-tools/src/ts/valuehelp.ts b/abap-api-tools/src/ts/valuehelp.ts index 4029cde7..157d0546 100644 --- a/abap-api-tools/src/ts/valuehelp.ts +++ b/abap-api-tools/src/ts/valuehelp.ts @@ -167,7 +167,7 @@ const helpOption = [ } const table = this.frontend.table_init({ Param: Parameter, Field: Field }); if (table) { - result.resultTable = table.html; + result.resultTable = table.html || ""; } return result;