2019-02-08 13:50:06 +00:00
|
|
|
const is = require('@sindresorhus/is');
|
2018-05-09 06:03:59 +00:00
|
|
|
const { get } = require('../../manager');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getUpdatedPackageFiles,
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getUpdatedPackageFiles(config) {
|
|
|
|
logger.debug('manager.getUpdatedPackageFiles()');
|
|
|
|
logger.trace({ config });
|
2018-07-18 18:18:40 +00:00
|
|
|
const updatedFileContents = {};
|
2018-07-19 07:14:34 +00:00
|
|
|
const packageFileManagers = {};
|
|
|
|
const packageFileUpdatedDeps = {};
|
2018-05-09 06:03:59 +00:00
|
|
|
|
|
|
|
for (const upgrade of config.upgrades) {
|
2018-07-19 07:14:34 +00:00
|
|
|
const { manager, packageFile, depName } = upgrade;
|
|
|
|
packageFileManagers[packageFile] = manager;
|
|
|
|
packageFileUpdatedDeps[packageFile] =
|
|
|
|
packageFileUpdatedDeps[packageFile] || [];
|
|
|
|
packageFileUpdatedDeps[packageFile].push(depName);
|
2018-07-04 08:11:53 +00:00
|
|
|
if (upgrade.updateType !== 'lockFileMaintenance') {
|
2018-05-09 06:03:59 +00:00
|
|
|
const existingContent =
|
2018-07-18 18:18:40 +00:00
|
|
|
updatedFileContents[packageFile] ||
|
|
|
|
(await platform.getFile(packageFile, config.parentBranch));
|
2018-05-09 06:03:59 +00:00
|
|
|
let newContent = existingContent;
|
|
|
|
const updateDependency = get(manager, 'updateDependency');
|
|
|
|
newContent = await updateDependency(existingContent, upgrade);
|
|
|
|
if (!newContent) {
|
|
|
|
if (config.parentBranch) {
|
|
|
|
logger.info('Rebasing branch after error updating content');
|
|
|
|
return getUpdatedPackageFiles({
|
|
|
|
...config,
|
|
|
|
parentBranch: undefined,
|
|
|
|
});
|
|
|
|
}
|
2018-10-17 04:04:39 +00:00
|
|
|
logger.debug(
|
|
|
|
{ existingContent, config: upgrade },
|
|
|
|
'Error updating file'
|
|
|
|
);
|
2018-10-09 18:03:37 +00:00
|
|
|
throw new Error('update-failure');
|
2018-05-09 06:03:59 +00:00
|
|
|
}
|
|
|
|
if (newContent !== existingContent) {
|
|
|
|
if (config.parentBranch) {
|
2019-01-06 13:47:42 +00:00
|
|
|
// This ensure it's always 1 commit from the bot
|
2018-05-09 06:03:59 +00:00
|
|
|
logger.info('Need to update package file so will rebase first');
|
|
|
|
return getUpdatedPackageFiles({
|
|
|
|
...config,
|
|
|
|
parentBranch: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
logger.debug('Updating packageFile content');
|
2018-07-18 18:18:40 +00:00
|
|
|
updatedFileContents[packageFile] = newContent;
|
2018-05-09 06:03:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 18:18:40 +00:00
|
|
|
const updatedPackageFiles = Object.keys(updatedFileContents).map(name => ({
|
|
|
|
name,
|
|
|
|
contents: updatedFileContents[name],
|
|
|
|
}));
|
2019-02-08 13:31:30 +00:00
|
|
|
const updatedArtifacts = [];
|
|
|
|
const artifactErrors = [];
|
2018-07-19 07:14:34 +00:00
|
|
|
for (const packageFile of updatedPackageFiles) {
|
|
|
|
const manager = packageFileManagers[packageFile.name];
|
|
|
|
const updatedDeps = packageFileUpdatedDeps[packageFile.name];
|
2018-09-30 08:10:42 +00:00
|
|
|
const getArtifacts = get(manager, 'getArtifacts');
|
|
|
|
if (getArtifacts) {
|
2019-02-08 13:50:06 +00:00
|
|
|
const results = await getArtifacts(
|
2018-07-19 07:14:34 +00:00
|
|
|
packageFile.name,
|
|
|
|
updatedDeps,
|
2018-09-10 13:58:24 +00:00
|
|
|
packageFile.contents,
|
|
|
|
config
|
2018-07-19 07:14:34 +00:00
|
|
|
);
|
2019-02-08 13:50:06 +00:00
|
|
|
if (is.nonEmptyArray(results)) {
|
|
|
|
for (const res of results) {
|
|
|
|
const { file, artifactError } = res;
|
|
|
|
if (file) {
|
|
|
|
updatedArtifacts.push(file);
|
|
|
|
} else if (artifactError) {
|
|
|
|
artifactErrors.push(artifactError);
|
|
|
|
}
|
2018-10-01 14:29:50 +00:00
|
|
|
}
|
2018-07-19 07:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 06:03:59 +00:00
|
|
|
return {
|
|
|
|
parentBranch: config.parentBranch, // Need to overwrite original config
|
2018-07-18 18:18:40 +00:00
|
|
|
updatedPackageFiles,
|
2019-02-08 13:31:30 +00:00
|
|
|
updatedArtifacts,
|
|
|
|
artifactErrors,
|
2018-05-09 06:03:59 +00:00
|
|
|
};
|
|
|
|
}
|