@sap-ux/project-access 1.8.4 → 1.9.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.
@@ -48,5 +48,22 @@ export declare function getCapModelAndServices(projectRoot: string): Promise<{
48
48
  * @returns - environment config for 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");
@@ -129,4 +129,103 @@ function getCapEnvironment(capProjectPath) {
129
129
  });
130
130
  }
131
131
  exports.getCapEnvironment = getCapEnvironment;
132
+ /**
133
+ * Get absolute path to a resource.
134
+ *
135
+ * @param projectRoot - project root of a CAP project
136
+ * @param relativeUri - relative resource path.
137
+ * @returns {string} - absolute path.
138
+ */
139
+ const toAbsoluteUri = (projectRoot, relativeUri) => (0, path_1.join)(projectRoot, relativeUri);
140
+ exports.toAbsoluteUri = toAbsoluteUri;
141
+ /**
142
+ * Converts to referenced uri to be used in using statements.
143
+ *
144
+ * @param projectRoot - project root of a CAP project
145
+ * @param relativeUriFrom - relative uri of from directory
146
+ * @param relativeUriTo - relative uri of to directory
147
+ * @returns {Promise<string>} - reference uri
148
+ */
149
+ const toReferenceUri = (projectRoot, relativeUriFrom, relativeUriTo) => __awaiter(void 0, void 0, void 0, function* () {
150
+ let relativeUri = '';
151
+ const indexNodeModules = relativeUriTo.lastIndexOf('node_modules');
152
+ if (indexNodeModules >= 0) {
153
+ // extract module name from fileUri - e.g. '@sap/cds/common' from '../../node_modules/@sap/cds/common.cds'
154
+ const indexLastDot = relativeUriTo.lastIndexOf('.');
155
+ if (indexLastDot > indexNodeModules + 13) {
156
+ relativeUri = relativeUriTo.slice(indexNodeModules + 13, indexLastDot);
157
+ }
158
+ else {
159
+ relativeUri = relativeUriTo.slice(indexNodeModules + 13);
160
+ }
161
+ }
162
+ else if (relativeUriTo.startsWith('../') || relativeUriTo.startsWith('..\\')) {
163
+ // file outside current project (e.g. mono repo)
164
+ const result = yield getPackageNameInFolder(projectRoot, relativeUriTo);
165
+ if (result.packageName) {
166
+ relativeUri = result.packageName + relativeUriTo.slice(result.packageFolder.length);
167
+ }
168
+ }
169
+ if (!relativeUri) {
170
+ // build relative path
171
+ const fromDir = (0, path_1.dirname)((0, exports.toAbsoluteUri)(projectRoot, relativeUriFrom));
172
+ relativeUri = (0, path_1.relative)(fromDir, (0, exports.toAbsoluteUri)(projectRoot, relativeUriTo));
173
+ if (!relativeUri.startsWith('.')) {
174
+ relativeUri = './' + relativeUri;
175
+ }
176
+ }
177
+ // remove file extension
178
+ const fileExtension = relativeUri.lastIndexOf('.') > 0 ? relativeUri.slice(relativeUri.lastIndexOf('.') + 1) : '';
179
+ if (['CDS', 'JSON'].includes(fileExtension.toUpperCase())) {
180
+ relativeUri = relativeUri.slice(0, relativeUri.length - fileExtension.length - 1);
181
+ }
182
+ // always use '/' instead of platform specific separator
183
+ return relativeUri.split(path_1.sep).join('/');
184
+ });
185
+ exports.toReferenceUri = toReferenceUri;
186
+ /**
187
+ * Gets package name from the folder.
188
+ *
189
+ * @param baseUri - base uri of the cap project
190
+ * @param relativeUri - relative uri to the resource folder
191
+ * @returns {Promise<{ packageName: string; packageFolder: string }>} - package name and folder
192
+ */
193
+ function getPackageNameInFolder(baseUri, relativeUri) {
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ const refUriParts = relativeUri.split(path_1.sep);
196
+ const result = { packageName: '', packageFolder: relativeUri };
197
+ for (let i = refUriParts.length - 1; i >= 0 && !result.packageName; i--) {
198
+ const currentFolder = refUriParts.slice(0, i).join(path_1.sep);
199
+ result.packageName = yield readPackageNameForFolder(baseUri, currentFolder);
200
+ if (result.packageName) {
201
+ result.packageFolder = currentFolder;
202
+ }
203
+ }
204
+ return result;
205
+ });
206
+ }
207
+ /**
208
+ * Reads package name from package json of the folder.
209
+ *
210
+ * @param baseUri - base uri of the cap project
211
+ * @param relativeUri - relative uri to the resource folder
212
+ * @returns {Promise<string>} - package name
213
+ */
214
+ function readPackageNameForFolder(baseUri, relativeUri) {
215
+ return __awaiter(this, void 0, void 0, function* () {
216
+ let packageName = '';
217
+ try {
218
+ const path = (0, path_1.normalize)(baseUri + '/' + relativeUri + '/' + 'package.json');
219
+ const content = yield (0, file_1.readFile)(path);
220
+ if (content) {
221
+ const parsed = JSON.parse(content);
222
+ packageName = parsed.name;
223
+ }
224
+ }
225
+ catch (e) {
226
+ packageName = '';
227
+ }
228
+ return packageName;
229
+ });
230
+ }
132
231
  //# 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.0",
4
4
  "description": "Library to access SAP Fiori tools projects",
5
5
  "repository": {
6
6
  "type": "git",