Skip to content

Commit

Permalink
Merge pull request #28 from kiko240/feature/sortResults
Browse files Browse the repository at this point in the history
Sorting CSN read results, so contract entities will be sorted
  • Loading branch information
HeneryHawk authored Jun 10, 2022
2 parents 5e4f063 + 4c4c60d commit 0ac26c8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"I",
"--format",
"--json",
"--debug"
"--debug",
"--sort"
],
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",
Expand Down
4 changes: 4 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 20 additions & 3 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export class Program {
*/
public async run(options: IOptions): Promise<void> {
// 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) {
Expand Down Expand Up @@ -84,9 +87,23 @@ export class Program {
* @returns {Promise<any>}
* @memberof Program
*/
private async loadCdsAndConvertToJSON(path: string): Promise<unknown> {
private async loadCdsAndConvertToJSON(
path: string,
sort: boolean
): Promise<unknown> {
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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ export interface IOptions {
debug: boolean;
version: string;
format: boolean;
sort: boolean;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0ac26c8

Please sign in to comment.