Skip to content

Commit

Permalink
Merge pull request #14 from alejandromg/master
Browse files Browse the repository at this point in the history
Added autoinstaller
  • Loading branch information
koostudios committed Apr 29, 2012
2 parents 52f8566 + 27fe63e commit 5c24313
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 45 deletions.
8 changes: 8 additions & 0 deletions Makefile
@@ -0,0 +1,8 @@

previous:
@npm install

install:
@node setup/install

PHONY: previous install
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,17 @@ A super lightweight and modular blogging engine or content management system bui
- Built with NodeJS and CouchDB
- Made to be hackable and licensed under the MIT License

## Install

In order to install CouchPress you only need to do:

> cd path/to/couchpress;
> make
> make install

This command is going to help you to configure your local CouchPress installation trough a few questions, also
is going to setup the database and the views.

## Changelog

### Version 0.1
Expand Down
38 changes: 0 additions & 38 deletions config.coffee

This file was deleted.

38 changes: 38 additions & 0 deletions example.config.coffee
@@ -0,0 +1,38 @@
exports.config =

# Site Information
site:
# Site Title - appears in title of pages
title: 'CouchPress Site'
# Site Description
description: 'Just another CouchPress Site.'
# Site Copyright Holder
copyright: 'Joe Bloggs'
# Twitter Handle - optional
twitter: 'joebloggs'
# Site port - get this from your NodeJS host
port: ''
# Session Secret - a random string used to compute the session hash
secret: 'somestringhere'
# CouchPress Version
version: '0.1.2'

# Theme Information
theme:
# Folder Name of Admin Theme
admin: 'admin'
# Folder Name of Frontend Theme
front: 'soothe'

# Database Settings - get this info from your CouchDB host
db:
# CouchDB Host URL
host: 'http://www.example.com'
# CouchDB Host Port
port: 80

exports.admin =
# CouchDB Admin - create an admin with a password to secure your site
user: 'admin'
# CouchDB Admin Password
pass: ''
15 changes: 8 additions & 7 deletions package.json
Expand Up @@ -13,13 +13,14 @@
"engines": {"node": ">=0.6.12"},
"main": "server.js",
"dependencies": {
"coffee-script" : "1.2.x",
"express" : "2.5.x",
"jade" : "0.20.x",
"cradle" : "0.5.x",
"moment" : "1.5.x",
"passport" : "0.1.x",
"passport-local" : "0.1.x"
"coffee-script" : "1.2.x",
"express" : "2.5.x",
"jade" : "0.20.x",
"cradle" : "0.5.x",
"moment" : "1.5.x",
"passport" : "0.1.x",
"passport-local" : "0.1.x",
"read" : "0.0.2"
},
"license": "MIT"
}
259 changes: 259 additions & 0 deletions setup/install.js
@@ -0,0 +1,259 @@
var read = require('read')
, fs = require('fs')
, cradle = require('cradle')
, path = require('path')
, crypto = require('crypto')
;
/* */

var data = {
site : {
title :"CouchPress Site",
description :"Just another CouchPress Site",
copyright :"Joe Bloggs",
twitter :"joebloggs",
port :"3000",
secret :"somethingsecret"
},
theme: {
admin: "admin",
front:"soothe"
},
db:{
host:"http://localhost",
port:"5984"
}
}
var admin = {
user :"admin",
pass :"admin"
}
var site = data.site
, theme = data.theme
, db = data.db;

function init (cb){
console.log('\n\t Welcome to the CouchPress installation process ')
console.log('I\'m going to ask you some question, respond only the true ;)\n')
promiseChain(cb)
(function(cb){
var packageJSON = require('../package.json');
var mo = Object.keys(packageJSON.dependencies);
if (path.existsSync(__dirname + '/../node_modules')){
var modules = fs.readdirSync(__dirname +'/../node_modules')
var validKeys = mo.filter(function(v){
return modules.indexOf(v) > -1;
});
if (mo.length === validKeys.length) return cb()
return cb(new Error('You need to run `npm install` before this script'))
}
})
( read
, [{prompt: "Site Title: ", default: site.title}]
, function (n) { site.title = n }
)
( read
, [{prompt:"Site Description: ",default: site.description}]
, function (n) {site.description = n}
)
( read
, [{prompt:"Copyright: ",default: site.copyright}]
, function (n) {site.copyright = n}
)
( read
, [{prompt:"Twitter Handler: ",default: site.twitter}]
, function (n) {site.twitter = n}
)
( read
, [{prompt:"Port to listen: ",default: site.port}]
, function (n) {site.port = n}
)
( read
, [{prompt:"Secret word: ",default: site.secret}]
, function (n) {site.secret = n}
)
(function(cb){
console.log('\n\t:: Now we are going to setup the theme ::\n');
return cb()
})
( read
, [{prompt:"Admin theme: ",default: theme.admin}]
, function (n) {theme.admin = n}
)
( read
, [{prompt:"Front theme: ",default: theme.front}]
, function (n) {theme.front = n}
)
(function(cb){
console.log('\n\t:: Database Setup ::\n');
return cb()
})
( read
, [{prompt:"Database host: ",default: db.host}]
, function (n) {db.host = n}
)
( read
, [{prompt:"Database port: ",default: db.port}]
, function (n) {db.port = n}
)
(read
, [{prompt:"Database username: ",default: admin.user}]
, function (n) {admin.user = n}
)
(read
, [{prompt:"Username pass: ",default: admin.pass}]
, function (n) {admin.pass = n}
)
(function(cb){
console.log('\n\t:: About to write ::\n');
console.log('config = ', data ,'\nadmin = ', admin);
read({ prompt: "\nIs this ok? ", default: "yes" }, function (er, ok) {
if (er) return cb(er)
if (ok.toLowerCase().charAt(0) !== "y") {
return cb(new Error("cancelled"))
}
return cb();
})
})
(function(cb){
var template = 'exports.config = '+ JSON.stringify(data) +
';exports.admin = '+ JSON.stringify(admin);
fs.writeFile('./config.js',template,'utf8', function(err,resp){
if (err) return cb(err)
console.log('config.json wrote sucessfully')
return cb();
});
})
(function(cb){
read({ prompt: "\nInstall default views to database? ", default: "yes" }, function (er, ok) {
if (er) return cb(er)
if (ok.toLowerCase().charAt(0) !== "y") {
return cb(new Error("cancelled"))
}
installViews(data,cb);
});
})
(function(cb){
read({ prompt: "\nDo you want to add an extra user? ", default: "yes" }, function (er, ok) {
if (er) return cb(er)
if (ok.toLowerCase().charAt(0) !== "y") {
return cb()
} else {
var user ={}
promiseChain(cb)
( read
, [{prompt:"Username:",default:"dave"}]
, function (n) {user.user = n}
)
(read
, [{prompt:"Password:",default:"dave"}]
, function (n) { user.password = n}
)
( read
, [{prompt:"email:",default:"dave@example.com"}]
, function (n) { user.email = n}
)(function(cb){
var url = data.db.host;
if (!~url.indexOf('://')) url = 'http://' + url;
var c = new(cradle.Connection)(url,data.db.host,{
auth: { username: admin.user, password: admin.pass }
});
var salt = crypto.createHash('md5').update(new Date().toString()).update(Math.random().toString()).digest('hex');
var toSave = {
_id : 'org.couchdb.user:'+ user.user,
name : user.user,
salt : salt,
password : crypto.createHash('sha1').update(user.password.trim()+salt).digest('hex'),
email : user.email,
roles : [],
type : 'user'
};
var db = c.database('_users');
db.save(toSave, function(err,resp){
if (err && err.error !== 'conflict') return cb(err);
else if (err) console.log('\nI can\'t update your username already exists'); return cb();
console.log('User saved...');
return cb();
});
})(function(cb){
return cb();
})()
}
})
})
(function (cb) {
console.log('\nNow run `node server`\n\n:: Done :: ');
})();
}

function installViews(data,cb){


var url = data.db.host
if (!~url.indexOf('://')) url = 'http://' + url;
var c = new(cradle.Connection)(url,data.db.host,{
auth: { username: admin.user, password: admin.pass }
});
var db = c.database('couchpress');
function seed(){
db.save('_design/couchpress', {
views: {
all: {
map: 'function(doc) {\n emit(doc.created_at, doc);\n}\n'
}
}
}, function(error,res){
if (error) return cb(error);
console.log('\n Database and views saved...');
return cb();
});
}
db.exists(function (err, exists) {
if (err) {
return cb(new Error(err));
} else if (exists) {
seed();
} else {
db.create();
setTimeout(seed , 500);
}
});
}

function promiseChain (cb) {
var steps = []
, vals = []
, context = this
function go () {
var step = steps.shift()
if (!step) return cb()
try { step[0].apply(context, step[1]) }
catch (ex) { cb(ex) }
}
return function pc (fn, args, success) {
if (arguments.length === 0) return go()
// add the step
steps.push
( [ fn
, (args || []).concat([ function (er) {
if (er) return cb(er)
var a = Array.prototype.slice.call(arguments, 1)
try { success && success.apply(context, a) }
catch (ex) { return cb(ex) }
go()
}])
]
)
return pc
}
}
console.log("\n\
_____ _ \n\
| |___ _ _ ___| |_ ___ ___ ___ ___ ___ \n\
| --| . | | | _| | . | _| -_|_ -|_ -|\n\
|_____|___|___|___|_|_| _|_| |___|___|___|\n\
|_| \n\
")
init(function(err){
console.log(err)
})

0 comments on commit 5c24313

Please sign in to comment.