Skip to content

Instantly share code, notes, and snippets.

@timboslice69
Last active July 29, 2016 08:27
Show Gist options
  • Save timboslice69/243759e3848502db2f80fad43df6ed2c to your computer and use it in GitHub Desktop.
Save timboslice69/243759e3848502db2f80fad43df6ed2c to your computer and use it in GitHub Desktop.
A shell script to create a basic KeystoneJS installation
#!/usr/bin/env bash
echo "******************************"
echo "Setting up Keystone"
echo "******************************"
echo "*** Define Project"
read -p "Name your project (lowercase, default is 'keystone_cms'): " projectName
projectName=${projectName:-"keystone_cms"}
#echo "Project Name is $projectName"
read -p "Project Path (default is './$projectName'): " projectPath
projectPath=${projectPath:-"./$projectName"}
#echo "Project Path is $projectPath"
if [ -d "$projectPath" ]; then
echo "*** ERROR: Project path already exists"
exit 1
fi
read -p "MongoDB URI (default is 'mongodb://localhost/$projectName'): " mongoURI
mongoURI=${mongoURI:-"mongodb://localhost/$projectName"}
#echo "MongoDB URI is $mongoURI"
mkdir "$projectPath"
cd "$projectPath"
echo "*** Define Project...done"
# *****************************
# Setup Node Package Manager
# *****************************
echo "*** NPM Configuration"
cat > package.json <<EOF
{
"name": "$projectName",
"version": "1.0.0",
"description": "",
"main": "web.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "web.js"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}
EOF
npm install --save keystone
npm install --save underscore
npm install --save handlebars
echo "*** NPM Configuration...done"
# *****************************
# Setup Keystone
# *****************************
echo "*** Setup Keystone"
cat > web.js <<EOF
var keystone = require('keystone');
keystone.init({
'name': '$projectName',
'favicon': 'public/favicon.ico',
'static': ['public'],
'views': 'templates/views',
'view engine': 'handlebars',
'auto update': true,
'mongo': '$mongoURI',
'session': true,
'auth': true,
'user model': 'User',
'cookie secret': '(your secret here)',
'compress': true
});
require('./models');
keystone.set('routes', require('./routes'));
keystone.start();
EOF
echo "*** Setup Keystone...done"
# ******************************
# Setup Libraries
# ******************************
echo "*** Setup Keystone Libraries"
mkdir lib
echo "*** Setup Keystone Libraries...done"
# ******************************
# Setup Models
# ******************************
echo "*** Setup Keystone Models"
mkdir models
cat > models/index.js <<EOF
require('./users.js');
EOF
cat > models/users.js <<EOF
var keystone = require('keystone'),
Types = keystone.Field.Types;
var User = new keystone.List('User');
User.add({
name: { type: Types.Name, required: true, index: true },
email: { type: Types.Email, initial: true, required: true, index: true },
password: { type: Types.Password, initial: true },
canAccessKeystone: { type: Boolean, initial: true }
});
User.register();
EOF
echo "*** Setup Keystone Models...done"
# *****************************
# Setup Routes
# *****************************
echo "*** Setup Keystone Routes"
mkdir routes
cat > routes/index.js <<EOF
var keystone = require('keystone'),
middleware = require('./middleware'),
importRoutes = keystone.importer(__dirname);
// Common Middleware
keystone.pre('routes', middleware.initErrorHandlers);
keystone.pre('routes', middleware.initLocals);
keystone.pre('render', middleware.flashMessages);
// Handle 404 errors
keystone.set('404', function(req, res, next) {
res.notfound();
});
// Handle other errors
keystone.set('500', function(err, req, res, next) {
var title, message;
if (err instanceof Error) {
message = err.message;
err = err.stack;
}
res.err(err, title, message);
});
// Load Routes
var routes = {
views: importRoutes('./views')
};
// Bind Routes
exports = module.exports = function(app) {
app.get('/', routes.views.index);
}
User.register();
EOF
touch routes/middleware.js
echo "*** Setup Keystone Routes...done"
# ***************************
# Setup Views
# ***************************
echo "*** Setup Keystone Views"
mkdir routes/views
cat > routes/views/index.js <<EOF
var keystone = require('keystone');
exports = module.exports = function(req, res) {
var view = new keystone.View(req, res);
view.render('index');
}
EOF
echo "*** Setup Keystone Views...done"
# ******************************
# Setup Templates
# ******************************
echo "*** Setup Keystone Templates"
mkdir templates
mkdir templates/layouts
mkdir templates/views
cat > templates/layouts/default.hbs <<EOF
<html>
<head>
</head>
<body>
{{body}}
</body>
</html>
EOF
cat > templates/views/index.hbs <<EOF
extends ../layouts/default
<h1>$projectName</h1>
EOF
echo "*** Setup Keystone Templates...done"
echo "******************************"
echo "... Finished"
echo "******************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment