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

Commit

Permalink
feat: support bundleDependencies: true
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
isaacs committed Aug 13, 2019
1 parent 4e1e4d2 commit 76f6f42
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
18 changes: 18 additions & 0 deletions read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = readJson

// put more stuff on here to customize.
readJson.extraSet = [
bundleDependencies,
gypfile,
serverjs,
scriptpath,
Expand Down Expand Up @@ -324,6 +325,23 @@ function bins_ (file, data, bins, cb) {
return cb(null, data)
}

function bundleDependencies (file, data, cb) {
var bd = 'bundleDependencies'
var bdd = 'bundledDependencies'
// normalize key name
if (data[bdd] !== undefined) {
if (data[bd] === undefined) data[bd] = data[bdd]
delete data[bdd]
}
if (data[bd] === false) delete data[bd]
else if (data[bd] === true) {
data[bd] = Object.keys(data.dependencies || {})
} else if (data[bd] !== undefined && !Array.isArray(data[bd])) {
delete data[bd]
}
return cb(null, data)
}

function githead (file, data, cb) {
if (data.gitHead) return cb(null, data)
var dir = path.dirname(file)
Expand Down
68 changes: 68 additions & 0 deletions test/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const t = require('tap')
const read = require('../')
const { resolve } = require('path')

t.test('bundle-true', t => {
const fixture = resolve(__dirname, 'fixtures/bundle-true.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.match(data, {
name: 'bundletrue',
version: '1.2.3',
dependencies: { a: '', b: '' },
optionalDependencies: { b: '' },
devDependencies: { c: '' },
bundleDependencies: [ 'a' ],
readme: 'ERROR: No README data found!',
_id: 'bundletrue@1.2.3'
})
t.end()
})
})

t.test('bundle-null', t => {
const fixture = resolve(__dirname, 'fixtures/bundle-null.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.notOk(data.bundleDependencies, 'no bundleDependencies')
t.notOk(data.bundledDependencies, 'no bundledDependencies')
t.end()
})
})

t.test('bundle-array', t => {
const fixture = resolve(__dirname, 'fixtures/bundle-array.json')
read(fixture, (er, data) => {
t.match(data, {
name: 'bundlearray',
version: '1.2.3',
dependencies: { a: '', b: '', c: '*' },
optionalDependencies: { b: '' },
devDependencies: { c: '' },
bundleDependencies: [ 'a', 'b', 'c' ],
readme: 'ERROR: No README data found!',
_id: 'bundlearray@1.2.3'
})
t.end()
})
})

t.test('bundle-false', t => {
const fixture = resolve(__dirname, 'fixtures/bundle-false.json')
read(fixture, (er, data) => {
t.match(data, {
name: 'bundlefalse',
version: '1.2.3',
dependencies: { a: '', b: '' },
optionalDependencies: { b: '' },
devDependencies: { c: '' },
readme: 'ERROR: No README data found!',
_id: 'bundlefalse@1.2.3'
})
t.end()
})
})
18 changes: 18 additions & 0 deletions test/fixtures/bundle-array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "bundlearray",
"version": "1.2.3",
"dependencies": {
"a": ""
},
"optionalDependencies": {
"b": ""
},
"devDependencies": {
"c": ""
},
"bundledDependencies": [
"a",
"b",
"c"
]
}
15 changes: 15 additions & 0 deletions test/fixtures/bundle-false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "bundlefalse",
"version": "1.2.3",
"dependencies": {
"a": ""
},
"optionalDependencies": {
"b": ""
},
"devDependencies": {
"c": ""
},
"bundleDependencies": false,
"bundledDependencies": true
}
15 changes: 15 additions & 0 deletions test/fixtures/bundle-null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "bundlenull",
"version": "1.2.3",
"dependencies": {
"a": ""
},
"optionalDependencies": {
"b": ""
},
"devDependencies": {
"c": ""
},
"bundleDependencies": null,
"bundledDependencies": true
}
14 changes: 14 additions & 0 deletions test/fixtures/bundle-true.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "bundletrue",
"version": "1.2.3",
"dependencies": {
"a": ""
},
"optionalDependencies": {
"b": ""
},
"devDependencies": {
"c": ""
},
"bundledDependencies": true
}

0 comments on commit 76f6f42

Please sign in to comment.