Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skyway777 have you tested if single curly braces could work with mustache? Maybe we could remove the old replacing mechanism entirely?!

Copy link
Copy Markdown
Author

@skyway777 skyway777 Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't support single curly braces...

if (pkg[key] === undefined) { return org; }
return pkg[key];
});
});

return obj;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 () {
Expand Down