2018-06-08 06:44:16 +00:00
|
|
|
const semverComposer = require('../../versioning')('semverComposer');
|
2018-06-08 04:15:13 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
extractDependencies,
|
|
|
|
};
|
|
|
|
|
2018-08-24 15:49:32 +00:00
|
|
|
async function extractDependencies(content, fileName) {
|
2018-06-08 04:15:13 +00:00
|
|
|
logger.debug('composer.extractDependencies()');
|
|
|
|
let packageJson;
|
|
|
|
try {
|
|
|
|
packageJson = JSON.parse(content);
|
|
|
|
} catch (err) {
|
2018-08-24 15:49:32 +00:00
|
|
|
logger.info({ fileName }, 'Invalid JSON');
|
2018-06-08 04:15:13 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const deps = [];
|
|
|
|
const depTypes = ['require', 'require-dev'];
|
|
|
|
for (const depType of depTypes) {
|
|
|
|
if (packageJson[depType]) {
|
|
|
|
try {
|
|
|
|
for (const [depName, version] of Object.entries(packageJson[depType])) {
|
|
|
|
const currentValue = version.trim();
|
|
|
|
const dep = {
|
|
|
|
depType,
|
|
|
|
depName,
|
|
|
|
currentValue,
|
2018-06-08 06:44:16 +00:00
|
|
|
versionScheme: 'semverComposer',
|
2018-06-08 04:15:13 +00:00
|
|
|
purl: 'pkg:packagist/' + depName,
|
|
|
|
};
|
|
|
|
if (!depName.includes('/')) {
|
|
|
|
dep.skipReason = 'unsupported';
|
|
|
|
}
|
2018-06-08 06:44:16 +00:00
|
|
|
if (!semverComposer.isValid(currentValue)) {
|
2018-06-08 04:15:13 +00:00
|
|
|
dep.skipReason = 'unsupported-constraint';
|
|
|
|
}
|
2018-06-08 06:44:16 +00:00
|
|
|
if (currentValue === '*') {
|
|
|
|
dep.skipReason = 'any-version';
|
|
|
|
}
|
2018-06-08 04:15:13 +00:00
|
|
|
deps.push(dep);
|
|
|
|
}
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.info(
|
2018-08-24 15:49:32 +00:00
|
|
|
{ fileName, depType, err, message: err.message },
|
2018-06-08 04:15:13 +00:00
|
|
|
'Error parsing composer.json'
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!deps.length) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-19 07:14:34 +00:00
|
|
|
let composerLock = false;
|
2018-08-24 15:49:32 +00:00
|
|
|
const filePath = fileName.replace(/\.json$/, '.lock');
|
2018-07-19 07:14:34 +00:00
|
|
|
if (await platform.getFile(filePath)) {
|
2018-08-24 15:49:32 +00:00
|
|
|
logger.debug({ packageFile: fileName }, 'Found composer lock file');
|
2018-07-19 07:14:34 +00:00
|
|
|
composerLock = filePath;
|
|
|
|
}
|
2018-10-14 04:46:17 +00:00
|
|
|
const res = { deps, composerLock };
|
|
|
|
if (packageJson.repositories) {
|
|
|
|
res.registryUrls = packageJson.repositories;
|
|
|
|
}
|
|
|
|
return res;
|
2018-06-08 04:15:13 +00:00
|
|
|
}
|