Skip to content

Commit 449e629

Browse files
authored
Merge pull request #166 from devMYC/fix
fixed command line bug when passing env variables to post-deploy hook
2 parents 2f22bb1 + 7525672 commit 449e629

2 files changed

Lines changed: 77 additions & 6 deletions

File tree

deploy.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ function deployForEnv(deploy_conf, env, args, cb) {
8787
if (process.platform !== 'win32' && process.platform !== 'win64')
8888
target_conf.path = path.resolve(target_conf.path);
8989

90+
var originalPostDeploy = typeof target_conf['post-deploy'] === 'string'
91+
? target_conf['post-deploy']
92+
: '';
9093
if (Array.isArray(target_conf.host)) {
9194
series(target_conf.host.reduce(function (jobs, host) {
9295
jobs.push(function (done) {
@@ -96,10 +99,12 @@ function deployForEnv(deploy_conf, env, args, cb) {
9699
}
97100

98101
target_conf.host = host;
99-
target_conf['post-deploy'] = 'export ' + objectToEnvVars(target_conf.env) + ' && ' + target_conf['post-deploy']
100-
var custom_data = JSON.stringify(target_conf);
102+
target_conf['post-deploy'] = prependEnvVars(
103+
originalPostDeploy,
104+
objectToEnvVars(target_conf.env)
105+
);
101106

102-
spawn(custom_data, args, done);
107+
spawn(JSON.stringify(target_conf), args, done);
103108
});
104109
return jobs;
105110
}, []), cb);
@@ -109,7 +114,11 @@ function deployForEnv(deploy_conf, env, args, cb) {
109114
console.log('--> on host %s', target_conf.host);
110115
}
111116

112-
target_conf['post-deploy'] = 'export ' + objectToEnvVars(target_conf.env) + ' && ' + target_conf['post-deploy']
117+
target_conf['post-deploy'] = prependEnvVars(
118+
originalPostDeploy,
119+
objectToEnvVars(target_conf.env)
120+
);
121+
113122
spawn(JSON.stringify(target_conf), args, cb);
114123
}
115124

@@ -122,6 +131,14 @@ function objectToEnvVars(obj) {
122131
}).join(' ')
123132
}
124133

134+
/**
135+
* @param {string} cmd
136+
* @param {string} envVars
137+
*/
138+
function prependEnvVars(cmd, envVars) {
139+
return (envVars && 'export ' + envVars + (cmd && ' && ')) + cmd;
140+
}
141+
125142
module.exports = {
126143
deployForEnv: deployForEnv
127144
};

test/deploy.mocha.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ describe('deploy', function() {
3232
host: 'host',
3333
repo: 'repo',
3434
path: 'path',
35-
ref: 'ref'
35+
ref: 'ref',
36+
'post-deploy': 'post-deploy',
3637
}
3738
}
3839
})
@@ -145,7 +146,26 @@ describe('deploy', function() {
145146
echoData.repo.should.eql(conf.staging.repo)
146147
echoData.path.should.eql(path.resolve(conf.staging.path))
147148
echoData.host.should.eql(conf.staging.host)
148-
done()
149+
echoData['post-deploy'].should.eql(conf.staging['post-deploy'])
150+
151+
conf.staging.env = { a: 1, b: 2 }
152+
deploy.deployForEnv(conf, 'staging', [], function() {
153+
spawnCalls.length.should.equal(2)
154+
spawnCalls[1][1][1].should.be.a.String
155+
echoData = JSON.parse( spawnCalls[1][1][1].match(/^echo '(.+?)'/)[1] )
156+
echoData['post-deploy'].should.eql(
157+
'export A=1 B=2 && ' + conf.staging['post-deploy']
158+
)
159+
160+
conf.staging['post-deploy'] = ''
161+
deploy.deployForEnv(conf, 'staging', [], function() {
162+
spawnCalls.length.should.equal(3)
163+
spawnCalls[2][1][1].should.be.a.String
164+
echoData = JSON.parse( spawnCalls[2][1][1].match(/^echo '(.+?)'/)[1] )
165+
echoData['post-deploy'].should.eql('export A=1 B=2')
166+
done()
167+
})
168+
})
149169
})
150170
})
151171

@@ -231,6 +251,7 @@ describe('deploy', function() {
231251
echoData.repo.should.eql(conf.staging.repo)
232252
echoData.path.should.eql(path.resolve(conf.staging.path))
233253
echoData.host.should.eql(hosts[spawnCount])
254+
echoData['post-deploy'].should.eql(conf.staging['post-deploy'])
234255

235256
spawnCount += 1
236257

@@ -245,6 +266,39 @@ describe('deploy', function() {
245266
})
246267
})
247268

269+
it('echoes JSON blobs with customized host and env attributes', function(done) {
270+
var spawnCount = 0
271+
272+
spawnNotifier.on('spawned', function(proc) {
273+
var pipeFrom = spawnCalls[spawnCount][1][1].split(/\s*\|\s*/)[0]
274+
pipeFrom.should.be.ok
275+
276+
var echoJSON = pipeFrom.match(/^echo '(.+?)'/)[1]
277+
echoJSON.should.be.ok
278+
279+
var echoData = JSON.parse(echoJSON)
280+
echoData.should.be.an.Object
281+
282+
echoData.ref.should.eql(conf.staging.ref)
283+
echoData.repo.should.eql(conf.staging.repo)
284+
echoData.path.should.eql(path.resolve(conf.staging.path))
285+
echoData.host.should.eql(hosts[spawnCount])
286+
echoData['post-deploy'].should.eql('export A=1 B=2 && ' + conf.staging['post-deploy'])
287+
288+
spawnCount += 1
289+
290+
process.nextTick(function() {
291+
proc.emit('close', 0)
292+
})
293+
})
294+
295+
Object.assign(conf.staging, { env: { a: 1, b: 2 } })
296+
deploy.deployForEnv(conf, 'staging', [], function() {
297+
spawnCount.should.eql(4)
298+
done()
299+
})
300+
})
301+
248302
it('invokes our callback with supplied argument arrays', function(done) {
249303
var argsIn = [1,2,'three','four']
250304
spawnNotifier.on('spawned', function(proc) {

0 commit comments

Comments
 (0)