refactor: use common manager resolve function

This commit is contained in:
Rhys Arkins 2018-03-30 07:38:38 +02:00
parent abb12d850a
commit 5b3d7a9f86
12 changed files with 30 additions and 109 deletions

View file

@ -1,6 +1,5 @@
const { extractDependencies } = require('./extract'); const { extractDependencies } = require('./extract');
const { getPackageUpdates } = require('./package'); const { getPackageUpdates } = require('./package');
const { resolvePackageFile } = require('./resolve');
const { updateDependency } = require('./update'); const { updateDependency } = require('./update');
const filePattern = new RegExp('(^|/)WORKSPACE$'); const filePattern = new RegExp('(^|/)WORKSPACE$');
@ -11,6 +10,5 @@ module.exports = {
extractDependencies, extractDependencies,
filePattern, filePattern,
getPackageUpdates, getPackageUpdates,
resolvePackageFile,
updateDependency, updateDependency,
}; };

View file

@ -1,18 +0,0 @@
const configParser = require('../../config');
module.exports = {
resolvePackageFile,
};
async function resolvePackageFile(config, inputFile) {
const packageFile = configParser.mergeChildConfig(config.bazel, inputFile);
logger.debug(
`Resolving packageFile ${JSON.stringify(packageFile.packageFile)}`
);
packageFile.content = await platform.getFile(packageFile.packageFile);
if (!packageFile.content) {
logger.debug('No packageFile content');
return null;
}
return packageFile;
}

View file

@ -1,16 +1,16 @@
const { extractDependencies } = require('./extract'); const { extractDependencies } = require('./extract');
const { getPackageUpdates } = require('../docker/package'); const { getPackageUpdates } = require('../docker/package');
const { resolvePackageFile } = require('./resolve');
const { updateDependency } = require('./update'); const { updateDependency } = require('./update');
const filePattern = new RegExp('^.circleci/config.yml$'); const filePattern = new RegExp('^.circleci/config.yml$');
const contentPattern = new RegExp('(^|\\n)\\s*- image: '); const contentPattern = new RegExp('(^|\\n)\\s*- image: ');
const parentManager = 'docker';
module.exports = { module.exports = {
contentPattern, contentPattern,
extractDependencies, extractDependencies,
filePattern, filePattern,
getPackageUpdates, getPackageUpdates,
resolvePackageFile, parentManager,
updateDependency, updateDependency,
}; };

View file

@ -1,19 +0,0 @@
const { mergeChildConfig } = require('../../config');
module.exports = {
resolvePackageFile,
};
async function resolvePackageFile(config, inputFile) {
const composeConfig = mergeChildConfig(config.docker, config.circleci);
const packageFile = mergeChildConfig(composeConfig, inputFile);
logger.debug(
`Resolving packageFile ${JSON.stringify(packageFile.packageFile)}`
);
packageFile.content = await platform.getFile(packageFile.packageFile);
if (!packageFile.content) {
logger.debug('No packageFile content');
return null;
}
return packageFile;
}

View file

@ -5,12 +5,14 @@ const { updateDependency } = require('./update');
const filePattern = new RegExp('(^|/)docker-compose[^/]*\\.ya?ml$'); const filePattern = new RegExp('(^|/)docker-compose[^/]*\\.ya?ml$');
const contentPattern = new RegExp('(^|\\n)\\s*image:'); const contentPattern = new RegExp('(^|\\n)\\s*image:');
const parentManager = 'docker';
module.exports = { module.exports = {
contentPattern, contentPattern,
extractDependencies, extractDependencies,
filePattern, filePattern,
getPackageUpdates, getPackageUpdates,
parentManager,
resolvePackageFile, resolvePackageFile,
updateDependency, updateDependency,
}; };

View file

@ -159,11 +159,12 @@ async function resolvePackageFiles(config) {
? config.packageFiles ? config.packageFiles
: await detectPackageFiles(config); : await detectPackageFiles(config);
logger.debug({ allPackageFiles }, 'allPackageFiles'); logger.debug({ allPackageFiles }, 'allPackageFiles');
function resolvePackageFile(p) { async function resolvePackageFile(p) {
const packageFile = typeof p === 'string' ? { packageFile: p } : p; let packageFile = typeof p === 'string' ? { packageFile: p } : p;
const fileName = packageFile.packageFile.split('/').pop(); const fileName = packageFile.packageFile.split('/').pop();
packageFile.manager = packageFile.manager || getManager(fileName); packageFile.manager = packageFile.manager || getManager(fileName);
if (!packageFile.manager) { const { manager } = packageFile;
if (!manager) {
// Config error // Config error
const error = new Error('config-validation'); const error = new Error('config-validation');
error.configFile = packageFile.packageFile; error.configFile = packageFile.packageFile;
@ -172,10 +173,25 @@ async function resolvePackageFiles(config) {
'Please correct the file name in your packageFiles array'; 'Please correct the file name in your packageFiles array';
throw error; throw error;
} }
return managers[packageFile.manager].resolvePackageFile( if (managers[manager].resolvePackageFile) {
config, return managers[manager].resolvePackageFile(config, packageFile);
packageFile }
const { parentManager } = managers[manager];
const parentManagerConfig = parentManager ? config[parentManager] : {};
const managerConfig = mergeChildConfig(
parentManagerConfig,
config[manager]
); );
packageFile = mergeChildConfig(managerConfig, packageFile);
logger.debug(
`Resolving packageFile ${JSON.stringify(packageFile.packageFile)}`
);
packageFile.content = await platform.getFile(packageFile.packageFile);
if (!packageFile.content) {
logger.debug('No packageFile content');
return null;
}
return packageFile;
} }
// TODO: throttle how many we resolve in parallel // TODO: throttle how many we resolve in parallel
const queue = allPackageFiles.map(p => resolvePackageFile(p)); const queue = allPackageFiles.map(p => resolvePackageFile(p));

View file

@ -1,6 +1,5 @@
const { extractDependencies } = require('./extract'); const { extractDependencies } = require('./extract');
const { getPackageUpdates } = require('../npm/package'); const { getPackageUpdates } = require('../npm/package');
const { resolvePackageFile } = require('./resolve');
const { updateDependency } = require('./update'); const { updateDependency } = require('./update');
const filePattern = new RegExp('(^|/)package.js$'); const filePattern = new RegExp('(^|/)package.js$');
@ -11,6 +10,5 @@ module.exports = {
extractDependencies, extractDependencies,
filePattern, filePattern,
getPackageUpdates, getPackageUpdates,
resolvePackageFile,
updateDependency, updateDependency,
}; };

View file

@ -1,18 +0,0 @@
const configParser = require('../../config');
module.exports = {
resolvePackageFile,
};
async function resolvePackageFile(config, inputFile) {
const packageFile = configParser.mergeChildConfig(config.meteor, inputFile);
logger.debug(
`Resolving packageFile ${JSON.stringify(packageFile.packageFile)}`
);
packageFile.content = await platform.getFile(packageFile.packageFile);
if (!packageFile.content) {
logger.debug('No packageFile content');
return null;
}
return packageFile;
}

View file

@ -1,14 +1,14 @@
const { extractDependencies } = require('./extract'); const { extractDependencies } = require('./extract');
const { getPackageUpdates } = require('./package'); const { getPackageUpdates } = require('./package');
const { resolvePackageFile } = require('./resolve');
const { updateDependency } = require('./update'); const { updateDependency } = require('./update');
const filePattern = new RegExp('^.nvmrc$'); const filePattern = new RegExp('^.nvmrc$');
const parentManager = 'node';
module.exports = { module.exports = {
extractDependencies, extractDependencies,
filePattern, filePattern,
getPackageUpdates, getPackageUpdates,
resolvePackageFile, parentManager,
updateDependency, updateDependency,
}; };

View file

@ -1,19 +0,0 @@
const { mergeChildConfig } = require('../../config');
module.exports = {
resolvePackageFile,
};
async function resolvePackageFile(config, inputFile) {
const nvmConfig = mergeChildConfig(config.node, config.nvm);
const packageFile = mergeChildConfig(nvmConfig, inputFile);
logger.debug(
`Resolving packageFile ${JSON.stringify(packageFile.packageFile)}`
);
packageFile.content = await platform.getFile(packageFile.packageFile);
if (!packageFile.content) {
logger.debug('No packageFile content');
return null;
}
return packageFile;
}

View file

@ -1,16 +1,16 @@
const { extractDependencies } = require('./extract'); const { extractDependencies } = require('./extract');
const { getPackageUpdates } = require('./package'); const { getPackageUpdates } = require('./package');
const { resolvePackageFile } = require('./resolve');
const { updateDependency } = require('./update'); const { updateDependency } = require('./update');
const filePattern = new RegExp('^.travis.yml$'); const filePattern = new RegExp('^.travis.yml$');
const contentPattern = new RegExp('(^|\\n)node_js:\\n'); const contentPattern = new RegExp('(^|\\n)node_js:\\n');
const parentManager = 'node';
module.exports = { module.exports = {
contentPattern, contentPattern,
extractDependencies, extractDependencies,
filePattern, filePattern,
getPackageUpdates, getPackageUpdates,
resolvePackageFile, parentManager,
updateDependency, updateDependency,
}; };

View file

@ -1,19 +0,0 @@
const { mergeChildConfig } = require('../../config');
module.exports = {
resolvePackageFile,
};
async function resolvePackageFile(config, inputFile) {
const travisConfig = mergeChildConfig(config.node, config.travis);
const packageFile = mergeChildConfig(travisConfig, inputFile);
logger.debug(
`Resolving packageFile ${JSON.stringify(packageFile.packageFile)}`
);
packageFile.content = await platform.getFile(packageFile.packageFile);
if (!packageFile.content) {
logger.debug('No packageFile content');
return null;
}
return packageFile;
}