Skip to content

Commit 20bef7e

Browse files
committed
fix #143 prepend opts.dir || tmpDir if no path is given in the template
1 parent 0900dd5 commit 20bef7e

File tree

7 files changed

+16
-8
lines changed

7 files changed

+16
-8
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,14 @@ console.log('Dir: ', tmpobj.name);
237237

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

240+
IMPORTANT NOTE: template no longer accepts a path. Use the dir option instead if you
241+
require tmp to create your temporary filesystem object in a different place than the
242+
default `tmp.tmpdir`.
243+
240244
```javascript
241245
var tmp = require('tmp');
242246

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

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

257-
var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' });
261+
var tmpobj = tmp.dirSync({ template: 'tmp-XXXXXX' });
258262
console.log('Dir: ', tmpobj.name);
259263
```
260264

lib/tmp.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ function _generateTmpName(opts) {
121121

122122
// mkstemps like template
123123
if (opts.template) {
124-
return opts.template.replace(TEMPLATE_PATTERN, _randomChars(6));
124+
var template = opts.template;
125+
// make sure that we prepend the tmp path if none was given
126+
if (path.basename(template) == template)
127+
template = path.join(opts.dir || tmpDir, template);
128+
return template.replace(TEMPLATE_PATTERN, _randomChars(6));
125129
}
126130

127131
// prefix and postfix

test/dir-sync-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ vows.describe('Synchronous directory creation').addBatch({
5656

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

6262
'should return with a name': Test.assertNameSync,

test/dir-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ vows.describe('Directory creation').addBatch({
5757

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

6363
'should not return with error': assert.isNull,

test/file-sync-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ vows.describe('Synchronous file creation').addBatch({
9797

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

103103
'should return with a name': Test.assertNameSync,

test/file-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ vows.describe('File creation').addBatch({
100100

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

106106
'should not return with an error': assert.isNull,

test/name-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ vows.describe('Name creation').addBatch({
4040

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

4646
'should not return with error': assert.isNull,

0 commit comments

Comments
 (0)