@sap-ux/odata-service-writer 0.19.2 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,12 +14,15 @@ const i18n_1 = require("../i18n");
14
14
  function getAnnotationNamespaces({ metadata, annotations }) {
15
15
  // Enhance service with annotations namespaces
16
16
  const schemaNamespaces = metadata ? getNamespaces(metadata) : [];
17
- if (annotations === null || annotations === void 0 ? void 0 : annotations.xml) {
17
+ const edmxAnnotations = annotations;
18
+ if (edmxAnnotations === null || edmxAnnotations === void 0 ? void 0 : edmxAnnotations.xml) {
18
19
  // Parse once
19
- const annotationsJson = xmlToJson(annotations.xml);
20
+ const annotationsJson = xmlToJson(edmxAnnotations.xml);
20
21
  return schemaNamespaces.map((schema) => {
21
22
  // Check if alias exists in backend annotation file, if so use it
22
- const annotationAlias = annotations.xml && schema.namespace ? getAliasFromAnnotation(annotationsJson, schema.namespace) : '';
23
+ const annotationAlias = edmxAnnotations.xml && schema.namespace
24
+ ? getAliasFromAnnotation(annotationsJson, schema.namespace)
25
+ : '';
23
26
  if (annotationAlias) {
24
27
  schema.alias = annotationAlias;
25
28
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.enhanceData = void 0;
4
+ const updates_1 = require("../updates");
4
5
  const constants_1 = require("./constants");
5
6
  /**
6
7
  * Sets the default path for a given service.
@@ -41,9 +42,17 @@ function setDefaultServiceModel(service) {
41
42
  * @param {OdataService} service - The service object whose annotations name needs to be set or modified.
42
43
  */
43
44
  function setDefaultAnnotationsName(service) {
44
- var _a, _b, _c, _d;
45
- if (((_a = service.annotations) === null || _a === void 0 ? void 0 : _a.technicalName) && !service.annotations.name) {
46
- service.annotations.name = (_d = (_c = (_b = service.annotations) === null || _b === void 0 ? void 0 : _b.technicalName) === null || _c === void 0 ? void 0 : _c.replace(/\//g, '_')) === null || _d === void 0 ? void 0 : _d.replace(/^_/, '');
45
+ var _a, _b;
46
+ /**
47
+ * In the manifest EJS template, annotation names are referred to for adding annotations to the manifest.json.
48
+ * For CAP projects, annotations are added to the annotations.cds file and not to the manifest.json.
49
+ */
50
+ if ((0, updates_1.serviceIsCds)(service)) {
51
+ return;
52
+ }
53
+ const annotations = service.annotations;
54
+ if ((annotations === null || annotations === void 0 ? void 0 : annotations.technicalName) && !annotations.name) {
55
+ annotations.name = (_b = (_a = annotations === null || annotations === void 0 ? void 0 : annotations.technicalName) === null || _a === void 0 ? void 0 : _a.replace(/\//g, '_')) === null || _b === void 0 ? void 0 : _b.replace(/^_/, '');
47
56
  }
48
57
  }
49
58
  /**
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Editor } from 'mem-fs-editor';
2
- import { OdataService, OdataVersion, ServiceType } from './types';
2
+ import { OdataService, OdataVersion, ServiceType, CdsAnnotationsInfo, EdmxAnnotationsInfo } from './types';
3
3
  /**
4
4
  * Try finding a package.json and a ui5.yaml for the given project by looking upwards in the folder hierachy.
5
5
  *
@@ -21,5 +21,5 @@ export declare function findProjectFiles(basePath: string, fs: Editor): Promise<
21
21
  * @returns {Promise<Editor>} the updated memfs editor instance
22
22
  */
23
23
  declare function generate(basePath: string, service: OdataService, fs?: Editor): Promise<Editor>;
24
- export { generate, OdataVersion, OdataService, ServiceType };
24
+ export { generate, OdataVersion, OdataService, ServiceType, EdmxAnnotationsInfo, CdsAnnotationsInfo };
25
25
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -75,7 +75,6 @@ exports.findProjectFiles = findProjectFiles;
75
75
  * @returns {Promise<Editor>} the updated memfs editor instance
76
76
  */
77
77
  function generate(basePath, service, fs) {
78
- var _a;
79
78
  return __awaiter(this, void 0, void 0, function* () {
80
79
  if (!fs) {
81
80
  fs = (0, mem_fs_editor_1.create)((0, mem_fs_1.create)());
@@ -85,13 +84,18 @@ function generate(basePath, service, fs) {
85
84
  (0, data_1.enhanceData)(service);
86
85
  // merge content into existing files
87
86
  const templateRoot = (0, path_1.join)(__dirname, '../templates');
87
+ // update cds files with annotations only if service type is CDS and annotations are provided
88
+ if ((0, updates_1.serviceIsCds)(service) && service.annotations) {
89
+ yield (0, updates_1.updateCdsFilesWithAnnotations)(service.annotations, fs);
90
+ }
88
91
  // manifest.json
89
92
  (0, updates_1.updateManifest)(basePath, service, fs, templateRoot);
90
93
  // update ui5.yaml if it exists
91
94
  let ui5Config;
92
95
  let ui5LocalConfig;
93
96
  let ui5LocalConfigPath;
94
- if (paths.ui5Yaml) {
97
+ if (!(0, updates_1.serviceIsCds)(service) && paths.ui5Yaml) {
98
+ // Dont extend backend middlewares if service type is CDS
95
99
  ui5Config = yield ui5_config_1.UI5Config.newInstance(fs.read(paths.ui5Yaml));
96
100
  try {
97
101
  ui5Config.addBackendToFioriToolsProxydMiddleware(service.previewSettings);
@@ -113,7 +117,8 @@ function generate(basePath, service, fs) {
113
117
  }
114
118
  }
115
119
  // Add mockserver entries
116
- if (service.metadata) {
120
+ if (!(0, updates_1.serviceIsCds)(service) && service.metadata) {
121
+ // mockserver entries are not required if service type is CDS
117
122
  // copy existing `ui5.yaml` as starting point for ui5-mock.yaml
118
123
  if (paths.ui5Yaml && ui5Config) {
119
124
  const webappPath = yield (0, project_access_1.getWebappPath)(basePath, fs);
@@ -142,9 +147,8 @@ function generate(basePath, service, fs) {
142
147
  if (ui5LocalConfigPath && ui5LocalConfig) {
143
148
  fs.write(ui5LocalConfigPath, ui5LocalConfig.toString());
144
149
  }
145
- if ((_a = service.annotations) === null || _a === void 0 ? void 0 : _a.xml) {
146
- fs.write((0, path_1.join)(basePath, 'webapp', 'localService', `${service.annotations.technicalName}.xml`), (0, prettify_xml_1.default)(service.annotations.xml, { indent: 4 }));
147
- }
150
+ // Write annotation xml if annotations are provided and service type is EDMX
151
+ (0, updates_1.writeAnnotationXmlFiles)(fs, basePath, service);
148
152
  return fs;
149
153
  });
150
154
  }
package/dist/types.d.ts CHANGED
@@ -11,6 +11,44 @@ export declare enum ServiceType {
11
11
  EDMX = "edmx",
12
12
  CDS = "cds"
13
13
  }
14
+ /**
15
+ * Interface representing information for EDMX annotations.
16
+ */
17
+ export interface EdmxAnnotationsInfo {
18
+ /**
19
+ * Optional name for the annotations.
20
+ */
21
+ name?: string;
22
+ /**
23
+ * Technical name of the annotations.
24
+ */
25
+ technicalName: string;
26
+ /**
27
+ * XML content of the annotations.
28
+ */
29
+ xml: string;
30
+ }
31
+ /**
32
+ * Interface representing information for CDS annotations.
33
+ */
34
+ export interface CdsAnnotationsInfo {
35
+ /**
36
+ * The contents to be written into the annotation cds file.
37
+ */
38
+ cdsFileContents: string;
39
+ /**
40
+ * The path to the cap project.
41
+ */
42
+ projectPath: string;
43
+ /**
44
+ * The relative path to the app folder within the cap project.
45
+ */
46
+ appPath: string;
47
+ /**
48
+ * The name of the project.
49
+ */
50
+ projectName: string;
51
+ }
14
52
  export interface OdataService {
15
53
  url?: string;
16
54
  client?: string;
@@ -24,11 +62,10 @@ export interface OdataService {
24
62
  name?: string;
25
63
  model?: string;
26
64
  metadata?: string;
27
- annotations?: {
28
- name?: string;
29
- technicalName: string;
30
- xml: string;
31
- };
65
+ /**
66
+ * Annotations can either be EDMX annotations or CDS annotations.
67
+ */
68
+ annotations?: EdmxAnnotationsInfo | CdsAnnotationsInfo;
32
69
  localAnnotationsName?: string;
33
70
  previewSettings?: Partial<ProxyBackend>;
34
71
  }
package/dist/updates.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import type { Editor } from 'mem-fs-editor';
2
- import type { OdataService } from './types';
2
+ import type { OdataService, CdsAnnotationsInfo } from './types';
3
+ /**
4
+ * Function to check if the service type is CDS.
5
+ *
6
+ * @param service - the OData service instance
7
+ * @returns true if the service type is CDS
8
+ */
9
+ export declare function serviceIsCds(service: OdataService): boolean;
3
10
  /**
4
11
  * Internal function that updates the manifest.json based on the given service configuration.
5
12
  *
@@ -9,6 +16,24 @@ import type { OdataService } from './types';
9
16
  * @param templateRoot - root folder contain the ejs templates
10
17
  */
11
18
  export declare function updateManifest(basePath: string, service: OdataService, fs: Editor, templateRoot: string): void;
19
+ /**
20
+ * Writes annotation XML files.
21
+ *
22
+ * @param {Editor} fs - The memfs editor instance.
23
+ * @param {string} basePath - The base path of the project.
24
+ * @param {OdataService} service - The OData service information.
25
+ */
26
+ export declare function writeAnnotationXmlFiles(fs: Editor, basePath: string, service: OdataService): void;
27
+ /**
28
+ * Updates cds files with the provided annotations.
29
+ * This function takes cds annotations and an Editor instance,
30
+ * then updates the relevant cds files with the given annotations.
31
+ *
32
+ * @param {CdsAnnotationsInfo} annotations - The cds annotations info.
33
+ * @param {Editor} fs - The memfs editor instance
34
+ * @returns {Promise<void>} A promise that resolves when the cds files have been updated.
35
+ */
36
+ export declare function updateCdsFilesWithAnnotations(annotations: CdsAnnotationsInfo, fs: Editor): Promise<void>;
12
37
  /**
13
38
  * Update the package.json with the required middlewares.
14
39
  *
package/dist/updates.js CHANGED
@@ -1,13 +1,63 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
2
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
36
  };
5
37
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.hasUI5CliV3 = exports.updatePackageJson = exports.updateManifest = void 0;
38
+ exports.hasUI5CliV3 = exports.updatePackageJson = exports.updateCdsFilesWithAnnotations = exports.writeAnnotationXmlFiles = exports.updateManifest = exports.serviceIsCds = void 0;
7
39
  const ejs_1 = require("ejs");
8
- const path_1 = require("path");
40
+ const path_1 = __importStar(require("path"));
9
41
  const i18n_1 = require("./i18n");
42
+ const types_1 = require("./types");
10
43
  const semver_1 = __importDefault(require("semver"));
44
+ const prettify_xml_1 = __importDefault(require("prettify-xml"));
45
+ /**
46
+ * Function to check if the service type is CDS.
47
+ *
48
+ * @param service - the OData service instance
49
+ * @returns true if the service type is CDS
50
+ */
51
+ function serviceIsCds(service) {
52
+ // if service type is not defined, set EDMX as default
53
+ let serviceType = service.type;
54
+ serviceType || (serviceType = types_1.ServiceType.EDMX);
55
+ if (serviceType === types_1.ServiceType.CDS) {
56
+ return true;
57
+ }
58
+ return false;
59
+ }
60
+ exports.serviceIsCds = serviceIsCds;
11
61
  /**
12
62
  * Internal function that updates the manifest.json based on the given service configuration.
13
63
  *
@@ -18,7 +68,6 @@ const semver_1 = __importDefault(require("semver"));
18
68
  */
19
69
  function updateManifest(basePath, service, fs, templateRoot) {
20
70
  var _a, _b, _c;
21
- // manifest.json
22
71
  const manifestPath = (0, path_1.join)(basePath, 'webapp', 'manifest.json');
23
72
  // Get component app id
24
73
  const manifest = fs.readJSON(manifestPath);
@@ -36,6 +85,73 @@ function updateManifest(basePath, service, fs, templateRoot) {
36
85
  fs.extendJSON(manifestPath, JSON.parse((0, ejs_1.render)(manifestJsonExt, manifestSettings, {})));
37
86
  }
38
87
  exports.updateManifest = updateManifest;
88
+ /**
89
+ * Updates the cds index or service file with the provided annotations.
90
+ * This function takes an Editor instance and cds annotations
91
+ * and updates either the index file or the service file with the given annotations.
92
+ *
93
+ * @param {Editor} fs - The memfs editor instance
94
+ * @param {CdsAnnotationsInfo} annotations - The cds annotations info.
95
+ * @returns {Promise<void>} A promise that resolves when the cds files have been updated.
96
+ */
97
+ function updateCdsIndexOrServiceFile(fs, annotations) {
98
+ var _a, _b;
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ const dirPath = (0, path_1.join)(annotations.projectName, 'annotations');
101
+ const annotationPath = path_1.default.normalize(dirPath).split(/[\\/]/g).join(path_1.default.posix.sep);
102
+ const annotationConfig = `\nusing from './${annotationPath}';`;
103
+ // get index and service file paths
104
+ const indexFilePath = (0, path_1.join)(annotations.projectPath, (_a = annotations.appPath) !== null && _a !== void 0 ? _a : '', 'index.cds');
105
+ const serviceFilePath = (0, path_1.join)(annotations.projectPath, (_b = annotations.appPath) !== null && _b !== void 0 ? _b : '', 'services.cds');
106
+ // extend index or service file with annotation config
107
+ if (indexFilePath && fs.exists(indexFilePath)) {
108
+ fs.append(indexFilePath, annotationConfig);
109
+ }
110
+ else if (fs.exists(serviceFilePath)) {
111
+ fs.append(serviceFilePath, annotationConfig);
112
+ }
113
+ else {
114
+ fs.write(serviceFilePath, annotationConfig);
115
+ }
116
+ });
117
+ }
118
+ /**
119
+ * Writes annotation XML files.
120
+ *
121
+ * @param {Editor} fs - The memfs editor instance.
122
+ * @param {string} basePath - The base path of the project.
123
+ * @param {OdataService} service - The OData service information.
124
+ */
125
+ function writeAnnotationXmlFiles(fs, basePath, service) {
126
+ if (serviceIsCds(service)) {
127
+ return;
128
+ }
129
+ // Write annotation xml if annotations are provided and service type is EDMX
130
+ const annotations = service.annotations;
131
+ if (annotations === null || annotations === void 0 ? void 0 : annotations.xml) {
132
+ fs.write((0, path_1.join)(basePath, 'webapp', 'localService', `${annotations.technicalName}.xml`), (0, prettify_xml_1.default)(annotations.xml, { indent: 4 }));
133
+ }
134
+ }
135
+ exports.writeAnnotationXmlFiles = writeAnnotationXmlFiles;
136
+ /**
137
+ * Updates cds files with the provided annotations.
138
+ * This function takes cds annotations and an Editor instance,
139
+ * then updates the relevant cds files with the given annotations.
140
+ *
141
+ * @param {CdsAnnotationsInfo} annotations - The cds annotations info.
142
+ * @param {Editor} fs - The memfs editor instance
143
+ * @returns {Promise<void>} A promise that resolves when the cds files have been updated.
144
+ */
145
+ function updateCdsFilesWithAnnotations(annotations, fs) {
146
+ var _a;
147
+ return __awaiter(this, void 0, void 0, function* () {
148
+ const annotationCdsPath = (0, path_1.join)(annotations.projectPath, (_a = annotations.appPath) !== null && _a !== void 0 ? _a : '', annotations.projectName, 'annotations.cds');
149
+ // write into annotations.cds file
150
+ fs.write(annotationCdsPath, annotations.cdsFileContents);
151
+ yield updateCdsIndexOrServiceFile(fs, annotations);
152
+ });
153
+ }
154
+ exports.updateCdsFilesWithAnnotations = updateCdsFilesWithAnnotations;
39
155
  /**
40
156
  * Determines model settings based on the UI5 version.
41
157
  *
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "bugs": {
10
10
  "url": "https://github.com/SAP/open-ux-tools/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Aodata-service-writer"
11
11
  },
12
- "version": "0.19.2",
12
+ "version": "0.20.1",
13
13
  "license": "Apache-2.0",
14
14
  "main": "dist/index.js",
15
15
  "files": [
@@ -27,8 +27,8 @@
27
27
  "mem-fs-editor": "9.4.0",
28
28
  "prettify-xml": "1.2.0",
29
29
  "semver": "7.5.4",
30
- "@sap-ux/mockserver-config-writer": "0.5.3",
31
- "@sap-ux/ui5-config": "0.22.5"
30
+ "@sap-ux/mockserver-config-writer": "0.5.4",
31
+ "@sap-ux/ui5-config": "0.22.6"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/ejs": "3.1.2",
@@ -38,7 +38,7 @@
38
38
  "@types/semver": "7.5.2",
39
39
  "fs-extra": "10.0.0",
40
40
  "lodash": "4.17.21",
41
- "@sap-ux/project-access": "1.20.1"
41
+ "@sap-ux/project-access": "1.21.1"
42
42
  },
43
43
  "engines": {
44
44
  "node": ">=18.x"