Skip to content

Commit 1e2aa0e

Browse files
committed
[FEATURE] Workspace: Add getModules method
Can be used to retrieve all modules referenced in a workspace to iterate over them. Required for #589
1 parent 2b41f2a commit 1e2aa0e

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

lib/graph/Workspace.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ class Workspace {
7474
return this.#configuration.metadata.name;
7575
}
7676

77+
/**
78+
* Returns an array of [Module]{@ui5/project/graph/Module} instances found in the configured
79+
* dependency-management resolution paths of this workspace, sorted by module ID.
80+
*
81+
* @public
82+
* @returns {Promise<@ui5/project/graph/Module[]>}
83+
* Array of Module instances sorted by module ID
84+
*/
85+
async getModules() {
86+
const {moduleIdMap} = await this._getResolvedModules();
87+
const sortedMap = new Map([...moduleIdMap].sort((a, b) => String(a[0]).localeCompare(b[0])));
88+
return Array.from(sortedMap.values());
89+
}
90+
7791
/**
7892
* For a given project name (e.g. the value of the <code>metadata.name</code> property in a ui5.yaml),
7993
* returns a [Module]{@ui5/project/graph/Module} instance or <code>undefined</code> depending on whether the project
@@ -82,7 +96,7 @@ class Workspace {
8296
* @public
8397
* @param {string} projectName Name of the project
8498
* @returns {Promise<@ui5/project/graph/Module|undefined>}
85-
* Module instance of <code>undefined</code> if none is found
99+
* Module instance or <code>undefined</code> if none is found
86100
*/
87101
async getModuleByProjectName(projectName) {
88102
const {projectNameMap} = await this._getResolvedModules();
@@ -98,7 +112,7 @@ class Workspace {
98112
* @public
99113
* @param {string} nodeId Node ID of the module
100114
* @returns {Promise<@ui5/project/graph/Module|undefined>}
101-
* Module instance of <code>undefined</code> if none is found
115+
* Module instance or <code>undefined</code> if none is found
102116
*/
103117
async getModuleByNodeId(nodeId) {
104118
const {moduleIdMap} = await this._getResolvedModules();

test/lib/graph/Workspace.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ test("Basic resolution", async (t) => {
8282
t.is(await workspace.getModuleByNodeId("library.d"), libD,
8383
"getModuleByNodeId returns correct module for library.d");
8484

85+
const modules = await workspace.getModules();
86+
t.deepEqual(modules, [libD, libE], "getModules returns modules sorted by module ID");
87+
8588
t.deepEqual(Array.from(moduleIdMap.keys()).sort(), ["library.d", "library.e"], "Correct module ID keys");
8689
moduleIdMap.forEach((value, key) => {
8790
t.is(value, projectNameMap.get(key), `Same instance of module ${key} in both maps`);

0 commit comments

Comments
 (0)