@sap-ux/mockserver-config-writer 0.6.7 → 0.7.0

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.
package/dist/app-info.js CHANGED
@@ -2,6 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getMainServiceDataSource = getMainServiceDataSource;
4
4
  exports.getODataSources = getODataSources;
5
+ /**
6
+ * Get the main service data source name from manifest.json.
7
+ *
8
+ * @param manifest - Parsed manifest.json
9
+ * @returns - data source name from manifest.json
10
+ */
11
+ function getMainServiceDataSourceName(manifest) {
12
+ const modelName = manifest['sap.ovp']?.globalFilterModel ?? '';
13
+ return manifest['sap.ui5']?.models?.[modelName]?.dataSource;
14
+ }
5
15
  /**
6
16
  * Get the main service data source entry from manifest.json.
7
17
  *
@@ -10,10 +20,7 @@ exports.getODataSources = getODataSources;
10
20
  */
11
21
  function getMainServiceDataSource(manifest) {
12
22
  let dataSource;
13
- const model = manifest['sap.ovp']?.globalFilterModel || '';
14
- const dataSourceName = manifest['sap.ui5'] && manifest['sap.ui5'].models?.[model]
15
- ? manifest['sap.ui5'].models[model].dataSource
16
- : undefined;
23
+ const dataSourceName = getMainServiceDataSourceName(manifest);
17
24
  if (dataSourceName) {
18
25
  dataSource = manifest['sap.app'].dataSources?.[dataSourceName];
19
26
  }
@@ -28,7 +35,7 @@ function getMainServiceDataSource(manifest) {
28
35
  */
29
36
  function getODataSources(manifest, dataSourceType = 'OData') {
30
37
  const result = {};
31
- const dataSources = manifest['sap.app']?.dataSources || {};
38
+ const dataSources = manifest['sap.app']?.dataSources ?? {};
32
39
  for (const dataSource in dataSources) {
33
40
  if (dataSources[dataSource].uri && dataSources[dataSource].type === dataSourceType) {
34
41
  result[dataSource] = dataSources[dataSource];
package/dist/i18n.js CHANGED
@@ -7,12 +7,13 @@ exports.initI18n = initI18n;
7
7
  exports.t = t;
8
8
  const i18next_1 = __importDefault(require("i18next"));
9
9
  const mockserver_config_writer_i18n_json_1 = __importDefault(require("./translations/mockserver-config-writer.i18n.json"));
10
- const NS = 'odata-service-writer';
10
+ const NS = 'mockserver-config-writer';
11
+ let i18nInstance;
11
12
  /**
12
13
  * Initialize i18next with the translations for this module.
13
14
  */
14
15
  async function initI18n() {
15
- await i18next_1.default.init({
16
+ i18nInstance = i18next_1.default.createInstance({
16
17
  resources: {
17
18
  en: {
18
19
  [NS]: mockserver_config_writer_i18n_json_1.default
@@ -23,6 +24,7 @@ async function initI18n() {
23
24
  defaultNS: NS,
24
25
  ns: [NS]
25
26
  });
27
+ await i18nInstance.init();
26
28
  }
27
29
  /**
28
30
  * Helper function facading the call to i18next.
@@ -32,7 +34,7 @@ async function initI18n() {
32
34
  * @returns {string} localized string stored for the given key
33
35
  */
34
36
  function t(key, options) {
35
- return i18next_1.default.t(key, options);
37
+ return i18nInstance.t(key, options);
36
38
  }
37
39
  initI18n().catch(() => {
38
40
  // Ignore any errors since the write will still work
@@ -16,5 +16,5 @@ export declare function generateMockserverConfig(basePath: string, data: Mockser
16
16
  * @param fs - the memfs editor instance
17
17
  * @returns Promise<Editor> - memfs editor instance with updated files
18
18
  */
19
- export declare function removeMockserverConfig(basePath: string, fs?: Editor): Editor;
19
+ export declare function removeMockserverConfig(basePath: string, fs?: Editor): Promise<Editor>;
20
20
  //# sourceMappingURL=index.d.ts.map
@@ -29,12 +29,13 @@ async function generateMockserverConfig(basePath, data, fs) {
29
29
  * @param fs - the memfs editor instance
30
30
  * @returns Promise<Editor> - memfs editor instance with updated files
31
31
  */
32
- function removeMockserverConfig(basePath, fs) {
32
+ async function removeMockserverConfig(basePath, fs) {
33
33
  if (!fs) {
34
34
  fs = (0, mem_fs_editor_1.create)((0, mem_fs_1.create)());
35
35
  }
36
36
  (0, package_json_1.removeFromPackageJson)(fs, basePath);
37
37
  (0, ui5_mock_yaml_1.removeUi5MockYaml)(fs, basePath);
38
+ await (0, ui5_mock_yaml_1.removeMockDataFolders)(fs, basePath);
38
39
  return fs;
39
40
  }
40
41
  //# sourceMappingURL=index.js.map
@@ -26,7 +26,7 @@ function enhancePackageJson(fs, basePath, config) {
26
26
  * @param version - npm version string
27
27
  */
28
28
  function enhanceDependencies(packageJson, mockserverModule = '@sap-ux/ui5-middleware-fe-mockserver', version = '2') {
29
- packageJson.devDependencies = packageJson.devDependencies || {};
29
+ packageJson.devDependencies = packageJson.devDependencies ?? {};
30
30
  delete packageJson.devDependencies['@sap/ux-ui5-fe-mockserver-middleware'];
31
31
  packageJson.devDependencies[mockserverModule] = version;
32
32
  if (isUi5CliHigherTwo(packageJson.devDependencies)) {
@@ -2,6 +2,8 @@ import type { Editor } from 'mem-fs-editor';
2
2
  import type { Ui5MockYamlConfig } from '../types';
3
3
  /**
4
4
  * Enhance or create the ui5-mock.yaml with mockserver config.
5
+ * Mockserver config services and annotations are collected from associated manifest.json file of the project.
6
+ * If there aren't any services or annotations defined in manifest dataSources section, then mockserver config will be generated without those.
5
7
  * Following enhancement strategy is applied:
6
8
  *
7
9
  * ui5-mock.yaml exists
@@ -21,6 +23,13 @@ import type { Ui5MockYamlConfig } from '../types';
21
23
  * @param config - optional config passed in by consumer
22
24
  */
23
25
  export declare function enhanceYaml(fs: Editor, basePath: string, webappPath: string, config?: Ui5MockYamlConfig): Promise<void>;
26
+ /**
27
+ * Deletes mock data folders for all services from mem-fs.
28
+ *
29
+ * @param fs - mem-fs reference to be used for file access
30
+ * @param basePath - path to project root, where package.json and ui5.yaml is
31
+ */
32
+ export declare function removeMockDataFolders(fs: Editor, basePath: string): Promise<void>;
24
33
  /**
25
34
  * Delete ui5-mock.yaml file from mem-fs.
26
35
  *
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.enhanceYaml = enhanceYaml;
4
+ exports.removeMockDataFolders = removeMockDataFolders;
4
5
  exports.removeUi5MockYaml = removeUi5MockYaml;
5
6
  const path_1 = require("path");
6
7
  const ui5_config_1 = require("@sap-ux/ui5-config");
8
+ const project_access_1 = require("@sap-ux/project-access");
7
9
  const app_info_1 = require("../app-info");
8
10
  /**
9
11
  * Enhance or create the ui5-mock.yaml with mockserver config.
12
+ * Mockserver config services and annotations are collected from associated manifest.json file of the project.
13
+ * If there aren't any services or annotations defined in manifest dataSources section, then mockserver config will be generated without those.
10
14
  * Following enhancement strategy is applied:
11
15
  *
12
16
  * ui5-mock.yaml exists
@@ -26,82 +30,127 @@ const app_info_1 = require("../app-info");
26
30
  * @param config - optional config passed in by consumer
27
31
  */
28
32
  async function enhanceYaml(fs, basePath, webappPath, config) {
33
+ const overwrite = !!config?.overwrite;
29
34
  const ui5MockYamlPath = (0, path_1.join)(basePath, 'ui5-mock.yaml');
30
35
  let mockConfig;
31
36
  const manifest = fs.readJSON((0, path_1.join)(webappPath, 'manifest.json'));
32
- const mockserverPath = config?.path || (0, app_info_1.getMainServiceDataSource)(manifest)?.uri;
37
+ // Prepare annotations list to be used in mockserver middleware config annotations
33
38
  const annotationSource = Object.values((0, app_info_1.getODataSources)(manifest, 'ODataAnnotation'));
34
39
  const annotationsConfig = annotationSource.map((annotation) => ({
35
40
  localPath: `./webapp/${annotation.settings?.localUri}`,
36
41
  urlPath: annotation.uri
37
42
  }));
43
+ // Prepare dataSources list to be used in mockserver middleware config services
44
+ const dataSources = (0, app_info_1.getODataSources)(manifest);
45
+ const dataSourcesConfig = [];
46
+ for (const dataSource in dataSources) {
47
+ const localUri = dataSources[dataSource].settings?.localUri;
48
+ dataSourcesConfig.push({
49
+ serviceName: dataSource,
50
+ servicePath: dataSources[dataSource].uri,
51
+ metadataPath: localUri ? `./webapp/${localUri}` : undefined
52
+ });
53
+ }
38
54
  if (fs.exists(ui5MockYamlPath)) {
39
- mockConfig = await updateUi5MockYamlConfig(fs, ui5MockYamlPath, mockserverPath, annotationsConfig);
55
+ mockConfig = await updateUi5MockYamlConfig(fs, ui5MockYamlPath, dataSourcesConfig, annotationsConfig, overwrite);
40
56
  }
41
57
  else {
42
58
  mockConfig = fs.exists((0, path_1.join)(basePath, 'ui5.yaml'))
43
- ? await generateUi5MockYamlBasedOnUi5Yaml(fs, basePath, mockserverPath, annotationsConfig)
44
- : await generateNewUi5MockYamlConfig(manifest['sap.app']?.id || '', mockserverPath, annotationsConfig);
59
+ ? await generateUi5MockYamlBasedOnUi5Yaml(fs, basePath, dataSourcesConfig, annotationsConfig)
60
+ : await generateNewUi5MockYamlConfig(manifest['sap.app']?.id || '', dataSourcesConfig, annotationsConfig);
45
61
  }
46
62
  const yaml = mockConfig.toString();
47
63
  fs.write(ui5MockYamlPath, yaml);
48
64
  }
49
65
  /**
50
- * Update existing ui5-mock.yaml config. This will add or replace existing middleware configuration
66
+ * Deletes mock data folders for all services from mem-fs.
67
+ *
68
+ * @param fs - mem-fs reference to be used for file access
69
+ * @param basePath - path to project root, where package.json and ui5.yaml is
70
+ */
71
+ async function removeMockDataFolders(fs, basePath) {
72
+ const webappPath = await (0, project_access_1.getWebappPath)(basePath, fs);
73
+ const manifestPath = (0, path_1.join)(webappPath, project_access_1.FileName.Manifest);
74
+ const manifest = fs.readJSON(manifestPath);
75
+ // Read service names from manifest.json
76
+ const dataSources = manifest['sap.app'].dataSources;
77
+ if (dataSources) {
78
+ const serviceNames = Object.keys(dataSources);
79
+ serviceNames.forEach((serviceName) => {
80
+ const mockdataPath = (0, path_1.join)(webappPath, project_access_1.DirName.LocalService, serviceName, project_access_1.DirName.Data);
81
+ if (mockdataPath) {
82
+ fs.delete(mockdataPath);
83
+ }
84
+ });
85
+ }
86
+ }
87
+ /**
88
+ * Update existing ui5-mock.yaml config. This will add or replace existing middleware configuration.
89
+ * If 'overwrite' is set to true, then mockserver middleware configuration would be replaced else only enhanced with data from 'name' and 'path'.
51
90
  * 'sap-fe-mockserver' with state of the art config.
52
91
  *
53
92
  * @param fs - Editor instance to read existing information
54
93
  * @param ui5MockYamlPath - path to ui5-mock.yaml file
55
- * @param [path] - optional url path the mockserver listens to
56
- * @param annotationsConfig - optional annotations config to add to mockserver middleware
57
- * @returns {*} {Promise<UI5Config>} - Update Yaml Doc
94
+ * @param dataSourcesConfig - dataSources config from manifest to add to mockserver middleware services list
95
+ * @param annotationsConfig - annotations config to add to mockserver mockserver middleware annotations list
96
+ * @param overwrite - optional, whether to overwrite existing annotations and services
97
+ * @returns {*} {Promise<UI5Config>} - Updated Yaml Doc
58
98
  */
59
- async function updateUi5MockYamlConfig(fs, ui5MockYamlPath, path, annotationsConfig) {
99
+ async function updateUi5MockYamlConfig(fs, ui5MockYamlPath, dataSourcesConfig, annotationsConfig, overwrite = false) {
60
100
  const existingUi5MockYamlConfig = await ui5_config_1.UI5Config.newInstance(fs.read(ui5MockYamlPath));
61
- existingUi5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig));
101
+ if (overwrite) {
102
+ const newMockserverMiddleware = await getNewMockserverMiddleware(dataSourcesConfig, annotationsConfig);
103
+ existingUi5MockYamlConfig.updateCustomMiddleware(newMockserverMiddleware);
104
+ }
105
+ else {
106
+ for (const dataSourceName in dataSourcesConfig) {
107
+ existingUi5MockYamlConfig.addServiceToMockserverMiddleware(dataSourcesConfig[dataSourceName], undefined, annotationsConfig);
108
+ }
109
+ }
62
110
  return existingUi5MockYamlConfig;
63
111
  }
64
112
  /**
65
113
  * Create a new ui5-mock.yaml based on existing ui5.yaml.
66
114
  *
67
115
  * @param fs - Editor instance to read existing information
68
- * @param basePath -
69
- * @param [path] - optional path for mockserver config
70
- * @param annotationsConfig - optional annotations config to add to mockserver middleware
116
+ * @param basePath - the base path where the package.json and ui5.yaml is
117
+ * @param dataSourcesConfig - dataSources config from manifest to add to mockserver middleware services list
118
+ * @param annotationsConfig - annotations config to add to mockserver mockserver middleware annotations list
71
119
  * @returns {*} {Promise<UI5Config>} - Update Yaml Doc
72
120
  */
73
- async function generateUi5MockYamlBasedOnUi5Yaml(fs, basePath, path, annotationsConfig) {
74
- const ui5MockYamlConfig = await ui5_config_1.UI5Config.newInstance(fs.read((0, path_1.join)(basePath, 'ui5.yaml')));
75
- ui5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig));
121
+ async function generateUi5MockYamlBasedOnUi5Yaml(fs, basePath, dataSourcesConfig, annotationsConfig) {
122
+ const ui5MockYamlConfig = await (0, project_access_1.readUi5Yaml)(basePath, project_access_1.FileName.Ui5Yaml, fs);
123
+ const ui5MockServerMiddleware = await getNewMockserverMiddleware(dataSourcesConfig, annotationsConfig);
124
+ ui5MockYamlConfig.updateCustomMiddleware(ui5MockServerMiddleware);
76
125
  return ui5MockYamlConfig;
77
126
  }
78
127
  /**
79
128
  * Create fresh ui5-mock.yaml configuration which can be stringified and written.
80
129
  *
81
130
  * @param appId - application id
82
- * @param [path] - optional url path the mockserver listens to
83
- * @param annotationsConfig - optional annotations config to add to mockserver middleware
131
+ * @param dataSourcesConfig - dataSources config from manifest to add to mockserver middleware services list
132
+ * @param annotationsConfig - annotations config to add to mockserver mockserver middleware annotations list
84
133
  * @returns {*} {Promise<UI5Config>} - Update Yaml Doc
85
134
  */
86
- async function generateNewUi5MockYamlConfig(appId, path, annotationsConfig) {
135
+ async function generateNewUi5MockYamlConfig(appId, dataSourcesConfig, annotationsConfig) {
87
136
  const ui5MockYaml = await ui5_config_1.UI5Config.newInstance('# yaml-language-server: $schema=https://sap.github.io/ui5-tooling/schema/ui5.yaml.json\n\nspecVersion: "2.5"');
88
137
  ui5MockYaml.setMetadata({ name: appId });
89
138
  ui5MockYaml.setType('application');
90
139
  ui5MockYaml.addFioriToolsProxydMiddleware({ ui5: {} });
91
140
  ui5MockYaml.addFioriToolsAppReloadMiddleware();
92
- ui5MockYaml.addMockServerMiddleware(path, annotationsConfig);
141
+ ui5MockYaml.addMockServerMiddleware(dataSourcesConfig, annotationsConfig);
93
142
  return ui5MockYaml;
94
143
  }
95
144
  /**
96
145
  * Return new mockserver middleware.
97
146
  *
98
- * @param [path] - optional path for mockserver config
99
- * @param annotationsConfig - optional annotations config to add to mockserver middleware
147
+ * @param dataSourcesConfig - dataSources config from manifest to add to mockserver middleware services list
148
+ * @param annotationsConfig - annotations config to add to mockserver mockserver middleware annotations list
100
149
  * @returns - mockserver middleware
101
150
  */
102
- async function getNewMockserverMiddleware(path, annotationsConfig) {
151
+ async function getNewMockserverMiddleware(dataSourcesConfig, annotationsConfig) {
103
152
  const ui5MockYaml = await ui5_config_1.UI5Config.newInstance('');
104
- ui5MockYaml.addMockServerMiddleware(path, annotationsConfig);
153
+ ui5MockYaml.addMockServerMiddleware(dataSourcesConfig, annotationsConfig);
105
154
  const mockserverMiddleware = ui5MockYaml.findCustomMiddleware('sap-fe-mockserver');
106
155
  if (!mockserverMiddleware) {
107
156
  throw Error('Could not create new mockserver config');
@@ -5,11 +5,13 @@ import type { PromptObject } from 'prompts';
5
5
  *
6
6
  * @param params - optional parameters used to fill default values
7
7
  * @param params.webappPath - optional path to webapp folder, where manifest is
8
+ * @param params.askForOverwrite - optional, whether to overwrite services in mockserver config
8
9
  * @param params.fs - optional memfs editor instance
9
10
  * @returns - array of questions that serves as input for prompt module
10
11
  */
11
12
  export declare function getMockserverConfigQuestions(params?: {
12
13
  webappPath?: string;
14
+ askForOverwrite?: boolean;
13
15
  fs?: Editor;
14
16
  }): PromptObject[];
15
17
  //# sourceMappingURL=index.d.ts.map
@@ -11,34 +11,45 @@ const __1 = require("..");
11
11
  *
12
12
  * @param params - optional parameters used to fill default values
13
13
  * @param params.webappPath - optional path to webapp folder, where manifest is
14
+ * @param params.askForOverwrite - optional, whether to overwrite services in mockserver config
14
15
  * @param params.fs - optional memfs editor instance
15
16
  * @returns - array of questions that serves as input for prompt module
16
17
  */
17
18
  function getMockserverConfigQuestions(params) {
18
- const question = {
19
+ const prompts = [];
20
+ const questionPath = {
19
21
  name: 'path',
20
22
  message: (0, __1.t)('questions.pathToMock')
21
23
  };
22
24
  if (params?.webappPath) {
23
- const fs = params.fs || (0, mem_fs_editor_1.create)((0, mem_fs_1.create)());
25
+ const fs = params.fs ?? (0, mem_fs_editor_1.create)((0, mem_fs_1.create)());
24
26
  const manifest = JSON.parse(fs.read((0, path_1.join)(params.webappPath, 'manifest.json')));
25
- const mainDataSourceUri = (0, app_info_1.getMainServiceDataSource)(manifest)?.uri || '';
27
+ const mainDataSourceUri = (0, app_info_1.getMainServiceDataSource)(manifest)?.uri ?? '';
26
28
  const oDataSources = (0, app_info_1.getODataSources)(manifest);
27
29
  const choices = [];
28
30
  for (const dsName in oDataSources) {
29
31
  choices.push({
30
32
  title: `${dsName}: ${oDataSources[dsName].uri}`,
31
33
  value: oDataSources[dsName].uri,
32
- description: oDataSources[dsName].settings?.odataVersion || undefined
34
+ description: oDataSources[dsName].settings?.odataVersion ?? undefined
33
35
  });
34
36
  }
35
37
  if (choices.length > 0) {
36
- question.type = 'select';
37
- question.choices = choices;
38
- question.initial = choices.findIndex((c) => c.value === mainDataSourceUri);
38
+ questionPath.type = 'select';
39
+ questionPath.choices = choices;
40
+ questionPath.initial = choices.findIndex((c) => c.value === mainDataSourceUri);
39
41
  }
40
42
  }
41
- question.type ||= 'text';
42
- return [question];
43
+ questionPath.type ||= 'text';
44
+ prompts.push(questionPath);
45
+ if (params?.askForOverwrite) {
46
+ const questionOverwrite = {
47
+ type: 'confirm',
48
+ name: 'overwrite',
49
+ message: (0, __1.t)('questions.overwrite')
50
+ };
51
+ prompts.push(questionOverwrite);
52
+ }
53
+ return prompts;
43
54
  }
44
55
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "questions": {
3
- "pathToMock": "Path to mocked service"
3
+ "pathToMock": "Path to mocked service",
4
+ "overwrite": "Overwrite services"
4
5
  }
5
6
  }
@@ -9,6 +9,6 @@ export interface PackageJsonMockConfig {
9
9
  mockserverVersion?: string;
10
10
  }
11
11
  export interface Ui5MockYamlConfig {
12
- path?: string;
12
+ overwrite?: boolean;
13
13
  }
14
14
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sap-ux/mockserver-config-writer",
3
3
  "description": "Add or update configuration for SAP Fiori tools mockserver",
4
- "version": "0.6.7",
4
+ "version": "0.7.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/SAP/open-ux-tools.git",
@@ -22,14 +22,14 @@
22
22
  "i18next": "20.6.1",
23
23
  "mem-fs": "2.1.0",
24
24
  "mem-fs-editor": "9.4.0",
25
- "@sap-ux/ui5-config": "0.25.2"
25
+ "@sap-ux/ui5-config": "0.26.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/mem-fs": "1.1.2",
29
29
  "@types/mem-fs-editor": "7.0.1",
30
30
  "@types/prompts": "2.4.4",
31
31
  "prompts": "2.4.2",
32
- "@sap-ux/project-access": "1.28.7"
32
+ "@sap-ux/project-access": "1.28.8"
33
33
  },
34
34
  "engines": {
35
35
  "node": ">=18.x"