Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 2bd3a05

Browse files
author
Mark Reynolds
committed
Allow overriding of environment variables for the rsync process
1 parent 4f87bf1 commit 2bd3a05

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ rsync.cwd(__dirname); // Set cwd to __dirname
160160
rsync.cwd(); // Get cwd value
161161
```
162162

163+
### env(envObj)
164+
165+
Set or get the value for rsync process environment variables.
166+
167+
Default: process.env
168+
169+
```javascript
170+
rsync.env(process.env); // Set env to process.env
171+
rsync.env(); // Get env values
172+
```
173+
163174
### output(stdoutHandler, stderrHandler)
164175

165176
Register output handler functions for the commands stdout and stderr output. The handlers will be
@@ -394,6 +405,10 @@ If there is something broken (which there probably is), the same applies: fork,
394405

395406
# Changelog
396407

408+
v0.6.0
409+
410+
- Added env() option to set the process environment variables (#51)
411+
397412
v0.5.0
398413

399414
- Properly treat flags as String

rsync.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ function Rsync(config) {
7575

7676
this._cwd = process.cwd();
7777

78+
// Allow child_process.spawn env overriding
79+
this._env = process.env;
80+
7881
// Debug parameter
7982
this._debug = hasOP(config, 'debug') ? config.debug : false;
8083
}
@@ -428,6 +431,24 @@ Rsync.prototype.cwd = function(cwd) {
428431
return this._cwd;
429432
};
430433

434+
/**
435+
* Get and set rsync process environment variables
436+
*
437+
* @param {string} env= Environment variables
438+
* @return {string} Return current _env.
439+
*/
440+
Rsync.prototype.env = function(env) {
441+
if (arguments.length > 0) {
442+
if (typeof env !== 'object') {
443+
throw new Error('Environment should be an object');
444+
}
445+
446+
this._env = env;
447+
}
448+
449+
return this._env;
450+
};
451+
431452
/**
432453
* Register an output handlers for the commands stdout and stderr streams.
433454
* These functions will be called once data is streamed on one of the output buffers
@@ -479,11 +500,11 @@ Rsync.prototype.execute = function(callback, stdoutHandler, stderrHandler) {
479500
var cmdProc;
480501
if ('win32' === process.platform) {
481502
cmdProc = spawn('cmd.exe', ['/s', '/c', '"' + this.command() + '"'],
482-
{ stdio: 'pipe', windowsVerbatimArguments: true, cwd: this._cwd });
503+
{ stdio: 'pipe', windowsVerbatimArguments: true, cwd: this._cwd, env: this._env });
483504
}
484505
else {
485506
cmdProc = spawn(this._executableShell, ['-c', this.command()],
486-
{ stdio: 'pipe', cwd: this._cwd });
507+
{ stdio: 'pipe', cwd: this._cwd, env: this._env });
487508
}
488509

489510
// Capture stdout and stderr if there are output handlers configured

tests/accessors.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('accessors', function () {
3939

4040
describe('#cwd', function () {
4141

42-
it('should set the the executable shell to use', function () {
42+
it('should set the the cwd to use', function () {
4343
var rsync = Rsync.build({
4444
'source': 'a.txt',
4545
'destination': 'b.txt',
@@ -51,4 +51,18 @@ describe('accessors', function () {
5151

5252
});
5353

54+
describe('#env', function () {
55+
56+
it('should set the the env variables to use', function () {
57+
var rsync = Rsync.build({
58+
'source': 'a.txt',
59+
'destination': 'b.txt',
60+
'env': {'red': 'blue'}
61+
});
62+
63+
assert.equal('blue', rsync.env().red, 'env was set');
64+
});
65+
66+
});
67+
5468
});

0 commit comments

Comments
 (0)