Skip to content

Commit

Permalink
[FEATURE] Workspace: Add getModules method
Browse files Browse the repository at this point in the history
Can be used to retrieve all modules referenced in a workspace to
iterate over them.

Required for #589
  • Loading branch information
matz3 committed Mar 30, 2023
1 parent 2b41f2a commit 1e2aa0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/graph/Workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ class Workspace {
return this.#configuration.metadata.name;
}

/**
* Returns an array of [Module]{@ui5/project/graph/Module} instances found in the configured
* dependency-management resolution paths of this workspace, sorted by module ID.
*
* @public
* @returns {Promise<@ui5/project/graph/Module[]>}
* Array of Module instances sorted by module ID
*/
async getModules() {
const {moduleIdMap} = await this._getResolvedModules();
const sortedMap = new Map([...moduleIdMap].sort((a, b) => String(a[0]).localeCompare(b[0])));
return Array.from(sortedMap.values());
}

/**
* For a given project name (e.g. the value of the <code>metadata.name</code> property in a ui5.yaml),
* returns a [Module]{@ui5/project/graph/Module} instance or <code>undefined</code> depending on whether the project
Expand All @@ -82,7 +96,7 @@ class Workspace {
* @public
* @param {string} projectName Name of the project
* @returns {Promise<@ui5/project/graph/Module|undefined>}
* Module instance of <code>undefined</code> if none is found
* Module instance or <code>undefined</code> if none is found
*/
async getModuleByProjectName(projectName) {
const {projectNameMap} = await this._getResolvedModules();
Expand All @@ -98,7 +112,7 @@ class Workspace {
* @public
* @param {string} nodeId Node ID of the module
* @returns {Promise<@ui5/project/graph/Module|undefined>}
* Module instance of <code>undefined</code> if none is found
* Module instance or <code>undefined</code> if none is found
*/
async getModuleByNodeId(nodeId) {
const {moduleIdMap} = await this._getResolvedModules();
Expand Down
3 changes: 3 additions & 0 deletions test/lib/graph/Workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ test("Basic resolution", async (t) => {
t.is(await workspace.getModuleByNodeId("library.d"), libD,
"getModuleByNodeId returns correct module for library.d");

const modules = await workspace.getModules();
t.deepEqual(modules, [libD, libE], "getModules returns modules sorted by module ID");

t.deepEqual(Array.from(moduleIdMap.keys()).sort(), ["library.d", "library.e"], "Correct module ID keys");
moduleIdMap.forEach((value, key) => {
t.is(value, projectNameMap.get(key), `Same instance of module ${key} in both maps`);
Expand Down

0 comments on commit 1e2aa0e

Please sign in to comment.