Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored the punch commands

  • Loading branch information...
commit 3c7593251c436ff6954ead65f6ba7dd2b9d1c129 1 parent 9846dd9
@laktek authored
View
83 bin/punch
@@ -1,84 +1,7 @@
#!/usr/bin/env node
+var path = require("path");
-var path = require('path');
-var fs = require('fs');
-
-var fs = require("fs");
-var generator = require(path.join(__dirname,'../lib/generator.js'));
-var server = require(path.join(__dirname,'../lib/server.js'));
+var cli = require(path.join(__dirname,'../lib/cli.js'));
var args = process.argv.slice(2);
-
-if(args[0] === "setup"){
- // setup site
- fs.mkdir("templates", function(err){
- if(!err)
- console.log("Created templates directory.");
- });
-
- fs.mkdir("contents", function(err){
- if(!err)
- console.log("Created contents directory.");
- });
-
- var config_file = '{ \
- "template_dir": "templates", \
- "content_dir": "contents", \
- "output_dir": "public" \
- }';
-
- fs.writeFile('config.json', config_file, function (err) {
- if(!err)
- console.log("Created the config.json");
- });
-
-}
-else if(args[0] == "server") {
- var config_file = "config.json";
-
- fs.readFile(config_file, function(err, data){
-
- // if there's an error we assume the config doesn't exist.
- if(err){
- var supplied_config = {};
- } else {
- var supplied_config = JSON.parse(data);
- }
-
- if(parseInt(args[1]) > 0){
- supplied_config["server"]["port"] = parseInt(args[1]);
- }
-
- supplied_config.on_start = function(callback){
- console.log("Generating the site...");
- callback();
- }
-
- // start server
- server.startServer(supplied_config);
-
- });
-} else {
- var config_file = args[0] || "config.json";
-
- fs.readFile(config_file, function(err, data){
-
- // if there's an error we assume the config doesn't exist.
- if(err){
- var supplied_config = {};
- } else {
- var supplied_config = JSON.parse(data);
- }
-
- supplied_config.on_start = function(callback){
- console.log("Generating the site...");
- callback();
- }
-
- // call generate with loaded config
- generator.generate(supplied_config);
-
- });
-}
-
-
+cli.init(args);
View
130 lib/cli.js
@@ -0,0 +1,130 @@
+var path = require('path');
+var fs = require('fs');
+var _ = require("underscore");
+
+var generator = require(path.join(__dirname,'../lib/generator.js'));
+var server = require(path.join(__dirname,'../lib/server.js'));
+
+var get_config = function(config_file, callback){
+ fs.readFile(config_file, function(err, data){
+
+ // if there's an error we assume the config doesn't exist.
+ if(err){
+ var supplied_config = {};
+ } else {
+ var supplied_config = JSON.parse(data);
+ }
+
+ callback(supplied_config);
+ });
+}
+
+module.exports = {
+
+ setup: function(){
+
+ // setup site
+ fs.mkdir("templates", function(err){
+ if(!err)
+ console.log("Created templates directory.");
+ });
+
+ fs.mkdir("contents", function(err){
+ if(!err)
+ console.log("Created contents directory.");
+ });
+
+ var config_file = '{ \
+ "template_dir": "templates", \
+ "content_dir": "contents", \
+ "output_dir": "public" \
+ }';
+
+ fs.writeFile('config.json', config_file, function (err) {
+ if(!err)
+ console.log("Created the config.json");
+ });
+
+ },
+
+ server: function(args){
+ var config_file = "config.json";
+
+ get_config(config_file, function(supplied_config){
+
+ if(parseInt(args[0]) > 0){
+ supplied_config["server"]["port"] = parseInt(args[1]);
+ }
+
+ supplied_config.on_start = function(callback){
+ console.log("Generating the site...");
+ callback();
+ }
+
+ // start server
+ server.startServer(supplied_config);
+
+ });
+
+ },
+
+ generate: function(args){
+ var config_file = args[0] || "config.json";
+
+ get_config(config_file, function(supplied_config){
+
+ var start_time = (new Date()).getTime();
+
+ supplied_config.on_start = function(callback){
+ console.log("Generating the site...");
+ callback();
+ }
+
+ supplied_config.on_complete = function(){
+ var time_elapsed = ((new Date()).getTime() - start_time)/1000;
+ console.log("Finished generation. (" + time_elapsed + " seconds)");
+ }
+
+ supplied_config.on_each = function(action, file){
+ var actions = {"render": "Rendered", "copy": "Copied"};
+ console.log(actions[action] + " " + file);
+ }
+
+ // call generate with loaded config
+ generator.generate(supplied_config);
+ });
+
+ },
+
+ help: function(){
+ console.log('Usage: punch COMMAND \[ARGS]\n');
+ console.log('You can use following commands:');
+ console.log(' setup - create punch directory strucutre.');
+ console.log(' generate - generate HTML pages. (shorcut `punch g`)');
+ console.log(' server - starts the development server. (shortcut `punch s`)');
+ console.log(' help - show help. (shortcut `punch h`)\n');
+ console.log('For more information about Punch visit: http://laktek.github.com/punch');
+ },
+
+ init: function(args){
+
+ var commands = ["setup", "server", "generate", "help"];
+
+ var short_codes = { "s": "server", "g": "generate", "h": "help" };
+
+ var command = args.shift();
+
+ if(_.include(commands, command)){
+ return this[command](args);
+ } else if(_.include(_.keys(short_codes), command)){
+ return this[short_codes[command]](args);
+ } else {
+ return this["help"]();
+ }
+ }
+
+};
+
+
+
+
View
2  lib/generator.js
@@ -423,7 +423,7 @@ module.exports = {
self.config = _.extend(_.clone(default_config), supplied_config);
var on_start = (typeof self.config.on_start === "function") ? self.config.on_start : function(c){ c() };
- var on_complete = (typeof self.config.on_complete === "function") ? self.config.on_complete : function(c){ c() };
+ var on_complete = (typeof self.config.on_complete === "function") ? self.config.on_complete : function(){ };
var on_each = (typeof self.config.on_each === "function") ? self.config.on_each : function(){ };
// register renderers
View
2  lib/server.js
@@ -10,7 +10,7 @@ var mime = require("mime");
var _ = require("underscore");
var default_config = require("./default_config.js");
-var generator = require("./generator.js")
+var generator = require("./generator.js");
module.exports = {
View
2  sample/output/test.html5
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
- <title>Test Page | CurdBee</title>
+ <title> | CurdBee</title>
</head>
<body>
Please sign in to comment.
Something went wrong with that request. Please try again.