Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit 76f6f42

Browse files
committed
feat: support bundleDependencies: true
What ends up in the parsed output is still an array of names, but we generate that name from the keys if it's set to true. Also, avoid the footgun of people not realizing that bundleDependencies should be an array of names, rather than a {name:version} object. If bundleDependencies is something other than a boolean or array, it is removed from the manifest.
1 parent 4e1e4d2 commit 76f6f42

6 files changed

+148
-0
lines changed

read-json.js

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = readJson
1717

1818
// put more stuff on here to customize.
1919
readJson.extraSet = [
20+
bundleDependencies,
2021
gypfile,
2122
serverjs,
2223
scriptpath,
@@ -324,6 +325,23 @@ function bins_ (file, data, bins, cb) {
324325
return cb(null, data)
325326
}
326327

328+
function bundleDependencies (file, data, cb) {
329+
var bd = 'bundleDependencies'
330+
var bdd = 'bundledDependencies'
331+
// normalize key name
332+
if (data[bdd] !== undefined) {
333+
if (data[bd] === undefined) data[bd] = data[bdd]
334+
delete data[bdd]
335+
}
336+
if (data[bd] === false) delete data[bd]
337+
else if (data[bd] === true) {
338+
data[bd] = Object.keys(data.dependencies || {})
339+
} else if (data[bd] !== undefined && !Array.isArray(data[bd])) {
340+
delete data[bd]
341+
}
342+
return cb(null, data)
343+
}
344+
327345
function githead (file, data, cb) {
328346
if (data.gitHead) return cb(null, data)
329347
var dir = path.dirname(file)

test/bundle.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const t = require('tap')
2+
const read = require('../')
3+
const { resolve } = require('path')
4+
5+
t.test('bundle-true', t => {
6+
const fixture = resolve(__dirname, 'fixtures/bundle-true.json')
7+
read(fixture, (er, data) => {
8+
if (er) {
9+
throw er
10+
}
11+
t.match(data, {
12+
name: 'bundletrue',
13+
version: '1.2.3',
14+
dependencies: { a: '', b: '' },
15+
optionalDependencies: { b: '' },
16+
devDependencies: { c: '' },
17+
bundleDependencies: [ 'a' ],
18+
readme: 'ERROR: No README data found!',
19+
_id: 'bundletrue@1.2.3'
20+
})
21+
t.end()
22+
})
23+
})
24+
25+
t.test('bundle-null', t => {
26+
const fixture = resolve(__dirname, 'fixtures/bundle-null.json')
27+
read(fixture, (er, data) => {
28+
if (er) {
29+
throw er
30+
}
31+
t.notOk(data.bundleDependencies, 'no bundleDependencies')
32+
t.notOk(data.bundledDependencies, 'no bundledDependencies')
33+
t.end()
34+
})
35+
})
36+
37+
t.test('bundle-array', t => {
38+
const fixture = resolve(__dirname, 'fixtures/bundle-array.json')
39+
read(fixture, (er, data) => {
40+
t.match(data, {
41+
name: 'bundlearray',
42+
version: '1.2.3',
43+
dependencies: { a: '', b: '', c: '*' },
44+
optionalDependencies: { b: '' },
45+
devDependencies: { c: '' },
46+
bundleDependencies: [ 'a', 'b', 'c' ],
47+
readme: 'ERROR: No README data found!',
48+
_id: 'bundlearray@1.2.3'
49+
})
50+
t.end()
51+
})
52+
})
53+
54+
t.test('bundle-false', t => {
55+
const fixture = resolve(__dirname, 'fixtures/bundle-false.json')
56+
read(fixture, (er, data) => {
57+
t.match(data, {
58+
name: 'bundlefalse',
59+
version: '1.2.3',
60+
dependencies: { a: '', b: '' },
61+
optionalDependencies: { b: '' },
62+
devDependencies: { c: '' },
63+
readme: 'ERROR: No README data found!',
64+
_id: 'bundlefalse@1.2.3'
65+
})
66+
t.end()
67+
})
68+
})

test/fixtures/bundle-array.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "bundlearray",
3+
"version": "1.2.3",
4+
"dependencies": {
5+
"a": ""
6+
},
7+
"optionalDependencies": {
8+
"b": ""
9+
},
10+
"devDependencies": {
11+
"c": ""
12+
},
13+
"bundledDependencies": [
14+
"a",
15+
"b",
16+
"c"
17+
]
18+
}

test/fixtures/bundle-false.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "bundlefalse",
3+
"version": "1.2.3",
4+
"dependencies": {
5+
"a": ""
6+
},
7+
"optionalDependencies": {
8+
"b": ""
9+
},
10+
"devDependencies": {
11+
"c": ""
12+
},
13+
"bundleDependencies": false,
14+
"bundledDependencies": true
15+
}

test/fixtures/bundle-null.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "bundlenull",
3+
"version": "1.2.3",
4+
"dependencies": {
5+
"a": ""
6+
},
7+
"optionalDependencies": {
8+
"b": ""
9+
},
10+
"devDependencies": {
11+
"c": ""
12+
},
13+
"bundleDependencies": null,
14+
"bundledDependencies": true
15+
}

test/fixtures/bundle-true.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "bundletrue",
3+
"version": "1.2.3",
4+
"dependencies": {
5+
"a": ""
6+
},
7+
"optionalDependencies": {
8+
"b": ""
9+
},
10+
"devDependencies": {
11+
"c": ""
12+
},
13+
"bundledDependencies": true
14+
}

0 commit comments

Comments
 (0)