@sap/cds 6.8.4 → 7.0.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.
Files changed (217) hide show
  1. package/CHANGELOG.md +66 -4
  2. package/README.md +0 -1
  3. package/bin/cds-serve.js +50 -3
  4. package/bin/deploy/to-hana.js +1 -0
  5. package/bin/serve.js +16 -20
  6. package/lib/auth/basic-auth.js +6 -4
  7. package/lib/auth/index.js +4 -3
  8. package/lib/auth/jwt-auth.js +2 -5
  9. package/lib/compile/cds-compile.js +34 -89
  10. package/lib/compile/cdsc.js +11 -0
  11. package/lib/compile/etc/properties.js +2 -2
  12. package/lib/compile/for/lean_drafts.js +36 -69
  13. package/lib/compile/for/nodejs.js +2 -1
  14. package/lib/compile/load.js +3 -3
  15. package/lib/compile/minify.js +2 -0
  16. package/lib/compile/to/csn.js +74 -0
  17. package/{bin/build/provider/hana/2tabledata.js → lib/compile/to/hdbtabledata.js} +4 -6
  18. package/lib/compile/to/json.js +1 -1
  19. package/lib/compile/to/sql.js +8 -6
  20. package/lib/dbs/cds-deploy.js +174 -114
  21. package/lib/env/cds-env.js +64 -79
  22. package/lib/env/cds-requires.js +11 -28
  23. package/lib/env/defaults.js +13 -3
  24. package/lib/env/plugins.js +1 -12
  25. package/lib/env/presets.js +25 -21
  26. package/lib/index.js +121 -147
  27. package/lib/{core/reflect.js → linked/models.js} +2 -2
  28. package/lib/{core/infer.js → linked/queries.js} +2 -0
  29. package/lib/{core/index.js → linked/types.js} +2 -1
  30. package/lib/log/cds-error.js +13 -7
  31. package/lib/log/format/cf.js +1 -1
  32. package/lib/plugins.js +49 -0
  33. package/lib/ql/Query.js +0 -9
  34. package/lib/ql/STREAM.js +0 -1
  35. package/lib/req/context.js +2 -7
  36. package/lib/req/request.js +6 -2
  37. package/lib/req/response.js +23 -10
  38. package/lib/srv/middlewares/ctx-model.js +2 -2
  39. package/lib/srv/middlewares/errors.js +1 -1
  40. package/lib/srv/protocols/_legacy.js +1 -0
  41. package/lib/srv/protocols/graphql.js +7 -16
  42. package/lib/srv/protocols/index.js +59 -45
  43. package/lib/srv/protocols/odata-v2-proxy.js +2 -70
  44. package/lib/srv/protocols/odata-v4.js +9 -4
  45. package/lib/srv/srv-api.js +9 -3
  46. package/lib/srv/srv-dispatch.js +12 -9
  47. package/lib/srv/srv-models.js +4 -21
  48. package/lib/srv/srv-tx.js +15 -12
  49. package/lib/utils/cds-test.js +14 -9
  50. package/lib/utils/cds-utils.js +2 -12
  51. package/lib/utils/check-version.js +17 -0
  52. package/{bin/build → lib/utils}/csv-reader.js +23 -24
  53. package/libx/_runtime/auth/index.js +27 -23
  54. package/libx/_runtime/cds-services/adapter/odata-v4/ODataRequest.js +15 -72
  55. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/create.js +1 -1
  56. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/metadata.js +0 -2
  57. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/read.js +33 -63
  58. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/request.js +14 -18
  59. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/update.js +15 -5
  60. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/ExpressionToCQN.js +5 -4
  61. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/readToCQN.js +37 -40
  62. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/updateToCQN.js +7 -1
  63. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/utils.js +101 -38
  64. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/errors/AbstractError.js +5 -1
  65. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/uri/UriTokenizer.js +5 -8
  66. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/utils/ValueConverter.js +2 -1
  67. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/deserializer/ResourceJsonDeserializer.js +9 -8
  68. package/libx/_runtime/cds-services/adapter/odata-v4/to.js +1 -1
  69. package/libx/_runtime/cds-services/adapter/odata-v4/utils/data.js +15 -11
  70. package/libx/_runtime/cds-services/adapter/odata-v4/utils/readAfterWrite.js +4 -0
  71. package/libx/_runtime/cds-services/adapter/odata-v4/utils/result.js +5 -2
  72. package/libx/_runtime/cds-services/adapter/odata-v4/utils/stream.js +1 -123
  73. package/libx/_runtime/cds-services/services/Service.js +79 -107
  74. package/libx/_runtime/cds-services/services/utils/columns.js +23 -19
  75. package/libx/_runtime/cds-services/services/utils/compareJson.js +11 -1
  76. package/libx/_runtime/cds-services/services/utils/differ.js +7 -2
  77. package/libx/_runtime/cds-services/util/assert.js +65 -2
  78. package/libx/_runtime/common/composition/data.js +1 -0
  79. package/libx/_runtime/common/generic/auth/expand.js +1 -1
  80. package/libx/_runtime/common/generic/auth/restrict.js +5 -10
  81. package/libx/_runtime/common/generic/auth/restrictions.js +40 -0
  82. package/libx/_runtime/common/generic/auth/utils.js +1 -2
  83. package/libx/_runtime/common/generic/crud.js +32 -16
  84. package/libx/_runtime/common/generic/etag.js +133 -104
  85. package/libx/_runtime/common/generic/input.js +6 -21
  86. package/libx/_runtime/common/generic/put.js +1 -1
  87. package/libx/_runtime/common/generic/stream.js +52 -0
  88. package/libx/_runtime/common/generic/temporal.js +25 -8
  89. package/libx/_runtime/common/i18n/messages.properties +0 -2
  90. package/libx/_runtime/common/utils/cqn.js +1 -1
  91. package/libx/_runtime/common/utils/cqn2cqn4sql.js +5 -2
  92. package/libx/_runtime/common/utils/csn.js +0 -51
  93. package/libx/_runtime/common/utils/etag.js +30 -0
  94. package/libx/_runtime/common/utils/keys.js +1 -1
  95. package/libx/_runtime/common/utils/normalizeTimestamp.js +25 -0
  96. package/libx/_runtime/common/utils/path.js +1 -1
  97. package/libx/_runtime/common/utils/resolveView.js +2 -1
  98. package/libx/_runtime/common/utils/rewriteAsterisks.js +6 -4
  99. package/libx/_runtime/common/utils/search2cqn4sql.js +12 -16
  100. package/libx/_runtime/common/utils/stream.js +140 -0
  101. package/libx/_runtime/common/utils/streamProp.js +29 -12
  102. package/libx/_runtime/common/utils/templateProcessorPathSerializer.js +0 -2
  103. package/libx/_runtime/db/generic/index.js +0 -2
  104. package/libx/_runtime/db/query/delete.js +2 -2
  105. package/libx/_runtime/db/query/insert.js +2 -2
  106. package/libx/_runtime/db/query/read.js +2 -2
  107. package/libx/_runtime/db/query/run.js +2 -2
  108. package/libx/_runtime/db/query/update.js +2 -2
  109. package/libx/_runtime/db/sql-builder/BaseBuilder.js +0 -6
  110. package/libx/_runtime/db/sql-builder/ExpressionBuilder.js +23 -12
  111. package/libx/_runtime/db/sql-builder/FunctionBuilder.js +18 -6
  112. package/libx/_runtime/db/sql-builder/InsertBuilder.js +1 -0
  113. package/libx/_runtime/db/sql-builder/SelectBuilder.js +3 -7
  114. package/libx/_runtime/db/sql-builder/UpsertBuilder.js +1 -0
  115. package/libx/_runtime/db/utils/normalizeTimeData.js +7 -3
  116. package/libx/_runtime/fiori/draft.js +2 -0
  117. package/libx/_runtime/fiori/generic/activate.js +8 -9
  118. package/libx/_runtime/fiori/generic/before.js +30 -20
  119. package/libx/_runtime/fiori/generic/cancel.js +5 -3
  120. package/libx/_runtime/fiori/generic/delete.js +5 -3
  121. package/libx/_runtime/fiori/generic/edit.js +7 -7
  122. package/libx/_runtime/fiori/generic/index.js +10 -16
  123. package/libx/_runtime/fiori/generic/new.js +5 -3
  124. package/libx/_runtime/fiori/generic/patch.js +11 -8
  125. package/libx/_runtime/fiori/generic/prepare.js +13 -6
  126. package/libx/_runtime/fiori/generic/read.js +12 -6
  127. package/libx/_runtime/fiori/lean-draft.js +207 -152
  128. package/libx/_runtime/fiori/utils/delete.js +10 -5
  129. package/libx/_runtime/fiori/utils/req.js +17 -5
  130. package/libx/_runtime/fiori/utils/stream.js +36 -0
  131. package/libx/_runtime/hana/Service.js +12 -9
  132. package/libx/_runtime/hana/conversion.js +10 -15
  133. package/libx/_runtime/hana/driver.js +2 -0
  134. package/libx/_runtime/hana/execute.js +28 -6
  135. package/libx/_runtime/hana/pool.js +36 -122
  136. package/libx/_runtime/hana/search2cqn4sql.js +34 -36
  137. package/libx/_runtime/messaging/enterprise-messaging-utils/getTenantInfo.js +2 -6
  138. package/libx/_runtime/messaging/enterprise-messaging-utils/registerEndpoints.js +3 -1
  139. package/libx/_runtime/messaging/enterprise-messaging.js +10 -58
  140. package/libx/_runtime/messaging/outbox/utils.js +1 -1
  141. package/libx/_runtime/remote/Service.js +20 -1
  142. package/libx/_runtime/remote/utils/client.js +3 -5
  143. package/libx/_runtime/sqlite/Service.js +4 -6
  144. package/libx/_runtime/sqlite/conversion.js +3 -13
  145. package/libx/_runtime/sqlite/customBuilder/CustomFunctionBuilder.js +9 -6
  146. package/libx/_runtime/sqlite/customBuilder/CustomUpsertBuilder.js +6 -1
  147. package/libx/_runtime/sqlite/execute.js +5 -16
  148. package/libx/odata/afterburner.js +22 -6
  149. package/libx/odata/grammar.pegjs +6 -1
  150. package/libx/odata/parser.js +1 -1
  151. package/libx/rest/RestAdapter.js +16 -9
  152. package/libx/rest/RestRequest.js +1 -1
  153. package/libx/rest/middleware/input.js +2 -1
  154. package/libx/rest/middleware/operation.js +1 -0
  155. package/libx/rest/middleware/parse.js +3 -2
  156. package/libx/rest/middleware/payload.js +9 -8
  157. package/libx/rest/middleware/read.js +1 -0
  158. package/package.json +9 -16
  159. package/server.js +1 -1
  160. package/app/fiori/preview.js +0 -270
  161. package/app/fiori/routes.js +0 -59
  162. package/bin/build/buildTaskEngine.js +0 -360
  163. package/bin/build/buildTaskFactory.js +0 -283
  164. package/bin/build/buildTaskHandler.js +0 -241
  165. package/bin/build/buildTaskProvider.js +0 -22
  166. package/bin/build/buildTaskProviderFactory.js +0 -175
  167. package/bin/build/cds.js +0 -5
  168. package/bin/build/constants.js +0 -66
  169. package/bin/build/index.js +0 -58
  170. package/bin/build/provider/buildTaskHandlerEdmx.js +0 -82
  171. package/bin/build/provider/buildTaskHandlerFeatureToggles.js +0 -131
  172. package/bin/build/provider/buildTaskHandlerInternal.js +0 -254
  173. package/bin/build/provider/buildTaskProviderInternal.js +0 -383
  174. package/bin/build/provider/fiori/index.js +0 -171
  175. package/bin/build/provider/hana/2migration.js +0 -179
  176. package/bin/build/provider/hana/index.js +0 -505
  177. package/bin/build/provider/hana/migrationtable.js +0 -472
  178. package/bin/build/provider/hana/template/.hdiconfig-haas +0 -163
  179. package/bin/build/provider/hana/template/.hdiconfig-hanacloud +0 -137
  180. package/bin/build/provider/hana/template/.hdinamespace +0 -4
  181. package/bin/build/provider/hana/template/package.json +0 -12
  182. package/bin/build/provider/hana/template/undeploy.json +0 -5
  183. package/bin/build/provider/java/index.js +0 -111
  184. package/bin/build/provider/java-cf/index.js +0 -1
  185. package/bin/build/provider/mtx/index.js +0 -268
  186. package/bin/build/provider/mtx/resourcesTarBuilder.js +0 -95
  187. package/bin/build/provider/mtx-extension/index.js +0 -131
  188. package/bin/build/provider/mtx-sidecar/index.js +0 -137
  189. package/bin/build/provider/node-cf/index.js +0 -1
  190. package/bin/build/provider/nodejs/index.js +0 -192
  191. package/bin/build/util.js +0 -299
  192. package/bin/cds.js +0 -125
  193. package/bin/deploy/to-hana/cfUtil.js +0 -355
  194. package/bin/deploy/to-hana/gitUtil.js +0 -57
  195. package/bin/deploy/to-hana/hana.js +0 -306
  196. package/bin/deploy/to-hana/hdiDeployUtil.js +0 -153
  197. package/bin/deploy/to-hana/index.js +0 -16
  198. package/bin/deploy/to-hana/mtaUtil.js +0 -170
  199. package/bin/mtx/in-cds.js +0 -17
  200. package/bin/plugins.js +0 -32
  201. package/bin/run.js +0 -24
  202. package/bin/utils/log.js +0 -24
  203. package/bin/version.js +0 -178
  204. package/libx/_runtime/audit/Service.js +0 -222
  205. package/libx/_runtime/audit/generic/personal/access.js +0 -61
  206. package/libx/_runtime/audit/generic/personal/index.js +0 -56
  207. package/libx/_runtime/audit/generic/personal/modification.js +0 -132
  208. package/libx/_runtime/audit/generic/personal/utils.js +0 -186
  209. package/libx/_runtime/audit/utils/log.js +0 -23
  210. package/libx/_runtime/audit/utils/v2.js +0 -176
  211. package/libx/_runtime/db/data-conversion/timestamp.js +0 -9
  212. package/libx/_runtime/db/generic/integrity.js +0 -455
  213. package/srv/audit-log.cds +0 -87
  214. package/srv/mtx.cds +0 -2
  215. package/srv/mtx.js +0 -8
  216. /package/lib/{core → linked}/classes.js +0 -0
  217. /package/lib/{core → linked}/entities.js +0 -0
@@ -1,355 +0,0 @@
1
- // eslint-disable-next-line no-console
2
-
3
- const cp = require('child_process');
4
- const fsp = require('fs').promises;
5
- const os = require('os');
6
- const path = require('path');
7
- const util = require('util');
8
-
9
- const IS_WIN = os.platform() === 'win32';
10
- const execAsync = util.promisify(cp.exec);
11
-
12
- const cds = require('../../../lib');
13
- const LOG = cds.log ? cds.log('deploy') : console;
14
- const DEBUG = cds.debug('deploy');
15
-
16
- const { bold, warn } = require('../../utils/term');
17
-
18
- const CF_COMMAND = 'cf';
19
-
20
- const POLL_COUNTER = 40;
21
- const POLL_DELAY = 2500; //ms
22
-
23
- const OPERATION_STATE_INITIAL = 'initial';
24
- const OPERATION_STATE_IN_PROGRESS = 'in progress';
25
- const OPERATION_STATE_FAILED = 'failed';
26
- const OPERATION_STATE_SUCCEEDED = 'succeeded';
27
-
28
- const CF_CLIENT_MINIMUM_VERSION = 8;
29
-
30
-
31
- class CfUtil {
32
-
33
- _clear() {
34
- this.spaceInfo = null;
35
- }
36
-
37
- async _sleep(ms) {
38
- return new Promise((resolve) => {
39
- setTimeout(resolve, ms);
40
- });
41
- }
42
-
43
- async _cfRun(...args) {
44
- args = args.map(arg => arg.replace(/"/g, '\\"'));
45
- const cmdLine = `${CF_COMMAND} "${args.join('" "')}"`;
46
- DEBUG && console.time(cmdLine);
47
- LOG.debug('>>>', cmdLine);
48
-
49
- try {
50
- const result = await execAsync(cmdLine, {
51
- shell: IS_WIN,
52
- stdio: ['inherit', 'pipe', 'inherit']
53
- });
54
- result.stdout = result.stdout?.trim();
55
- result.stderr = result.stderr?.trim();
56
- return result;
57
- } catch (err) {
58
- err.stdout = err.stdout?.trim();
59
- err.stderr = err.stderr?.trim();
60
- throw err;
61
- } finally {
62
- // eslint-disable-next-line no-console
63
- DEBUG && console.timeEnd(cmdLine);
64
- }
65
- }
66
-
67
- async _cfRequest(urlPath, queryObj, bodyObj) {
68
- if (queryObj) {
69
- const entries = Object.entries(queryObj);
70
- const queryStr = entries.map(([key, value]) => {
71
- // commas cause problems in cf curl when not double encoded
72
- value = value.replace(/,/g, encodeURIComponent(','));
73
- return `${key}=${encodeURIComponent(value)}`;
74
- }).join('&');
75
-
76
- urlPath = urlPath + `?${queryStr}`;
77
- }
78
-
79
- // cf curl PATH [-iv] [-X METHOD] [-H HEADER]... [-d DATA] [--output FILE]
80
- const args = ['curl', urlPath];
81
- if (bodyObj) {
82
- args.push('-d');
83
- args.push(JSON.stringify(bodyObj)); // cfRun uses spawn so no special handling for quotes on cli required
84
- }
85
-
86
- const result = await this._cfRun(...args);
87
- let response = {};
88
- if (result.stdout) {
89
- response = JSON.parse(result.stdout);
90
- } else if (result.stderr) {
91
- response = { errors: [{ title: result.stderr }] };
92
- }
93
-
94
- if (response.errors) {
95
- const errorMessage = response.errors.map((entry) => `${entry.title || ''}: ${entry.detail || ''} (${entry.code || ''})`).join('\n');
96
- throw new Error(errorMessage);
97
- }
98
-
99
- return response;
100
- }
101
-
102
- _extract(string, pattern, errorMsg) {
103
- const match = string.match(pattern);
104
- if (match?.[1]) {
105
- return match[1];
106
- }
107
- throw new Error(errorMsg);
108
- }
109
-
110
- async getCfTargetFromConfigFile() {
111
- const cfHome = process.env.CF_HOME || process.env.cf_home || path.join(os.homedir(), '.cf');
112
- try {
113
- const fileContent = await fsp.readFile(path.join(cfHome, 'config.json'));
114
- const config = JSON.parse(fileContent);
115
- if (config) {
116
- return {
117
- apiEndpoint: this._extract(config.Target, /\s*(.+)\s*/, `CF API endpoint is missing. Use 'cf login' to login.`),
118
- org: this._extract(config.OrganizationFields.Name, /\s*(.+)\s*/, `CF org is missing. Use 'cf target -o <ORG> to specify.`),
119
- space: this._extract(config.SpaceFields.Name, /\s*(.+)\s*/, `CF space is missing. Use 'cf target -s <SPACE>' to specify.`)
120
- }
121
- }
122
- } catch (err) {
123
- LOG.debug(`getCfTargetFromConfigFile: ${err}`);
124
- }
125
- }
126
-
127
- async getCfTargetFromCli() {
128
- const result = await this._cfRun('target');
129
- if (result?.stdout) {
130
- return {
131
- apiEndpoint: this._extract(result.stdout, /api endpoint\s*:\s*([^\s]+)/i, `CF API endpoint is missing. Use 'cf login' to login.`),
132
- user: this._extract(result.stdout, /user\s*:\s*(.+)/i, `CF user is missing. Use 'cf login' to login.`),
133
- org: this._extract(result.stdout, /org\s*:\s*(.+)/i, `CF org is missing. Use 'cf target -o <ORG> to specify.`),
134
- space: this._extract(result.stdout, /space\s*:\s*(.+)/i, `CF space is missing. Use 'cf target -s <SPACE>' to specify.`),
135
- };
136
- }
137
- }
138
-
139
- async getCfTarget() {
140
- await this.checkCliVersion();
141
- await this._cfRun('oauth-token'); // check if token is valid or expired / missing
142
- return await this.getCfTargetFromConfigFile() || await this.getCfTargetFromCli();
143
- }
144
-
145
- async checkCliVersion() {
146
- const result = await this._cfRun('-v');
147
- const version = result?.stdout?.match(/version.*(\d+\.\d+\.\d+)/i)
148
- if (parseInt(version?.[1]) < CF_CLIENT_MINIMUM_VERSION) {
149
- console.log(warn(`
150
- [Warning] You are using Cloud Foundry client version ${version[1]}. We recommend version ${CF_CLIENT_MINIMUM_VERSION} or higher.
151
- Deployment will stop in the near future for Cloud Foundry client versions < ${CF_CLIENT_MINIMUM_VERSION}.
152
- `));
153
- }
154
- }
155
-
156
- async getCfSpaceInfo() {
157
- if (!this.spaceInfo) {
158
- LOG.debug('getting space info');
159
-
160
- const target = await this.getCfTarget();
161
-
162
- const { org, space } = target;
163
- const orgs = await this._cfRequest(`/v3/organizations`, { names: org });
164
- if (!orgs?.resources?.length) {
165
- throw new Error(`CF org ${bold(org)} not found!`);
166
- }
167
-
168
- const orgGuid = orgs.resources[0].guid;
169
- const spaces = await this._cfRequest(`/v3/spaces`, { names: space, organization_guids: orgGuid });
170
- if (!spaces?.resources?.length) {
171
- throw new Error(`CF space ${bold(space)} not found in org ${bold(org)}!`);
172
- }
173
-
174
- const spaceGuid = spaces.resources[0].guid;
175
-
176
- this.spaceInfo = Object.assign({}, target, { orgGuid, spaceGuid });
177
- }
178
-
179
- return this.spaceInfo;
180
- }
181
-
182
- async getService(serviceName, showMessage = true) {
183
- showMessage && console.log(`Getting service ${bold(serviceName)}`);
184
- const spaceInfo = await this.getCfSpaceInfo();
185
-
186
- let counter = POLL_COUNTER;
187
- while (counter > 0) {
188
- counter--;
189
- const serviceInstances = await this._cfRequest('/v3/service_instances', {
190
- names: serviceName,
191
- space_guids: spaceInfo.spaceGuid,
192
- organization_guids: spaceInfo.orgGuid
193
- });
194
- if (!serviceInstances?.resources?.length) {
195
- return null;
196
- }
197
-
198
- const serviceInstance = serviceInstances.resources[0];
199
- switch (serviceInstance?.last_operation?.state?.toLowerCase()) {
200
- case OPERATION_STATE_INITIAL:
201
- case OPERATION_STATE_IN_PROGRESS:
202
- await this._sleep(POLL_DELAY);
203
- break;
204
-
205
- case OPERATION_STATE_SUCCEEDED:
206
- return serviceInstance;
207
-
208
- case OPERATION_STATE_FAILED:
209
- throw new Error(`The returned service reported state '${OPERATION_STATE_FAILED}'.\n${JSON.stringify(serviceInstance, null, 4)}`);
210
-
211
- default:
212
- console.error(`Unsupported server response state '${serviceInstance?.last_operation?.state}'. Waiting for next response.`);
213
- break;
214
- }
215
- }
216
-
217
- throw new Error(`Timeout occurred while getting service ${bold(serviceName)}`);
218
- }
219
-
220
-
221
- async getOrCreateService(serviceOfferingName, planName, serviceName, options) {
222
-
223
- const probeService = await this.getService(serviceName, false);
224
- if (probeService) {
225
- console.log(`Getting service ${bold(serviceName)}`);
226
- return probeService;
227
- }
228
-
229
- console.log(`Creating service ${bold(serviceName)} - please be patient...`);
230
-
231
- const spaceInfo = await this.getCfSpaceInfo();
232
-
233
- const servicePlan = await this._cfRequest(`/v3/service_plans`, {
234
- names: planName,
235
- space_guids: spaceInfo.spaceGuid,
236
- organization_guids: spaceInfo.orgGuid,
237
- service_offering_names: serviceOfferingName
238
- });
239
-
240
- if (!servicePlan?.resources?.length) {
241
- throw new Error(`No service plans found`);
242
- }
243
-
244
- const body = {
245
- type: 'managed',
246
- name: serviceName,
247
- tags: [serviceOfferingName],
248
- relationships: {
249
- space: {
250
- data: {
251
- guid: spaceInfo.spaceGuid
252
- }
253
- },
254
- service_plan: {
255
- data: {
256
- guid: servicePlan.resources[0].guid
257
- }
258
- }
259
- }
260
- }
261
-
262
- if (options) {
263
- body.parameters = { ...options };
264
- }
265
-
266
- const postResult = await this._cfRequest('/v3/service_instances', undefined, body);
267
- if (postResult?.errors) {
268
- throw new Error(postResult.errors[0].detail);
269
- }
270
-
271
- const newService = await this.getService(serviceName, false);
272
- if (newService) {
273
- return newService;
274
- }
275
-
276
- throw new Error(`Could not create service ${bold(serviceName)}`);
277
- }
278
-
279
-
280
- async getServiceKey(serviceInstance, serviceKeyName, showMessage = true) {
281
- showMessage && console.log(`Getting service key ${bold(serviceKeyName)}`);
282
-
283
- let counter = POLL_COUNTER;
284
- while (counter > 0) {
285
- counter--;
286
- const bindings = await this._cfRequest(`/v3/service_credential_bindings`, { names: serviceKeyName, service_instance_guids: serviceInstance.guid });
287
- if (!bindings?.resources?.length) {
288
- return null;
289
- }
290
-
291
- const binding = bindings.resources[0];
292
- switch (binding?.last_operation?.state?.toLowerCase()) {
293
- case OPERATION_STATE_INITIAL:
294
- case OPERATION_STATE_IN_PROGRESS:
295
- await this._sleep(POLL_DELAY);
296
- break;
297
-
298
- case OPERATION_STATE_SUCCEEDED: {
299
- const keyDetails = await this._cfRequest(`/v3/service_credential_bindings/${encodeURIComponent(binding.guid)}/details`);
300
- return keyDetails.credentials;
301
- }
302
-
303
- case OPERATION_STATE_FAILED:
304
- throw new Error(`The returned binding reported state '${OPERATION_STATE_FAILED}'.\n${JSON.stringify(binding, null, 4)}`);
305
-
306
- default:
307
- console.error(`Unsupported server response state '${binding?.last_operation?.state}'. Waiting for next response.`);
308
- break;
309
- }
310
- }
311
-
312
- throw new Error(`Timeout occurred while getting service key ${bold(serviceKeyName)}`);
313
- }
314
-
315
-
316
- async getOrCreateServiceKey(serviceInstance, serviceKeyName) {
317
-
318
- const serviceKey = await this.getServiceKey(serviceInstance, serviceKeyName, false);
319
- if (serviceKey) {
320
- console.log(`Getting service key ${bold(serviceKeyName)}`);
321
- return serviceKey;
322
- }
323
-
324
- console.log(`Creating service key ${bold(serviceKeyName)} - please be patient...`);
325
-
326
- const body = {
327
- type: 'key',
328
- name: serviceKeyName,
329
- relationships: {
330
- service_instance: {
331
- data: {
332
- guid: serviceInstance.guid
333
- }
334
- }
335
- },
336
- parameters: {
337
- permissions: 'development'
338
- }
339
- }
340
-
341
- const postResult = await this._cfRequest('/v3/service_credential_bindings', undefined, body);
342
- if (postResult?.errors) {
343
- throw new Error(postResult.errors[0].detail);
344
- }
345
-
346
- const newServiceKey = await this.getServiceKey(serviceInstance, serviceKeyName, false);
347
- if (newServiceKey) {
348
- return newServiceKey;
349
- }
350
-
351
- throw new Error(`Could not create service key ${bold(serviceKeyName)}`);
352
- }
353
- }
354
-
355
- module.exports = new CfUtil();
@@ -1,57 +0,0 @@
1
- const cp = require('child_process');
2
- const path = require('path');
3
- const fs = require('fs').promises;
4
- const util = require('util');
5
-
6
- const cds = require('../../../lib');
7
- const LOG = cds.log ? cds.log('deploy') : console;
8
-
9
- const GIT_IGNORE_FILE = '.gitignore';
10
-
11
- class GitUtil {
12
-
13
- constructor() {
14
- this.execAsync = util.promisify(cp.exec);
15
- }
16
-
17
- async ensureFileIsGitignored(file, currentFolder = process.cwd()) {
18
- const gitIgnorePath = path.join(currentFolder, GIT_IGNORE_FILE);
19
-
20
- try {
21
- await this.execAsync(`git check-ignore -n -v ${file}`, {
22
- cwd: currentFolder
23
- });
24
- return; // file is git ignored
25
- } catch (err) {
26
- if (err.code === 'ENOENT') { // git command not found
27
- LOG.debug('git command not found');
28
- return;
29
- }
30
-
31
- if (!err.stdout.match(/::/)) { // included or not a git repo
32
- LOG.debug(err.message);
33
- return;
34
- }
35
- }
36
-
37
- console.log(`adding entry '${file}' to ${GIT_IGNORE_FILE}.`);
38
- let gitignore = await this.readFileSafely(gitIgnorePath);
39
- gitignore = gitignore + `
40
- # added by cds deploy
41
- ${file}
42
- `;
43
-
44
- await fs.mkdir(path.dirname(gitIgnorePath), { recursive: true });
45
- await fs.writeFile(gitIgnorePath, gitignore, 'utf8');
46
- }
47
-
48
- async readFileSafely(file) {
49
- try {
50
- return await fs.readFile(file, 'utf8');
51
- } catch (err) {
52
- return '';
53
- }
54
- }
55
- }
56
-
57
- module.exports = new GitUtil();