Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 309 lines (296 sloc) 9.75 KB
#!/usr/bin/env node
//CLI
var web = require('webjs');
var program = require('commander');
var mkdirp = require('mkdirp');
var fs = require('fs');
var asynclist = require('asynclist');
program
.version(web.version)
.usage('[option] <value..>')
.option('-c, --cookie', 'Add cookie support')
.option('-s, --session', 'Add session support')
.option('-b, --body', 'Add body parser support')
.option('--compress', 'Add compress support')
.option('--compiler <engine>', 'Add compiler support', list)
.option('-r, --rest', 'Add more HTTP methods support')
.option('--mustache', 'Add mustache template engine support (defaults to jade)')
.option('--ejs', 'Add ejs template engine support (defaults to jade)')
.option('--tmpl <engine>', 'Set the template engine')
// TODOS
// .option('--seajs', 'Add SeaJS support')
// .option('--requirejs', 'Add RequireJS support')
.option('--mode <mode>', 'Set the mode (defaults to production)')
.option('--port <port>', 'Set the port (defaults to 80)')
.option('-a --all', 'Enable all functions')
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ webjs');
console.log(' $ webjs init -s -c -b -r --mode pro --port 8888 --compress --compiler less');
console.log(' $ webjs init --all');
console.log(' $ webjs run 8080');
console.log('');
});
var path = program.args.shift() || process.cwd();
program
.command('run [port]')
.description('run a simple http server here')
.action(function (port) {
port = port || 8080;
web.run(port)
.use(web.static(path));
console.log([
'webjs Simple HTTP Server is running in current directory.',
'Please visit http://127.0.0.1:' + port + '.'
].join('\r\n'));
});
program
.command('init')
.description('Initialize a webjs app here')
.action(function () {
program.inite = true;
mkdir(path, function(){
//Public
var paths = [
path + '/static',
path + '/controllers',
path + '/models',
path + '/views',
path + '/static/js',
path + '/static/css'
];
var list = paths.map(function (_path) {
return function () {
mkdir(_path, function () {
tasks.trigger(_path);
});
};
});
var tasks = new asynclist(list);
tasks.assign(function () {
var files = [
[path + '/static/css/style.css' , css],
[path + '/static/css/style.less', less],
[path + '/static/index.html' , index_html],
[path + '/controllers/index.js' , index],
[path + '/controllers/router.js', router],
[path + '/views/hello.jade' , hello],
[path + '/views/layout.jade' , layout],
[path + '/server.js' , server]
];
var _list = files.map(function (file) {
return function () {
write(file[0], file[1], function () {
filesTasks.trigger(file);
});
};
});
var filesTasks = new asynclist(_list);
filesTasks.assign(function () {
console.log('Enjoy!');
if (program.compiler || program.all)
console.log('Please run \'npm install less sass\' to install the compilers.');
process.exit(1);
});
filesTasks.run();
});
tasks.run();
});
});
program.parse(process.argv);
program.template = 'jade';
if (program.mustache) program.template = 'mustache';
if (program.ejs) program.template = 'ejs';
program.mode = program.mode || 'pro';
if (!program.port) program.port = '';
var index_html = [
'<!DOCTYPE html>',
'<html>',
' <head>',
' <title>webjs Quick Start</title>',
' <link rel=\'stylesheet\' href=\'/css/style.' + (program.compiler || program.all ? 'less' : 'css') + '\' />',
' </head>',
' <body>',
' <header>',
' <h1>Thanks for using webjs</h1>',
' </header>',
' <section>',
' <article>',
' <p>Let\'s get start with webjs.</p> <button class="red"><a href="/hello" target="_top" class="label">Example</a></button>',
' </article>',
' </section>',
' </body>',
'</html>'
].join('\r\n');
var css = [
'body {',
' padding: 50px;',
' font: 30px Yanone Kaffeesatz, Lucida Grande, Mirosoft Yahei, Helvetica, Arial, sans-serif;',
'}',
'.red {',
' border: 1px solid #c51236;',
' background: #dc143c;',
' background: -webkit-linear-gradient(top, #eb1f48, #dc143c);',
' background: -moz-linear-gradient(top, #eb1f48, #dc143c);',
' background: -ms-linear-gradient(top, #eb1f48, #dc143c);',
' background: -o-linear-gradient(top, #eb1f48, #dc143c);',
' -webkit-transition: all .20s;',
' -moz-transition: all .20s;',
' -o-transition: all .20s;',
' transition: all .20s;',
' cursor: pointer;',
' margin: 5px;',
' padding: 5px 6px 4px 6px;',
' -webkit-border-radius: 2px;',
' -moz-border-radius: 2px;',
' border-radius: 2px;',
'}',
'.red:hover {',
' border: 1px solid #b71132;',
' background: #c51236;',
' background: -webkit-linear-gradient(top, #eb1f48, #ce1338);',
' background: -moz-linear-gradient(top, #eb1f48, #ce1338);',
' background: -ms-linear-gradient(top, #eb1f48, #ce1338);',
' background: -o-linear-gradient(top, #eb1f48, #ce1338);',
'}',
'.red .label {',
' font-weight: bold;',
' padding: 0 17px;',
' color: #FFF;',
' text-shadow: 0 1px 0 #ad102f;',
' text-decoration: none;',
' line-height: 15px;',
' display: inline-block;',
' height: 18px;',
' overflow: hidden;',
' -webkit-transition: color .20s;',
' -moz-transition: color .20s;',
' -o-transition: color .20s;',
' transition: color .20s;',
'}'
].join('\r\n');
var less = [
'body {',
' padding: 50px;',
' font: 30px Yanone Kaffeesatz, Lucida Grande, Mirosoft Yahei, Helvetica, Arial, sans-serif;',
'}',
'.jazz_button (@color: #F1F1F1) {',
' @light: lighten(@color, 5%);',
' @dark: darken(@color, 5%);',
' @text_shadow: darken(@color, 10%);',
' border: 1px solid @dark;',
' background: @color;',
' background: -webkit-linear-gradient(top, @light, @color);',
' background: -moz-linear-gradient(top, @light, @color);',
' background: -ms-linear-gradient(top, @light, @color);',
' background: -o-linear-gradient(top, @light, @color);',
' -webkit-transition: all .20s;',
' -moz-transition: all .20s;',
' -o-transition: all .20s;',
' transition: all .20s;',
' cursor: pointer;',
' margin: 5px;',
' padding: 5px 6px 4px 6px;',
' -webkit-border-radius: 2px;',
' -moz-border-radius: 2px;',
' border-radius: 2px;',
' &:hover {',
' border: 1px solid darken(@dark, 3%);',
' background: darken(@color, 5%);',
' background: -webkit-linear-gradient(top, @light, darken(@color, 3%));',
' background: -moz-linear-gradient(top, @light, darken(@color, 3%));',
' background: -ms-linear-gradient(top, @light, darken(@color, 3%));',
' background: -o-linear-gradient(top, @light, darken(@color, 3%));',
' }',
' .label {',
' font-weight: bold;',
' padding: 0 17px;',
' color: #FFF;',
' text-shadow: 0 1px 0 @text_shadow;',
' text-decoration: none;',
' line-height: 15px;',
' display: inline-block;',
' height: 18px;',
' overflow: hidden;',
' -webkit-transition: color .20s;',
' -moz-transition: color .20s;',
' -o-transition: color .20s;',
' transition: color .20s;',
' }',
'}',
'.red {',
' .jazz_button(crimson);',
'}'
].join('\r\n');
var index = [
'var web = require(\'webjs\');',
'',
'web.run(' + program.port + ')',
' .config({',
' \'view engine\': \'' + program.template + '\',',
' \'views\': __dirname + \'/../views\',',
(program.rest || program.all ? ' \'readonly\': false,' : ''),
(program.mode == 'dev' ? ' \'dbgPath\': __dirname + \'/..\',' : ''),
' \'mode\': \'' + program.mode + '\'',
' })',
' .use(',
(program.cookie || program.all ? ' web.cookieParser(\'webjs\'),' : ''),
(program.session || program.all ? ' web.session(),' : ''),
(program.body || program.all ? ' web.bodyParser(),' : ''),
(program.compiler || program.all ? ' web.compiler({ enable: ' + JSON.stringify((program.compiler ? program.compiler : ["less", "sass"])) + ' }),' : ''),
(program.compress || program.all ? ' web.compress(),' : ''),
' web.static(__dirname + \'/../static\')',
' )',
' .extend(__dirname + \'/router\');',
'',
'console.log(\'The app is running!\');'
].join('\r\n');
var router = [
'module.exports = function (web) {',
' web.get(\'/hello\', function (req, res) {',
(program.session || program.all ? ' if (isNaN(req.session.times))\r\n req.session.times = 0\r\n else\r\n req.session.times++;' : ''),
' res.render(\'hello\', {',
(program.session || program.all ? ' times: req.session.times,' : ''),
' title: \'webjs\'',
' });',
' });',
'};'
].join('\r\n');
var server = [
'require(__dirname + \'/controllers/\');'
].join('\r\n');
var hello = [
'h1 Hello World!',
'p Welcome to #{title}',
(program.session || program.all ? 'p You had refreshed #{times} times' : '')
].join('\r\n');
var layout = [
'html',
' head',
' title= title',
' link(type=\'text/css\', rel=\'stylesheet\', href=\'/css/style.' + (program.compiler || program.all ? 'less' : 'css') + '\')',
' != body'
].join('\r\n');
function emptyDirectory (path, fn) {
fs.readdir(path, function(err, files){
if (err && 'ENOENT' != err.code) throw err;
fn(!files || !files.length);
});
}
function write (path, str, fn) {
fs.writeFile(path, str, function () {
console.log(' \x1b[36mcreate\x1b[0m : ' + path);
fn();
});
}
function mkdir (path, fn) {
mkdirp(path, 0755, function(err){
if (err) throw err;
console.log(' \033[36mcreate\033[0m : ' + path);
fn && fn();
});
}
function list (val) {
return val.split(',');
}
Something went wrong with that request. Please try again.