Skip to content

Commit

Permalink
Merge branch 'release-v0.12.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sintaxi committed Mar 27, 2014
2 parents a6f9b54 + 880c6c1 commit 57411b4
Show file tree
Hide file tree
Showing 36 changed files with 435 additions and 104 deletions.
10 changes: 7 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var fs = require('fs')
var path = require('path')
var mime = require('mime')
var polymer = require("terraform")
var terraform = require('terraform')
var fse = require('fs-extra')

var envy = require('envy-json')

/**
*
Expand Down Expand Up @@ -157,13 +157,17 @@ exports.setup = function(projectPath, env){
e.filename = configPath
e.stack = contents
e.lineno = -1
throw new polymer.helpers.TerraformError(e)
throw new terraform.helpers.TerraformError(e)
}

if(!cfg.hasOwnProperty('globals')) cfg['globals'] = {}

cfg.globals.environment = process.env.NODE_ENV || env

// replace values that look like environment variables
// e.g. '$foo' -> process.env.foo
cfg = envy(cfg)

return {
projectPath : projectPath,
publicPath : publicPath,
Expand Down
42 changes: 20 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/**
* TODO: Remove `mkdirp`. Use `fs-extra` instead.
*/

var path = require("path")
var fs = require("fs-extra")
var pkg = require('../package.json')
var polymer = require('terraform')
var helpers = require("./helpers")
var async = require("async")
var connect = require("connect")
var middleware = require("./middleware")
var path = require('path')
var fs = require('fs-extra')
var terraform = require('terraform')
var async = require('async')
var connect = require('connect')
var mime = require('mime')
var helpers = require('./helpers')
var middleware = require('./middleware')
var pkg = require('../package.json')


/**
Expand All @@ -31,7 +28,7 @@ exports.server = function(dirPath, options, callback){
middleware.poly,
middleware.process,
middleware.fallback
).listen(options.port || 9966, callback)
).listen(options.port || 9000, callback)
}


Expand All @@ -55,7 +52,7 @@ exports.multihost = function(dirPath, options, callback){
middleware.poly,
middleware.process,
middleware.fallback
).listen(options.port || 9966, callback)
).listen(options.port || 9000, callback)
}

/**
Expand Down Expand Up @@ -108,21 +105,22 @@ exports.mount = function(mountPoint, root){
*/

exports.pipeline = function(root){
console.log("Depricated, please use MOUNT instead, this will be removed in a future version.");
var publicPath = path.resolve(root)
var poly = polymer.root(publicPath)
var terra = terraform.root(publicPath)

return function(req, rsp, next){
var normalizedPath = helpers.normalizeUrl(req.url)
var priorityList = polymer.helpers.buildPriorityList(normalizedPath)
var sourceFile = polymer.helpers.findFirstFile(publicPath, priorityList)
var priorityList = terraform.helpers.buildPriorityList(normalizedPath)
var sourceFile = terraform.helpers.findFirstFile(publicPath, priorityList)

if(!sourceFile) return next()

poly.render(sourceFile, function(error, body){
terra.render(sourceFile, function(error, body){
if(error) return next(error)
if(!body) return next() // 404

var outputType = polymer.helpers.outputType(sourceFile)
var outputType = terraform.helpers.outputType(sourceFile)
var mimeType = helpers.mimeType(outputType)
var charset = mime.charsets.lookup(mimeType)
rsp.statusCode = 200
Expand Down Expand Up @@ -168,7 +166,7 @@ exports.compile = function(projectPath, outputPath, callback){
try{
outputPath = path.resolve(projectPath, outputPath)
var setup = helpers.setup(projectPath, "production")
var poly = polymer.root(setup.publicPath, setup.config.globals)
var terra = terraform.root(setup.publicPath, setup.config.globals)
}catch(err){
return callback(err)
}
Expand All @@ -194,12 +192,12 @@ exports.compile = function(projectPath, outputPath, callback){
*/

var compileFile = function(file, done){
poly.render(file, function(error, body){
terra.render(file, function(error, body){
if(error){
done(error)
}else{
if(body){
var dest = path.resolve(outputPath, polymer.helpers.outputPath(file))
var dest = path.resolve(outputPath, terraform.helpers.outputPath(file))
fs.mkdirp(path.dirname(dest), function(err){
fs.writeFile(dest, body, done)
})
Expand All @@ -219,7 +217,7 @@ exports.compile = function(projectPath, outputPath, callback){
*/
var copyFile = function(file, done){
var ext = path.extname(file)
if(!polymer.helpers.shouldIgnore(file) && [".jade", ".ejs", ".md", ".styl", ".less", ".scss", ".coffee"].indexOf(ext) === -1){
if(!terraform.helpers.shouldIgnore(file) && [".jade", ".ejs", ".md", ".styl", ".less", ".scss", ".coffee"].indexOf(ext) === -1){
var localPath = path.resolve(outputPath, file)
fs.mkdirp(path.dirname(localPath), function(err){
fs.copy(path.resolve(setup.publicPath, file), localPath, done)
Expand Down
75 changes: 45 additions & 30 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var path = require("path")
var fs = require("fs")
var helpers = require("./helpers")
var mime = require("mime")
var polymer = require('terraform')
var path = require('path')
var fs = require('fs')
var helpers = require('./helpers')
var mime = require('mime')
var terraform = require('terraform')
var pkg = require('../package.json')
var skin = require('./skin')
var connect = require('connect')
Expand Down Expand Up @@ -40,21 +40,30 @@ exports.notMultihostURL = function(req, rsp, next){
}
}

var reservedDomains = ["harp.io", "harpdev.io", "harpapp.io"];
exports.index = function(dirPath){
return function(req, rsp, next){
var host = req.headers.host;
var hostname = host.split(':')[0];
var arr = hostname.split(".");
var port = host.split(':')[1] ? ':' + host.split(':')[1] : '';
var poly = polymer.root(__dirname + "/templates");
var poly = terraform.root(__dirname + "/templates");

if(arr.length == 2){
fs.readdir(dirPath, function(err, files){
var projects = [];

files.forEach(function(file){
var local = file.split('.');
var appPart = local.shift();

var appPart = local.join("_");

if (local.length > 2) {
var domain = local.slice(Math.max(local.length - 2, 1)).join(".");
if (reservedDomains.indexOf(domain) != -1) {
appPart = local[0];
}
}

// DOT files are ignored.
if (file[0] !== ".") {
Expand All @@ -69,12 +78,10 @@ exports.index = function(dirPath){
poly.render("index.jade", { pkg: pkg, projects: projects, layout: "_layout.jade" }, function(error, body){
rsp.end(body)
});

})
} else {
next();
}

}
}

Expand All @@ -86,10 +93,19 @@ exports.hostProjectFinder = function(dirPath){

fs.readdir(dirPath, function(err, files){
var appPart = hostname.split(".")[0];

files.forEach(function(file){
var fp = file.split('.');
if (appPart == fp[0]) {
var filePart;
// Check against Reserved Domains first.
if (fp.length > 2) {
var domain = fp.slice(Math.max(fp.length - 2, 1)).join(".");
if (reservedDomains.indexOf(domain) != -1) {
fp = fp.slice(0, Math.max(fp.length - 2))
}
}

filePart = fp.join("_");
if (appPart == filePart) {
matches.push(file);
}
});
Expand All @@ -112,7 +128,6 @@ exports.regProjectFinder = function(projectPath){
}
}


/**
* Fallbacks
*
Expand Down Expand Up @@ -171,8 +186,8 @@ var custom200static = function(req, rsp, next){

var custom200dynamic = function(req, rsp, next){
skin(req, rsp, [poly], function(){
var priorityList = polymer.helpers.buildPriorityList("200.html")
var sourceFile = polymer.helpers.findFirstFile(req.setup.publicPath, priorityList)
var priorityList = terraform.helpers.buildPriorityList("200.html")
var sourceFile = terraform.helpers.findFirstFile(req.setup.publicPath, priorityList)
if(!sourceFile) return next()

req.poly.render(sourceFile, function(error, body){
Expand Down Expand Up @@ -231,8 +246,8 @@ var custom404static = function(req, rsp, next){

var custom404dynamic = function(req, rsp, next){
skin(req, rsp, [poly], function(){
var priorityList = polymer.helpers.buildPriorityList("404.html")
var sourceFile = polymer.helpers.findFirstFile(req.setup.publicPath, priorityList)
var priorityList = terraform.helpers.buildPriorityList("404.html")
var sourceFile = terraform.helpers.findFirstFile(req.setup.publicPath, priorityList)
if(!sourceFile) return next()

req.poly.render(sourceFile, function(error, body){
Expand Down Expand Up @@ -267,7 +282,7 @@ var default404 = function(req, rsp, next){
name: "Page Not Found",
pkg: pkg
}
polymer.root(__dirname + "/templates").render("404.jade", locals, function(err, body){
terraform.root(__dirname + "/templates").render("404.jade", locals, function(err, body){
var type = helpers.mimeType("html")
var charset = mime.charsets.lookup(type)
rsp.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''));
Expand All @@ -285,7 +300,7 @@ var default404 = function(req, rsp, next){
*
*/
exports.underscore = function(req, rsp, next){
if(polymer.helpers.shouldIgnore(req.url)){
if(terraform.helpers.shouldIgnore(req.url)){
notFound(req, rsp, next)
}else{
next()
Expand All @@ -301,7 +316,7 @@ exports.underscore = function(req, rsp, next){
exports.mwl = function(req, rsp, next){
var ext = path.extname(req.url).replace(/^\./, '')

if(polymer.helpers.processors["html"].indexOf(ext) !== -1 || polymer.helpers.processors["css"].indexOf(ext) !== -1 || polymer.helpers.processors["js"].indexOf(ext) !== -1){
if(terraform.helpers.processors["html"].indexOf(ext) !== -1 || terraform.helpers.processors["css"].indexOf(ext) !== -1 || terraform.helpers.processors["js"].indexOf(ext) !== -1){
notFound(req, rsp, next)
}else{
next()
Expand Down Expand Up @@ -364,7 +379,7 @@ exports.static = function(req, res, next) {
*/

exports.setup = function(req, rsp, next){
if(req.hasOwnProperty('setup')) next()
if(req.hasOwnProperty('setup')) return next()

try{
req.setup = helpers.setup(req.projectPath)
Expand All @@ -377,7 +392,7 @@ exports.setup = function(req, rsp, next){
pkg: pkg
}

return polymer.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
return terraform.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
rsp.statusCode = 500
rsp.end(body)
})
Expand Down Expand Up @@ -422,15 +437,15 @@ var poly = exports.poly = function(req, rsp, next){
if(req.hasOwnProperty("poly")) return next()

try{
req.poly = polymer.root(req.setup.publicPath, req.setup.config.globals)
req.poly = terraform.root(req.setup.publicPath, req.setup.config.globals)
}catch(error){
error.stack = helpers.stacktrace(error.stack, { lineno: error.lineno })
var locals = {
project: req.headers.host,
error: error,
pkg: pkg
}
return polymer.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
return terraform.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
rsp.statusCode = 500
rsp.end(body)
})
Expand All @@ -445,8 +460,8 @@ var poly = exports.poly = function(req, rsp, next){

exports.process = function(req, rsp, next){
var normalizedPath = helpers.normalizeUrl(req.url)
var priorityList = polymer.helpers.buildPriorityList(normalizedPath)
var sourceFile = polymer.helpers.findFirstFile(req.setup.publicPath, priorityList)
var priorityList = terraform.helpers.buildPriorityList(normalizedPath)
var sourceFile = terraform.helpers.findFirstFile(req.setup.publicPath, priorityList)

/**
* We GTFO if we don't have a source file.
Expand All @@ -456,7 +471,7 @@ exports.process = function(req, rsp, next){


/**
* Now we let Polymer handle the asset pipeline.
* Now we let terraform handle the asset pipeline.
*/

req.poly.render(sourceFile, function(error, body){
Expand All @@ -468,8 +483,8 @@ exports.process = function(req, rsp, next){
error: error,
pkg: pkg
}
if(polymer.helpers.outputType(sourceFile) == 'css'){
var outputType = polymer.helpers.outputType(sourceFile)
if(terraform.helpers.outputType(sourceFile) == 'css'){
var outputType = terraform.helpers.outputType(sourceFile)
var mimeType = helpers.mimeType(outputType)
var charset = mime.charsets.lookup(mimeType)
var body = helpers.cssError(locals)
Expand All @@ -486,7 +501,7 @@ exports.process = function(req, rsp, next){
// var loc = loc.join(path.sep) + path.sep
// if(error.filename) error.filename = error.filename.replace(loc, "")

polymer.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
terraform.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
var mimeType = helpers.mimeType('html')
var charset = mime.charsets.lookup(mimeType)
rsp.statusCode = 500
Expand All @@ -499,7 +514,7 @@ exports.process = function(req, rsp, next){
// 404
if(!body) return next()

var outputType = polymer.helpers.outputType(sourceFile)
var outputType = terraform.helpers.outputType(sourceFile)
var mimeType = helpers.mimeType(outputType)
var charset = mime.charsets.lookup(mimeType)
rsp.statusCode = 200
Expand Down
Loading

0 comments on commit 57411b4

Please sign in to comment.