Skip to content

Commit

Permalink
Add "badges.greenkeeper"-option to .thought/config.js
Browse files Browse the repository at this point in the history
- "greenkeeper"-badge can be enabled and disabled manually
  to avoid HTTP-request to parse the badge.
  • Loading branch information
nknapp committed Apr 7, 2017
1 parent 70516eb commit 12d151a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .thought/config.js
@@ -1,5 +1,8 @@
module.exports = {
plugins: [
require('thought-plugin-jsdoc')
]
],
badges: {
greenkeeper: true
}
}
17 changes: 11 additions & 6 deletions bin/thought.js
Expand Up @@ -17,11 +17,13 @@

var program = require('commander')
var thought = require('../')
var debug = require('debug')('thought:bin')
var findPackage = require('find-package')
var path = require('path')

Error.stackTraceLimit = 0

debug('started')
program
.version(require('../package').version)
.option('-d, --debug', 'higher stack-trace-limit, long stack-traces', function (option) {
Expand All @@ -44,19 +46,22 @@ program
'If you want that, run `thought init`\n')
/* eslint-enable no-console */
}
debug('running thought')
thought({
addToGit: options.addToGit,
debug: program.debug
}).done(function (filenames) {
/* eslint-disable no-console */
console.log('The following files were updated: ' + filenames.join(', '))
/* eslint-enable no-console */
})
.done(function (filenames) {
debug('done')
/* eslint-disable no-console */
console.log('The following files were updated: ' + filenames.join(', '))
/* eslint-enable no-console */
})
})

program
.command('init')
.description("Register scripts in the curent module's package.json")
.description('Register scripts in the curent module\'s package.json')
.action(function () {
changeDir()
require('../lib/check-engines.js')()
Expand Down Expand Up @@ -106,5 +111,5 @@ function changeDir () {
var moduleRoot = path.dirname(packageJson.paths.absolute)
process.chdir(moduleRoot)
// eslint-disable-next-line no-console
console.log("I'm running inside module '" + packageJson.name + "' in '" + moduleRoot)
console.log('I\'m running inside module \'' + packageJson.name + '\' in \'' + moduleRoot)
}
27 changes: 21 additions & 6 deletions customize.js
Expand Up @@ -9,10 +9,22 @@

const path = require('path')
const fs = require('fs')
const debug = require('debug')

// Default config if the file `.thought/config.js` does not exist
/**
* Default configuration for .thought. Override this configuration by creating a file `.thought/config.js`
* @api public
*/
const defaultConfig = {
plugins: []
plugins: [],
badges: {
/**
* Should there be a greenkeeper badge?
* `undefined` means autodetect (by parsing the badge for the repo-url)
* @property
*/
greenkeeper: undefined
}
}

/**
Expand All @@ -24,8 +36,10 @@ const defaultConfig = {
*/
module.exports = function createSpec (workingDir) {
return function thoughtSpec (customize) {
debug('creating customize config')
const configFile = path.resolve('.thought', 'config.js')
var config = fs.existsSync(configFile) ? require(configFile) : defaultConfig
const config = fs.existsSync(configFile) ? require(configFile) : defaultConfig
debug('config loaded', config)
return customize
.registerEngine('handlebars', require('customize-engine-handlebars'))
.merge({
Expand All @@ -35,7 +49,8 @@ module.exports = function createSpec (workingDir) {
helpers: require.resolve('./handlebars/helpers.js'),
data: {
'package': require(path.resolve(workingDir, 'package.json')),
'workingDir': workingDir
config: config,
workingDir: workingDir
},
preprocessor: require('./handlebars/preprocessor.js'),
hbsOptions: {
Expand All @@ -45,10 +60,10 @@ module.exports = function createSpec (workingDir) {
})
// Apply any customization from the config-files (such as loading modules)
.load(function (customize) {
const result = config.plugins.reduce((prev, plugin) => {
debug('Loading modules', config)
return config.plugins.reduce((prev, plugin) => {
return prev.load(plugin)
}, customize)
return result
})
.merge({
handlebars: {
Expand Down
6 changes: 6 additions & 0 deletions handlebars/helpers.js
Expand Up @@ -330,6 +330,12 @@ function hasCoveralls () {
* @api public
*/
function hasGreenkeeper (options) {
const config = options.data.root.config
const showBadge = config && config.badges && config.badges.greenkeeper
if (showBadge != null) { // not undefined and not null ?
return showBadge
}
// otherwise autodetect via badge
const slug = githubRepo(options)
return popsicle.get(`https://badges.greenkeeper.io/${slug}.svg`)
.then(function (response) {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/exeq.js
Expand Up @@ -9,9 +9,9 @@ var cp = require('child_process')
/**
* Call child_process.execFile with `{ encoding: utf-8 }` and return a promise of `[ stdout, stderr]`
*/
module.exports = function (cmd, args) {
module.exports = function (cmd, args, options) {
return new Promise(function (resolve, reject) {
return cp.execFile(cmd, args, function (err, stdout, stderr) {
return cp.execFile(cmd, args, options, function (err, stdout, stderr) {
if (err) {
return reject(err)
}
Expand Down
73 changes: 64 additions & 9 deletions test/helper-spec.js
Expand Up @@ -37,7 +37,6 @@ function executeInDir (directory) {
* Return an expectation for the result of a handlebars run
* @param {string} template the handlebars-template
* @param {object} input the input object
* @param {string=} workingDirectory the working directory to execute the test in
* @returns {*}
*/
function expectHbs (template, input) {
Expand Down Expand Up @@ -325,7 +324,8 @@ describe('thought-helpers:', function () {
})

describe('The "githubRepo"-helper', function () {
it('should return the name of organization/repository in a module with a configured repository on github (e.g. Thought itself)', function () {
it('should return the name of organization/repository in a module with a configured ' +
'repository on github (e.g. Thought itself)', function () {
return expectHbs('{{githubRepo}}', {
'package': {
'name': 'no-github-repo',
Expand Down Expand Up @@ -372,37 +372,52 @@ describe('thought-helpers:', function () {
name: 'no-git-repo'
}
})
.not.to.eventually.be.false()
.to.eventually.equal('false')
})

it('should return false, project does not have a github url', function () {
it('should return false, if the project does not have a github url', function () {
httpMocks.greenkeeperError('/null.svg', 404)

return expectHbs('{{hasGreenkeeper}}', {
package: {
name: 'no-git-repo',
name: 'no-github-repo',
'repository': {
'type': 'git',
'url': 'https://custom-git.com/somepath.git'
}
}
})
.not.to.eventually.be.false()
.to.eventually.equal('false')
})

it('should return false, if the project does not have greenkeeper enabled', function () {
httpMocks.greenkeeperDisabled('/group/repo.svg')

return expectHbs('{{hasGreenkeeper}}', {
package: {
name: 'no-git-repo',
name: 'greenkeeper-disabled',
'repository': {
'type': 'git',
'url': 'https://github.com/group/repo.git'
}
}
})
.to.eventually.equal('false')
})

it('should return true, if the project does have greenkeeper enabled (parsing the badge)', function () {
httpMocks.greenkeeperEnabled('/group/repo.svg')

return expectHbs('{{hasGreenkeeper}}', {
package: {
name: 'greenkeeper-enabled',
'repository': {
'type': 'git',
'url': 'https://github.com/group/repo.git'
}
}
})
.not.to.eventually.be.false()
.to.eventually.equal('true')
})

it('should throw an error, if the greenkeeper-badge returns an error other than 404', function () {
Expand All @@ -417,7 +432,47 @@ describe('thought-helpers:', function () {
}
}
})
.to.be.rejectedWith('')
.to.be.rejectedWith('{"statusCode":500,"error":"Error"}')
})

it('should return true, if the badge is enabled in the config', function () {
httpMocks.greenkeeperDisabled('/group/repo.svg')

return expectHbs('{{hasGreenkeeper}}', {
package: {
name: 'no-git-repo',
'repository': {
'type': 'git',
'url': 'https://github.com/group/repo.git'
}
},
config: {
badges: {
greenkeeper: true
}
}
})
.to.eventually.equal('true')
})

it('should return true, if the badge is enabled in the config', function () {
httpMocks.greenkeeperEnabled('/group/repo.svg')

return expectHbs('{{hasGreenkeeper}}', {
package: {
name: 'enabledRepo',
'repository': {
'type': 'git',
'url': 'https://github.com/group/repo.git'
}
},
config: {
badges: {
greenkeeper: false
}
}
})
.to.eventually.equal('false')
})
})
})
2 changes: 1 addition & 1 deletion test/lib/http-mocks.js
Expand Up @@ -29,7 +29,7 @@ module.exports = {
greenkeeperError: function (urlPath, statusCode) {
nock(`https://badges.greenkeeper.io/`)
.get(urlPath)
.reply(statusCode, `{"statusCode":${statusCode},"error":"Not Found"}`)
.reply(statusCode, `{"statusCode":${statusCode},"error":"Error"}`)
},

/**
Expand Down

0 comments on commit 12d151a

Please sign in to comment.