Skip to content

Commit 9fd78dc

Browse files
committed
[FIX] npm t8r: Again, handle npm optionalDependencies correctly
Fixes the fix done in #60. Fixes: #51
1 parent 7fe6269 commit 9fd78dc

File tree

8 files changed

+56
-22
lines changed

8 files changed

+56
-22
lines changed

lib/translators/npm.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,6 @@ class NpmTranslator {
3333
let dependencies = pkg.dependencies || {};
3434
let optDependencies = pkg.devDependencies || {};
3535

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 (const 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-
5536
const version = pkg.version;
5637

5738
// Also look for "napa" dependencies (see https://github.com/shama/napa)
@@ -96,7 +77,8 @@ class NpmTranslator {
9677
return this.getDepProjects({
9778
cwd,
9879
parentName: moduleName,
99-
dependencies
80+
dependencies,
81+
optionalDependencies: pkg.optionalDependencies
10082
}).then((depProjects) => {
10183
// Array needs to be flattened because:
10284
// getDepProjects returns array * 2 = array with two arrays
@@ -141,11 +123,21 @@ class NpmTranslator {
141123
}
142124
}
143125

144-
getDepProjects({cwd, dependencies, parentName}) {
126+
getDepProjects({cwd, dependencies, optionalDependencies, parentName}) {
145127
return Promise.all(
146128
Object.keys(dependencies).map((moduleName) => {
147129
return this.findModulePath(cwd, moduleName).then((modulePath) => {
148130
return this.readProject({modulePath, moduleName, parentName});
131+
}, (err) => {
132+
// Due to normalization done by by the "read-pkg-up" module the values
133+
// in optionalDependencies get added to dependencies. Also as described here:
134+
// https://github.com/npm/normalize-package-data#what-normalization-currently-entails
135+
// Resolution errors of optionalDependencies are being ignored
136+
if (optionalDependencies && optionalDependencies[moduleName]) {
137+
return null;
138+
} else {
139+
throw err;
140+
}
149141
});
150142
})
151143
).then((depProjects) => {

test/fixtures/application.g/node_modules/library.d/main/src/library/d/.library

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/application.g/node_modules/library.d/main/src/library/d/some.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/application.g/node_modules/library.d/main/test/library/d/Test.html

Whitespace-only changes.

test/fixtures/application.g/node_modules/library.d/package.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/application.g/node_modules/library.d/ui5.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/application.g/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Simple SAPUI5 based application - test for npm optionalDependencies",
55
"main": "index.html",
66
"optionalDependencies": {
7+
"library.d": "file:../library.d",
78
"library.nonexistent": "file:../library.nonexistent"
89
},
910
"scripts": {

test/lib/translators/npm.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,12 @@ const applicationGTree = {
238238
id: "application.g",
239239
version: "1.0.0",
240240
path: applicationGPath,
241-
dependencies: []
241+
dependencies: [
242+
{
243+
id: "library.d",
244+
version: "1.0.0",
245+
path: path.join(applicationGPath, "node_modules", "library.d"),
246+
dependencies: []
247+
}
248+
]
242249
};

0 commit comments

Comments
 (0)