forked from appium/appium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempdir.js
60 lines (51 loc) · 1.48 KB
/
tempdir.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* This library is originated from temp.js at http://github.com/bruce/node-temp */
"use strict";
var fs = require('fs')
, os = require('os')
, path = require('path')
, cnst = require('constants');
var RDWR_EXCL = cnst.O_CREAT | cnst.O_TRUNC | cnst.O_RDWR | cnst.O_EXCL;
var tempDir = function () {
var now = new Date();
var filePath = path.join(os.tmpDir(),
[now.getYear(), now.getMonth(), now.getDate(),
'-',
process.pid,
'-',
(Math.random() * 0x100000000 + 1).toString(36)].join(''));
if (!fs.existsSync(filePath)) {
fs.mkdirSync(filePath);
}
return filePath;
};
var generateName = function (rawAffixes, defaultPrefix) {
var affixes = parseAffixes(rawAffixes, defaultPrefix);
var name = [affixes.prefix, affixes.suffix].join('');
return path.join(tempDir(), name);
};
var open = function (affixes, callback) {
var filePath = generateName(affixes, 'f-');
fs.open(filePath, RDWR_EXCL, 384, function (err, fd) {
if (callback)
callback(err, {path: filePath, fd: fd});
});
};
var parseAffixes = function (rawAffixes, defaultPrefix) {
var affixes = {prefix: null, suffix: null};
if (rawAffixes) {
switch (typeof(rawAffixes)) {
case 'string':
affixes.prefix = rawAffixes;
break;
case 'object':
affixes = rawAffixes;
break;
default:
throw ("Unknown affix declaration: " + affixes);
}
} else {
affixes.prefix = defaultPrefix;
}
return affixes;
};
exports.open = open;