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

Commit

Permalink
Merge pull request #52 from lostthetrail/master
Browse files Browse the repository at this point in the history
Allow overriding of environment variables for the rsync process
  • Loading branch information
Mattijs Hoitink authored Dec 23, 2016
2 parents 4f87bf1 + 2bd3a05 commit 4f21801
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ rsync.cwd(__dirname); // Set cwd to __dirname
rsync.cwd(); // Get cwd value
```

### env(envObj)

Set or get the value for rsync process environment variables.

Default: process.env

```javascript
rsync.env(process.env); // Set env to process.env
rsync.env(); // Get env values
```

### output(stdoutHandler, stderrHandler)

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

# Changelog

v0.6.0

- Added env() option to set the process environment variables (#51)

v0.5.0

- Properly treat flags as String
Expand Down
25 changes: 23 additions & 2 deletions rsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ function Rsync(config) {

this._cwd = process.cwd();

// Allow child_process.spawn env overriding
this._env = process.env;

// Debug parameter
this._debug = hasOP(config, 'debug') ? config.debug : false;
}
Expand Down Expand Up @@ -428,6 +431,24 @@ Rsync.prototype.cwd = function(cwd) {
return this._cwd;
};

/**
* Get and set rsync process environment variables
*
* @param {string} env= Environment variables
* @return {string} Return current _env.
*/
Rsync.prototype.env = function(env) {
if (arguments.length > 0) {
if (typeof env !== 'object') {
throw new Error('Environment should be an object');
}

this._env = env;
}

return this._env;
};

/**
* Register an output handlers for the commands stdout and stderr streams.
* These functions will be called once data is streamed on one of the output buffers
Expand Down Expand Up @@ -479,11 +500,11 @@ Rsync.prototype.execute = function(callback, stdoutHandler, stderrHandler) {
var cmdProc;
if ('win32' === process.platform) {
cmdProc = spawn('cmd.exe', ['/s', '/c', '"' + this.command() + '"'],
{ stdio: 'pipe', windowsVerbatimArguments: true, cwd: this._cwd });
{ stdio: 'pipe', windowsVerbatimArguments: true, cwd: this._cwd, env: this._env });
}
else {
cmdProc = spawn(this._executableShell, ['-c', this.command()],
{ stdio: 'pipe', cwd: this._cwd });
{ stdio: 'pipe', cwd: this._cwd, env: this._env });
}

// Capture stdout and stderr if there are output handlers configured
Expand Down
16 changes: 15 additions & 1 deletion tests/accessors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('accessors', function () {

describe('#cwd', function () {

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

});

describe('#env', function () {

it('should set the the env variables to use', function () {
var rsync = Rsync.build({
'source': 'a.txt',
'destination': 'b.txt',
'env': {'red': 'blue'}
});

assert.equal('blue', rsync.env().red, 'env was set');
});

});

});

0 comments on commit 4f21801

Please sign in to comment.