Skip to content

Commit

Permalink
refactor: move routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lipp committed Jan 5, 2016
1 parent b4b83bd commit 993032a
Show file tree
Hide file tree
Showing 10 changed files with 497 additions and 352 deletions.
97 changes: 2 additions & 95 deletions bin/doclets.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ require('colors')
var path = require('path')
var fs = require('fs')
var yaml = require('js-yaml')
var GitHubApi = require('github')
var repoName = require('git-repo-name')
prompt.message = 'doclets'.blue
var webhookUrl = 'http://api.doclets.io/github/callback'
var gather = require('../lib/gather')

var log = function (message) {
Expand All @@ -36,95 +34,9 @@ var getDefaultConfig = function () {
}
}

var initRepo = function () {
var configDefaults = getDefaultConfig()
var repoOwner
if (configDefaults.repository && configDefaults.repository.url) {
var match = configDefaults.repository.url.match(/github\.com\/([^\/]+)\/.*/)
if (match) {
repoOwner = match[1]
}
}
var setupRepo = function (config) {
var github = new GitHubApi({
version: '3.0.0',
// debug: true,
protocol: 'https',
host: 'api.github.com',
timeout: 5000,
headers: {
'user-agent': 'doclets'
}

})
github.authenticate({
type: 'basic',
username: config.user,
password: config.password
})
github.repos.createHook({
user: config.owner,
repo: config.repository,
name: 'web',
activate: true,
events: ['push', 'create'],
config: {
secret: '12345678',
url: webhookUrl,
'content_type': 'json'
}
}, function (err, res) {
if (err) {
try {
err = JSON.parse(err.message).message
} catch (_) {
err = err.message
}
logerr(err)
} else {
log('Webhook successfully added'.green)
}
})
}

var initSchema = {
properties: {
user: {
description: 'GitHub user name',
required: true,
default: repoOwner
},
password: {
description: 'GitHub user password',
hidden: true,
required: true
},
repository: {
description: 'GitHub repository name',
required: true,
default: repoName.sync()
},
owner: {
description: 'GitHub repository owner',
required: true,
default: repoOwner
}
}
}
prompt.get(initSchema, function (err, result) {
if (err) {
console.error('Sorry, information not complete'.red)
process.exit(1)
} else {
setupRepo(result)
}
})
}

var config = function () {
var writeConfigYaml = function (inputs) {
var config = {}
config.name = inputs.name
config.dir = inputs.dir
config.flavor = 'jsdoc'
if (inputs.readme && inputs.readme !== '') {
Expand All @@ -141,11 +53,6 @@ var config = function () {
var configDefaults = getDefaultConfig()
var configSchema = {
properties: {
name: {
description: 'The project name to appear on doclets.io',
required: true,
default: configDefaults.name
},
dir: {
description: 'Root folder of source code',
default: configDefaults.main || 'lib'
Expand Down Expand Up @@ -215,6 +122,7 @@ var preview = function () {
process.exit(1)
} else {
var db = require('../lib/db-fake')
process.env.NODE_ENV = 'test'
var server = require('../lib/server')
server.init(result.port, db)
var docData = gather.gatherDocletsAndMeta('./')
Expand All @@ -228,15 +136,14 @@ var preview = function () {
}

var commands = {
init: initRepo,
config: config,
preview: preview
}

var command = process.argv[2]

if (!command || Object.keys(commands).indexOf(command) === -1) {
console.log('usage: doclets init | config | preview'.yellow)
console.log('usage: doclets config | preview'.yellow)
process.exit(1)
}

Expand Down
4 changes: 4 additions & 0 deletions lib/db-fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ module.exports.getVersionsByUserAndRepo = function (user, repo, done) {
})
done(mods.length === 0 && 'not found', mods)
}

module.exports.getReposByUser = function (user, done) {
done(null, [])
}
15 changes: 15 additions & 0 deletions lib/db-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports.get = function () {
var db = {}
if (process.env.COUCHDB_DEBUG_URL) {
var parsed = require('url').parse(process.env.COUCHDB_DEBUG_URL)
db.protocol = parsed.protocol
db.host = parsed.host
db.port = parsed.port
} else {
// from docker-compose
db.port = process.env.COUCHDB_PORT_5984_TCP_PORT
db.host = 'couchdb'
db.protocol = 'http:'
}
return db
}
16 changes: 3 additions & 13 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global emit */
var cradle = require('cradle')
var dbHost = require('./db-host').get()
var doclets
var users
var repos
Expand Down Expand Up @@ -63,19 +64,8 @@ var initRepos = function (connection) {
var failCount = 0

var init = function (done) {
var couchhost
var couchport
if (process.env.COUCHDB_DEBUG_URL) {
var parsed = require('url').parse(process.env.COUCHDB_DEBUG_URL)
couchhost = parsed.protocol + '//' + parsed.host
couchport = parsed.port
} else {
// from docker-compose
couchport = process.env.COUCHDB_PORT_5984_TCP_PORT
couchhost = 'http://couchdb'
}
console.log('connect', couchhost, couchport)
var connection = new (cradle.Connection)(couchhost, couchport, {
var couchhost = dbHost.protocol + '//' + dbHost.host
var connection = new (cradle.Connection)(couchhost, dbHost.port, {
retries: 10,
retryTimeout: 1000
})
Expand Down
63 changes: 63 additions & 0 deletions lib/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var fs = require('fs')
var fse = require('fs-extra')
var path = require('path')
var exec = require('sync-exec')
var GitHubApi = require('github')
var _ = require('underscore')

var execThrow = function (cmd, options) {
var ret = exec(cmd, options)
Expand All @@ -10,6 +12,24 @@ var execThrow = function (cmd, options) {
}
}

var githubApi

var github = module.exports.github = function () {
if (githubApi === undefined) {
githubApi = new GitHubApi({
version: '3.0.0',
// debug: true,
protocol: 'https',
host: 'api.github.com',
timeout: 5000,
headers: {
'user-agent': 'doclets'
}
})
}
return githubApi
}

module.exports.checkout = function (repoUrl, branch, dir) {
var gitDir = path.join(dir, repoUrl.split('github.com')[1].replace('/', '_'))

Expand All @@ -34,3 +54,46 @@ module.exports.checkout = function (repoUrl, branch, dir) {
execThrow('git ' + ['checkout', branch].join(' '), options)
return gitDir
}

var hookUrl = 'http://api.doclets.io/github/callback'

module.exports.addHook = function (user, repo, auth, done) {
github().authenticate(auth)
github().repos.createHook({
user: user,
repo: repo,
name: 'web',
activate: true,
events: ['push', 'create'],
config: {
secret: '12345678',
url: hookUrl,
'content_type': 'json'
}
}, done)
}

module.exports.removeHook = function (user, repo, auth, done) {
github().authenticate(auth)
github().repos.getHooks({
user: user,
repo: repo,
per_page: 100
}, function (err, hooks) {
if (err) {
done(err)
}
var hook = _.find(hooks, function (hook) {
return hook.config.url === hookUrl
})
if (hook) {
github().repos.deleteHook({
user: user,
repo: repo,
id: hook.id
}, done)
} else {
done(null)
}
})
}
Loading

0 comments on commit 993032a

Please sign in to comment.