Skip to content

Commit

Permalink
Large refactor of config and helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jan 11, 2017
1 parent b3a89c1 commit b498c52
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/node_modules
/src/config.js
/config.js
/npm-debug.log
112 changes: 55 additions & 57 deletions src/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,66 @@ const logger = new (winston.Logger)({
],
});

module.exports = {
module.exports = function init() {
// This function reads in all configs and merges them
init(argv) {
/* eslint-disable global-require */
const defaultConfig = require('../defaults');
let customConfig = {};
try {
customConfig = require('../config');
} catch (err) {
// Do nothing
/* eslint-disable global-require */
const defaultConfig = require('../defaults');
let customConfig = {};
try {
customConfig = require('../../config');
} catch (err) {
// Do nothing
}
/* eslint-enable global-require */
const cliConfig = {};
if (process.env.LOG_LEVEL) {
cliConfig.logLevel = process.env.LOG_LEVEL;
}
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 = [
{
name: repoName,
packageFiles: [packageFile],
},
];
}
const config = Object.assign(defaultConfig, customConfig, cliConfig);
// First, convert any strings to objects
config.repositories.forEach((repo, index) => {
if (typeof repo === 'string') {
config.repositories[index] = { name: repo };
}
/* eslint-enable global-require */
const cliConfig = {};
if (process.env.LOG_LEVEL) {
cliConfig.logLevel = process.env.LOG_LEVEL;
});
// Add 'package.json' if missing
config.repositories.forEach((repo, index) => {
if (!repo.packageFiles || !repo.packageFiles.length) {
config.repositories[index].packageFiles = ['package.json'];
}
if (process.env.RENOVATE_TOKEN) {
cliConfig.token = process.env.RENOVATE_TOKEN;
}
// Check if repository name and package file are provided via CLI
const repoName = argv[2];
const packageFile = argv[3] || 'package.json';
if (repoName) {
cliConfig.repositories = [
{
name: repoName,
packageFiles: [packageFile],
},
];
}
const config = Object.assign(defaultConfig, customConfig, cliConfig);
// First, convert any strings to objects
config.repositories.forEach((repo, index) => {
if (typeof repo === 'string') {
config.repositories[index] = { name: repo };
}
});
// Add 'package.json' if missing
config.repositories.forEach((repo, index) => {
if (!repo.packageFiles || !repo.packageFiles.length) {
config.repositories[index].packageFiles = ['package.json'];
}
});
});


// Winston log level can be controlled via config or env
if (config.logLevel) {
logger.level = config.logLevel;
}
logger.verbose(config);
config.logger = logger;
// Winston log level can be controlled via config or env
if (config.logLevel) {
logger.level = config.logLevel;
}
logger.verbose(config);
config.logger = logger;

// token must be defined
if (typeof config.token === 'undefined') {
logger.error('Error: A GitHub token must be configured');
process.exit(1);
}
// We also need a repository
if (!config.repositories || config.repositories.length === 0) {
logger.error('Error: At least one repository must be configured');
}
// token must be defined
if (typeof config.token === 'undefined') {
logger.error('Error: A GitHub token must be configured');
process.exit(1);
}
// We also need a repository
if (!config.repositories || config.repositories.length === 0) {
logger.error('Error: At least one repository must be configured');
}

return config;
},
return config;
};
53 changes: 26 additions & 27 deletions src/helpers/github.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
const ghGot = require('gh-got');

let config = {};
const config = {};
let logger = null;

module.exports = {
init,
module.exports = function github(setLogger) {
logger = setLogger;
this.initRepo = initRepo;
// Package File
getPackageFile,
getPackageFileContents,
writePackageFile,
this.getPackageFile = getPackageFile;
this.getPackageFileContents = getPackageFileContents;
this.writePackageFile = writePackageFile;
// Branch
createBranch,
this.createBranch = createBranch;
// PR
checkForClosedPr,
createPr,
getPr,
updatePr,
this.checkForClosedPr = checkForClosedPr;
this.createPr = createPr;
this.getPr = getPr;
this.updatePr = updatePr;
return this;
};

// Initialize GitHub by getting base branch and SHA
function init(setConfig, repoName, packageFile) {
config = setConfig;
logger = config.logger;
function initRepo(token, repoName) {
config.token = token;
config.repoName = repoName;
config.packageFile = packageFile;

return getRepo()
.then(processRepo)
.catch((err) => {
logger.error(`GitHub init error: ${err}`);
throw err;
});

function getRepo() {
logger.debug(`Getting repo ${repoName}`);
return ghGot(`repos/${config.repoName}`, { token: config.token })
.then(res => res.body);
}

function processRepo(repo) {
logger.debug(`Processing repo ${repoName}`);
config.owner = repo.owner.login;
config.defaultBranch = repo.default_branch;
}

function getRepoSHA() {
return ghGot(`repos/${config.repoName}/git/refs/head`, {
token: config.token,
}).then((res) => {
Expand All @@ -50,22 +56,15 @@ function init(setConfig, repoName, packageFile) {
return Promise.resolve();
});
}

return getRepo()
.then(processRepo)
.then(getRepoSHA)
.catch((err) => {
logger.error(`GitHub init error: ${err}`);
throw err;
});
}

// Package File
function getPackageFile(branchName) {
return getFile(config.packageFile, branchName);
}

function getPackageFileContents() {
function getPackageFileContents(packageFile) {
config.packageFile = packageFile;
return getFileContents(config.packageFile);
}

Expand Down
91 changes: 38 additions & 53 deletions src/helpers/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,50 @@ const got = require('got');
const semver = require('semver');
const stable = require('semver-stable');

let config = {};
let logger = null;

module.exports = {
init(setConfig) {
config = setConfig;
logger = config.logger;
},
getDependencies(packageContents) {
const allDependencies = [];
const dependencyTypes = ['dependencies', 'devDependencies'];
dependencyTypes.forEach((depType) => {
Object.keys(packageContents[depType]).forEach((depName) => {
allDependencies.push({
depType,
depName,
currentVersion: packageContents[depType][depName],
});
});
});
return allDependencies;
},
getAllDependencyUpgrades(packageContents) {
const allDependencyChecks = [];
const allDependencyUpgrades = [];
const dependencyTypes = ['dependencies', 'devDependencies'];
dependencyTypes.forEach((depType) => {
if (!packageContents[depType]) {
module.exports = function npm(setLogger) {
logger = setLogger;
this.getAllDependencyUpgrades = getAllDependencyUpgrades;
return this;
};

function getAllDependencyUpgrades(packageContents) {
const allDependencyChecks = [];
const allDependencyUpgrades = [];
const dependencyTypes = ['dependencies', 'devDependencies'];
dependencyTypes.forEach((depType) => {
if (!packageContents[depType]) {
return;
}
Object.keys(packageContents[depType]).forEach((depName) => {
const currentVersion = packageContents[depType][depName];
if (!isValidVersion(currentVersion)) {
logger.verbose(`${depName}: Skipping invalid version ${currentVersion}`);
return;
}
Object.keys(packageContents[depType]).forEach((depName) => {
const currentVersion = packageContents[depType][depName];
if (!isValidVersion(currentVersion)) {
logger.verbose(`${depName}: Skipping invalid version ${currentVersion}`);
return;
}
allDependencyChecks.push(
getDependencyUpgrades(depName, currentVersion).then((res) => {
if (res.length > 0) {
logger.verbose(`${depName}: Upgrades = ${JSON.stringify(res)}`);
res.forEach((upgrade) => {
allDependencyUpgrades.push({
depType,
depName,
currentVersion,
upgradeType: upgrade.type,
newVersion: upgrade.version,
});
allDependencyChecks.push(
getDependencyUpgrades(depName, currentVersion).then((res) => {
if (res.length > 0) {
logger.verbose(`${depName}: Upgrades = ${JSON.stringify(res)}`);
res.forEach((upgrade) => {
allDependencyUpgrades.push({
depType,
depName,
currentVersion,
upgradeType: upgrade.type,
newVersion: upgrade.version,
});
} else {
logger.verbose(`${depName}: No upgrades required`);
}
return Promise.resolve();
}));
});
});
} else {
logger.verbose(`${depName}: No upgrades required`);
}
return Promise.resolve();
}));
});
return Promise.all(allDependencyChecks).then(() => allDependencyUpgrades);
},
};
});
return Promise.all(allDependencyChecks).then(() => allDependencyUpgrades);
}

function getDependency(depName) {
// supports scoped packages, e.g. @user/package
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/packageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const _ = require('lodash');

let logger = null;

module.exports = function packageJson(config) {
logger = config.logger;
module.exports = function packageJson(setLogger) {
logger = setLogger;
this.setNewValue = setNewValue;
return this;
};
Expand Down
Loading

0 comments on commit b498c52

Please sign in to comment.