Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
explain-eresolve.js65 linesDownload Raw Back to utils
1// this is called when an ERESOLVE error is caught in the exit-handler, or when there's a log.warn('eresolve', msg, explanation), to turn it into a human-intelligible explanation of what's wrong and how to fix.
2const { explainEdge, explainNode, printNode } = require('./explain-dep.js')
3
4// expl is an explanation object that comes from Arborist.
5// Depth is how far we want to want to descend into the object making a report.
6// The full report (ie, depth=Infinity) is always written to the cache folder at ${cache}/eresolve-report.txt along with full json.
7const explain = (expl, chalk, depth) => {
8  const { edge, dep, current, peerConflict, currentEdge } = expl
9
10  const out = []
11  const whileInstalling = dep && dep.whileInstalling ||
12    current && current.whileInstalling ||
13    edge && edge.from && edge.from.whileInstalling
14  if (whileInstalling) {
15    out.push('While resolving: ' + printNode(whileInstalling, chalk))
16  }
17
18  // it "should" be impossible for an ERESOLVE explanation to lack both current and currentEdge, but better to have a less helpful error than a crashing failure.
19  if (current) {
20    out.push('Found: ' + explainNode(current, depth, chalk))
21  } else if (peerConflict && peerConflict.current) {
22    out.push('Found: ' + explainNode(peerConflict.current, depth, chalk))
23  } else if (currentEdge) {
24    out.push('Found: ' + explainEdge(currentEdge, depth, chalk))
25  } else /* istanbul ignore else - should always have one */ if (edge) {
26    out.push('Found: ' + explainEdge(edge, depth, chalk))
27  }
28
29  out.push('\nCould not resolve dependency:\n' +
30    explainEdge(edge, depth, chalk))
31
32  if (peerConflict) {
33    const heading = '\nConflicting peer dependency:'
34    const pc = explainNode(peerConflict.peer, depth, chalk)
35    out.push(heading + ' ' + pc)
36  }
37
38  return out.join('\n')
39}
40
41// generate a full verbose report and tell the user how to fix it
42const report = (expl, chalk, noColorChalk) => {
43  const flags = [
44    expl.strictPeerDeps ? '--no-strict-peer-deps' : '',
45    '--force',
46    '--legacy-peer-deps',
47  ].filter(Boolean)
48
49  const or = (arr) => arr.length <= 2
50    ? arr.join(' or ') :
51    arr.map((v, i, l) => i + 1 === l.length ? `or ${v}` : v).join(', ')
52
53  const fix = `Fix the upstream dependency conflict, or retry this command with ${or(flags)} to accept an incorrect (and potentially broken) dependency resolution.`
54
55  return {
56    explanation: `${explain(expl, chalk, 4)}\n\n${fix}`,
57    file: `# npm resolution error report\n\n${explain(expl, noColorChalk, Infinity)}\n\n${fix}`,
58  }
59}
60
61module.exports = {
62  explain,
63  report,
64}
65 
codekingpro/portable-devtools · Team Ai