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/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/program.ts b/src/program.ts index 3f056d0..a17f29a 100644 --- a/src/program.ts +++ b/src/program.ts @@ -27,7 +27,10 @@ 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) { @@ -84,9 +87,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)); + + const result: ICsn = JSON.parse(cds.compile.to.json(csn)); + + if (sort) { + result.definitions = Object.fromEntries( + Object.entries(result.definitions).sort((key, value) => + String(key[0]).localeCompare(value[0]) + ) + ); + } + + return result; } /** 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; } 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,