From fdaf9b57ffe761a3bc6a970383e951185bd851f9 Mon Sep 17 00:00:00 2001 From: Sal Date: Wed, 27 Apr 2016 13:12:03 -0400 Subject: [PATCH] 0.10.0 --- index.js | 37 ++++++++++++++++++++++--------------- package.json | 6 +++--- test/test.js | 13 +++++++++++++ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index d778358..37ee6f2 100644 --- a/index.js +++ b/index.js @@ -127,17 +127,24 @@ function TemplateEngine() { set: function (transformed) { var deferred = new Promise(function (resolve, reject) { - var parts = transformed.contents.split(/(?=templateUrl)(?!,)/g); + try { + var parts = transformed.contents.split(/(?=templateUrl)(?!,)/g); - parts.forEach(function (element, index, arr) { + if (parts.length === 1) + throw 'unable to set template: no templateUrl clause'; - var match = (element.match(/(?!,)templateUrl(.*)$/gm)); + parts.forEach(function (element, index, arr) { - if (match) - arr[index] = arr[index].replace(/(?!,)templateUrl(.*)(?!,)$/gm, 'template: \'' + transformed.templates.shift().replace(/'/g, "\\'") + '\',') - }); + var match = (element.match(/(?!,)templateUrl(.*)$/gm)); + + if (match) + arr[index] = arr[index].replace(/(?!,)templateUrl(.*)(?!,)$/gm, 'template: \'' + transformed.templates.shift().replace(/'/g, "\\'") + '\',') + }); - resolve(parts.join('')); + resolve(parts.join('')); + } catch (err) { + reject(err) + } }); return deferred; @@ -160,13 +167,13 @@ function TemplateManager() { engine.config.set(config); } - var base, css; + var base, js; - function run(css) { - engine.templates.get(css).then(function (transformed) { + function run(js) { + engine.templates.get(js).then(function (transformed) { engine.templates.set(transformed).then(function (output) { done(null, output); // -- out - }, function(err){ + }, function (err) { done(err) // -- templates.set promise error }); }, function (err) { @@ -176,13 +183,13 @@ function TemplateManager() { if (input instanceof Buffer) { base = '/' + path.dirname(path.relative(__dirname, config.target)); - css = engine.source.hash(input.toString(), base); - run(css); + js = engine.source.hash(input.toString(), base); + run(js); } else { engine.source.read(input).then(function (data) { base = path.dirname(input); - css = engine.source.hash(data, base); - run(css); + js = engine.source.hash(data, base); + run(js); }); } } diff --git a/package.json b/package.json index b2cf48d..08fb6d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-tpl2js", - "version": "0.0.9", + "version": "0.0.10", "description": "Convert Angular templates to inline JavaScript", "files": [ "index.js" @@ -22,9 +22,9 @@ "vinyl": "^1.1.1" }, "dependencies": { - "async": "^1.5.2", + "async": "^2.0.0-rc.3", "cheerio": "^0.20.0", - "html-minifier": "^1.2.0" + "html-minifier": "^2.1.0" }, "scripts": { "test": "gulp test" diff --git a/test/test.js b/test/test.js index 29aaad2..fb81c54 100644 --- a/test/test.js +++ b/test/test.js @@ -321,6 +321,19 @@ describe('tpl2js: engine', function () { done(); }); }); + + it('should gracefully abort on error encountered when setting a template - error exist', function (done) { + + var js = new File({ + path: '/nomatter', + contents: new Buffer('this doesn\'t seem right!') + }); + + tpl2js.inline(js.contents, {target: '/nomatter'}, function (err, actual) { + expect(err).to.exist && expect(err).to.equal('unable to set template: no templateUrl clause') + done(); + }); + }); }); describe('tpl2js', function () {