Skip to content

Commit da707d7

Browse files
committed
[FIX] npm t8r: Handle npm optionalDependencies correctly
In the past, optionalDependencies that could not be located caused a "Failed to locate module". Fixes #51
1 parent 45a25c2 commit da707d7

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

lib/translators/npm.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ class NpmTranslator {
3232

3333
let dependencies = pkg.dependencies || {};
3434
let optDependencies = pkg.devDependencies || {};
35+
36+
// Due to normalization done by by the "read-pkg-up" module the values
37+
// in optionalDependencies get added to dependencies. Also as described here:
38+
// https://github.com/npm/normalize-package-data#what-normalization-currently-entails
39+
// Note: optionalDependencies are treated the same as devDependencies in the npm translator
40+
if (pkg.optionalDependencies) {
41+
for (let depName in pkg.optionalDependencies) {
42+
if (pkg.optionalDependencies.hasOwnProperty(depName)) {
43+
// Remove entry from "hard" dependencies map
44+
if (dependencies[depName]) {
45+
delete dependencies[depName];
46+
}
47+
// Add to optional dependencies map (of not already done)
48+
if (optDependencies.hasOwnProperty(depName)) {
49+
optDependencies[depName] = pkg.optionalDependencies[depName];
50+
}
51+
}
52+
}
53+
}
54+
3555
const version = pkg.version;
3656

3757
// Also look for "napa" dependencies (see https://github.com/shama/napa)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "application.g",
3+
"version": "1.0.0",
4+
"description": "Simple SAPUI5 based application - test for npm optionalDependencies",
5+
"main": "index.html",
6+
"optionalDependencies": {
7+
"library.nonexistent": "file:../library.nonexistent"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
}
12+
}

test/fixtures/application.g/ui5.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
specVersion: "0.1"
3+
type: application
4+
metadata:
5+
name: application.g
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"_version": "1.1.0",
3+
"sap.app": {
4+
"_version": "1.1.0",
5+
"id": "id1",
6+
"type": "application",
7+
"applicationVersion": {
8+
"version": "1.2.2"
9+
},
10+
"embeds": ["embedded"],
11+
"title": "{{title}}"
12+
}
13+
}

test/lib/translators/npm.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const applicationC2Path = path.join(__dirname, "..", "..", "fixtures", "applicat
77
const applicationC3Path = path.join(__dirname, "..", "..", "fixtures", "application.c3");
88
const applicationDPath = path.join(__dirname, "..", "..", "fixtures", "application.d");
99
const applicationFPath = path.join(__dirname, "..", "..", "fixtures", "application.f");
10+
const applicationGPath = path.join(__dirname, "..", "..", "fixtures", "application.g");
1011
const errApplicationAPath = path.join(__dirname, "..", "..", "fixtures", "err.application.a");
1112

1213
test("AppA: project with collection dependency", (t) => {
@@ -48,6 +49,12 @@ test("AppF: project with UI5-dependencies", (t) => {
4849
});
4950
});
5051

52+
test("AppG: project with npm 'optionalDependencies' should not fail if optional dependency cannot be resolved", (t) => {
53+
return npmTranslator.generateDependencyTree(applicationGPath).then((parsedTree) => {
54+
t.deepEqual(parsedTree, applicationGTree, "Parsed correctly");
55+
});
56+
});
57+
5158
test("Error: missing package.json", async (t) => {
5259
const dir = path.parse(__dirname).root;
5360
const error = await t.throws(npmTranslator.generateDependencyTree(dir));
@@ -226,3 +233,10 @@ const applicationFTree = {
226233
}
227234
]
228235
};
236+
237+
const applicationGTree = {
238+
id: "application.g",
239+
version: "1.0.0",
240+
path: applicationGPath,
241+
dependencies: []
242+
};

0 commit comments

Comments
 (0)