diff --git a/shell.js b/shell.js index 7a4f4c8..b829f5e 100644 --- a/shell.js +++ b/shell.js @@ -1698,18 +1698,22 @@ function tempDir() { if (state.tempDir) return state.tempDir; // from cache - state.tempDir = writeableDir(process.env['TMPDIR']) || - writeableDir(process.env['TEMP']) || - writeableDir(process.env['TMP']) || - writeableDir(process.env['Wimp$ScrapDir']) || // RiscOS - writeableDir('C:\\TEMP') || // Windows - writeableDir('C:\\TMP') || // Windows - writeableDir('\\TEMP') || // Windows - writeableDir('\\TMP') || // Windows - writeableDir('/tmp') || - writeableDir('/var/tmp') || - writeableDir('/usr/tmp') || - writeableDir('.'); // last resort + state.tempDir = os.tmpDir ? + // node 0.8+ has `os.tmpDir()` + os.tmpDir() : + // back-compat for older node installs + writeableDir(process.env['TMPDIR']) || + writeableDir(process.env['TEMP']) || + writeableDir(process.env['TMP']) || + writeableDir(process.env['Wimp$ScrapDir']) || // RiscOS + writeableDir('C:\\TEMP') || // Windows + writeableDir('C:\\TMP') || // Windows + writeableDir('\\TEMP') || // Windows + writeableDir('\\TMP') || // Windows + writeableDir('/tmp') || + writeableDir('/var/tmp') || + writeableDir('/usr/tmp') || + writeableDir('.'); // last resort return state.tempDir; } diff --git a/test/tempdir.js b/test/tempdir.js index 704ca56..ebf5914 100644 --- a/test/tempdir.js +++ b/test/tempdir.js @@ -2,7 +2,8 @@ var shell = require('..'); var assert = require('assert'), path = require('path'), - fs = require('fs'); + fs = require('fs'), + os = require('os'); // Node shims for < v0.7 fs.existsSync = fs.existsSync || path.existsSync; @@ -21,6 +22,10 @@ shell.mkdir('tmp'); // var tmp = shell.tempdir(); +// node 0.8+ +if (os.tmpDir) { + assert.equal(os.tmpDir(), tmp); +} assert.equal(shell.error(), null); assert.equal(fs.existsSync(tmp), true);