@sap-ux/project-access 1.8.4 → 1.9.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.
@@ -45,8 +45,25 @@ export declare function getCapModelAndServices(projectRoot: string): Promise<{
45
45
  * Get CAP CDS project environment config for project root.
46
46
  *
47
47
  * @param capProjectPath - project root of a CAP project
48
- * @returns - environment config for CAP project
48
+ * @returns - environment config for a CAP project
49
49
  */
50
50
  export declare function getCapEnvironment(capProjectPath: string): Promise<CdsEnvironment>;
51
+ /**
52
+ * Get absolute path to a resource.
53
+ *
54
+ * @param projectRoot - project root of a CAP project
55
+ * @param relativeUri - relative resource path.
56
+ * @returns {string} - absolute path.
57
+ */
58
+ export declare const toAbsoluteUri: (projectRoot: string, relativeUri: string) => string;
59
+ /**
60
+ * Converts to referenced uri to be used in using statements.
61
+ *
62
+ * @param projectRoot - project root of a CAP project
63
+ * @param relativeUriFrom - relative uri of from directory
64
+ * @param relativeUriTo - relative uri of to directory
65
+ * @returns {Promise<string>} - reference uri
66
+ */
67
+ export declare const toReferenceUri: (projectRoot: string, relativeUriFrom: string, relativeUriTo: string) => Promise<string>;
51
68
  export {};
52
69
  //# sourceMappingURL=cap.d.ts.map
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getCapEnvironment = exports.getCapModelAndServices = exports.getCapCustomPaths = exports.getCapProjectType = exports.isCapJavaProject = exports.isCapNodeJsProject = void 0;
12
+ exports.toReferenceUri = exports.toAbsoluteUri = exports.getCapEnvironment = exports.getCapModelAndServices = exports.getCapCustomPaths = exports.getCapProjectType = exports.isCapJavaProject = exports.isCapNodeJsProject = void 0;
13
13
  const path_1 = require("path");
14
14
  const constants_1 = require("../constants");
15
15
  const file_1 = require("../file");
@@ -99,7 +99,7 @@ exports.getCapCustomPaths = getCapCustomPaths;
99
99
  */
100
100
  function getCapModelAndServices(projectRoot) {
101
101
  return __awaiter(this, void 0, void 0, function* () {
102
- const cds = yield (0, module_loader_1.loadModuleFromProject)(projectRoot, '@sap/cds');
102
+ const cds = yield loadCdsModuleFromProject(projectRoot);
103
103
  const capProjectPaths = yield getCapCustomPaths(projectRoot);
104
104
  const modelPaths = [
105
105
  (0, path_1.join)(projectRoot, capProjectPaths.app),
@@ -119,14 +119,124 @@ exports.getCapModelAndServices = getCapModelAndServices;
119
119
  * Get CAP CDS project environment config for project root.
120
120
  *
121
121
  * @param capProjectPath - project root of a CAP project
122
- * @returns - environment config for CAP project
122
+ * @returns - environment config for a CAP project
123
123
  */
124
124
  function getCapEnvironment(capProjectPath) {
125
125
  return __awaiter(this, void 0, void 0, function* () {
126
- const module = yield (0, module_loader_1.loadModuleFromProject)(capProjectPath, '@sap/cds');
127
- const cds = 'default' in module ? module.default : module;
126
+ const cds = yield loadCdsModuleFromProject(capProjectPath);
128
127
  return cds.env.for('cds', capProjectPath);
129
128
  });
130
129
  }
131
130
  exports.getCapEnvironment = getCapEnvironment;
131
+ /**
132
+ * Load CAP CDS module for a project based on its root.
133
+ *
134
+ * @param capProjectPath - project root of a CAP project
135
+ * @returns - CAP CDS module for a CAP project
136
+ */
137
+ function loadCdsModuleFromProject(capProjectPath) {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ const module = yield (0, module_loader_1.loadModuleFromProject)(capProjectPath, '@sap/cds');
140
+ return 'default' in module ? module.default : module;
141
+ });
142
+ }
143
+ /**
144
+ * Get absolute path to a resource.
145
+ *
146
+ * @param projectRoot - project root of a CAP project
147
+ * @param relativeUri - relative resource path.
148
+ * @returns {string} - absolute path.
149
+ */
150
+ const toAbsoluteUri = (projectRoot, relativeUri) => (0, path_1.join)(projectRoot, relativeUri);
151
+ exports.toAbsoluteUri = toAbsoluteUri;
152
+ /**
153
+ * Converts to referenced uri to be used in using statements.
154
+ *
155
+ * @param projectRoot - project root of a CAP project
156
+ * @param relativeUriFrom - relative uri of from directory
157
+ * @param relativeUriTo - relative uri of to directory
158
+ * @returns {Promise<string>} - reference uri
159
+ */
160
+ const toReferenceUri = (projectRoot, relativeUriFrom, relativeUriTo) => __awaiter(void 0, void 0, void 0, function* () {
161
+ let relativeUri = '';
162
+ const indexNodeModules = relativeUriTo.lastIndexOf('node_modules');
163
+ if (indexNodeModules >= 0) {
164
+ // extract module name from fileUri - e.g. '@sap/cds/common' from '../../node_modules/@sap/cds/common.cds'
165
+ const indexLastDot = relativeUriTo.lastIndexOf('.');
166
+ if (indexLastDot > indexNodeModules + 13) {
167
+ relativeUri = relativeUriTo.slice(indexNodeModules + 13, indexLastDot);
168
+ }
169
+ else {
170
+ relativeUri = relativeUriTo.slice(indexNodeModules + 13);
171
+ }
172
+ }
173
+ else if (relativeUriTo.startsWith('../') || relativeUriTo.startsWith('..\\')) {
174
+ // file outside current project (e.g. mono repo)
175
+ const result = yield getPackageNameInFolder(projectRoot, relativeUriTo);
176
+ if (result.packageName) {
177
+ relativeUri = result.packageName + relativeUriTo.slice(result.packageFolder.length);
178
+ }
179
+ }
180
+ if (!relativeUri) {
181
+ // build relative path
182
+ const fromDir = (0, path_1.dirname)((0, exports.toAbsoluteUri)(projectRoot, relativeUriFrom));
183
+ relativeUri = (0, path_1.relative)(fromDir, (0, exports.toAbsoluteUri)(projectRoot, relativeUriTo));
184
+ if (!relativeUri.startsWith('.')) {
185
+ relativeUri = './' + relativeUri;
186
+ }
187
+ }
188
+ // remove file extension
189
+ const fileExtension = relativeUri.lastIndexOf('.') > 0 ? relativeUri.slice(relativeUri.lastIndexOf('.') + 1) : '';
190
+ if (['CDS', 'JSON'].includes(fileExtension.toUpperCase())) {
191
+ relativeUri = relativeUri.slice(0, relativeUri.length - fileExtension.length - 1);
192
+ }
193
+ // always use '/' instead of platform specific separator
194
+ return relativeUri.split(path_1.sep).join('/');
195
+ });
196
+ exports.toReferenceUri = toReferenceUri;
197
+ /**
198
+ * Gets package name from the folder.
199
+ *
200
+ * @param baseUri - base uri of the cap project
201
+ * @param relativeUri - relative uri to the resource folder
202
+ * @returns {Promise<{ packageName: string; packageFolder: string }>} - package name and folder
203
+ */
204
+ function getPackageNameInFolder(baseUri, relativeUri) {
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const refUriParts = relativeUri.split(path_1.sep);
207
+ const result = { packageName: '', packageFolder: relativeUri };
208
+ for (let i = refUriParts.length - 1; i >= 0 && !result.packageName; i--) {
209
+ const currentFolder = refUriParts.slice(0, i).join(path_1.sep);
210
+ result.packageName = yield readPackageNameForFolder(baseUri, currentFolder);
211
+ if (result.packageName) {
212
+ result.packageFolder = currentFolder;
213
+ }
214
+ }
215
+ return result;
216
+ });
217
+ }
218
+ /**
219
+ * Reads package name from package json of the folder.
220
+ *
221
+ * @param baseUri - base uri of the cap project
222
+ * @param relativeUri - relative uri to the resource folder
223
+ * @returns {Promise<string>} - package name
224
+ */
225
+ function readPackageNameForFolder(baseUri, relativeUri) {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ let packageName = '';
228
+ try {
229
+ const path = (0, path_1.normalize)(baseUri + '/' + relativeUri + '/' + 'package.json');
230
+ const content = yield (0, file_1.readFile)(path);
231
+ if (content) {
232
+ const parsed = JSON.parse(content);
233
+ packageName = parsed.name;
234
+ }
235
+ }
236
+ catch (e) {
237
+ packageName = '';
238
+ }
239
+ return packageName;
240
+ });
241
+ }
132
242
  //# sourceMappingURL=cap.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sap-ux/project-access",
3
- "version": "1.8.4",
3
+ "version": "1.9.1",
4
4
  "description": "Library to access SAP Fiori tools projects",
5
5
  "repository": {
6
6
  "type": "git",