Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Resolve properly package.json dependency aliases #608

Merged
merged 7 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/graph/providers/NodePackageDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NodePackageDependencies {
}
return Promise.all(node._dependencies.map(async ({name, optional}) => {
const modulePath = await this._resolveModulePath(node.path, name, workspace);
return this._getNode(modulePath, optional);
return this._getNode(modulePath, optional, name);
}));
}

Expand Down Expand Up @@ -99,15 +99,25 @@ class NodePackageDependencies {
}
}

async _getNode(modulePath, optional) {
/**
* Resolves a Node module by reading its package.json
*
* @param {string} modulePath Path to the module.
* @param {boolean} optional Whether this dependency is optional.
* @param {string} [nameAlias] The name of the module. It's usually the same as the name definfed
* in package.json and could easily be skipped. Useful when defining dependency as an alias:
* {@link https://github.com/npm/rfcs/blob/main/implemented/0001-package-aliases.md}
* @returns {Promise<object>}
*/
async _getNode(modulePath, optional, nameAlias) {
d3xter666 marked this conversation as resolved.
Show resolved Hide resolved
log.verbose(`Reading package.json in directory ${modulePath}...`);
const packageJson = await readPackage({
cwd: modulePath,
normalize: false
});

return {
id: packageJson.name,
id: nameAlias || packageJson.name,
version: packageJson.version,
path: modulePath,
optional,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/fixtures/application.a.aliases/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "application.a.aliases",
"version": "1.0.0",
"description": "Simple SAPUI5 based application",
"main": "index.html",
"dependencies": {
"extension.a.esm.alias": "file:../extension.a.esm"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
23 changes: 23 additions & 0 deletions test/fixtures/application.a.aliases/ui5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
specVersion: "3.0"
type: application
metadata:
name: application.a.aliases

--- # Everything below this line could also be put into the ui5.yaml of a standalone extension module
specVersion: "3.0"
kind: extension
type: project-shim
metadata:
name: my.application.thirdparty
shims:
configurations:
extension.a.esm.alias: # name as defined in package.json
specVersion: "3.0"
type: module # Use module type
metadata:
name: extension.a.esm.alias
resources:
configuration:
paths:
/resources/my/application/thirdparty/: "" # map root directory of lodash module
9 changes: 9 additions & 0 deletions test/fixtures/application.a.aliases/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Application A</title>
</head>
<body>

</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/application.a.aliases/webapp/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"_version": "1.1.0",
"sap.app": {
"_version": "1.1.0",
"id": "id1",
"type": "application",
"applicationVersion": {
"version": "1.2.2"
},
"embeds": ["embedded"],
"title": "{{title}}"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/application.a.aliases/webapp/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function test(paramA) {
var variableA = paramA;
console.log(variableA);
}
test();
18 changes: 18 additions & 0 deletions test/lib/graph/providers/NodePackageDependencies.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sinonGlobal from "sinon";
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const applicationAPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.a");
const applicationAAliasesPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.a.aliases");
const applicationCPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.c");
const applicationC2Path = path.join(__dirname, "..", "..", "..", "fixtures", "application.c2");
const applicationC3Path = path.join(__dirname, "..", "..", "..", "fixtures", "application.c3");
Expand Down Expand Up @@ -68,6 +69,23 @@ test("AppA: project with collection dependency", async (t) => {
]);
});

test("AppA: project with an alias dependency", async (t) => {
const workspace = {
getName: () => "workspace name",
getModuleByNodeId: t.context.sinon.stub().resolves(undefined).onFirstCall().resolves({
getPath: () => path.join(applicationAAliasesPath, "node_modules", "extension.a.esm.alias"),
getVersion: () => "1.0.0",
})
};
const npmProvider = new NodePackageDependenciesProvider({
cwd: applicationAAliasesPath
});
await testGraphCreationDfs(t, npmProvider, [
"extension.a.esm.alias",
"application.a.aliases",
], workspace);
});

test("AppA: project with workspace overrides", async (t) => {
const workspace = {
getName: () => "workspace name",
Expand Down