1 | var common = require('./common');
|
2 | var os = require('os');
|
3 | var fs = require('fs');
|
4 |
|
5 | common.register('tempdir', _tempDir, {
|
6 | allowGlobbing: false,
|
7 | wrapOutput: false,
|
8 | });
|
9 |
|
10 |
|
11 | function writeableDir(dir) {
|
12 | if (!dir || !fs.existsSync(dir)) return false;
|
13 |
|
14 | if (!common.statFollowLinks(dir).isDirectory()) return false;
|
15 |
|
16 | var testFile = dir + '/' + common.randomFileName();
|
17 | try {
|
18 | fs.writeFileSync(testFile, ' ');
|
19 | common.unlinkSync(testFile);
|
20 | return dir;
|
21 | } catch (e) {
|
22 |
|
23 | return false;
|
24 | }
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function _tempDir() {
|
40 | var state = common.state;
|
41 | if (state.tempDir) return state.tempDir;
|
42 |
|
43 | state.tempDir = writeableDir(os.tmpdir()) ||
|
44 | writeableDir(process.env.TMPDIR) ||
|
45 | writeableDir(process.env.TEMP) ||
|
46 | writeableDir(process.env.TMP) ||
|
47 | writeableDir(process.env.Wimp$ScrapDir) ||
|
48 | writeableDir('C:\\TEMP') ||
|
49 | writeableDir('C:\\TMP') ||
|
50 | writeableDir('\\TEMP') ||
|
51 | writeableDir('\\TMP') ||
|
52 | writeableDir('/tmp') ||
|
53 | writeableDir('/var/tmp') ||
|
54 | writeableDir('/usr/tmp') ||
|
55 | writeableDir('.');
|
56 |
|
57 | return state.tempDir;
|
58 | }
|
59 | module.exports = _tempDir;
|