Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hjson & CSON files are automatically converted to JSON now #102

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

## 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

**HTML** – EJS, Jade, Markdown
**CSS** – LESS, Stylus, Sass (SCSS)
**JavaScript** – CoffeeScript
**JSON** - CSON, Hjson

## Install

Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/memoized.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
106 changes: 87 additions & 19 deletions lib/helpers/raw.js
Original file line number Diff line number Diff line change
@@ -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')


/**
Expand All @@ -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"]
}


Expand Down Expand Up @@ -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 + '$')
Expand Down Expand Up @@ -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){
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
88 changes: 71 additions & 17 deletions lib/javascript/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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){

Expand All @@ -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)
}

})

Expand Down
3 changes: 3 additions & 0 deletions lib/javascript/processors/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.compile = function(filePath, fileContents, callback){
callback(null, fileContents.toString())
}
54 changes: 54 additions & 0 deletions lib/json/index.js
Original file line number Diff line number Diff line change
@@ -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)
})

})

}
23 changes: 23 additions & 0 deletions lib/json/processors/cson.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
24 changes: 24 additions & 0 deletions lib/json/processors/hjson.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
Loading