diff --git a/index.js b/index.js index 35e12c3..fb69e6c 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var exec = require('child_process').exec; var defineOpts = require('define-options'); var semver = require('semver'); var isBinaryFile = require('isbinaryfile'); +var Mustache = require('mustache'); var validateConfig, validateRepos, validateRepo, userConfig; @@ -53,15 +54,21 @@ function readPackageJSON (encoding) { function filterConfig (configTmpl, pkg) { // create a config object from the config template // replace {key} with the key's value in package.json + // replace ${key} with eval(key) var obj = extend({}, configTmpl); + var replacibleKeys = { + env: process.env, + package: pkg + }; Object.keys(obj).forEach(function (key) { var value = obj[key]; if (typeof value != 'string') { return; } - obj[key] = value.replace(/{([^}]+)}/g, function (org, key) { - if (pkg[key] === undefined) { return org; } - return pkg[key]; - }); + obj[key] = Mustache.render(value, replacibleKeys) + .replace(/{([^}]+)}/g, function (org, key) { + if (pkg[key] === undefined) { return org; } + return pkg[key]; + }); }); return obj; diff --git a/package.json b/package.json index b5b3944..90609c6 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "fs-walk": "0.0.1", "isbinaryfile": "^3.0.0", "jszip": "^2.5.0", + "mustache": "^2.3.0", "semver": "^5.1.0", "util-extend": "^1.0.3" }, diff --git a/test.js b/test.js index 5823c83..6168e8f 100644 --- a/test.js +++ b/test.js @@ -42,6 +42,13 @@ const GROUP_ID = 'com.dummy', classifier: TEST_CLASSIFIER, generatePom: false, pomFile: 'existing-pom.xml' + }, + TEST_CONFIG_WITH_ENV_VARIABLE = { + groupId: GROUP_ID, + repositories: [DUMMY_REPO_SNAPSHOT, DUMMY_REPO_RELEASE], + classifier: TEST_CLASSIFIER, + generatePom: false, + finalName: '{name}-{{env.NODE_ENV}}.{{package.version}}', }; var childProcessMock; @@ -118,6 +125,7 @@ describe('maven-deploy', function () { execSpy = childProcessMock.exec; fs = createFakeFS(); + process.env.NODE_ENV = 'test'; maven = proxyquire('./index.js', { 'child_process': childProcessMock, @@ -140,6 +148,19 @@ describe('maven-deploy', function () { }); }); }); + it('should render correct config with {env} and {package} values', function () { + const EXPECTED_ARGS = [ + '-Dfile=dist' + path.sep + TEST_PKG_JSON.name + '-' + + process.env.NODE_ENV + + '.' + semver.inc(TEST_PKG_JSON.version, 'patch') + + '-SNAPSHOT' + '.war', + ]; + maven.config(TEST_CONFIG_WITH_ENV_VARIABLE); + maven.install(); + + assert.equal(process.env.NODE_ENV, 'test'); + assertArgs(execSpy.args[0][0], EXPECTED_ARGS); + }); }); describe('package', function () {