| 199 | } |
| 200 | |
| 201 | #packageOutput (data, name) { |
| 202 | const json = this.npm.config.get('json') |
| 203 | const versions = Object.keys(data) |
| 204 | const includeVersions = versions.length > 1 |
| 205 | |
| 206 | let includeFields |
| 207 | const res = versions.flatMap((v) => { |
| 208 | const fields = Object.entries(data[v]) |
| 209 | |
| 210 | includeFields ||= (fields.length > 1) |
| 211 | |
| 212 | const msg = json ? {} : [] |
| 213 | |
| 214 | for (let [f, d] of fields) { |
| 215 | d = cleanup(d) |
| 216 | |
| 217 | if (json) { |
| 218 | msg[f] = d |
| 219 | continue |
| 220 | } |
| 221 | |
| 222 | if (includeVersions || includeFields || typeof d !== 'string') { |
| 223 | d = inspect(d, { |
| 224 | showHidden: false, |
| 225 | depth: 5, |
| 226 | colors: this.npm.color, |
| 227 | maxArrayLength: null, |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | if (f && includeFields) { |
| 232 | f += ' = ' |
| 233 | } |
| 234 | |
| 235 | msg.push(`${includeVersions ? `${name}@${v} ` : ''}${includeFields ? f : ''}${d}`) |
| 236 | } |
| 237 | |
| 238 | return msg |
| 239 | }) |
| 240 | |
| 241 | if (json) { |
| 242 | // TODO(BREAKING_CHANGE): all unwrapping should be removed. |
| 243 | // Users should know based on their arguments if they can expect an array or an object. |
| 244 | // And this unwrapping can break that assumption. |
| 245 | // e.g. `npm view abbrev@^2` should always return an array, but currently since there is only one version matching `^2` this will return a single object instead. |
| 246 | const first = Object.keys(res[0] || {}) |
| 247 | const jsonRes = first.length === 1 ? res.map(m => m[first[0]]) : res |
| 248 | if (jsonRes.length === 0) { |
| 249 | return |
| 250 | } |
| 251 | if (jsonRes.length === 1) { |
| 252 | return jsonRes[0] |
| 253 | } |
| 254 | return jsonRes |
| 255 | } |
| 256 | |
| 257 | return res.join('\n').trim() |
| 258 | } |