UNPKG

730 BJavaScriptView Raw
1const mergePorts = require('./mergePorts')
2
3function verifyPorts(pack, prop, origin = 'package.json') {
4 const ports = pack[prop]
5
6 if (Array.isArray(ports)) {
7 if (ports.length) {
8 // remove non-numeral ports
9 const numeralPorts = pack[prop].filter(p => typeof p === 'number')
10
11 // remove duplicates
12 const valid = mergePorts(numeralPorts)
13
14 if (valid.length !== pack[prop].length) {
15 throw new Error(`invalid ${prop} in ${origin}`)
16 }
17
18 // everything ok
19 return
20 }
21 } else if (ports) {
22 // wrong type
23 throw new TypeError(prop + ' in package.json must be an array or undefined')
24 }
25
26 // undefined or empty array
27 pack[prop] = null
28}
29
30module.exports = verifyPorts