diff --git a/README.md b/README.md index d1057940f..e32dda67a 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ ## Features -- pre-processorse +- pre-processors - layouts - partials -- metadata (via _data.json) +- metadata (via \_data.json/\_data.cson/\_data.hjson) - LRU cache (production mode) ### Supported Pre-Processors @@ -15,6 +15,7 @@ **HTML** – EJS, Jade, Markdown **CSS** – LESS, Stylus, Sass (SCSS) **JavaScript** – CoffeeScript +**JSON** - CSON, Hjson ## Install diff --git a/lib/helpers/memoized.js b/lib/helpers/memoized.js index 5b79ebafe..73ab01eb1 100644 --- a/lib/helpers/memoized.js +++ b/lib/helpers/memoized.js @@ -32,4 +32,5 @@ exports.walkData = helpers.walkData exports.isTemplate = helpers.isTemplate exports.isStylesheet = helpers.isStylesheet exports.isJavaScript = helpers.isJavaScript - +exports.needsBrowserify = helpers.needsBrowserify +exports.isJSON = helpers.isJSON diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index 5a0bd006b..681384c27 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -1,6 +1,8 @@ var path = require('path') var fs = require('fs') var TerraformError = exports.TerraformError = require("../error").TerraformError +var HJSON = require('hjson') +var CSON = require('cson') /** @@ -15,7 +17,8 @@ var TerraformError = exports.TerraformError = require("../error").TerraformError var processors = exports.processors = { "html": ["jade", "ejs", "md"], "css" : ["styl", "less", "scss", "sass"], - "js" : ["coffee"] + "js" : ["coffee", "es"], + "json": ["cson", "hjson"] } @@ -62,6 +65,13 @@ var buildPriorityList = exports.buildPriorityList = function(filePath){ if(processor){ + if (ext === 'json') { + // foo.json => foo.json.jade + processors['html'].forEach(function(p){ + list.push(filePath + '.' + p) + }) + } + // foo.html => foo.jade processor.forEach(function(p){ var regexp = new RegExp(ext + '$') @@ -203,7 +213,9 @@ var isEmpty = function(obj) { */ var dataTree = exports.dataTree = function (filename) { - var dirPath = path.resolve(filename) + var i, file, files, exception, e, format, dataPath, fileData, data + var dirPath = path.resolve(filename) + try{ var list = fs.readdirSync(dirPath) }catch(e){ @@ -218,25 +230,60 @@ var dataTree = exports.dataTree = function (filename) { var obj = {} obj._contents = [] - try{ - var dataPath = path.resolve(dirPath, "_data.json") - var fileData = fs.readFileSync(dataPath) - obj._data = JSON.parse(fileData) - }catch(e){ - if(e.code && e.code === "ENOENT"){ - // data file failed or does not exist - }else{ - e.source = "Data" - e.dest = "Globals" - e.lineno = -1 - e.filename = dataPath - e.stack = fileData.toString() - throw new TerraformError(e) + files = ["_data.json"] + + processors['json'].forEach(function(p) { + files.push("_data." + p) + }) + files.push("") + + exception = true + for (i = 0; i < files.length && exception; ++i) { + file = files[i] + exception = false + try { + if (file) { + format = path.extname(file).substring(1).toUpperCase() + dataPath = path.resolve(dirPath, file) + fileData = fs.readFileSync(dataPath).toString() + + if (!fileData || fileData.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == '') { + fileData = "" + } else { + // attempt to parse the file + switch(format) { + case "CSON": + data = CSON.parse(fileData) + if (data.constructor != Object) throw data + obj._data = data + break + case "HJSON": + obj._data = HJSON.parse(fileData) + break + default: + obj._data = JSON.parse(fileData) + } + } + } else { + // No data file found + format = "" + fileData = "" + dataPath = null + } + } catch(e) { + if (e.code === "ENOENT") { + exception = true // file not readable or does not exists + } else { + e.source = format + " Data" + e.dest = "Globals" + e.lineno = -1 + e.filename = dataPath + e.stack = fileData + throw new TerraformError(e) + } } - //console.log(e.code) - } - + list.forEach(function(file){ var filePath = path.resolve(dirPath, file) var stat = fs.statSync(filePath) @@ -496,3 +543,24 @@ exports.isJavaScript = function(filePath){ return processors["js"].indexOf(ext) !== -1 } + +/** + * needsBrowserify + * + * returns true if the code uses require, exports or module but doesn't declare them + */ + +exports.needsBrowserify = function(source) { + return /^[^#\/*'"]*\b(require|module|exports)\b/m.test(source) + && !(/^[^#\/*'"\w]*(function|var|global) +(require|module|exports)\b|^([^#\/*'"\w]*|[^#\/*'"]*window\.)(module|require) *=[^=]/m.test(source)) +} + +/** + * isJSON(filePath) + * + * returns true if file is a pre-processor JSON file + */ +exports.isJSON = function(filePath) { + var ext = path.extname(filePath).replace(/^\./, '') + return processors['json'].indexOf(ext) !== -1 +} diff --git a/lib/javascript/index.js b/lib/javascript/index.js index d5ec6b6bf..798ef740d 100644 --- a/lib/javascript/index.js +++ b/lib/javascript/index.js @@ -1,7 +1,9 @@ -var path = require("path") -var fs = require("fs") -var helpers = require('../helpers') -var minify = require('minify') +var path = require("path") +var fs = require("fs") +var helpers = require('../helpers') +var minify = require('minify') +var browserify = require('browserify') +var through = require('through') /** * Build Processor list for javascripts. @@ -13,10 +15,13 @@ var minify = require('minify') * } * */ - var processors = {} +var extensions = [], processors = {}, exceptionHandler = null + helpers.processors["js"].forEach(function(sourceType){ + extensions.push('.' + sourceType) processors[sourceType] = require("./processors/" + sourceType) }) +processors['js'] = processors['es'] // so it's possible to require .js files module.exports = function(root, filePath, callback){ @@ -41,18 +46,67 @@ module.exports = function(root, filePath, callback){ * Lookup Directories */ - var render = processors[ext].compile(srcPath, data, function(err, js) { - if (err) return callback(err); - - /** - * Consistently minify - */ - var post = minify.js(js, { - compress: false, - mangle: true - }); - callback(null, post); - }) + var render = function(ext, data, cb) { + processors[ext].compile(srcPath, data, function(err, js) { + if (err) return cb(err) + + /** + * Consistently minify + */ + var post = minify.js(js, { + compress: false, + mangle: true + }) + cb(null, post) + }) + } + + if(helpers.needsBrowserify(data.toString())) { + var post = '', success = true + + if (exceptionHandler) { + process.removeListener('uncaughtException', exceptionHandler) + } + exceptionHandler = function(err) { + console.log(err.message) + if (success) { + success = false + render(ext, data, callback) + } + } + + process.on('uncaughtException', exceptionHandler) + + browserify(srcPath, {extensions: extensions}).transform(function(file) { + var result = '' + return through(write, end) + + function write(buf) { + result += buf + } + function end() { + if(success) { + var that = this + render(path.extname(file).replace(/^\./, '').toLowerCase(), result, function(err, data) { + that.queue(data) + that.queue(null) + }) + } + } + }).on('error', exceptionHandler).bundle() + .on('data', function(buf) { + if (success) { + post += buf + } + }).on('end', function() { + if (success) { + callback(null, post) + } + }) + } + else { + render(ext, data, callback) + } }) diff --git a/lib/javascript/processors/es.js b/lib/javascript/processors/es.js new file mode 100644 index 000000000..4291f1541 --- /dev/null +++ b/lib/javascript/processors/es.js @@ -0,0 +1,3 @@ +exports.compile = function(filePath, fileContents, callback){ + callback(null, fileContents.toString()) +} diff --git a/lib/json/index.js b/lib/json/index.js new file mode 100644 index 000000000..68954c1ec --- /dev/null +++ b/lib/json/index.js @@ -0,0 +1,54 @@ +var path = require("path") +var fs = require("fs") +var helpers = require('../helpers') + +/** + * Build Processor list for JSON data. + * + * same as doing... + * + * var processors = { + * "cson" : require("./processors/cson") + * } + * + */ +var extensions = [], processors = {}, exceptionHandler = null + +helpers.processors["json"].forEach(function(sourceType){ + extensions.push('.' + sourceType) + processors[sourceType] = require("./processors/" + sourceType) +}) + +module.exports = function(root, filePath, callback){ + + var srcPath = path.resolve(root, filePath) + var ext = path.extname(srcPath).replace(/^\./, '') + + fs.readFile(srcPath, function(err, data){ + + /** + * File not Found + */ + + if(err && err.code === 'ENOENT') return callback(null, null) + + /** + * Read File Error + */ + + if(err) return callback(err) + + /** + * Lookup Directories + */ + + processors[ext].compile(srcPath, data, function(err, obj) { + if (err) return callback(err) + + var post = JSON.stringify(obj) + callback(null, post) + }) + + }) + +} diff --git a/lib/json/processors/cson.js b/lib/json/processors/cson.js new file mode 100644 index 000000000..9211ced14 --- /dev/null +++ b/lib/json/processors/cson.js @@ -0,0 +1,23 @@ +var CSON = require("cson") +var TerraformError = require("../../error").TerraformError + +exports.compile = function(filePath, fileContents, callback){ + + var formatError = function(e){ + return new TerraformError({ + source: "CSON", + dest: "JSON", + lineno: parseInt(e.line || -1), + name: e.type + "Error", + filename: filePath, + message: e.message, + stack: fileContents.toString() + }) + } + + CSON.parse(fileContents.toString(), {}, function(err, obj) { + if (err) + return callback(formatError(err)) + callback(null, obj) + }) +} diff --git a/lib/json/processors/hjson.js b/lib/json/processors/hjson.js new file mode 100644 index 000000000..6a4b5067a --- /dev/null +++ b/lib/json/processors/hjson.js @@ -0,0 +1,24 @@ +var HJSON = require("hjson") +var TerraformError = require("../../error").TerraformError + +exports.compile = function(filePath, fileContents, callback){ + + var formatError = function(e){ + return new TerraformError({ + source: "HJSON", + dest: "JSON", + lineno: parseInt(e.line || -1), + name: e.type + "Error", + filename: filePath, + message: e.message, + stack: fileContents.toString() + }) + } + + try { + var obj = HJSON.parse(fileContents.toString()) + callback(null, obj) + } catch (ex) { + callback(formatError(ex)) + } +} diff --git a/lib/terraform.js b/lib/terraform.js index 3e386f06a..97afcb5fe 100644 --- a/lib/terraform.js +++ b/lib/terraform.js @@ -3,6 +3,7 @@ var path = require('path') var stylesheet = require('./stylesheet') var template = require('./template') var javascript = require('./javascript') +var json = require('./json') var helpers = require('./helpers') @@ -147,6 +148,8 @@ exports.root = function(root, globals){ stylesheet(root, filePath, callback) }else if(helpers.isJavaScript(filePath)){ javascript(root, filePath, callback) + }else if(helpers.isJSON(filePath)) { + json(root, filePath, callback) }else{ callback(null, null) } diff --git a/package.json b/package.json index d74702f8f..334a0e4e5 100644 --- a/package.json +++ b/package.json @@ -12,32 +12,74 @@ }, "author": "Brock Whitten ", "contributors": [ - { "name": "Brock Whitten", "email": "brock@chloi.io" }, - { "name": "Brian Donovan", "email": "donovan@squareup.com" }, - { "name": "Kenneth Ormandy", "email": "kenneth@chloi.io" }, - { "name": "Zhang Yichao", "email": "echaozh@gmail.com" }, - { "name": "Carlos Rodriguez" }, - { "name": "Zeke Sikelianos", "email": "zeke@sikelianos.com" }, - { "name": "Guilherme Rodrigues", "email": "gadr90@gmail.com" }, - { "name": "Radu Brehar", "email": "radu@jslog.com" }, - { "name": "Glen Maddern", "email": "glenmaddern@gmail.com" }, - { "name": "Jed Foster", "email": "jed@jedfoster.com" }, - { "name": "Sehrope Sarkuni", "email": "sehrope@jackdb.com" }, - { "name": "Keiichiro Matsumoto", "email": "matsumos@gmail.com" }, - { "name": "Najam Khn", "email": "najamkhn@gmail.com" } + { + "name": "Brock Whitten", + "email": "brock@chloi.io" + }, + { + "name": "Brian Donovan", + "email": "donovan@squareup.com" + }, + { + "name": "Kenneth Ormandy", + "email": "kenneth@chloi.io" + }, + { + "name": "Zhang Yichao", + "email": "echaozh@gmail.com" + }, + { + "name": "Carlos Rodriguez" + }, + { + "name": "Zeke Sikelianos", + "email": "zeke@sikelianos.com" + }, + { + "name": "Guilherme Rodrigues", + "email": "gadr90@gmail.com" + }, + { + "name": "Radu Brehar", + "email": "radu@jslog.com" + }, + { + "name": "Glen Maddern", + "email": "glenmaddern@gmail.com" + }, + { + "name": "Jed Foster", + "email": "jed@jedfoster.com" + }, + { + "name": "Sehrope Sarkuni", + "email": "sehrope@jackdb.com" + }, + { + "name": "Keiichiro Matsumoto", + "email": "matsumos@gmail.com" + }, + { + "name": "Najam Khn", + "email": "najamkhn@gmail.com" + } ], "license": "MIT", "dependencies": { - "lru-cache": "2.6.1", - "jade": "git://github.com/harp/jade#v1.9.3-bc.2", + "autoprefixer": "5.1.0", + "browserify": "^10.2.4", "coffee-script": "1.9.2", + "cson": "^3.0.1", "ejs": "1.0.0", - "node-sass": "3.0.0-beta.5", - "marked": "0.3.3", + "hjson": "^1.7.3", + "jade": "git://github.com/harp/jade#v1.9.3-bc.2", "less": "2.5.0", - "stylus": "0.47.3", + "lru-cache": "2.6.1", + "marked": "0.3.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", - "autoprefixer": "5.1.0" + "node-sass": "3.0.0-beta.5", + "stylus": "0.47.3", + "through": "^2.3.7" }, "devDependencies": { "mocha": "1.8.2", diff --git a/test/data.js b/test/data.js index b1f78d65d..f7e840d04 100644 --- a/test/data.js +++ b/test/data.js @@ -55,7 +55,7 @@ describe("data", function(){ var poly = polymer.root(root) }catch(error){ should.exist(error) - error.should.have.property('source', "Data") + error.should.have.property('source', "JSON Data") error.should.have.property('dest', "Globals") error.should.have.property('lineno') error.should.have.property('filename') @@ -97,3 +97,161 @@ describe("data", function(){ }) }) + +describe("data-cson", function(){ + + describe("valid", function(){ + var root = __dirname + "/fixtures/data-cson/valid" + var poly = polymer.root(root) + + it("should be available in the layouts", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

My Articles

") + body.should.include('
Earth people, New York to California
') + done() + }) + }) + + it("should be available in the template", function(done){ + poly.render("articles/hello-jupiter.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Brock Whitten

") + done() + }) + }) + + it("should handle escaped html", function(done){ + poly.render("articles/hello-pluto.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

Harp

") + done() + }) + }) + + + it("should be available to override data when calling partial", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Kool Keith

") + done() + }) + }) + }) + + describe("invalid", function(){ + it("should return errors when invalid _data.cson file", function(done){ + var root = __dirname + "/fixtures/data-cson/invalid" + try{ + var poly = polymer.root(root) + }catch(error){ + should.exist(error) + error.should.have.property('source', "CSON Data") + error.should.have.property('dest', "Globals") + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + } + }) + }) + + describe("public", function(){ + it("should return public object", function(done){ + var root = __dirname + "/fixtures/data-cson/valid" + var poly = polymer.root(root) + poly.render("pub.json.jade", { "layout": false }, function(err, result){ + var pub = JSON.parse(result) + should.not.exist(pub[".foo"]) + done() + }) + }) + }) + +}) + +describe("data-hjson", function(){ + + describe("valid", function(){ + var root = __dirname + "/fixtures/data-hjson/valid" + var poly = polymer.root(root) + + it("should be available in the layouts", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

My Articles

") + body.should.include('
Earth people, New York to California
') + done() + }) + }) + + it("should be available in the template", function(done){ + poly.render("articles/hello-jupiter.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Brock Whitten

") + done() + }) + }) + + it("should handle escaped html", function(done){ + poly.render("articles/hello-pluto.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

Harp

") + done() + }) + }) + + + it("should be available to override data when calling partial", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Kool Keith

") + done() + }) + }) + }) + + describe("invalid", function(){ + it("should return errors when invalid _data.hjson file", function(done){ + var root = __dirname + "/fixtures/data-hjson/invalid" + try{ + var poly = polymer.root(root) + }catch(error){ + should.exist(error) + error.should.have.property('source', "HJSON Data") + error.should.have.property('dest', "Globals") + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + } + }) + }) + + describe("public", function(){ + it("should return public object", function(done){ + var root = __dirname + "/fixtures/data-hjson/valid" + var poly = polymer.root(root) + poly.render("pub.json.jade", { "layout": false }, function(err, result){ + var pub = JSON.parse(result) + should.not.exist(pub[".foo"]) + done() + }) + }) + }) + +}) diff --git a/test/errors.js b/test/errors.js index 02ff4b414..19fe21c4f 100644 --- a/test/errors.js +++ b/test/errors.js @@ -259,5 +259,37 @@ describe("errors", function(){ }) }) }) + + describe(".hjson", function() { + it("should get error if property name is missing", function(done) { + poly.render("json/invalid.hjson", function(error, body) { + should.not.exist(body) + should.exist(error) + error.should.have.property('source') + error.should.have.property('dest') + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + }) + }) + }) + + describe(".cson", function() { + it("should get error if syntax is invalid", function(done) { + poly.render("json/invalid.cson", function(error, body) { + should.not.exist(body) + should.exist(error) + error.should.have.property('source') + error.should.have.property('dest') + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + }) + }) + }) }) diff --git a/test/fixtures/data-cson/invalid/_data.cson b/test/fixtures/data-cson/invalid/_data.cson new file mode 100644 index 000000000..aa0202d32 --- /dev/null +++ b/test/fixtures/data-cson/invalid/_data.cson @@ -0,0 +1,7 @@ +hello-world: + title: "Earth people, New York to California" + author: "Brock Whitten" + +"hello-jupiter" + title: "I was born on Jupiter" + author: "Brock Whitten" diff --git a/test/fixtures/data-cson/valid/.foo/placeholder.txt b/test/fixtures/data-cson/valid/.foo/placeholder.txt new file mode 100644 index 000000000..5c532474c --- /dev/null +++ b/test/fixtures/data-cson/valid/.foo/placeholder.txt @@ -0,0 +1 @@ +This file is here to test ignoring directories that begin with a dot (".") diff --git a/test/fixtures/data-cson/valid/_layout.jade b/test/fixtures/data-cson/valid/_layout.jade new file mode 100644 index 000000000..e46014a31 --- /dev/null +++ b/test/fixtures/data-cson/valid/_layout.jade @@ -0,0 +1,3 @@ +h1 My Articles +h5.feature= public.articles._data['hello-world'].title +!= yield \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/articles/_data.cson b/test/fixtures/data-cson/valid/articles/_data.cson new file mode 100644 index 000000000..f27294a6c --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/_data.cson @@ -0,0 +1,10 @@ +"hello-world": + title: "Earth people, New York to California" + author: "Brock Whitten" + +"hello-jupiter": + title: "I was born on Jupiter" + author: "Brock Whitten" + +"hello-pluto": + title: 'Harp' diff --git a/test/fixtures/data-cson/valid/articles/hello-jupiter.jade b/test/fixtures/data-cson/valid/articles/hello-jupiter.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/hello-jupiter.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/articles/hello-pluto.jade b/test/fixtures/data-cson/valid/articles/hello-pluto.jade new file mode 100644 index 000000000..9b7a09f5b --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/hello-pluto.jade @@ -0,0 +1,3 @@ +//- Testing whether you can write escaped markup inside `_data.json` files + +h1!= title diff --git a/test/fixtures/data-cson/valid/articles/hello-world.jade b/test/fixtures/data-cson/valid/articles/hello-world.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/hello-world.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/index.jade b/test/fixtures/data-cson/valid/index.jade new file mode 100644 index 000000000..a0ce712f0 --- /dev/null +++ b/test/fixtures/data-cson/valid/index.jade @@ -0,0 +1,6 @@ +h2 Home + +h3 Articles + +for article, slug in public.articles._data + != partial('articles/' + slug + '.jade', { author: "Kool Keith" }) \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/pub.json.jade b/test/fixtures/data-cson/valid/pub.json.jade new file mode 100644 index 000000000..d6b8764c8 --- /dev/null +++ b/test/fixtures/data-cson/valid/pub.json.jade @@ -0,0 +1 @@ +!= JSON.stringify(public) \ No newline at end of file diff --git a/test/fixtures/data-hjson/invalid/_data.hjson b/test/fixtures/data-hjson/invalid/_data.hjson new file mode 100644 index 000000000..9e56ad7e2 --- /dev/null +++ b/test/fixtures/data-hjson/invalid/_data.hjson @@ -0,0 +1,10 @@ +{ + hello-world { + title: Earth people, New York to California + author: Brock Whitten + } + hello-jupiter: { + title: I was born on Jupiter + author: Brock Whitten + } +} diff --git a/test/fixtures/data-hjson/valid/.foo/placeholder.txt b/test/fixtures/data-hjson/valid/.foo/placeholder.txt new file mode 100644 index 000000000..5c532474c --- /dev/null +++ b/test/fixtures/data-hjson/valid/.foo/placeholder.txt @@ -0,0 +1 @@ +This file is here to test ignoring directories that begin with a dot (".") diff --git a/test/fixtures/data-hjson/valid/_layout.jade b/test/fixtures/data-hjson/valid/_layout.jade new file mode 100644 index 000000000..e46014a31 --- /dev/null +++ b/test/fixtures/data-hjson/valid/_layout.jade @@ -0,0 +1,3 @@ +h1 My Articles +h5.feature= public.articles._data['hello-world'].title +!= yield \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/articles/_data.hjson b/test/fixtures/data-hjson/valid/articles/_data.hjson new file mode 100644 index 000000000..cd121e5b6 --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/_data.hjson @@ -0,0 +1,13 @@ +{ + hello-world: { + title: Earth people, New York to California + author: Brock Whitten + } + hello-jupiter: { + title: I was born on Jupiter + author: Brock Whitten + } + hello-pluto: { + title: Harp + } +} diff --git a/test/fixtures/data-hjson/valid/articles/hello-jupiter.jade b/test/fixtures/data-hjson/valid/articles/hello-jupiter.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/hello-jupiter.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/articles/hello-pluto.jade b/test/fixtures/data-hjson/valid/articles/hello-pluto.jade new file mode 100644 index 000000000..9b7a09f5b --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/hello-pluto.jade @@ -0,0 +1,3 @@ +//- Testing whether you can write escaped markup inside `_data.json` files + +h1!= title diff --git a/test/fixtures/data-hjson/valid/articles/hello-world.jade b/test/fixtures/data-hjson/valid/articles/hello-world.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/hello-world.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/index.jade b/test/fixtures/data-hjson/valid/index.jade new file mode 100644 index 000000000..a0ce712f0 --- /dev/null +++ b/test/fixtures/data-hjson/valid/index.jade @@ -0,0 +1,6 @@ +h2 Home + +h3 Articles + +for article, slug in public.articles._data + != partial('articles/' + slug + '.jade', { author: "Kool Keith" }) \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/pub.json.jade b/test/fixtures/data-hjson/valid/pub.json.jade new file mode 100644 index 000000000..d6b8764c8 --- /dev/null +++ b/test/fixtures/data-hjson/valid/pub.json.jade @@ -0,0 +1 @@ +!= JSON.stringify(public) \ No newline at end of file diff --git a/test/fixtures/errors/json/invalid.cson b/test/fixtures/errors/json/invalid.cson new file mode 100644 index 000000000..9845617d9 --- /dev/null +++ b/test/fixtures/errors/json/invalid.cson @@ -0,0 +1 @@ +prop: ++ diff --git a/test/fixtures/errors/json/invalid.hjson b/test/fixtures/errors/json/invalid.hjson new file mode 100644 index 000000000..a08b2f8e3 --- /dev/null +++ b/test/fixtures/errors/json/invalid.hjson @@ -0,0 +1,3 @@ +{ + : +} diff --git a/test/fixtures/javascripts/browserify/Math.coffee b/test/fixtures/javascripts/browserify/Math.coffee new file mode 100644 index 000000000..ee5932f46 --- /dev/null +++ b/test/fixtures/javascripts/browserify/Math.coffee @@ -0,0 +1,4 @@ +# Let's see if we can mix .coffee & .es + +exports.pow = (num) -> + num * num diff --git a/test/fixtures/javascripts/browserify/Math.js b/test/fixtures/javascripts/browserify/Math.js new file mode 100644 index 000000000..c462e3352 --- /dev/null +++ b/test/fixtures/javascripts/browserify/Math.js @@ -0,0 +1,5 @@ +// Let's see if we can mix .es & .js + +exports.pow = function(num) { + return num * num; +}; diff --git a/test/fixtures/javascripts/browserify/comment.coffee b/test/fixtures/javascripts/browserify/comment.coffee new file mode 100644 index 000000000..a1fb7b864 --- /dev/null +++ b/test/fixtures/javascripts/browserify/comment.coffee @@ -0,0 +1,3 @@ +# pow = require('./Math').pow; + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/comment.es b/test/fixtures/javascripts/browserify/comment.es new file mode 100644 index 000000000..93418d714 --- /dev/null +++ b/test/fixtures/javascripts/browserify/comment.es @@ -0,0 +1,5 @@ +/* + * pow = require('./Math').pow; + */ + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/declared.coffee b/test/fixtures/javascripts/browserify/declared.coffee new file mode 100644 index 000000000..c209946aa --- /dev/null +++ b/test/fixtures/javascripts/browserify/declared.coffee @@ -0,0 +1,6 @@ +require = (file) -> + # custom implementation + +pow = require('./Math').pow + +console.log pow(4) diff --git a/test/fixtures/javascripts/browserify/declared.es b/test/fixtures/javascripts/browserify/declared.es new file mode 100644 index 000000000..40484a0fb --- /dev/null +++ b/test/fixtures/javascripts/browserify/declared.es @@ -0,0 +1,7 @@ +var require = function(file) { + // custom implementation +}; + +var pow = require('./Math').pow; + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/require_coffee.coffee b/test/fixtures/javascripts/browserify/require_coffee.coffee new file mode 100644 index 000000000..43b6e2e87 --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_coffee.coffee @@ -0,0 +1,3 @@ +pow = require('./Math.coffee').pow + +console.log pow(4) diff --git a/test/fixtures/javascripts/browserify/require_coffee.es b/test/fixtures/javascripts/browserify/require_coffee.es new file mode 100644 index 000000000..86a4bc2a3 --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_coffee.es @@ -0,0 +1,3 @@ +var pow = require('./Math.coffee').pow; + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/require_js.coffee b/test/fixtures/javascripts/browserify/require_js.coffee new file mode 100644 index 000000000..614e2afc8 --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_js.coffee @@ -0,0 +1,3 @@ +pow = require('./Math.js').pow + +console.log pow(4) diff --git a/test/fixtures/javascripts/browserify/require_js.es b/test/fixtures/javascripts/browserify/require_js.es new file mode 100644 index 000000000..2d2e69f5e --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_js.es @@ -0,0 +1,3 @@ +var pow = require('./Math.js').pow; + +console.log(pow(4)); diff --git a/test/fixtures/json/cson/data.cson b/test/fixtures/json/cson/data.cson new file mode 100644 index 000000000..9d91420c4 --- /dev/null +++ b/test/fixtures/json/cson/data.cson @@ -0,0 +1,40 @@ +# Comments!!! + +# An Array with no commas! +greatDocumentaries: [ + 'earthlings.com' + 'forksoverknives.com' + 'cowspiracy.com' +] + +# An Object without braces! +importantFacts: + # Multi-Line Strings! Without Quote Escaping! + emissions: ''' + Livestock and their byproducts account for at least 32,000 million tons of carbon dioxide (CO2) per year, or 51% of all worldwide greenhouse gas emissions. + Goodland, R Anhang, J. “Livestock and Climate Change: What if the key actors in climate change were pigs, chickens and cows?” + WorldWatch, November/December 2009. Worldwatch Institute, Washington, DC, USA. Pp. 10–19. + http://www.worldwatch.org/node/6294 + ''' + + landuse: ''' + Livestock covers 45% of the earth’s total land. + Thornton, Phillip, Mario Herrero, and Polly Ericksen. “Livestock and Climate Change.” Livestock Exchange, no. 3 (2011). + https://cgspace.cgiar.org/bitstream/handle/10568/10601/IssueBrief3.pdf + ''' + + burger: ''' + One hamburger requires 660 gallons of water to produce – the equivalent of 2 months’ worth of showers. + Catanese, Christina. “Virtual Water, Real Impacts.” Greenversations: Official Blog of the U.S. EPA. 2012. + http://blog.epa.gov/healthywaters/2012/03/virtual-water-real-impacts-world-water-day-2012/ + “50 Ways to Save Your River.” Friends of the River. + http://www.friendsoftheriver.org/site/PageServer?pagename=50ways + ''' + + milk: ''' + 1,000 gallons of water are required to produce 1 gallon of milk. + “Water trivia facts.” United States Environmental Protection Agency. + http://water.epa.gov/learn/kids/drinkingwater/water_trivia_facts.cfm#_edn11 + ''' + + more: 'http://cowspiracy.com/facts' diff --git a/test/fixtures/json/cson/invalid.cson b/test/fixtures/json/cson/invalid.cson new file mode 100644 index 000000000..9845617d9 --- /dev/null +++ b/test/fixtures/json/cson/invalid.cson @@ -0,0 +1 @@ +prop: ++ diff --git a/test/fixtures/json/hjson/data.hjson b/test/fixtures/json/hjson/data.hjson new file mode 100644 index 000000000..2ec2d4fd6 --- /dev/null +++ b/test/fixtures/json/hjson/data.hjson @@ -0,0 +1,16 @@ +{ + # specify rate in requests/second (because comments are helpful!) + rate: 1000 + + // prefer c-style comments? + /* feeling old fashioned? */ + + # did you notice that rate doesn't need quotes? + hey: look ma, no quotes for strings either! + + # best of all + notice: [] + anything: ? + + # yes, commas are optional! +} diff --git a/test/fixtures/json/hjson/invalid.hjson b/test/fixtures/json/hjson/invalid.hjson new file mode 100644 index 000000000..a08b2f8e3 --- /dev/null +++ b/test/fixtures/json/hjson/invalid.hjson @@ -0,0 +1,3 @@ +{ + : +} diff --git a/test/helpers.js b/test/helpers.js index f7dbf3075..d8ebd2086 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -34,11 +34,15 @@ describe("helpers", function(){ it('should look for templates on json files.', function(done){ var list = polymer.helpers.buildPriorityList('profile.json') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) + list.should.have.lengthOf(7) list.should.include('profile.json.jade') list.should.include('profile.json.ejs') list.should.include('profile.json.md') - list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md'. split(', ')) + list.should.include('profile.cson') + list.should.include('profile.hjson') + list.should.include('profile.json.cson') + list.should.include('profile.json.hjson') + list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md, profile.cson, profile.hjson, profile.json.cson, profile.json.hjson'. split(', ')) done() }) diff --git a/test/javascripts.js b/test/javascripts.js index 432b16d35..707861573 100644 --- a/test/javascripts.js +++ b/test/javascripts.js @@ -41,5 +41,69 @@ describe("javascripts", function(){ }) }) + + describe("browserify", function() { + var root = __dirname + "/fixtures/javascripts/browserify" + var poly = polymer.root(root) + + process.chdir(root) + + it("should require coffeescript file in coffeescript", function(done) { + poly.render("require_coffee.coffee", function(errors, body) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should require javascript file in coffeescript", function(done) { + poly.render("require_js.coffee", function(errors, body) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should require coffeescript file in javascript", function(done) { + poly.render("require_coffee.es", function(errors, body) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should require javascript file in javascript", function(done) { + poly.render("require_js.es", function(errors, body) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip commented require in coffeescript", function(done) { + poly.render("comment.coffee", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip commented require in javascript", function(done) { + poly.render("comment.es", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip already declared require in coffeescript", function(done) { + poly.render("declared.coffee", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip already declared require in javascript", function(done) { + poly.render("declared.es", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + }) }) diff --git a/test/json.js b/test/json.js new file mode 100644 index 000000000..feb678423 --- /dev/null +++ b/test/json.js @@ -0,0 +1,53 @@ +var should = require('should') +var polymer = require('../') + +describe("JSON", function(){ + + describe(".cson", function(){ + var root = __dirname + "/fixtures/json/cson" + var poly = polymer.root(root) + + it("should convert CSON to JSON", function(done){ + poly.render("data.cson", function(errors, body){ + should.not.exist(errors) + should.exist(body) + done() + }) + }) + it("should return errors if syntax is invalid", function(done){ + poly.render("invalid.cson", function(errors, body){ + should.exist(errors) + should.not.exist(body) + errors.should.have.property("name") + errors.should.have.property("message") + errors.should.have.property("stack") + done() + }) + }) + + }) + + describe(".hjson", function() { + var root = __dirname + "/fixtures/json/hjson" + var poly = polymer.root(root) + + it("should convert Hjson to JSON", function(done){ + poly.render("data.hjson", function(errors, body){ + should.not.exist(errors) + should.exist(body) + done() + }) + }) + it("should return errors if property name is missing", function(done){ + poly.render("invalid.hjson", function(errors, body){ + should.exist(errors) + should.not.exist(body) + errors.should.have.property("name") + errors.should.have.property("message") + errors.should.have.property("stack") + done() + }) + }) + }) + +})