From c2cb29b46fbaaa4767299b6a7693f3ab0e9f1444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Garc=C3=ADa?= Date: Wed, 30 Mar 2022 10:46:24 +0200 Subject: [PATCH 1/4] Adding sort option to cli.ts --- src/cli.ts | 4 ++++ src/utils/types.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/src/cli.ts b/src/cli.ts index 221fbbe..d08d763 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -29,6 +29,10 @@ function main(): void { "-f, --format", "Flag, whether to format the outputted source code or not (will try to format with prettier rules in the project)" ) + .option( + "-s, --sort", + "Flag, whether to sort outputted source code or not" + ) .parse(process.argv); if (!process.argv.slice(2).length) { diff --git a/src/utils/types.ts b/src/utils/types.ts index 66f597a..2d263bc 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -161,4 +161,5 @@ export interface IOptions { debug: boolean; version: string; format: boolean; + sort: boolean; } From 0fe30898a9073cac9cc5c560914fc5f09b19c8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Garc=C3=ADa?= Date: Wed, 30 Mar 2022 11:56:24 +0200 Subject: [PATCH 2/4] Innitial upload csn results sort --- .vscode/launch.json | 3 ++- src/program.ts | 31 ++++++++++++++++++++++++++++--- tsconfig.json | 2 +- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 6e9a6ec..e77b7aa 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,7 +18,8 @@ "I", "--format", "--json", - "--debug" + "--debug", + "--sort" ], "runtimeArgs": ["-r", "ts-node/register"], "cwd": "${workspaceRoot}", diff --git a/src/program.ts b/src/program.ts index 3f056d0..d5235ba 100644 --- a/src/program.ts +++ b/src/program.ts @@ -27,13 +27,24 @@ export class Program { */ public async run(options: IOptions): Promise { // Load compiled CDS. - const jsonObj = await this.loadCdsAndConvertToJSON(options.cds); + const jsonObj = await this.loadCdsAndConvertToJSON( + options.cds, + options.sort + ); // Write the compiled CDS JSON to disc for debugging. if (options.json) { fs.writeFileSync(options.output + ".json", JSON.stringify(jsonObj)); } + // if (options.sort && typeof jsonObj === "unknow") { + // jsonObj.definitions = Object.fromEntries( + // Object.entries(jsonObj.definitions).sort((a, b) => + // String(a[0]).localeCompare(b[0]) + // ) + // ); + // } + // Parse compile CDS. const parsed = new CDSParser().parse(jsonObj as ICsn); @@ -84,9 +95,23 @@ export class Program { * @returns {Promise} * @memberof Program */ - private async loadCdsAndConvertToJSON(path: string): Promise { + private async loadCdsAndConvertToJSON( + path: string, + sort: boolean + ): Promise { const csn = await cds.load(path); - return JSON.parse(cds.compile.to.json(csn)); + + let result: ICsn = JSON.parse(cds.compile.to.json(csn)); + + if (sort) { + result.definitions = Object.fromEntries( + Object.entries(result.definitions).sort((a, b) => + String(a[0]).localeCompare(b[0]) + ) + ); + } + + return result; } /** diff --git a/tsconfig.json b/tsconfig.json index e67be2a..d6f2f2b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es2015", "module": "commonjs", - "lib": ["es6", "es2015", "es2016", "dom"], + "lib": ["es6", "es2015", "es2016", "es2020", "dom"], "rootDir": "./", "downlevelIteration": true, "strict": true, From 50c9843b0b2105f115a6c5f02bbb5baf66f5110c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Garc=C3=ADa?= Date: Wed, 30 Mar 2022 12:01:01 +0200 Subject: [PATCH 3/4] Improve sort --- src/program.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/program.ts b/src/program.ts index d5235ba..327965f 100644 --- a/src/program.ts +++ b/src/program.ts @@ -101,12 +101,12 @@ export class Program { ): Promise { const csn = await cds.load(path); - let result: ICsn = JSON.parse(cds.compile.to.json(csn)); + const result: ICsn = JSON.parse(cds.compile.to.json(csn)); if (sort) { result.definitions = Object.fromEntries( - Object.entries(result.definitions).sort((a, b) => - String(a[0]).localeCompare(b[0]) + Object.entries(result.definitions).sort((key, value) => + String(key[0]).localeCompare(value[0]) ) ); } From 4c4c60d38fe5ba369e783a6117c3c139f4e21e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Garc=C3=ADa?= Date: Wed, 30 Mar 2022 19:25:20 +0200 Subject: [PATCH 4/4] Deleting old sort code --- src/program.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/program.ts b/src/program.ts index 327965f..a17f29a 100644 --- a/src/program.ts +++ b/src/program.ts @@ -37,14 +37,6 @@ export class Program { fs.writeFileSync(options.output + ".json", JSON.stringify(jsonObj)); } - // if (options.sort && typeof jsonObj === "unknow") { - // jsonObj.definitions = Object.fromEntries( - // Object.entries(jsonObj.definitions).sort((a, b) => - // String(a[0]).localeCompare(b[0]) - // ) - // ); - // } - // Parse compile CDS. const parsed = new CDSParser().parse(jsonObj as ICsn);