Skip to content

Commit c73c7fa

Browse files
BrigittaK307KavithaSivadeekshas8cloud-sdk-js
authored
feat: esm support on Odata (#6061)
* First modifications: ESM support for OData * Added one test case for ESM * Added ESM flag and parsed it in the neccessary functions * Added changeset file * Modified changeset file * undo esm config settings * Added ESM support for the remaining files and corrected the changeset file * Fixed pipeline issues * Changes from lint:fix * Deleted ESM flag, reverted index.js file * Changes from lint:fix * Solved PR comments * Reverted generator file and made tsConfig common for openapi and generator packages * Apply suggestion from @deekshas8 Co-authored-by: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com> * Apply suggestion from @deekshas8 Co-authored-by: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com> * Apply suggestion from @deekshas8 Co-authored-by: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com> * Modified tsConfig structure, tests and solved PR comments * Solved linter issues * Changes from lint:fix * Code refactoring and modification of tests * Solved linter issues * Changes from lint:fix * Solved errors from pipelines * Changes from lint:fix * Moved module type and renamed test cases * Changes from lint:fix * Moved esm flag outside describe * Added generateESM to ts-config and modified generator.ts * Changes from lint:fix * fix module and resolution for generateESM * remove deleted options * Changes from lint:fix * improve * Changes from lint:fix * Changes from lint:fix --------- Co-authored-by: KavithaSiva <32287936+KavithaSiva@users.noreply.github.com> Co-authored-by: deeksha sinha <deeksha.sinha@sap.com> Co-authored-by: cloud-sdk-js <cloud-sdk-js@github.com> Co-authored-by: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com>
1 parent 76f76de commit c73c7fa

File tree

43 files changed

+547
-282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+547
-282
lines changed

.changeset/honest-candles-kick.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sap-cloud-sdk/generator': minor
3+
'@sap-cloud-sdk/generator-common': minor
4+
---
5+
6+
[New Functionality] Introduce option `generateESM` in OData generator to generate ESM compatible code.

.github/actions/check-public-api/index.js

Lines changed: 35 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
2-
"name": "check-license",
3-
"version": "1.0.0",
4-
"description": "Checks if the licenses of the dependencies are allowed.",
5-
"license": "Apache-2.0",
6-
"private": true,
7-
"scripts": {
8-
"compile": "tsc -p tsconfig.json",
9-
"postcompile": "ncc build lib/build-packages/check-license/index.js --out ../../.github/actions/check-license/",
10-
"lint": "eslint --ignore-pattern '!index.ts' && prettier --check **/*.ts",
11-
"lint:fix": "eslint --ignore-pattern '!index.ts' --fix --quiet && prettier --write **/*.ts",
12-
"check:dependencies": "depcheck --skip-missing=true .",
13-
"all": "yarn && yarn run lint && yarn run compile"
14-
},
15-
"dependencies": {
16-
"@actions/core": "^1.10.1"
17-
},
18-
"devDependencies": {
19-
"@vercel/ncc": "^0.38.4",
20-
"depcheck": "^1.4.7",
21-
"eslint": "^8.57.0",
22-
"prettier": "^3.6.2",
23-
"typescript": "~5.9.3"
24-
}
25-
}
2+
"name": "check-license",
3+
"version": "1.0.0",
4+
"description": "Checks if the licenses of the dependencies are allowed.",
5+
"license": "Apache-2.0",
6+
"private": true,
7+
"scripts": {
8+
"compile": "tsc -p tsconfig.json",
9+
"postcompile": "ncc build lib/build-packages/check-license/index.js --out ../../.github/actions/check-license/",
10+
"lint": "eslint --ignore-pattern '!index.ts' && prettier --check **/*.ts",
11+
"lint:fix": "eslint --ignore-pattern '!index.ts' --fix --quiet && prettier --write **/*.ts",
12+
"check:dependencies": "depcheck --skip-missing=true .",
13+
"all": "yarn && yarn run lint && yarn run compile"
14+
},
15+
"dependencies": {
16+
"@actions/core": "^1.10.1"
17+
},
18+
"devDependencies": {
19+
"@vercel/ncc": "^0.38.4",
20+
"depcheck": "^1.4.7",
21+
"eslint": "^8.57.0",
22+
"prettier": "^3.6.2",
23+
"typescript": "~5.9.3"
24+
}
25+
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"compilerOptions": {
3-
"rootDir": "../../",
4-
"outDir": "./lib",
5-
"tsBuildInfoFile": "./lib/.tsbuildinfo",
6-
"composite": false,
7-
"incremental": true,
8-
"declaration": false,
9-
"esModuleInterop": true,
10-
"target": "ESNext",
11-
"module": "NodeNext"
12-
},
13-
"include": ["*.ts"],
14-
"exclude": []
15-
}
2+
"compilerOptions": {
3+
"rootDir": "../../",
4+
"outDir": "./lib",
5+
"tsBuildInfoFile": "./lib/.tsbuildinfo",
6+
"composite": false,
7+
"incremental": true,
8+
"declaration": false,
9+
"esModuleInterop": true,
10+
"target": "ESNext",
11+
"module": "NodeNext"
12+
},
13+
"include": ["*.ts"],
14+
"exclude": []
15+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"compilerOptions": {
3-
"outDir": "./lib",
4-
"tsBuildInfoFile": "./lib/.tsbuildinfo",
5-
"composite": false,
6-
"incremental": true,
7-
"declaration": false,
8-
"esModuleInterop": true,
9-
"skipLibCheck": true,
10-
"allowSyntheticDefaultImports": true,
11-
"target": "es2021",
12-
"module": "Node16",
13-
"lib": ["ES2021", "dom"]
14-
},
15-
"include": ["index.ts"],
16-
"exclude": []
17-
}
2+
"compilerOptions": {
3+
"outDir": "./lib",
4+
"tsBuildInfoFile": "./lib/.tsbuildinfo",
5+
"composite": false,
6+
"incremental": true,
7+
"declaration": false,
8+
"esModuleInterop": true,
9+
"skipLibCheck": true,
10+
"allowSyntheticDefaultImports": true,
11+
"target": "es2021",
12+
"module": "Node16",
13+
"lib": ["ES2021", "dom"]
14+
},
15+
"include": ["index.ts"],
16+
"exclude": []
17+
}

build-packages/get-changelog/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"lint": "eslint --ignore-pattern '!index.ts' && prettier --check **/*.ts",
1111
"lint:fix": "eslint --ignore-pattern '!index.ts' --fix --quiet && prettier --write **/*.ts",
1212
"check:dependencies": "depcheck --skip-missing=true .",
13-
1413
"all": "yarn && yarn run lint && yarn run compile"
1514
},
1615
"dependencies": {

packages/generator-common/src/file-writer/package-json.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @internal
3+
*/
4+
export type ModuleType = 'commonjs' | 'esm';
5+
16
/**
27
* @internal
38
*/
@@ -14,6 +19,10 @@ export interface PackageJsonOptions {
1419
* @internal
1520
*/
1621
description: string;
22+
/**
23+
* @internal
24+
*/
25+
moduleType?: ModuleType;
1726
}
1827

1928
/* eslint-disable valid-jsdoc */
@@ -23,7 +32,7 @@ export interface PackageJsonOptions {
2332
export function packageJsonBase(
2433
options: PackageJsonOptions
2534
): Record<string, any> {
26-
return {
35+
const basePackageJson: Record<string, any> = {
2736
name: options.npmPackageName,
2837
version: '1.0.0',
2938
description: options.description,
@@ -39,4 +48,10 @@ export function packageJsonBase(
3948
url: ''
4049
}
4150
};
51+
52+
if (options.moduleType === 'esm') {
53+
basePackageJson.type = 'module';
54+
}
55+
56+
return basePackageJson;
4257
}

packages/generator-common/src/internal.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export * from './options';
2+
export type { Options, ParsedOptions } from './options-parser';
3+
14
export * from './sdk-metadata';
25
export * from './compiler';
36
export * from './readme';
@@ -8,5 +11,4 @@ export * from './package-description';
811
export * from './ts-config';
912
export * from './options-parser';
1013
export * from './cli-parser';
11-
export * from './options';
1214
export * from './options-per-service';

packages/generator-common/src/options.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ export function getCommonCliOptions(serviceType: ServiceType) {
117117
describe: getReadmeText(serviceType),
118118
default: false,
119119
hidden: true
120+
},
121+
generateESM: {
122+
describe:
123+
'When enabled, all generated files follow the ECMAScript module syntax.',
124+
type: 'boolean',
125+
default: false
120126
}
121127
} as const;
122128
}
@@ -193,4 +199,8 @@ export interface CommonGeneratorOptions {
193199
* Generate default `README.md` files in the client directories.
194200
*/
195201
readme?: boolean;
202+
/**
203+
* Generate ECMAScript modules instead of CommonJS modules.
204+
*/
205+
generateESM?: boolean;
196206
}
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import { defaultTsConfig } from '@sap-cloud-sdk/generator-common/internal';
21
import mock from 'mock-fs';
3-
import { tsconfigJson } from './tsconfig-json';
4-
import type { ParsedGeneratorOptions } from './options';
2+
import { defaultTsConfig, tsconfigJson } from './ts-config';
53

64
describe('tsconfigJson', () => {
75
afterEach(() => {
86
mock.restore();
97
});
108

119
it('returns the default tsconfig if transpilation is enabled', async () => {
12-
const tsConfig = await tsconfigJson({
13-
transpile: true
14-
} as ParsedGeneratorOptions);
15-
expect(JSON.parse(tsConfig!)).toEqual(defaultTsConfig);
10+
const tsConfig = await tsconfigJson(true);
11+
expect(JSON.parse(tsConfig!)).toEqual(defaultTsConfig(false));
12+
});
13+
14+
it('returns the default tsconfig with ESM config when generateESM is true', async () => {
15+
const tsConfig = await tsconfigJson(true, undefined, true);
16+
expect(JSON.parse(tsConfig!)).toEqual(defaultTsConfig(true));
1617
});
1718

1819
it('returns undefined if transpilation is disabled', async () => {
19-
const tsConfig = await tsconfigJson({
20-
transpile: false
21-
} as ParsedGeneratorOptions);
20+
const tsConfig = await tsconfigJson();
2221
expect(tsConfig).toBeUndefined();
2322
});
2423

@@ -29,9 +28,7 @@ describe('tsconfigJson', () => {
2928
'customConfig.json': JSON.stringify(customConfig)
3029
}
3130
});
32-
const tsConfig = await tsconfigJson({
33-
tsconfig: './path/customConfig.json'
34-
} as ParsedGeneratorOptions);
31+
const tsConfig = await tsconfigJson(false, './path/customConfig.json');
3532
expect(JSON.parse(tsConfig!)).toEqual(customConfig);
3633
});
3734

@@ -42,18 +39,14 @@ describe('tsconfigJson', () => {
4239
'tsconfig.json': JSON.stringify(customConfig)
4340
}
4441
});
45-
const tsConfig = await tsconfigJson({
46-
tsconfig: './path'
47-
} as ParsedGeneratorOptions);
42+
const tsConfig = await tsconfigJson(false, './path');
4843
expect(JSON.parse(tsConfig!)).toEqual(customConfig);
4944
});
5045

5146
it('returns custom config content if custom file or directory does not exist', async () => {
5247
mock({});
53-
await expect(() =>
54-
tsconfigJson({
55-
tsconfig: './path'
56-
} as ParsedGeneratorOptions)
57-
).rejects.toThrow('Could not read tsconfig.json at ./path.');
48+
await expect(() => tsconfigJson(false, './path')).rejects.toThrow(
49+
'Could not read tsconfig.json at ./path.'
50+
);
5851
});
5952
});

0 commit comments

Comments
 (0)