Permalink
Browse files

gen and server working

  • Loading branch information...
1 parent aa95654 commit 764e03d546d84045a84d902a2f74c2e8c03e2992 @twilson63 committed Feb 19, 2012
Showing with 387 additions and 7 deletions.
  1. +2 −0 .npmignore
  2. +30 −0 Cakefile
  3. +7 −0 bin/bam
  4. +71 −0 lib/gen.js
  5. +37 −0 lib/index.js
  6. +83 −0 lib/new.js
  7. +52 −0 lib/run.js
  8. +24 −0 lib/serve.js
  9. +7 −3 package.json
  10. +5 −0 readme.md
  11. +51 −2 src/gen.coffee
  12. +3 −1 src/index.coffee
  13. +2 −1 src/run.coffee
  14. +13 −0 src/serve.coffee
View
@@ -0,0 +1,2 @@
+src
+node_modules
View
@@ -0,0 +1,30 @@
+fs = require 'fs'
+{print} = require 'util'
+{spawn, exec} = require 'child_process'
+
+# ANSI Terminal Colors
+bold = '\033[0;1m'
+green = '\033[0;32m'
+reset = '\033[0m'
+red = '\033[0;31m'
+
+log = (message, color, explanation) ->
+ console.log color + message + reset + ' ' + (explanation or '')
+
+build = (watch, callback) ->
+ if typeof watch is 'function'
+ callback = watch
+ watch = false
+ options = ['-b','-c', '-o', 'lib', 'src']
+ options.unshift '-w' if watch
+
+ coffee = spawn 'coffee', options
+ coffee.stdout.on 'data', (data) -> print data.toString()
+ coffee.stderr.on 'data', (data) -> log data.toString(), red
+ coffee.on 'exit', (status) -> callback?() if status is 0
+
+task 'build', ->
+ build -> log ":)", green
+
+task 'watch', ->
+ build true, -> log ":)", green
View
@@ -0,0 +1,7 @@
+#!/usr/bin/env node
+
+var path = require('path');
+var fs = require('fs');
+var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
+
+require(lib);
View
@@ -0,0 +1,71 @@
+var checkexists, eco, filed, fs, ghm, renderMarkdown, renderTemplate, wrench;
+
+fs = require('fs');
+
+eco = require('eco');
+
+ghm = require('github-flavored-markdown');
+
+filed = require('filed');
+
+wrench = require('wrench');
+
+renderMarkdown = function(name) {
+ var md;
+ md = fs.readFileSync("./pages" + name + ".md").toString();
+ return ghm.parse(md);
+};
+
+renderTemplate = function(body) {
+ var template;
+ if (body == null) body = "";
+ template = fs.readFileSync("./layout.html", "utf8");
+ return eco.render(template, {
+ body: body
+ });
+};
+
+checkexists = function(name, cb) {
+ return fs.stat(name, function(err, stat) {
+ return cb(err != null ? false : true);
+ });
+};
+
+module.exports = function(proj) {
+ var gen;
+ if (proj == null) proj = '.';
+ gen = "" + proj + "/gen";
+ return checkexists(gen, function(exists) {
+ var body, css, dir, image, images, javascripts, js, page, pages, stylesheets, _i, _j, _k, _l, _len, _len2, _len3, _len4, _len5, _m, _ref;
+ if (exists) wrench.rmdirSyncRecursive(gen);
+ fs.mkdirSync(gen);
+ pages = fs.readdirSync('./pages');
+ for (_i = 0, _len = pages.length; _i < _len; _i++) {
+ page = pages[_i];
+ body = renderMarkdown('/' + page.replace('.md', ''));
+ page = page.replace('.md', '.html');
+ fs.writeFileSync("" + gen + "/" + page, renderTemplate(body), 'utf8');
+ }
+ _ref = ['images', 'javascripts', 'stylesheets'];
+ for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
+ dir = _ref[_j];
+ fs.mkdirSync("" + gen + "/" + dir);
+ }
+ images = fs.readdirSync('./images');
+ for (_k = 0, _len3 = images.length; _k < _len3; _k++) {
+ image = images[_k];
+ filed("./images/" + image).pipe(filed("" + gen + "/images/" + image));
+ }
+ stylesheets = fs.readdirSync('./stylesheets');
+ for (_l = 0, _len4 = stylesheets.length; _l < _len4; _l++) {
+ css = stylesheets[_l];
+ filed("./stylesheets/" + css).pipe(filed("" + gen + "/stylesheets/" + css));
+ }
+ javascripts = fs.readdirSync('./javascripts');
+ for (_m = 0, _len5 = javascripts.length; _m < _len5; _m++) {
+ js = javascripts[_m];
+ filed("./javascripts/" + js).pipe(filed("" + gen + "/javascripts/" + js));
+ }
+ return console.log('Generated Static Site in the gen folder...');
+ });
+};
View
@@ -0,0 +1,37 @@
+var app, cmdGen, cmdNew, cmdRun, cmdServe, flatiron;
+
+cmdNew = require('./new');
+
+cmdRun = require('./run');
+
+cmdGen = require('./gen');
+
+cmdServe = require('./serve');
+
+flatiron = require('flatiron');
+
+require('pkginfo')(module);
+
+app = flatiron.app;
+
+app.version = exports.version;
+
+app.use(flatiron.plugins.cli, {
+ dir: __dirname,
+ usage: ["Bam 0.0.1", "Easiest Static Site Generator on the Planet", "", "bam new [foo]", "cd [foo]", "bam run foo", "bam gen foo"],
+ version: true
+});
+
+app.cmd('version', function() {
+ return console.log('BAM v' + app.version);
+});
+
+app.cmd('new', cmdNew);
+
+app.cmd('run', cmdRun);
+
+app.cmd('gen', cmdGen);
+
+app.cmd('serve', cmdServe);
+
+app.start();
View
@@ -0,0 +1,83 @@
+var buildFiles, buildFolders, copy, copyAssets, eco, fs, log, renderTemplate;
+
+eco = require('eco');
+
+fs = require('fs');
+
+log = console.log;
+
+renderTemplate = function(name, project, data) {
+ var template;
+ if (data == null) data = {};
+ template = fs.readFileSync(__dirname + ("/../templates/" + name + ".eco"), "utf8");
+ return fs.writeFileSync("./" + project + "/" + name, eco.render(template, data));
+};
+
+copy = function(src, dest) {
+ var destFile;
+ destFile = fs.createWriteStream(dest);
+ return fs.createReadStream(src).pipe(destFile);
+};
+
+buildFolders = function(proj, cb) {
+ var directory, _i, _len, _ref;
+ _ref = ["./" + proj, "./" + proj + "/pages", "./" + proj + "/stylesheets", "./" + proj + "/javascripts", "./" + proj + "/images"];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ directory = _ref[_i];
+ fs.mkdirSync(directory, 0755);
+ }
+ return cb();
+};
+
+buildFiles = function(proj, cb) {
+ var template, _i, _len, _ref;
+ _ref = ['layout.html', 'config.json', 'robots.txt', '404.html'];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ template = _ref[_i];
+ renderTemplate(template, proj, {
+ title: proj
+ });
+ }
+ return cb();
+};
+
+copyAssets = function(proj, cb) {
+ var css, dest, image, js, src, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3;
+ _ref = ['apple-touch-icon-114x114.png', 'apple-touch-icon-72x72.png', 'apple-touch-icon.png', 'favicon.ico'];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ image = _ref[_i];
+ src = [__dirname, '..', 'templates', 'images', image].join('/');
+ dest = [proj, 'images', image].join('/');
+ copy(src, dest);
+ }
+ _ref2 = ['tabs.js'];
+ for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
+ js = _ref2[_j];
+ src = [__dirname, '..', 'templates', 'javascripts', js].join('/');
+ dest = [proj, 'javascripts', js].join('/');
+ copy(src, dest);
+ }
+ _ref3 = ['base.css', 'layout.css', 'skeleton.css'];
+ for (_k = 0, _len3 = _ref3.length; _k < _len3; _k++) {
+ css = _ref3[_k];
+ src = [__dirname, '..', 'templates', 'stylesheets', css].join('/');
+ dest = [proj, 'stylesheets', css].join('/');
+ copy(src, dest);
+ }
+ return cb();
+};
+
+module.exports = function(proj) {
+ if (proj == null) proj = null;
+ if (proj == null) return console.log('Project Name Required!');
+ buildFolders(proj, function() {
+ return log('Building Folders...');
+ });
+ buildFiles(proj, function() {
+ return log('Creating Files...');
+ });
+ copyAssets(proj, function() {
+ return log('Creating Assets...');
+ });
+ return log('Done.');
+};
View
@@ -0,0 +1,52 @@
+var eco, filed, fs, ghm, http, log, renderMarkdown, renderTemplate, url;
+
+http = require('http');
+
+url = require('url');
+
+eco = require('eco');
+
+ghm = require('github-flavored-markdown');
+
+fs = require('fs');
+
+filed = require('filed');
+
+log = console.log;
+
+renderMarkdown = function(name) {
+ var md;
+ md = fs.readFileSync("./pages" + name + ".md").toString();
+ return ghm.parse(md);
+};
+
+renderTemplate = function(body) {
+ var template;
+ if (body == null) body = "";
+ template = fs.readFileSync("./layout.html", "utf8");
+ return eco.render(template, {
+ body: body
+ });
+};
+
+module.exports = function(proj) {
+ var server;
+ if (proj == null) proj = '.';
+ server = http.createServer(function(req, resp) {
+ var body, pathname;
+ pathname = url.parse(req.url).pathname;
+ if (pathname === '/') pathname = '/index.html';
+ if (pathname.match(/(stylesheets|images|javascripts)/)) {
+ return filed("." + pathname).pipe(resp);
+ } else {
+ pathname = pathname.replace('.html', '');
+ resp.writeHead(200, 'Content-Type: text/html');
+ body = renderMarkdown(pathname);
+ return resp.end(renderTemplate(body));
+ }
+ });
+ return server.listen(3000, function() {
+ log('Listening on 3000...');
+ return log('CTRL-C to exit..');
+ });
+};
View
@@ -0,0 +1,24 @@
+var filed, http, log, url;
+
+http = require('http');
+
+url = require('url');
+
+filed = require('filed');
+
+log = console.log;
+
+module.exports = function(proj) {
+ var server;
+ if (proj == null) proj = '.';
+ server = http.createServer(function(req, resp) {
+ var pathname;
+ pathname = url.parse(req.url).pathname;
+ if (pathname === '/') pathname = '/index.html';
+ return filed("" + proj + "/gen" + pathname).pipe(resp);
+ });
+ return server.listen(3000, function() {
+ log('Listening on 3000...');
+ return log('CTRL-C to exit..');
+ });
+};
View
@@ -1,8 +1,8 @@
{
"author": "Tom Wilson <tom@beautifulnode.com> (http://www.twilson63.com)",
"name": "bam",
- "description": "Easiest Static Site Generator on the Planet!",
- "version": "0.0.1",
+ "description": "The Easiest Static Site Generator on the Planet!",
+ "version": "0.1.0",
"homepage": "http://github.com/beautifulnode/bam",
"repository": {
"type": "git",
@@ -19,9 +19,13 @@
"pkginfo": "*",
"eco": "*",
"github-flavored-markdown": "*",
- "filed": "*"
+ "filed": "*",
+ "wrench": "*"
},
"devDependencies": {
+ },
+ "bin": {
+ "bam": "./bin/bam"
}
}
View
@@ -30,6 +30,11 @@ bam run
``` sh
bam gen
```
+## Test Generated site
+
+``` sh
+bam serve
+```
## More Info
Oops, something went wrong.

0 comments on commit 764e03d

Please sign in to comment.