|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Debug = require('debug'); |
| 4 | +const { Arborist } = require('@npmcli/arborist'); |
| 5 | +const Fs = require('fs'); |
| 6 | +const Path = require('path'); |
| 7 | +const Tmp = require('tmp'); |
| 8 | + |
| 9 | +const Package = require('./package'); |
| 10 | +const Utils = require('./utils'); |
| 11 | + |
| 12 | +const internals = {}; |
| 13 | + |
| 14 | + |
| 15 | +internals.log = Debug('detect-node-support'); |
| 16 | + |
| 17 | + |
| 18 | +internals.walk = (node, callback) => { |
| 19 | + |
| 20 | + callback(node); |
| 21 | + |
| 22 | + node.children.forEach((child) => { |
| 23 | + |
| 24 | + internals.walk(child, callback); |
| 25 | + }); |
| 26 | +}; |
| 27 | + |
| 28 | + |
| 29 | +internals.resolve = async ({ packageJson, lockfile }, options) => { |
| 30 | + |
| 31 | + const { deep, dev } = options; |
| 32 | + |
| 33 | + const tmpDir = Tmp.dirSync({ unsafeCleanup: true }); |
| 34 | + const path = tmpDir.name; |
| 35 | + |
| 36 | + Fs.writeFileSync(Path.join(path, 'package.json'), JSON.stringify(packageJson, null, ' ')); |
| 37 | + |
| 38 | + if (lockfile) { |
| 39 | + Fs.writeFileSync(Path.join(path, 'package-lock.json'), JSON.stringify(lockfile, null, ' ')); |
| 40 | + } |
| 41 | + |
| 42 | + const direct = new Set(); |
| 43 | + ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'].forEach((depType) => { |
| 44 | + |
| 45 | + if (!packageJson[depType]) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + Object.keys(packageJson[depType]).forEach((dep) => direct.add(dep)); |
| 50 | + }); |
| 51 | + |
| 52 | + const arborist = new Arborist({ path }); |
| 53 | + |
| 54 | + await arborist.buildIdealTree(); |
| 55 | + |
| 56 | + const map = {}; |
| 57 | + |
| 58 | + internals.walk(arborist.idealTree, (node) => { |
| 59 | + |
| 60 | + if (node.isRoot) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (!dev && node.dev) { |
| 65 | + // only include dev deps when `options.dev` flag is set |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (!deep && !node.parent.isRoot) { |
| 70 | + // only include deep deps when `options.deep` flag is set |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (!deep && !direct.has(node.name)) { |
| 75 | + // only include deep deps when `options.deep` flag is set |
| 76 | + // workaround for https://github.com/npm/arborist/issues/60 |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + map[node.name] = map[node.name] || new Set(); |
| 81 | + map[node.name].add(node.package.version); |
| 82 | + }); |
| 83 | + |
| 84 | + const result = {}; |
| 85 | + |
| 86 | + for (const name of Object.keys(map).sort()) { |
| 87 | + result[name] = [...map[name]]; |
| 88 | + } |
| 89 | + |
| 90 | + tmpDir.removeCallback(); |
| 91 | + |
| 92 | + return result; |
| 93 | +}; |
| 94 | + |
| 95 | +internals.tryLoad = async (loadFile, filename) => { |
| 96 | + |
| 97 | + try { |
| 98 | + return await loadFile(filename, { json: true }); |
| 99 | + } |
| 100 | + catch (err) { |
| 101 | + if (err.code !== 'ENOENT') { |
| 102 | + throw err; |
| 103 | + } |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +exports.detect = async ({ packageJson, loadFile }, options) => { |
| 108 | + |
| 109 | + const lockfile = (await internals.tryLoad(loadFile, 'package-lock.json')) || (await internals.tryLoad(loadFile, 'npm-shrinkwrap.json')); |
| 110 | + internals.log(lockfile ? 'Lock file present' : 'Lock file missing - things will be a bit slower'); |
| 111 | + |
| 112 | + const versions = await internals.resolve({ packageJson, lockfile }, options); |
| 113 | + |
| 114 | + const support = []; |
| 115 | + const errors = {}; |
| 116 | + let hasErrors = false; |
| 117 | + |
| 118 | + const packages = Object.keys(versions).sort(); |
| 119 | + const n = packages.length; |
| 120 | + |
| 121 | + for (let i = 0; i < n; ++i) { |
| 122 | + |
| 123 | + const packageName = packages[i]; |
| 124 | + internals.log(`Resolving dependency ${i + 1} of ${n}: ${packageName}`); |
| 125 | + |
| 126 | + try { |
| 127 | + const { result } = await Package.detect({ packageName }); |
| 128 | + support.push(result); |
| 129 | + } |
| 130 | + catch (err) { |
| 131 | + hasErrors = true; |
| 132 | + errors[packageName] = { |
| 133 | + message: Utils.getErrorMessage(err) |
| 134 | + }; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + const result = { support, versions }; |
| 139 | + |
| 140 | + if (hasErrors) { |
| 141 | + result.errors = errors; |
| 142 | + } |
| 143 | + |
| 144 | + return result; |
| 145 | +}; |
0 commit comments