import cds from "@sap/cds"; import {struct} from "@sap/cds/apis/csn"; import fs from "fs/promises"; import {toSnakeCase} from "js-convert-case"; import {Command} from "commander"; const program = new Command(); interface Options { schema: string; namespace: string; prefix: string; case: string; } program .option("-s, --schema ", "imported CDS") .option("-n, --namespace ", "imported namespace") .option("-p, --prefix ", "new namespace") .option("-c, --case ", "case to use: camel|snake|kebab") .parse(process.argv); class Main { static async formatCDS(options: Options) { let {schema, namespace, prefix} = options; const csn = await cds.load(schema); let model = `using {${namespace}} from './${schema.replace( ".cds", "" )}';\n`; Object.keys(csn.definitions) .filter((key) => key.startsWith(`${namespace}.`)) .forEach((key) => { let entity = csn.definitions[key] as struct; switch (entity.kind) { case "entity": model += `entity ${prefix}.${key} as projection on ${key} {\n`; Object.keys(entity.elements).forEach((element, index, array) => { // to be extended with more cases if needed let alias = options.case === "snake" ? toSnakeCase(element) : element; //tab model += "\t"; // element or alias model += element === alias ? element : `${element} as ${alias}`; // no comma for last element index === array.length || (model += ","); // line brake model += "\n"; }); model += "};\n"; break; default: break; } }); fs.writeFile(`${prefix}.cds`, model); } } //console.log(program.opts()); Main.formatCDS(program.opts() as Options);