Skip to content

Commit

Permalink
init package
Browse files Browse the repository at this point in the history
  • Loading branch information
kupriyanenko committed Nov 3, 2012
0 parents commit 4376702
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store
node_modules
3 changes: 3 additions & 0 deletions .npmignore
@@ -0,0 +1,3 @@
support
test
examples
72 changes: 72 additions & 0 deletions README.md
@@ -0,0 +1,72 @@
jsttojs
=======

A node.js module for precompile JavaScript templates (ex. mustache or jQuery.tmpl) to one file.


You can to precompile some static mustache, hogan.js, jQuery.tmpl, underscore.js or any other templates and include them in generated javascript function that could be Stitched and compressed in a single file.


Installation
-----

$ npm install jsttojs -g


Usage
-----

jsttojs allows templates to be precompiled and included in javascript code

```
Precompile JavaScript templates.
Usage: jsttojs <input> <output> [options]
Options:
-h, --help output usage information
-V, --version output the version number
-e, --ext <n> extension for templates, default "jst"
-n, --name <n> name variable stores temapltes, default "JSTmpl"
Examples:
$ jsttojs templates/ compiled/templates/index.js --ext mustache
```

Samples
-----

Templates:

```javascript
// tamplates/index.jst
Hello world {{ username }}
second line
```

```javascript
// tamplates/video/index.jst
Hello {{ username }} on index video page
```

Run precompile templates

$ jsttojs templates/ compiled/templates/index.js --name MyGlobalVariable

Output:

```javascript
// compiled/templates/index.js
window.MyGlobalVariable = {
"index" : "Hello world {{ username }} second line",
"video/index" : "Hello {{ username }} on index video page"
}
```

License
-----

Copyright (c) 2012 Alexey Kupriyanenko a.kupriyanenko@gmail.com

jsttojs is released under the MIT license.
3 changes: 3 additions & 0 deletions bin/jsttojs
@@ -0,0 +1,3 @@
#!/usr/bin/env node

module.exports = require('../lib/jsttojs');
2 changes: 2 additions & 0 deletions index.js
@@ -0,0 +1,2 @@

module.exports = require('./lib/jsttojs');
83 changes: 83 additions & 0 deletions lib/jsttojs.js
@@ -0,0 +1,83 @@
/*!
* JavaScript template precompiler
* Copyright(c) 2012 Alexey Kupriyanenko <a.kupriyanenko@gmail.com>
* MIT Licensed
*/

var fs = require('fs');
var walk = require('walk');
var options = require('commander');

// Get options
options
.version('0.0.1')
.usage('<input> <output> [options]')
.option('-e, --ext <n>', 'extension for templates, default "jst"', 'jst')
.option('-n, --name <n>', 'name variable stores temapltes, default "JSTmpl"', 'JSTmpl')
.parse(process.argv);

options.root = process.argv[2];
options.output = process.argv[3];

// Check options
if (!options.root || !options.output)
options.help();

// Get files from root derictory
var files = [];
var walker = walk.walk(options.root, { followLinks: false });

walker.on('file', function(root, stat, next) {
if (stat.name.indexOf(options.ext) + options.ext.length === stat.name.length)
files.push(root + '/' + stat.name);

next();
});

// Generate templates after read templates
walker.on('end', function() {
var templates = readTemplates(files);

generateTemplates(templates);
});

/**
* Read files and get templates
* @param Array files
* @return Object
*/
var readTemplates = function(files) {
var templates = {}
, template;

files.forEach(function(path) {
try {
template = fs.readFileSync(path, 'utf8');
} catch(e) {
console.log(e);
return;
}

template = template.replace(/(\n(\r)?)/g, ' ');
var index = path.replace(options.root, '').replace('.' + options.ext, '').substring(1);

templates[index] = template;
});

return templates;
}

/**
* Generate template and write in output file
* @param Object templates
*/
var generateTemplates = function(templates) {
var data = 'window.' + options.name + ' = ' + JSON.stringify(templates, null, 4);

fs.writeFile(options.output, data, function(err) {
if (err)
return console.log(err);

console.log('complete: ' + options.output);
});
}
30 changes: 30 additions & 0 deletions package.json
@@ -0,0 +1,30 @@
{
"name": "jsttojs",
"version": "0.0.1",
"description": "A node.js module for precompile JavaScript templates (ex. mustache or jQuery.tmpl) to one file",
"preferGlobal": "true",
"main": "index",
"bin": {
"jsttojs": "bin/jsttojs"
},
"author": {
"name": "Alexey Kupriyanenko",
"email": "a.kupriyanenko@gmail.com"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"keywords": ["jst", "template", "precompile", "compile", "mustache"],
"repository" : {
"type": "git",
"url": "https://github.com/upwards/jsttojs.git"
},
"dependencies": {
"commander": "1.0.x",
"walk": "2.2.x"
},
"engines": { "node": ">= 0.6.x" }
}

0 comments on commit 4376702

Please sign in to comment.