diff --git a/.gitignore b/.gitignore index 40b878d..a5199f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules/ \ No newline at end of file +node_modules/ +.idea \ No newline at end of file diff --git a/Gruntfile.coffee b/Gruntfile.coffee deleted file mode 100644 index 9d0b0cc..0000000 --- a/Gruntfile.coffee +++ /dev/null @@ -1,149 +0,0 @@ -'use strict' - -module.exports = (grunt) -> - # Project configuration. - grunt.initConfig - clean: - test: - src: ['./test/actual/'] - task: - src: ['./tasks/template.js'] - - template: - single: - files: - 'test/actual/main.html': 'test/source/main.template' - options: - data: - message: 'Single' - class: 'foo' - css: - files: - 'test/actual/foo.min.css': 'test/source/foo.css' - options: - minify: - pretty: - lang: 'css' - mode: 'minify' - min: - files: - 'test/actual/min.html': 'test/source/min.template' - options: - data: - message: 'Minified' - class: 'foo' - minify: - mode: 'html' - concat: - files: - 'test/actual/concat.html': [ - 'test/source/concat1.template', - 'test/source/concat2.template' - ] - options: - data: - message: 'Concat' - class: 'foo' - wrap: - files: - 'test/actual/wrapped.html': 'test/source/main.template' - options: - data: - message: 'Wrap' - class: 'foo' - wrap: - banner: '\n' - footer: '\n' - advWrap: - files: - 'test/actual/wrappedAdv.html': 'test/source/main.template' - options: - data: - message: 'Wrap' - class: 'foo' - wrap: - banner: '\n' - footer: '\n' - inject: [{ - prop: 'dest' - rem: 'test/' - }] - concatWrap: - files: - 'test/actual/wrappedConcat.html': [ - 'test/source/concat1.template', - 'test/source/concat2.template' - ] - options: - data: - message: 'WrapConcat' - class: 'foo' - wrap: - banner: '\n' - footer: '\n' - inject: [{ - prop: 'dest' - rem: 'test/' - }] - advWrapReplace: - files: - 'test/actual/wrappedReplace.html': 'test/source/main.template' - options: - data: - message: 'WrapReplace' - class: 'foo' - wrap: - banner: '\n' - footer: '\n' - inject: [{ - prop: 'src' - rem: 'test/' - repl: - ".template": ".html" - }] - - - coffeelintOptions: - no_trailing_whitespace: - level: 'error' - no_tabs: - level: 'error' - indentation: - value: 2 - level: 'error' - line_endings: - value: 'unix' - level: 'error' - max_line_length: - value: 80 - level: 'warn' - - coffeelint: - grunt: 'Gruntfile.coffee' - tasks: './tasks/*.coffee' - tests: './test/*.coffee' - - # Load local tasks. - grunt.loadTasks 'tasks' - - # Load plugin tasks - grunt.loadNpmTasks 'grunt-coffeelint' - grunt.loadNpmTasks 'grunt-contrib-clean' - grunt.loadNpmTasks 'grunt-contrib-coffee' - - grunt.registerTask 'mocha', 'run mocha', () -> - done = grunt.task.current.async() - require('child_process').exec( - 'mocha --compilers coffee:coffee-script/register -R spec' - , (err, stdout) -> - grunt.log.write stdout - done err - ) - - # Default task. - grunt.registerTask 'default', [ - 'coffeelint' - 'template' - 'mocha' - 'clean' - ] \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..38a8cd7 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,161 @@ +'use strict'; + +module.exports = function(grunt) { + grunt.initConfig({ + clean: { + test: { + src: ['./test/actual/'] + }, + task: { + src: ['./tasks/template.js'] + } + }, + template: { + single: { + files: { + 'test/actual/main.html': 'test/source/main.template' + }, + options: { + data: { + message: 'Single', + "class": 'foo' + } + } + }, + css: { + files: { + 'test/actual/foo.min.css': 'test/source/foo.css' + }, + options: { + minify: { + pretty: { + lang: 'css', + mode: 'minify' + } + } + } + }, + min: { + files: { + 'test/actual/min.html': 'test/source/min.template' + }, + options: { + data: { + message: 'Minified', + "class": 'foo' + }, + minify: { + mode: 'html' + } + } + }, + concat: { + files: { + 'test/actual/concat.html': ['test/source/concat1.template', 'test/source/concat2.template'] + }, + options: { + data: { + message: 'Concat', + "class": 'foo' + } + } + }, + wrap: { + files: { + 'test/actual/wrapped.html': 'test/source/main.template' + }, + options: { + data: { + message: 'Wrap', + "class": 'foo' + }, + wrap: { + banner: '\n', + footer: '\n' + } + } + }, + advWrap: { + files: { + 'test/actual/wrappedAdv.html': 'test/source/main.template' + }, + options: { + data: { + message: 'Wrap', + "class": 'foo' + }, + wrap: { + banner: '\n', + footer: '\n', + inject: [ + { + prop: 'dest', + rem: 'test/' + } + ] + } + } + }, + concatWrap: { + files: { + 'test/actual/wrappedConcat.html': ['test/source/concat1.template', 'test/source/concat2.template'] + }, + options: { + data: { + message: 'WrapConcat', + "class": 'foo' + }, + wrap: { + banner: '\n', + footer: '\n', + inject: [ + { + prop: 'dest', + rem: 'test/' + } + ] + } + } + }, + advWrapReplace: { + files: { + 'test/actual/wrappedReplace.html': 'test/source/main.template' + }, + options: { + data: { + message: 'WrapReplace', + "class": 'foo' + }, + wrap: { + banner: '\n', + footer: '\n', + inject: [ + { + prop: 'src', + rem: 'test/', + repl: { + ".template": ".html" + } + } + ] + } + } + } + } + }); + + grunt.loadTasks('tasks'); + grunt.loadNpmTasks('grunt-contrib-clean'); + grunt.loadNpmTasks('grunt-contrib-coffee'); + + grunt.registerTask('mocha', 'run mocha', function() { + var done = grunt.task.current.async(); + + require('child_process').exec('mocha --compilers coffee:coffee-script/register -R spec', function(err, stdout) { + grunt.log.write(stdout); + done(err); + }); + }); + + grunt.registerTask('default', ['template', 'mocha', 'clean']); +}; \ No newline at end of file diff --git a/package.json b/package.json index e36cf1a..a1cdcd2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-template-helper", "description": "Grunt template processor and wrapper.", - "version": "0.1.7", + "version": "0.2.0", "homepage": "https://github.com/ciddan/grunt-template-helper", "author": { "name": "Mikael Selander", @@ -23,18 +23,17 @@ "test": "grunt test" }, "dependencies": { - "grunt-lib-contrib": "0.4.x", - "prettydiff": "0.0.228" + "prettydiff": "1.14.7" }, "devDependencies": { "grunt": "~0.4.2", - "grunt-coffeelint": "~0.0.8", - "grunt-contrib-clean": "~0.5.0", - "grunt-contrib-coffee": "~0.9.0", - "chai": "~1.9.0", - "coffee-script": "~1.7.1" + "grunt-coffeelint": "~0.0.13", + "grunt-contrib-clean": "~0.6.0", + "grunt-contrib-coffee": "~0.13.0", + "chai": "~3.4.0", + "coffee-script": "~1.10.0" }, - "_id": "grunt-template-helper@0.1.7", + "_id": "grunt-template-helper@0.2.0", "_from": "grunt-template-helper0.1.0", "keywords": [ "gruntplugin", diff --git a/tasks/template.coffee b/tasks/template.coffee deleted file mode 100644 index cf86791..0000000 --- a/tasks/template.coffee +++ /dev/null @@ -1,85 +0,0 @@ -'use strict' - -module.exports = (grunt) -> - minifier = require 'prettydiff' - - grunt.registerMultiTask 'template', 'Process grunt template files', () -> - helpers = require('grunt-lib-contrib').init grunt - - options = helpers.options @, () -> - data : {} - wrap : {} - minify : {} - - grunt.verbose.writeflags @.options, 'options' - - @.files.forEach((f) -> - grunt.log.writeln "src: #{f.src}, dest: #{f.dest}" - - out = f.src.filter((filepath) -> - grunt.file.exists filepath - ).map((filepath) -> - compileTemplate filepath, options, f.dest - ).join grunt.util.normalizelf grunt.util.linefeed - - if out.length < 1 - grunt.log.warn 'Nothing written because compiled files were empty.' - else - out = minify out, options if options.minify? - grunt.file.write f.dest, out - grunt.log.writeln "File #{f.dest.cyan} created." - ) - - minify = (src, options) -> - options.minify.pretty ?= - mode: 'minify' - lang: 'markup' - html: 'html-yes' - - options.minify.pretty.source = src - - try - minified = minifier.api(options.minify.pretty)[0] - catch e - grunt.log.error e - grunt.fail.warn 'Oh noes! Template minification failed :(' - - minified - - - compileTemplate = (filepath, options, dest) -> - src = grunt.file.read filepath - - try - wrap = options.wrap?.banner? and options.wrap?.footer? - inject = wrap and options.wrap.inject?.length >= 1 - - processed = grunt.template.process src, options - - if wrap - processed = "#{options.wrap.banner}#{processed}#{options.wrap.footer}" - - if wrap and inject - for inj, pos in options.wrap.inject - inj.rem ?= '' - inj.repl ?= {} - - switch inj.prop - when 'src' - inject = filepath.replace inj.rem, '' - when 'dest' - inject = dest.replace inj.rem, '' - else - inject = '#{' + pos + '}' - grunt.log.warn( - "Invalid inject property supplied, not - replacing positional marker" - ) - - inject = inject.replace k, v for k, v of inj.repl - processed = processed.replace('#{' + pos + '}', inject) - - return processed - catch e - grunt.log.error e - grunt.fail.warn 'Template copilation failed.' \ No newline at end of file diff --git a/test/expected/foo.min.css b/test/expected/foo.min.css index 7fe61c9..6b06e2a 100644 --- a/test/expected/foo.min.css +++ b/test/expected/foo.min.css @@ -1 +1 @@ -.foo {color:red;font-size:20px} \ No newline at end of file +.foo{font-size:20px;color:red} \ No newline at end of file diff --git a/test/expected/min.html b/test/expected/min.html index 1e4e58a..1ba73ad 100644 --- a/test/expected/min.html +++ b/test/expected/min.html @@ -1,3 +1,3 @@ -

Minified

lala
+

Minified

lala
 lol
-  
\ No newline at end of file +
\ No newline at end of file diff --git a/test/template-test.coffee b/test/template-test.coffee deleted file mode 100644 index e0af5b5..0000000 --- a/test/template-test.coffee +++ /dev/null @@ -1,78 +0,0 @@ -chai = require 'chai' -grunt = require 'grunt' -fs = require 'fs' - -assert = chai.assert - -describe 'Task template', -> - it 'should create destination file', -> - assert.isTrue grunt.file.exists './test/actual/main.html' - - it 'should replace variables with data', -> - source = grunt.file.read './test/source/main.template' - actual = grunt.file.read './test/actual/main.html' - - assert.isTrue(source.indexOf('<%= message %>') > 0) - assert.isTrue(actual.indexOf('<%= message %>') is -1) - - it 'should correctly format the file', -> - expected = grunt.file.read './test/expected/main.html' - actual = grunt.file.read './test/actual/main.html' - - assert.strictEqual actual, expected - - it 'should manage to concatenate files', -> - assert.isTrue grunt.file.exists('./test/actual/concat.html') - - expected = grunt.file.read './test/expected/concat.html' - actual = grunt.file.read './test/actual/concat.html' - - assert.strictEqual actual, expected - - it 'should be able to wrap templates', -> - assert.isTrue grunt.file.exists './test/actual/wrapped.html' - - expected = grunt.file.read './test/expected/wrapped.html' - actual = grunt.file.read './test/actual/wrapped.html' - - assert.strictEqual actual, expected - - it 'should be able to inject current filepath in wrapping', -> - assert.isTrue grunt.file.exists './test/actual/wrappedAdv.html' - - expected = grunt.file.read './test/expected/wrappedAdv.html' - actual = grunt.file.read './test/actual/wrappedAdv.html' - - assert.strictEqual actual, expected - - it 'should wrap fragments individually when concatenating and wrapping', -> - assert.isTrue grunt.file.exists './test/actual/wrappedConcat.html' - - expected = grunt.file.read './test/expected/wrappedConcat.html' - actual = grunt.file.read './test/actual/wrappedConcat.html' - - assert.strictEqual actual, expected - - it 'should be able to replace part of an injected value', -> - assert.isTrue grunt.file.exists './test/actual/wrappedReplace.html' - - expected = grunt.file.read './test/expected/wrappedReplace.html' - actual = grunt.file.read './test/actual/wrappedReplace.html' - - assert.strictEqual actual, expected - - it 'should be able to minify html output', -> - assert.isTrue grunt.file.exists './test/actual/min.html' - - expected = grunt.file.read './test/expected/min.html' - actual = grunt.file.read './test/actual/min.html' - - assert.strictEqual actual, expected - - it 'should be able to minify a css file', -> - assert.isTrue grunt.file.exists './test/actual/foo.min.css' - - expected = grunt.file.read './test/expected/foo.min.css' - actual = grunt.file.read './test/actual/foo.min.css' - - assert.strictEqual actual, expected \ No newline at end of file diff --git a/test/template-test.js b/test/template-test.js new file mode 100644 index 0000000..fc53140 --- /dev/null +++ b/test/template-test.js @@ -0,0 +1,88 @@ +var chai = require('chai'), + grunt = require('grunt'), + fs = require('fs'), + assert = chai.assert; + +describe('Task template', function() { + it('should create destination file', function() { + assert.isTrue(grunt.file.exists('./test/actual/main.html')); + }); + + it('should replace variables with data', function() { + var source = grunt.file.read('./test/source/main.template'); + var actual = grunt.file.read('./test/actual/main.html'); + + assert.isTrue(source.indexOf('<%= message %>') > 0); + assert.isTrue(actual.indexOf('<%= message %>') === -1); + }); + + it('should correctly format the file', function() { + var expected = grunt.file.read('./test/expected/main.html'); + var actual = grunt.file.read('./test/actual/main.html'); + + assert.strictEqual(actual, expected); + }); + + it('should manage to concatenate files', function() { + assert.isTrue(grunt.file.exists('./test/actual/concat.html')); + + var expected = grunt.file.read('./test/expected/concat.html'); + var actual = grunt.file.read('./test/actual/concat.html'); + + assert.strictEqual(actual, expected); + }); + + it('should be able to wrap templates', function() { + assert.isTrue(grunt.file.exists('./test/actual/wrapped.html')); + + var expected = grunt.file.read('./test/expected/wrapped.html'); + var actual = grunt.file.read('./test/actual/wrapped.html'); + + assert.strictEqual(actual, expected); + }); + + it('should be able to inject current filepath in wrapping', function() { + assert.isTrue(grunt.file.exists('./test/actual/wrappedAdv.html')); + + var expected = grunt.file.read('./test/expected/wrappedAdv.html'); + var actual = grunt.file.read('./test/actual/wrappedAdv.html'); + + assert.strictEqual(actual, expected); + }); + + it('should wrap fragments individually when concatenating and wrapping', function() { + assert.isTrue(grunt.file.exists('./test/actual/wrappedConcat.html')); + + var expected = grunt.file.read('./test/expected/wrappedConcat.html'); + var actual = grunt.file.read('./test/actual/wrappedConcat.html'); + + assert.strictEqual(actual, expected); + }); + + it('should be able to replace part of an injected value', function() { + assert.isTrue(grunt.file.exists('./test/actual/wrappedReplace.html')); + + var expected = grunt.file.read('./test/expected/wrappedReplace.html'); + var actual = grunt.file.read('./test/actual/wrappedReplace.html'); + + assert.strictEqual(actual, expected); + }); + + it('should be able to minify html output', function() { + assert.isTrue(grunt.file.exists('./test/actual/min.html')); + + var expected = grunt.file.read('./test/expected/min.html'); + var actual = grunt.file.read('./test/actual/min.html'); + + assert.strictEqual(actual, expected); + }); + + it('should be able to minify a css file', function() { + assert.isTrue(grunt.file.exists('./test/actual/foo.min.css')); + + var expected = grunt.file.read('./test/expected/foo.min.css'); + var actual = grunt.file.read('./test/actual/foo.min.css'); + + assert.strictEqual(actual, expected); + }); +}); \ No newline at end of file