Skip to content
Snippets Groups Projects
Commit 58da519c authored by Rhys Arkins's avatar Rhys Arkins
Browse files

Support configurable repository and package file

parent 69fcc11c
No related branches found
No related tags found
No related merge requests found
...@@ -4,12 +4,10 @@ var config = {}; ...@@ -4,12 +4,10 @@ var config = {};
module.exports = { module.exports = {
// Initialize GitHub by getting base branch SHA // Initialize GitHub by getting base branch SHA
init: function(token, repoName, baseBranch, verbose = false) { init: function(setConfig, repoName, packageFile) {
config.token = token; config = setConfig;
config.repoName = repoName; config.repoName = repoName;
config.baseBranch = baseBranch; config.packageFile = packageFile;
config.verbose = verbose;
config.userName = repoName.split('/')[0]; config.userName = repoName.split('/')[0];
return ghGot(`repos/${config.repoName}/git/refs/head`, {token: config.token}).then(res => { return ghGot(`repos/${config.repoName}/git/refs/head`, {token: config.token}).then(res => {
......
...@@ -5,15 +5,13 @@ const github = require('./helpers/github'); ...@@ -5,15 +5,13 @@ const github = require('./helpers/github');
const npm = require('./helpers/npm'); const npm = require('./helpers/npm');
const packageJson = require('./helpers/packageJson'); const packageJson = require('./helpers/packageJson');
// Process arguments
const repoName = process.argv[2];
const packageFile = process.argv[3] || 'package.json';
const token = process.env.RENOVATE_TOKEN;
validateArguments();
const config = initConfig(); const config = initConfig();
validateArguments();
npm.init(config); npm.init(config);
const repoName = Object.keys(config.repositories)[0];
const packageFile = config.repositories[repoName] || 'package.json';
initGitHub() initGitHub()
.then(getPackageFileContents) .then(getPackageFileContents)
.then(determineUpgrades) .then(determineUpgrades)
...@@ -25,22 +23,6 @@ initGitHub() ...@@ -25,22 +23,6 @@ initGitHub()
console.log('renovate caught error: ' + err); console.log('renovate caught error: ' + err);
}); });
function validateArguments() {
// token must be defined
if (typeof token === 'undefined') {
console.error('Error: Environment variable RENOVATE_TOKEN must be defined');
process.exit(1);
}
// Check arguments
if (process.argv.length < 3 || process.argv.length > 4) {
console.error('Error: You must specify the GitHub repository and optionally path.');
console.log('Example: node src singapore/renovate');
console.log('Example: node src foo/bar baz/package.json');
process.exit(1);
}
}
function initConfig() { function initConfig() {
const defaultConfig = require('./defaults'); const defaultConfig = require('./defaults');
let customConfig = {}; let customConfig = {};
...@@ -49,19 +31,42 @@ function initConfig() { ...@@ -49,19 +31,42 @@ function initConfig() {
} catch(e) { } catch(e) {
// Do nothing // Do nothing
} }
return Object.assign(defaultConfig, customConfig); const cliConfig = {};
if (process.env.RENOVATE_TOKEN) {
cliConfig.token = process.env.RENOVATE_TOKEN;
}
// Check if repository name and package file are provided via CLI
const repoName = process.argv[2];
const packageFile = process.argv[3] || 'package.json';
if (repoName) {
cliConfig.repositories = {};
cliConfig.repositories[repoName] = packageFile;
}
return Object.assign(defaultConfig, customConfig, cliConfig);
}
function validateArguments() {
// token must be defined
if (typeof config.token === 'undefined') {
console.error('Error: A GitHub token must be configured');
process.exit(1);
}
// We also need a repository
if (typeof Object.keys(config.repositories).length === 0) {
console.error('Error: A repository must be configured');
}
} }
function initGitHub() { function initGitHub() {
if (config.verbose) { if (config.verbose) {
console.log('Initializing GitHub'); console.log('Initializing GitHub');
} }
return github.init(token, repoName, config.baseBranch, config.verbose); return github.init(config, repoName, packageFile);
} }
function getPackageFileContents() { function getPackageFileContents() {
console.log('Getting package file contents'); console.log('Getting package file contents');
return github.getFileContents(packageFile); return github.getFileContents(config.packageFile);
} }
function determineUpgrades(packageFileContents) { function determineUpgrades(packageFileContents) {
...@@ -135,7 +140,7 @@ function updateDependency({ upgradeType, depType, depName, currentVersion, newVe ...@@ -135,7 +140,7 @@ function updateDependency({ upgradeType, depType, depName, currentVersion, newVe
} }
function ensureCommit() { function ensureCommit() {
// Retrieve the package.json from this renovate branch // Retrieve the package.json from this renovate branch
return github.getFile(packageFile, branchName).then(res => { return github.getFile(config.packageFile, branchName).then(res => {
const currentSHA = res.body.sha; const currentSHA = res.body.sha;
const currentFileContent = new Buffer(res.body.content, 'base64').toString(); const currentFileContent = new Buffer(res.body.content, 'base64').toString();
const currentJson = JSON.parse(currentFileContent); const currentJson = JSON.parse(currentFileContent);
...@@ -148,7 +153,7 @@ function updateDependency({ upgradeType, depType, depName, currentVersion, newVe ...@@ -148,7 +153,7 @@ function updateDependency({ upgradeType, depType, depName, currentVersion, newVe
return github.writeFile(branchName, currentSHA, packageFile, newPackageContents, commitMessage); return github.writeFile(branchName, currentSHA, packageFile, newPackageContents, commitMessage);
} else { } else {
if (config.verbose) { if (config.verbose) {
console.log(`${depName}: Already up-to-date in branch ${branchName}`); console.log(`${depName}: branch ${branchName} is already up-to-date`);
} }
return; return;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment