read-shrinkwrap.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. const BB = require('bluebird')
  3. const fs = require('graceful-fs')
  4. const iferr = require('iferr')
  5. const inflateShrinkwrap = require('./inflate-shrinkwrap.js')
  6. const log = require('npmlog')
  7. const parseJSON = require('../utils/parse-json.js')
  8. const path = require('path')
  9. const PKGLOCK_VERSION = require('../npm.js').lockfileVersion
  10. const readFileAsync = BB.promisify(fs.readFile)
  11. module.exports = readShrinkwrap
  12. function readShrinkwrap (child, next) {
  13. if (child.package._shrinkwrap) return process.nextTick(next)
  14. BB.join(
  15. maybeReadFile('npm-shrinkwrap.json', child),
  16. // Don't read non-root lockfiles
  17. child.isTop && maybeReadFile('package-lock.json', child),
  18. (shrinkwrap, lockfile) => {
  19. if (shrinkwrap && lockfile) {
  20. log.warn('read-shrinkwrap', 'Ignoring package-lock.json because there is already an npm-shrinkwrap.json. Please use only one of the two.')
  21. }
  22. const name = shrinkwrap ? 'npm-shrinkwrap.json' : 'package-lock.json'
  23. const parsed = parsePkgLock(shrinkwrap || lockfile, name)
  24. if (parsed && parsed.lockfileVersion !== PKGLOCK_VERSION) {
  25. log.warn('read-shrinkwrap', `This version of npm is compatible with lockfileVersion@${PKGLOCK_VERSION}, but ${name} was generated for lockfileVersion@${parsed.lockfileVersion || 0}. I'll try to do my best with it!`)
  26. }
  27. child.package._shrinkwrap = parsed
  28. }
  29. ).then(() => next(), next)
  30. }
  31. function maybeReadFile (name, child) {
  32. return readFileAsync(
  33. path.join(child.path, name),
  34. 'utf8'
  35. ).catch({code: 'ENOENT'}, () => null)
  36. }
  37. module.exports.andInflate = function (child, next) {
  38. readShrinkwrap(child, iferr(next, function () {
  39. if (child.package._shrinkwrap) {
  40. return inflateShrinkwrap(child, child.package._shrinkwrap || {}, next)
  41. } else {
  42. return next()
  43. }
  44. }))
  45. }
  46. const PARENT_RE = /\|{7,}/g
  47. const OURS_RE = /<{7,}/g
  48. const THEIRS_RE = /={7,}/g
  49. const END_RE = />{7,}/g
  50. module.exports._isDiff = isDiff
  51. function isDiff (str) {
  52. return str.match(OURS_RE) && str.match(THEIRS_RE) && str.match(END_RE)
  53. }
  54. module.exports._parsePkgLock = parsePkgLock
  55. function parsePkgLock (str, filename) {
  56. if (!str) { return null }
  57. try {
  58. return parseJSON(str)
  59. } catch (e) {
  60. if (isDiff(str)) {
  61. log.warn('conflict', `A git conflict was detected in ${filename}. Attempting to auto-resolve.`)
  62. log.warn('conflict', 'To make this happen automatically on git rebase/merge, consider using the npm-merge-driver:')
  63. log.warn('conflict', '$ npx npm-merge-driver install -g')
  64. const pieces = str.split(/[\n\r]+/g).reduce((acc, line) => {
  65. if (line.match(PARENT_RE)) acc.state = 'parent'
  66. else if (line.match(OURS_RE)) acc.state = 'ours'
  67. else if (line.match(THEIRS_RE)) acc.state = 'theirs'
  68. else if (line.match(END_RE)) acc.state = 'top'
  69. else {
  70. if (acc.state === 'top' || acc.state === 'ours') acc.ours += line
  71. if (acc.state === 'top' || acc.state === 'theirs') acc.theirs += line
  72. if (acc.state === 'top' || acc.state === 'parent') acc.parent += line
  73. }
  74. return acc
  75. }, {
  76. state: 'top',
  77. ours: '',
  78. theirs: '',
  79. parent: ''
  80. })
  81. try {
  82. const ours = parseJSON(pieces.ours)
  83. const theirs = parseJSON(pieces.theirs)
  84. return reconcileLockfiles(ours, theirs)
  85. } catch (_e) {
  86. log.error('conflict', `Automatic conflict resolution failed. Please manually resolve conflicts in ${filename} and try again.`)
  87. log.silly('conflict', `Error during resolution: ${_e}`)
  88. throw e
  89. }
  90. } else {
  91. throw e
  92. }
  93. }
  94. }
  95. function reconcileLockfiles (parent, ours, theirs) {
  96. return Object.assign({}, ours, theirs)
  97. }