diff options
Diffstat (limited to 'deps/npm/lib/install/read-shrinkwrap.js')
-rw-r--r-- | deps/npm/lib/install/read-shrinkwrap.js | 68 |
1 files changed, 59 insertions, 9 deletions
diff --git a/deps/npm/lib/install/read-shrinkwrap.js b/deps/npm/lib/install/read-shrinkwrap.js index 45e883caa2..7074678011 100644 --- a/deps/npm/lib/install/read-shrinkwrap.js +++ b/deps/npm/lib/install/read-shrinkwrap.js @@ -25,14 +25,7 @@ function readShrinkwrap (child, next) { log.warn('read-shrinkwrap', 'Ignoring package-lock.json because there is already an npm-shrinkwrap.json. Please use only one of the two.') } const name = shrinkwrap ? 'npm-shrinkwrap.json' : 'package-lock.json' - let parsed = null - if (shrinkwrap || lockfile) { - try { - parsed = parseJSON(shrinkwrap || lockfile) - } catch (ex) { - throw ex - } - } + const parsed = parsePkgLock(shrinkwrap || lockfile, name) if (parsed && parsed.lockfileVersion !== PKGLOCK_VERSION) { 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!`) } @@ -43,7 +36,8 @@ function readShrinkwrap (child, next) { function maybeReadFile (name, child) { return readFileAsync( - path.join(child.path, name) + path.join(child.path, name), + 'utf8' ).catch({code: 'ENOENT'}, () => null) } @@ -56,3 +50,59 @@ module.exports.andInflate = function (child, next) { } })) } + +const PARENT_RE = /\|{7,}/g +const OURS_RE = /<{7,}/g +const THEIRS_RE = /={7,}/g +const END_RE = />{7,}/g + +module.exports._isDiff = isDiff +function isDiff (str) { + return str.match(OURS_RE) && str.match(THEIRS_RE) && str.match(END_RE) +} + +module.exports._parsePkgLock = parsePkgLock +function parsePkgLock (str, filename) { + if (!str) { return null } + try { + return parseJSON(str) + } catch (e) { + if (isDiff(str)) { + log.warn('conflict', `A git conflict was detected in ${filename}. Attempting to auto-resolve.`) + log.warn('conflict', 'To make this happen automatically on git rebase/merge, consider using the npm-merge-driver:') + log.warn('conflict', '$ npx npm-merge-driver install -g') + const pieces = str.split(/[\n\r]+/g).reduce((acc, line) => { + if (line.match(PARENT_RE)) acc.state = 'parent' + else if (line.match(OURS_RE)) acc.state = 'ours' + else if (line.match(THEIRS_RE)) acc.state = 'theirs' + else if (line.match(END_RE)) acc.state = 'top' + else { + if (acc.state === 'top' || acc.state === 'ours') acc.ours += line + if (acc.state === 'top' || acc.state === 'theirs') acc.theirs += line + if (acc.state === 'top' || acc.state === 'parent') acc.parent += line + } + return acc + }, { + state: 'top', + ours: '', + theirs: '', + parent: '' + }) + try { + const ours = parseJSON(pieces.ours) + const theirs = parseJSON(pieces.theirs) + return reconcileLockfiles(ours, theirs) + } catch (_e) { + log.error('conflict', `Automatic conflict resolution failed. Please manually resolve conflicts in ${filename} and try again.`) + log.silly('conflict', `Error during resolution: ${_e}`) + throw e + } + } else { + throw e + } + } +} + +function reconcileLockfiles (parent, ours, theirs) { + return Object.assign({}, ours, theirs) +} |