Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wmdmark committed May 22, 2013
0 parents commit 2fd61c3
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*~
*.bak
.DS_Store
npm-debug.log
lib/
node_modules/
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.lock-wscript
.svn/
.hg/
.git/
CVS/
*~
*.bak
.DS_Store
npm-debug.log
test/
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## swig-brunch
Compiles Swig template files for use in [brunch](http://brunch.io). Plugin checks for files that have one of the following extensions:

- .html
- .swig


## Usage
Add `"swig-brunch": "x.y.z"` to `package.json` of your brunch app.

Pick a plugin version that corresponds to your minor (y) brunch version.

If you want to use git version of plugin, add
`"swig-brunch": "git+ssh://git@github.com:wmdmark/swig-brunch.git"`.
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "swig-brunch",
"version": "1.0.0",
"description": "Compiles swig templates for brunch.",
"author": "Mark Johnson",
"homepage": "https://github.com/wmdmark/swig-brunch",
"repository": {
"type": "git",
"url": "git@github.com:wmdmark/swig-brunch.git"
},
"main": "./lib/index",
"scripts": {
"prepublish": "node setup.js prepublish",
"postpublish": "node setup.js postpublish",
"test": "node setup.js test"
},
"engines": {
"node": "~0.6.10 || 0.8 || 0.9"
},
"dependencies": {
"coffee-script": "1.4.0",
"swig": "*"
},
"devDependencies": {
"mocha": "1.8.1",
"chai": "1.4.2"
}
}
65 changes: 65 additions & 0 deletions setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var exec = require('child_process').exec;
var sysPath = require('path');
var fs = require('fs');

// Cross-platform node.js postinstall & test script for coffeescript projects.

var mode = process.argv[2];

var fsExists = fs.exists || sysPath.exists;

var getBinaryPath = function(binary) {
return sysPath.join('node_modules', '.bin', binary);
};

var execute = function(path, params, callback) {
if (callback == null) callback = function() {};
var command = 'node ' + path + ' ' + params;
console.log('Executing', command);
exec(command, function(error, stdout, stderr) {
if (error != null) return process.stderr.write(stderr.toString());
console.log(stdout.toString());
});
};

var togglePostinstall = function(add) {
var pkg = require('./package.json');

if (add) {
if (!pkg.scripts) pkg.scripts = {};
pkg.scripts.postinstall = 'node setup.js test';
} else if (pkg.scripts && pkg.scripts.postinstall) {
delete pkg.scripts.postinstall;
}

fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
};

switch (mode) {
// Remove `.postinstall` script to prevent stupid npm bugs.
case 'prepublish':
togglePostinstall(false);
execute(getBinaryPath('coffee'), '-o lib/ src/');
break;

// Bring back `.postinstall` script.
case 'postpublish':
togglePostinstall(true);
break;

// Compile coffeescript for git users.
case 'postinstall':
fsExists(sysPath.join(__dirname, 'lib'), function(exists) {
if (exists) return;
execute(getBinaryPath('coffee'), '-o lib/ src/');
});
break;

// Run tests.
case 'test':
execute(
getBinaryPath('mocha'),
'--compilers coffee:coffee-script --require test/common.js --colors'
);
break;
}
23 changes: 23 additions & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
fs = require "fs"
swig = require 'swig'
sysPath = require 'path'

module.exports = class SwigCompiler
brunchPlugin: yes
type: 'template'
extension: 'html'
pattern: /(\.(html|swig))$/

compile: (data, path, callback) ->
try
filename = sysPath.basename(path).split(".")[0]
result = "module.exports = swig.compile(#{JSON.stringify(data)}, { filename: '#{filename}'});";
catch err
error = err
finally
callback error, result

include: [
(sysPath.join __dirname, '..', 'vendor',
'swig.pack.min.js')
]
4 changes: 4 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global.expect = require('chai').expect;
global.Plugin = require('../lib/index');


24 changes: 24 additions & 0 deletions test/plugin_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
sysPath = require('path')
swig = require('swig')

describe 'Plugin', ->

plugin = null

beforeEach ->
plugin = new Plugin(paths: root: '.');

it 'should be an object', ->
expect(plugin).to.be.ok

it 'should has #compile method', ->
expect(plugin.compile).to.be.an.instanceof(Function)

it 'should compile and produce valid result', (done)->
content = 'Hi {{name}}!'
expected = 'Hi Mark!'
plugin.compile content, 'main', (error, data)->
expect(error).not.to.be.ok
tmpl = eval(data)
expect(tmpl(name: 'Mark')).to.equal(expected)
done()
2 changes: 2 additions & 0 deletions vendor/swig.pack.min.js

Large diffs are not rendered by default.

0 comments on commit 2fd61c3

Please sign in to comment.