Skip to content

Commit

Permalink
fix #143 prepend opts.dir || tmpDir if no path is given in the template
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Jul 14, 2017
1 parent 8f73d89 commit 4cde9c3
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 8 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,14 @@ console.log('Dir: ', tmpobj.name);

Creates a new temporary directory with mode `0700` and filename like `/tmp/tmp-nk2J1u`.

IMPORTANT NOTE: template no longer accepts a path. Use the dir option instead if you
require tmp to create your temporary filesystem object in a different place than the
default `tmp.tmpdir`.

```javascript
var tmp = require('tmp');

tmp.dir({ template: '/tmp/tmp-XXXXXX' }, function _tempDirCreated(err, path) {
tmp.dir({ template: 'tmp-XXXXXX' }, function _tempDirCreated(err, path) {
if (err) throw err;

console.log('Dir: ', path);
Expand All @@ -254,7 +258,7 @@ This will behave similarly to the asynchronous version.
```javascript
var tmp = require('tmp');

var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' });
var tmpobj = tmp.dirSync({ template: 'tmp-XXXXXX' });
console.log('Dir: ', tmpobj.name);
```

Expand Down
6 changes: 5 additions & 1 deletion lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ function _generateTmpName(opts) {

// mkstemps like template
if (opts.template) {
return opts.template.replace(TEMPLATE_PATTERN, _randomChars(6));
var template = opts.template;
// make sure that we prepend the tmp path if none was given
if (path.basename(template) == template)
template = path.join(opts.dir || tmpDir, template);
return template.replace(TEMPLATE_PATTERN, _randomChars(6));
}

// prefix and postfix
Expand Down
2 changes: 1 addition & 1 deletion test/dir-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ vows.describe('Synchronous directory creation').addBatch({

'when using template': {
topic: function () {
return tmp.dirSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
return tmp.dirSync({ template: 'clike-XXXXXX-postfix' });
},

'should return with a name': Test.assertNameSync,
Expand Down
2 changes: 1 addition & 1 deletion test/dir-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ vows.describe('Directory creation').addBatch({

'when using template': {
topic: function () {
tmp.dir({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
tmp.dir({ template: 'clike-XXXXXX-postfix' }, this.callback);
},

'should not return with error': assert.isNull,
Expand Down
2 changes: 1 addition & 1 deletion test/file-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ vows.describe('Synchronous file creation').addBatch({

'when using template': {
topic: function () {
return tmp.fileSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
return tmp.fileSync({ template: 'clike-XXXXXX-postfix' });
},

'should return with a name': Test.assertNameSync,
Expand Down
2 changes: 1 addition & 1 deletion test/file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ vows.describe('File creation').addBatch({

'when using template': {
topic: function () {
tmp.file({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
tmp.file({ template: 'clike-XXXXXX-postfix' }, this.callback);
},

'should not return with an error': assert.isNull,
Expand Down
2 changes: 1 addition & 1 deletion test/name-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ vows.describe('Name creation').addBatch({

'when using template': {
topic: function () {
tmp.tmpName({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
tmp.tmpName({ template: 'clike-XXXXXX-postfix' }, this.callback);
},

'should not return with error': assert.isNull,
Expand Down

0 comments on commit 4cde9c3

Please sign in to comment.