Comparing version 1.8.2 to 1.9.0
@@ -13,3 +13,3 @@ #!/usr/bin/env node | ||
, resolve = path.resolve | ||
, exists = fs.existsSync || path.existsSync | ||
, exists = fs.existsSync || /* istanbul ignore next */ path.existsSync | ||
, join = path.join | ||
@@ -36,2 +36,3 @@ , mkdirp = require('mkdirp') | ||
.option('-w, --watch', 'watch files for changes and automatically re-render') | ||
.option('-E, --extension <extension>', 'specify the output file extension') | ||
.option('--name-after-file', 'Name the template after the last section of the file path (requires --client and overriden by --name)') | ||
@@ -115,15 +116,20 @@ .option('--doctype <str>', 'Specify the doctype on the command line (useful if it is not specified by the template)') | ||
renderFile(filename); | ||
} catch (ex) { | ||
} catch (e) { | ||
// keep watching when error occured. | ||
console.error(ex.stack || ex.message || ex); | ||
console.error(e.stack || e.message || e); | ||
} | ||
fs.watchFile(filename, {persistent: true, interval: 200}, function (filename) { | ||
fs.watchFile(filename, {persistent: true, interval: 200}, | ||
function (curr, prev) { | ||
if (curr.mtime.getTime() === prev.mtime.getTime()) return; | ||
try { | ||
renderFile(filename); | ||
} catch (ex) { | ||
} catch (e) { | ||
// keep watching when error occured. | ||
console.error(ex.stack || ex.message || ex); | ||
console.error(e.stack || e.message || e); | ||
} | ||
}); | ||
}); | ||
process.on('SIGINT', function() { | ||
process.exit(1); | ||
}); | ||
} else { | ||
@@ -146,3 +152,2 @@ files.forEach(renderFile); | ||
var buf = ''; | ||
options.filename = '-'; | ||
process.stdin.setEncoding('utf8'); | ||
@@ -176,44 +181,31 @@ process.stdin.on('data', function(chunk){ buf += chunk; }); | ||
var re = /\.jade$/; | ||
fs.lstat(path, function(err, stat) { | ||
if (err) throw err; | ||
// Found jade file | ||
if (stat.isFile() && re.test(path)) { | ||
fs.readFile(path, 'utf8', function(err, str){ | ||
if (err) throw err; | ||
options.filename = path; | ||
if (program.nameAfterFile) { | ||
options.name = getNameFromFileName(path); | ||
} | ||
var fn = options.client ? jade.compileClient(str, options) : jade.compile(str, options); | ||
var extname = options.client ? '.js' : '.html'; | ||
path = path.replace(re, extname); | ||
if (program.out) path = join(program.out, basename(path)); | ||
var dir = resolve(dirname(path)); | ||
mkdirp(dir, 0755, function(err){ | ||
if (err) throw err; | ||
try { | ||
var output = options.client ? fn : fn(options); | ||
fs.writeFile(path, output, function(err){ | ||
if (err) throw err; | ||
console.log(' \033[90mrendered \033[36m%s\033[0m', path); | ||
}); | ||
} catch (e) { | ||
if (options.watch) { | ||
console.error(e.stack || e.message || e); | ||
} else { | ||
throw e | ||
} | ||
} | ||
}); | ||
}); | ||
// Found directory | ||
} else if (stat.isDirectory()) { | ||
fs.readdir(path, function(err, files) { | ||
if (err) throw err; | ||
files.map(function(filename) { | ||
return path + '/' + filename; | ||
}).forEach(renderFile); | ||
}); | ||
var stat = fs.lstatSync(path); | ||
// Found jade file/\.jade$/ | ||
if (stat.isFile() && re.test(path)) { | ||
var str = fs.readFileSync(path, 'utf8'); | ||
options.filename = path; | ||
if (program.nameAfterFile) { | ||
options.name = getNameFromFileName(path); | ||
} | ||
}); | ||
var fn = options.client ? jade.compileClient(str, options) : jade.compile(str, options); | ||
// --extension | ||
if (program.extension) var extname = '.' + program.extension; | ||
else if (options.client) var extname = '.js'; | ||
else var extname = '.html'; | ||
path = path.replace(re, extname); | ||
if (program.out) path = join(program.out, basename(path)); | ||
var dir = resolve(dirname(path)); | ||
mkdirp.sync(dir, 0755); | ||
var output = options.client ? fn : fn(options); | ||
fs.writeFileSync(path, output); | ||
console.log(' \033[90mrendered \033[36m%s\033[0m', path); | ||
// Found directory | ||
} else if (stat.isDirectory()) { | ||
var files = fs.readdirSync(path); | ||
files.map(function(filename) { | ||
return path + '/' + filename; | ||
}).forEach(renderFile); | ||
} | ||
} | ||
@@ -220,0 +212,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Jade template runtime", | ||
"version": "1.8.2", | ||
"version": "1.9.0", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "template" |
@@ -43,3 +43,3 @@ 'use strict'; | ||
exports.utils = require('./utils'); | ||
exports.utils = utils; | ||
@@ -154,2 +154,28 @@ /** | ||
/** | ||
* Get the template from a string or a file, either compiled on-the-fly or | ||
* read from cache (if enabled), and cache the template if needed. | ||
* | ||
* If `str` is not set, the file specified in `options.filename` will be read. | ||
* | ||
* If `options.cache` is true, this function reads the file from | ||
* `options.filename` so it must be set prior to calling this function. | ||
* | ||
* @param {Object} options | ||
* @param {String=} str | ||
* @return {Function} | ||
* @api private | ||
*/ | ||
function handleTemplateCache (options, str) { | ||
var key = options.filename; | ||
if (options.cache && exports.cache[key]) { | ||
return exports.cache[key]; | ||
} else { | ||
if (!str) str = fs.readFileSync(options.filename, 'utf8'); | ||
var templ = exports.compile(str, options); | ||
if (options.cache) exports.cache[key] = templ; | ||
return templ; | ||
} | ||
} | ||
/** | ||
* Compile a `Function` representation of the given jade `str`. | ||
@@ -198,3 +224,3 @@ * | ||
err.name = 'Warning'; | ||
console.error(err.stack || err.message); | ||
console.error(err.stack || /* istanbul ignore next */ err.message); | ||
return exports.compileClient(str, options); | ||
@@ -265,13 +291,4 @@ }; | ||
options = options || {}; | ||
var key = path + ':string'; | ||
options.filename = path; | ||
var str = options.cache | ||
? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8')) | ||
: fs.readFileSync(path, 'utf8'); | ||
return options.cache | ||
? exports.cache[path] || (exports.cache[path] = exports.compile(str, options)) | ||
: exports.compile(str, options); | ||
return handleTemplateCache(options); | ||
}; | ||
@@ -316,7 +333,3 @@ | ||
var path = options.filename; | ||
var tmpl = options.cache | ||
? exports.cache[path] || (exports.cache[path] = exports.compile(str, options)) | ||
: exports.compile(str, options); | ||
return tmpl(options); | ||
return handleTemplateCache(options, str)(options); | ||
}; | ||
@@ -351,9 +364,4 @@ | ||
var key = path + ':string'; | ||
options.filename = path; | ||
var str = options.cache | ||
? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8')) | ||
: fs.readFileSync(path, 'utf8'); | ||
return exports.render(str, options); | ||
return handleTemplateCache(options)(options); | ||
}; | ||
@@ -372,12 +380,15 @@ | ||
exports.compileFileClient = function(path, options){ | ||
var key = path + ':client'; | ||
options = options || {}; | ||
var key = path + ':string'; | ||
options.filename = path; | ||
var str = options.cache | ||
? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8')) | ||
: fs.readFileSync(path, 'utf8'); | ||
return exports.compileClient(str, options); | ||
if (options.cache && exports.cache[key]) { | ||
return exports.cache[key]; | ||
} | ||
var str = fs.readFileSync(options.filename, 'utf8'); | ||
var out = exports.compileClient(str, options); | ||
if (options.cache) exports.cache[key] = out; | ||
return out; | ||
}; | ||
@@ -384,0 +395,0 @@ |
{ | ||
"name": "jade", | ||
"description": "A clean, whitespace-sensitive template language for writing HTML", | ||
"version": "1.8.2", | ||
"version": "1.9.0", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca>", | ||
@@ -69,3 +69,3 @@ "maintainers": [ | ||
"test": "mocha -R spec && npm run coverage", | ||
"coverage": "istanbul cover node_modules/mocha/bin/_mocha", | ||
"coverage": "rimraf cov-pt* && istanbul cover --report none --dir cov-pt0 node_modules/mocha/bin/_mocha && istanbul report --include cov-pt\\*/coverage.json", | ||
"prepublish": "npm prune && linify transform bin && npm run build", | ||
@@ -72,0 +72,0 @@ "build": "npm run compile", |
@@ -12,6 +12,6 @@ # [![Jade - template engine ](http://i.imgur.com/5zf2aVt.png)](http://jade-lang.com/) | ||
[![Build Status](https://img.shields.io/travis/jadejs/jade/master.svg)](https://travis-ci.org/jadejs/jade) | ||
[![Dependency Status](https://img.shields.io/gemnasium/jadejs/jade.svg)](https://gemnasium.com/jadejs/jade) | ||
[![NPM version](https://img.shields.io/npm/v/jade.svg)](http://badge.fury.io/js/jade) | ||
[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/jadejs/jade?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![Build Status](https://img.shields.io/travis/jadejs/jade/master.svg?style=flat)](https://travis-ci.org/jadejs/jade) | ||
[![Dependency Status](https://img.shields.io/gemnasium/jadejs/jade.svg?style=flat)](https://gemnasium.com/jadejs/jade) | ||
[![NPM version](https://img.shields.io/npm/v/jade.svg?style=flat)](http://badge.fury.io/js/jade) | ||
[![Join Gitter Chat](https://img.shields.io/badge/gitter-join%20chat%20%E2%86%92-brightgreen.svg?style=flat)](https://gitter.im/jadejs/jade?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
@@ -18,0 +18,0 @@ ## Installation |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
365683
10283