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 | var cachedTempDir;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function _tempDir() {
|
42 | if (cachedTempDir) return cachedTempDir;
|
43 |
|
44 | cachedTempDir = writeableDir(os.tmpdir()) ||
|
45 | writeableDir(process.env.TMPDIR) ||
|
46 | writeableDir(process.env.TEMP) ||
|
47 | writeableDir(process.env.TMP) ||
|
48 | writeableDir(process.env.Wimp$ScrapDir) ||
|
49 | writeableDir('C:\\TEMP') ||
|
50 | writeableDir('C:\\TMP') ||
|
51 | writeableDir('\\TEMP') ||
|
52 | writeableDir('\\TMP') ||
|
53 | writeableDir('/tmp') ||
|
54 | writeableDir('/var/tmp') ||
|
55 | writeableDir('/usr/tmp') ||
|
56 | writeableDir('.');
|
57 |
|
58 | return cachedTempDir;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 | function isCached() {
|
64 | return cachedTempDir;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | function clearCache() {
|
70 | cachedTempDir = undefined;
|
71 | }
|
72 |
|
73 | module.exports.tempDir = _tempDir;
|
74 | module.exports.isCached = isCached;
|
75 | module.exports.clearCache = clearCache;
|