From a3f1c166276973a5dd5ccc63d0c84fcd1005a2a2 Mon Sep 17 00:00:00 2001 From: Aria Stewart Date: Fri, 5 Jul 2013 16:37:56 -0400 Subject: [PATCH] Add conditional section processing --- index.js | 9 ++++++++- package.json | 1 + test/conditions.js | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/conditions.js diff --git a/index.js b/index.js index 06a4e61..b1dadad 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ var path = require('path'); var fs = require('fs'); var expansions = require('gyp-expansions'); var load = require('gyp-load'); +var cond = require('gyp-conditions'); var gyp = module.exports = function gyp(arg, variables, cb) { if (typeof arg == 'string' || arg instanceof String) { @@ -96,7 +97,13 @@ function handle(thing, variables, which, cb) { } if (thing.conditions) { - /// @todo do conditional processing + thing.conditions.forEach(function (e) { + if (cond(e[0], variables)) { + thing = merge(e[1], thing); + } else if (e[2]) { + thing = merge(e[2], thing); + } + }); } oiter(thing, function(e, cont, key) { diff --git a/package.json b/package.json index 62f7c37..0323dc0 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "gyp-merge": "0.2.x", "gyp-expansions": "0.0.x", + "gyp-conditions": "0.0.4", "gyp-load": "0.0.x" }, "devDependencies": { diff --git a/test/conditions.js b/test/conditions.js new file mode 100644 index 0000000..961d568 --- /dev/null +++ b/test/conditions.js @@ -0,0 +1,16 @@ +var test = require('tap').test; +var gyp = require('..'); + +test("Conditional sections", function(t) { + var tests = 2; + gyp({ a: 1, conditions: [["merge==1", { b: 2}]] }, {merge: 1}, function(err, out) { + t.equal(out.a, "1", "a should be 1"); + t.equal(out.b, "2", "b should be 2"); + if (--tests == 0) t.end(); + }); + gyp({ a: 1, conditions: [["merge==2", { b: 2}]] }, {merge: 1}, function(err, out) { + t.equal(out.a, "1", "a should be 1"); + t.not(out.b, "2", "b should not be 2"); + if (--tests == 0) t.end(); + }); +});