@sap-ux/project-access 1.1.3 → 1.2.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.
@@ -1,6 +1,7 @@
1
1
  export declare const FileName: {
2
2
  readonly Manifest: "manifest.json";
3
3
  readonly Package: "package.json";
4
+ readonly Tsconfig: "tsconfig.json";
4
5
  readonly Ui5Yaml: "ui5.yaml";
5
6
  readonly Ui5LocalYaml: "ui5-local.yaml";
6
7
  readonly Ui5MockYaml: "ui5-mock.yaml";
package/dist/constants.js CHANGED
@@ -4,6 +4,7 @@ exports.FileName = void 0;
4
4
  exports.FileName = {
5
5
  Manifest: 'manifest.json',
6
6
  Package: 'package.json',
7
+ Tsconfig: 'tsconfig.json',
7
8
  Ui5Yaml: 'ui5.yaml',
8
9
  Ui5LocalYaml: 'ui5-local.yaml',
9
10
  Ui5MockYaml: 'ui5-mock.yaml'
@@ -5,10 +5,20 @@ import type { Editor } from 'mem-fs-editor';
5
5
  * @param filename - filename to search
6
6
  * @param root - root folder to start search
7
7
  * @param excludeFolders - list of folder names to exclude (search doesn't traverse into these folders)
8
- * @param fs - optional mem-fs-editor instance
8
+ * @param [memFs] - optional mem-fs-editor instance
9
9
  * @returns - array of paths that contain the filename
10
10
  */
11
- export declare function findFiles(filename: string, root: string, excludeFolders: string[], fs?: Editor): Promise<string[]>;
11
+ export declare function findFiles(filename: string, root: string, excludeFolders: string[], memFs?: Editor): Promise<string[]>;
12
+ /**
13
+ * Search for 'filename' starting from 'root'. Returns array of paths that contain the file.
14
+ *
15
+ * @param extension - file extension to search for including '.', e.g. '.ts'
16
+ * @param root - root folder to start search
17
+ * @param excludeFolders - list of folder names to exclude (search doesn't traverse into these folders)
18
+ * @param [memFs] - optional mem-fs-editor instance
19
+ * @returns - array of file paths that have the extension
20
+ */
21
+ export declare function findFilesByExtension(extension: string, root: string, excludeFolders: string[], memFs?: Editor): Promise<string[]>;
12
22
  /**
13
23
  * Find a file by name in parent folders starting from 'startPath'.
14
24
  *
@@ -12,76 +12,102 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.findFileUp = exports.findFiles = void 0;
15
+ exports.findFileUp = exports.findFilesByExtension = exports.findFiles = void 0;
16
16
  const path_1 = require("path");
17
17
  const findit2_1 = __importDefault(require("findit2"));
18
18
  const file_access_1 = require("./file-access");
19
19
  /**
20
- * Creates a filter function that removes files that have been deleted in an instance of a mem-fs-editor.
20
+ * Get deleted and modified files from mem-fs editor filtered by query and 'by' (name|extension).
21
21
  *
22
- * @param changes - changes recorded in an instance of a mem-fs-editor
23
- * @param filename - relevant filename
24
- * @returns a filter function for string arrays
22
+ * @param changes - memfs editor changes, usually retrieved by fs.dump()
23
+ * @param query - search string, file name or file extension
24
+ * @param by - search by file 'name' or file 'extension'
25
+ * @returns - array of deleted and modified files filtered by query
25
26
  */
26
- function getMemFsFilter(changes, filename) {
27
- const deleted = Object.entries(changes)
28
- .filter(([, info]) => info.state === 'deleted')
29
- .map(([file]) => (0, path_1.join)((0, path_1.basename)((0, path_1.join)(file)) === filename ? (0, path_1.dirname)(file) : file));
30
- return (path) => !deleted.find((entry) => path.startsWith(entry));
31
- }
32
- /**
33
- * Concatanates the given list of files with additional files that have been created using a mem-fs-editor and matching the filename.
34
- *
35
- * @param folders - existing list of folders
36
- * @param changes - changes recorded in an instance of a mem-fs-editor
37
- * @param filename - relevant filename
38
- * @returns Concatanated and deduped list of folders containing the given filename
39
- */
40
- function concatNewFoldersFromMemFs(folders, changes, filename) {
41
- const modified = Object.entries(changes)
42
- .filter(([file, info]) => info.state === 'modified' && (0, path_1.basename)((0, path_1.join)(file)) === filename)
43
- .map(([file]) => (0, path_1.dirname)((0, path_1.join)(file)));
44
- return [...new Set([...folders, ...modified])];
27
+ function getMemFsChanges(changes, query, by) {
28
+ const deleted = [];
29
+ const modified = [];
30
+ const getFilePartToCompare = by === 'extension' ? path_1.extname : path_1.basename;
31
+ for (const file of Object.keys(changes).filter((f) => getFilePartToCompare(f) === query)) {
32
+ if (changes[file].state === 'deleted') {
33
+ deleted.push((0, path_1.join)(file));
34
+ }
35
+ if (changes[file].state === 'modified') {
36
+ modified.push((0, path_1.join)(file));
37
+ }
38
+ }
39
+ return { deleted, modified };
45
40
  }
46
41
  /**
47
- * Search for 'filename' starting from 'root'. Returns array of paths that contain the file.
42
+ * Find function to search for files.
48
43
  *
49
- * @param filename - filename to search
50
- * @param root - root folder to start search
51
- * @param excludeFolders - list of folder names to exclude (search doesn't traverse into these folders)
52
- * @param fs - optional mem-fs-editor instance
53
- * @returns - array of paths that contain the filename
44
+ * @param options - find options
45
+ * @param options.query - search string
46
+ * @param options.by - search by file 'name' or file 'extension'
47
+ * @param options.root - folder to start recursive search
48
+ * @param options.excludeFolders - folder names to exclude
49
+ * @param [options.memFs] - optional memfs editor instance
50
+ * @returns - array of paths that contain the file if searched for name; array of full file paths in case of search by extension
54
51
  */
55
- function findFiles(filename, root, excludeFolders, fs) {
52
+ function findBy(options) {
53
+ const getFilePartToCompare = options.by === 'extension' ? path_1.extname : path_1.basename;
56
54
  return new Promise((resolve, reject) => {
57
55
  const results = [];
58
- const finder = (0, findit2_1.default)(root);
56
+ const finder = (0, findit2_1.default)(options.root);
59
57
  finder.on('directory', (dir, _stat, stop) => {
60
58
  const base = (0, path_1.basename)(dir);
61
- if (excludeFolders.includes(base)) {
59
+ if (options.excludeFolders.includes(base)) {
62
60
  stop();
63
61
  }
64
62
  });
65
63
  finder.on('file', (file) => {
66
- if (file.endsWith(path_1.sep + filename)) {
67
- results.push((0, path_1.dirname)(file));
64
+ if (getFilePartToCompare(file) === options.query) {
65
+ results.push(file);
68
66
  }
69
67
  });
70
68
  finder.on('end', () => {
71
- if (fs) {
72
- const changes = fs.dump('');
73
- resolve(concatNewFoldersFromMemFs(results, changes, filename).filter(getMemFsFilter(changes, filename)));
69
+ let searchResult = results;
70
+ if (options.memFs) {
71
+ const { modified, deleted } = getMemFsChanges(options.memFs.dump(''), options.query, options.by);
72
+ const merged = Array.from(new Set([...results, ...modified]));
73
+ searchResult = merged.filter((f) => !deleted.includes(f));
74
74
  }
75
- else {
76
- resolve(results);
75
+ if (options.by === 'name') {
76
+ searchResult = searchResult.map((f) => (0, path_1.dirname)(f));
77
77
  }
78
+ resolve(searchResult);
78
79
  });
79
80
  finder.on('error', (error) => {
80
81
  reject(error);
81
82
  });
82
83
  });
83
84
  }
85
+ /**
86
+ * Search for 'filename' starting from 'root'. Returns array of paths that contain the file.
87
+ *
88
+ * @param filename - filename to search
89
+ * @param root - root folder to start search
90
+ * @param excludeFolders - list of folder names to exclude (search doesn't traverse into these folders)
91
+ * @param [memFs] - optional mem-fs-editor instance
92
+ * @returns - array of paths that contain the filename
93
+ */
94
+ function findFiles(filename, root, excludeFolders, memFs) {
95
+ return findBy({ query: filename, by: 'name', root, excludeFolders, memFs });
96
+ }
84
97
  exports.findFiles = findFiles;
98
+ /**
99
+ * Search for 'filename' starting from 'root'. Returns array of paths that contain the file.
100
+ *
101
+ * @param extension - file extension to search for including '.', e.g. '.ts'
102
+ * @param root - root folder to start search
103
+ * @param excludeFolders - list of folder names to exclude (search doesn't traverse into these folders)
104
+ * @param [memFs] - optional mem-fs-editor instance
105
+ * @returns - array of file paths that have the extension
106
+ */
107
+ function findFilesByExtension(extension, root, excludeFolders, memFs) {
108
+ return findBy({ query: extension, by: 'extension', root, excludeFolders, memFs });
109
+ }
110
+ exports.findFilesByExtension = findFilesByExtension;
85
111
  /**
86
112
  * Find a file by name in parent folders starting from 'startPath'.
87
113
  *
@@ -1,3 +1,3 @@
1
1
  export { fileExists, readFile, readJSON } from './file-access';
2
- export { findFiles, findFileUp } from './file-search';
2
+ export { findFiles, findFilesByExtension, findFileUp } from './file-search';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findFileUp = exports.findFiles = exports.readJSON = exports.readFile = exports.fileExists = void 0;
3
+ exports.findFileUp = exports.findFilesByExtension = exports.findFiles = exports.readJSON = exports.readFile = exports.fileExists = void 0;
4
4
  var file_access_1 = require("./file-access");
5
5
  Object.defineProperty(exports, "fileExists", { enumerable: true, get: function () { return file_access_1.fileExists; } });
6
6
  Object.defineProperty(exports, "readFile", { enumerable: true, get: function () { return file_access_1.readFile; } });
7
7
  Object.defineProperty(exports, "readJSON", { enumerable: true, get: function () { return file_access_1.readJSON; } });
8
8
  var file_search_1 = require("./file-search");
9
9
  Object.defineProperty(exports, "findFiles", { enumerable: true, get: function () { return file_search_1.findFiles; } });
10
+ Object.defineProperty(exports, "findFilesByExtension", { enumerable: true, get: function () { return file_search_1.findFilesByExtension; } });
10
11
  Object.defineProperty(exports, "findFileUp", { enumerable: true, get: function () { return file_search_1.findFileUp; } });
11
12
  //# sourceMappingURL=index.js.map
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { FileName } from './constants';
2
- export { findAllApps, findProjectRoot, getAppRootFromWebappPath, getCapModelAndServices, getCapProjectType, getWebappPath, isCapJavaProject, isCapNodeJsProject, loadModuleFromProject } from './project';
2
+ export { findAllApps, findProjectRoot, getAppRootFromWebappPath, getAppProgrammingLanguage, getCapModelAndServices, getCapProjectType, getWebappPath, isCapJavaProject, isCapNodeJsProject, loadModuleFromProject } from './project';
3
3
  export * from './types';
4
4
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -14,13 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.loadModuleFromProject = exports.isCapNodeJsProject = exports.isCapJavaProject = exports.getWebappPath = exports.getCapProjectType = exports.getCapModelAndServices = exports.getAppRootFromWebappPath = exports.findProjectRoot = exports.findAllApps = exports.FileName = void 0;
17
+ exports.loadModuleFromProject = exports.isCapNodeJsProject = exports.isCapJavaProject = exports.getWebappPath = exports.getCapProjectType = exports.getCapModelAndServices = exports.getAppProgrammingLanguage = exports.getAppRootFromWebappPath = exports.findProjectRoot = exports.findAllApps = exports.FileName = void 0;
18
18
  var constants_1 = require("./constants");
19
19
  Object.defineProperty(exports, "FileName", { enumerable: true, get: function () { return constants_1.FileName; } });
20
20
  var project_1 = require("./project");
21
21
  Object.defineProperty(exports, "findAllApps", { enumerable: true, get: function () { return project_1.findAllApps; } });
22
22
  Object.defineProperty(exports, "findProjectRoot", { enumerable: true, get: function () { return project_1.findProjectRoot; } });
23
23
  Object.defineProperty(exports, "getAppRootFromWebappPath", { enumerable: true, get: function () { return project_1.getAppRootFromWebappPath; } });
24
+ Object.defineProperty(exports, "getAppProgrammingLanguage", { enumerable: true, get: function () { return project_1.getAppProgrammingLanguage; } });
24
25
  Object.defineProperty(exports, "getCapModelAndServices", { enumerable: true, get: function () { return project_1.getCapModelAndServices; } });
25
26
  Object.defineProperty(exports, "getCapProjectType", { enumerable: true, get: function () { return project_1.getCapProjectType; } });
26
27
  Object.defineProperty(exports, "getWebappPath", { enumerable: true, get: function () { return project_1.getWebappPath; } });
@@ -1,4 +1,5 @@
1
1
  export { getCapModelAndServices, getCapProjectType, isCapJavaProject, isCapNodeJsProject } from './cap';
2
+ export { getAppProgrammingLanguage } from './info';
2
3
  export { loadModuleFromProject } from './module-loader';
3
4
  export { findAllApps, findProjectRoot, getAppRootFromWebappPath } from './search';
4
5
  export { getWebappPath } from './ui5-config';
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWebappPath = exports.getAppRootFromWebappPath = exports.findProjectRoot = exports.findAllApps = exports.loadModuleFromProject = exports.isCapNodeJsProject = exports.isCapJavaProject = exports.getCapProjectType = exports.getCapModelAndServices = void 0;
3
+ exports.getWebappPath = exports.getAppRootFromWebappPath = exports.findProjectRoot = exports.findAllApps = exports.loadModuleFromProject = exports.getAppProgrammingLanguage = exports.isCapNodeJsProject = exports.isCapJavaProject = exports.getCapProjectType = exports.getCapModelAndServices = void 0;
4
4
  var cap_1 = require("./cap");
5
5
  Object.defineProperty(exports, "getCapModelAndServices", { enumerable: true, get: function () { return cap_1.getCapModelAndServices; } });
6
6
  Object.defineProperty(exports, "getCapProjectType", { enumerable: true, get: function () { return cap_1.getCapProjectType; } });
7
7
  Object.defineProperty(exports, "isCapJavaProject", { enumerable: true, get: function () { return cap_1.isCapJavaProject; } });
8
8
  Object.defineProperty(exports, "isCapNodeJsProject", { enumerable: true, get: function () { return cap_1.isCapNodeJsProject; } });
9
+ var info_1 = require("./info");
10
+ Object.defineProperty(exports, "getAppProgrammingLanguage", { enumerable: true, get: function () { return info_1.getAppProgrammingLanguage; } });
9
11
  var module_loader_1 = require("./module-loader");
10
12
  Object.defineProperty(exports, "loadModuleFromProject", { enumerable: true, get: function () { return module_loader_1.loadModuleFromProject; } });
11
13
  var search_1 = require("./search");
@@ -0,0 +1,11 @@
1
+ import type { Editor } from 'mem-fs-editor';
2
+ import type { AppProgrammingLanguage } from '../types';
3
+ /**
4
+ * Get the used programming language of an application.
5
+ *
6
+ * @param appRoot - root folder of the application
7
+ * @param [memFs] - optional mem-fs editor instance
8
+ * @returns - used language, JavaScript or TypeScript
9
+ */
10
+ export declare function getAppProgrammingLanguage(appRoot: string, memFs?: Editor): Promise<AppProgrammingLanguage>;
11
+ //# sourceMappingURL=info.d.ts.map
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getAppProgrammingLanguage = void 0;
13
+ const path_1 = require("path");
14
+ const constants_1 = require("../constants");
15
+ const file_1 = require("../file");
16
+ const ui5_config_1 = require("./ui5-config");
17
+ /**
18
+ * Get the used programming language of an application.
19
+ *
20
+ * @param appRoot - root folder of the application
21
+ * @param [memFs] - optional mem-fs editor instance
22
+ * @returns - used language, JavaScript or TypeScript
23
+ */
24
+ function getAppProgrammingLanguage(appRoot, memFs) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const ignoreFolders = ['node_modules', '.git'];
27
+ let appLanguage = '';
28
+ try {
29
+ const webappPath = yield (0, ui5_config_1.getWebappPath)(appRoot, memFs);
30
+ if (yield (0, file_1.fileExists)(webappPath, memFs)) {
31
+ if ((yield (0, file_1.fileExists)((0, path_1.join)(appRoot, constants_1.FileName.Tsconfig), memFs)) &&
32
+ (yield (0, file_1.findFilesByExtension)('.ts', webappPath, ignoreFolders, memFs)).length > 0) {
33
+ appLanguage = 'TypeScript';
34
+ }
35
+ else if ((yield (0, file_1.findFilesByExtension)('.js', webappPath, ignoreFolders, memFs)).length > 0) {
36
+ appLanguage = 'JavaScript';
37
+ }
38
+ }
39
+ }
40
+ catch (_a) {
41
+ // could not detect app language
42
+ }
43
+ return appLanguage;
44
+ });
45
+ }
46
+ exports.getAppProgrammingLanguage = getAppProgrammingLanguage;
47
+ //# sourceMappingURL=info.js.map
@@ -1,8 +1,10 @@
1
+ import type { Editor } from 'mem-fs-editor';
1
2
  /**
2
3
  * Get path to webapp.
3
4
  *
4
5
  * @param projectRoot - root path, where package.json or ui5.yaml is
6
+ * @param [memFs] - optional mem-fs editor instance
5
7
  * @returns - path to webapp folder
6
8
  */
7
- export declare function getWebappPath(projectRoot: string): Promise<string>;
9
+ export declare function getWebappPath(projectRoot: string, memFs?: Editor): Promise<string>;
8
10
  //# sourceMappingURL=ui5-config.d.ts.map
@@ -18,15 +18,16 @@ const file_1 = require("../file");
18
18
  * Get path to webapp.
19
19
  *
20
20
  * @param projectRoot - root path, where package.json or ui5.yaml is
21
+ * @param [memFs] - optional mem-fs editor instance
21
22
  * @returns - path to webapp folder
22
23
  */
23
- function getWebappPath(projectRoot) {
24
+ function getWebappPath(projectRoot, memFs) {
24
25
  var _a, _b;
25
26
  return __awaiter(this, void 0, void 0, function* () {
26
27
  let webappPath = (0, path_1.join)(projectRoot, 'webapp');
27
28
  const ui5YamlPath = (0, path_1.join)(projectRoot, constants_1.FileName.Ui5Yaml);
28
- if (yield (0, file_1.fileExists)(ui5YamlPath)) {
29
- const yamlString = yield (0, file_1.readFile)(ui5YamlPath);
29
+ if (yield (0, file_1.fileExists)(ui5YamlPath, memFs)) {
30
+ const yamlString = yield (0, file_1.readFile)(ui5YamlPath, memFs);
30
31
  const ui5Config = yield ui5_config_1.UI5Config.newInstance(yamlString);
31
32
  const relativeWebappPath = (_b = (_a = ui5Config.getConfiguration()) === null || _a === void 0 ? void 0 : _a.paths) === null || _b === void 0 ? void 0 : _b.webapp;
32
33
  if (relativeWebappPath) {
@@ -1,6 +1,7 @@
1
1
  export * from './cap';
2
- export * from './package';
3
2
  export * from './find';
3
+ export * from './info';
4
+ export * from './package';
4
5
  export * from './vscode';
5
6
  export * from './webapp';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -15,8 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./cap"), exports);
18
- __exportStar(require("./package"), exports);
19
18
  __exportStar(require("./find"), exports);
19
+ __exportStar(require("./info"), exports);
20
+ __exportStar(require("./package"), exports);
20
21
  __exportStar(require("./vscode"), exports);
21
22
  __exportStar(require("./webapp"), exports);
22
23
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ export type AppProgrammingLanguage = 'JavaScript' | 'TypeScript' | '';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sap-ux/project-access",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "description": "Library to access SAP Fiori tools projects",
5
5
  "repository": {
6
6
  "type": "git",