renovate/lib/manager/npm/extract/yarn.js
Rhys Arkins ecdcd9df4f
feat: refactor dependency extraction (#1912)
Rewrite of dependency extraction, particularly for npm. Paves way for easier addition of new package managers.

Closes #1882
2018-05-09 08:03:59 +02:00

32 lines
850 B
JavaScript

const yarnLockParser = require('@yarnpkg/lockfile');
module.exports = {
getYarnLock,
};
async function getYarnLock(filePath) {
const yarnLockRaw = await platform.getFile(filePath);
try {
const yarnLockParsed = yarnLockParser.parse(yarnLockRaw);
// istanbul ignore if
if (yarnLockParsed.type !== 'success') {
logger.info(
{ filePath, parseType: yarnLockParsed.type },
'Error parsing yarn.lock - not success'
);
return {};
}
const lockFile = {};
for (const [entry, val] of Object.entries(yarnLockParsed.object)) {
logger.trace({ entry, version: val.version });
lockFile[entry] = val.version;
}
return lockFile;
} catch (err) {
logger.info(
{ filePath, err, message: err.message },
'Warning: Exception parsing yarn.lock'
);
return {};
}
}