2018-10-16 04:33:42 +00:00
|
|
|
const is = require('@sindresorhus/is');
|
2018-12-08 14:51:34 +00:00
|
|
|
const semverComposer = require('../../versioning/semver-composer');
|
2018-06-08 04:15:13 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-11-04 17:51:23 +00:00
|
|
|
extractPackageFile,
|
2018-06-08 04:15:13 +00:00
|
|
|
};
|
|
|
|
|
2018-11-04 17:51:23 +00:00
|
|
|
async function extractPackageFile(content, fileName) {
|
|
|
|
logger.trace(`composer.extractPackageFile(${fileName})`);
|
2018-10-23 04:50:24 +00:00
|
|
|
let composerJson;
|
2018-06-08 04:15:13 +00:00
|
|
|
try {
|
2018-10-23 04:50:24 +00:00
|
|
|
composerJson = JSON.parse(content);
|
2018-06-08 04:15:13 +00:00
|
|
|
} 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) {
|
2018-10-23 04:50:24 +00:00
|
|
|
if (composerJson[depType]) {
|
2018-06-08 04:15:13 +00:00
|
|
|
try {
|
2018-10-23 04:50:24 +00:00
|
|
|
for (const [depName, version] of Object.entries(
|
|
|
|
composerJson[depType]
|
|
|
|
)) {
|
2018-06-08 04:15:13 +00:00
|
|
|
const currentValue = version.trim();
|
|
|
|
const dep = {
|
|
|
|
depType,
|
|
|
|
depName,
|
|
|
|
currentValue,
|
|
|
|
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 };
|
2018-10-23 04:50:24 +00:00
|
|
|
if (composerJson.repositories) {
|
|
|
|
if (is.array(composerJson.repositories)) {
|
|
|
|
res.registryUrls = composerJson.repositories;
|
|
|
|
} else if (is.object(composerJson.repositories)) {
|
2018-10-16 04:33:42 +00:00
|
|
|
try {
|
|
|
|
res.registryUrls = [];
|
2018-10-23 04:50:24 +00:00
|
|
|
for (const repository of Object.values(composerJson.repositories)) {
|
2018-10-16 04:33:42 +00:00
|
|
|
res.registryUrls.push(repository);
|
|
|
|
}
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.info({ err }, 'Error extracting composer repositories');
|
|
|
|
}
|
|
|
|
} /* istanbul ignore next */ else {
|
|
|
|
logger.info(
|
2018-10-23 04:50:24 +00:00
|
|
|
{ repositories: composerJson.repositories },
|
2018-10-16 04:33:42 +00:00
|
|
|
'Unknown composer repositories'
|
|
|
|
);
|
|
|
|
}
|
2018-10-14 04:46:17 +00:00
|
|
|
}
|
2018-10-23 04:50:24 +00:00
|
|
|
if (composerJson.type) {
|
|
|
|
res.composerJsonType = composerJson.type;
|
2018-10-23 04:40:35 +00:00
|
|
|
}
|
2018-10-14 04:46:17 +00:00
|
|
|
return res;
|
2018-06-08 04:15:13 +00:00
|
|
|
}
|