UNPKG

2.86 kBJavaScriptView Raw
1var fs = require('fs')
2var os = require('os')
3var randomPath = require('random-path')
4
5var retry = require('./retry')
6var WriteStream = require('./write-stream')
7
8var tmpdir = os.tmpdir()
9
10function open (template, flags, mode, cb) {
11 switch (flags) {
12 case 'w': flags = 'wx'; break
13 case 'w+': flags = 'wx+'; break
14 default: throw new Error('Unknown file open flag: ' + flags)
15 }
16
17 if (typeof mode === 'function') {
18 cb = mode
19 mode = undefined
20 }
21
22 var path
23
24 retry.async(function (cb) {
25 path = randomPath(tmpdir, template)
26 fs.open(path, flags, mode, cb)
27 }, function (err, fd) {
28 cb(err, err ? undefined : { fd: fd, path: path })
29 })
30}
31
32function openSync (template, flags, mode) {
33 switch (flags) {
34 case 'w': flags = 'wx'; break
35 case 'w+': flags = 'wx+'; break
36 default: throw new Error('Unknown file open flag: ' + flags)
37 }
38
39 var path
40
41 var fd = retry.sync(function () {
42 path = randomPath(tmpdir, template)
43 return fs.openSync(path, flags, mode)
44 })
45
46 return { fd: fd, path: path }
47}
48
49function mkdir (template, mode, cb) {
50 if (typeof mode === 'function') {
51 cb = mode
52 mode = undefined
53 }
54
55 var path
56
57 retry.async(function (cb) {
58 path = randomPath(tmpdir, template)
59 fs.mkdir(path, mode, cb)
60 }, function (err) {
61 cb(err, err ? undefined : path)
62 })
63}
64
65function mkdirSync (template, mode) {
66 var path
67
68 retry.sync(function () {
69 path = randomPath(tmpdir, template)
70 fs.mkdirSync(path, mode)
71 })
72
73 return path
74}
75
76function writeFile (template, data, options, cb) {
77 cb = arguments[arguments.length - 1]
78
79 if (typeof options === 'function' || !options) {
80 options = { flag: 'wx' }
81 } else if (typeof options === 'string') {
82 options = { encoding: options, flag: 'wx' }
83 } else if (typeof options === 'object') {
84 options.flag = 'wx'
85 } else {
86 throw new TypeError('Bad arguments')
87 }
88
89 var path
90
91 retry.async(function (cb) {
92 path = randomPath(tmpdir, template)
93 fs.writeFile(path, data, options, cb)
94 }, function (err) {
95 cb(err, err ? undefined : path)
96 })
97}
98
99function writeFileSync (template, data, options, cb) {
100 if (!options) {
101 options = { flag: 'wx' }
102 } else if (typeof options === 'string') {
103 options = { encoding: options, flag: 'wx' }
104 } else if (typeof options === 'object') {
105 options.flag = 'wx'
106 } else {
107 throw new TypeError('Bad arguments')
108 }
109
110 var path
111
112 retry.sync(function () {
113 path = randomPath(tmpdir, template)
114 fs.writeFileSync(path, data, options)
115 })
116
117 return path
118}
119
120function createWriteStream (template, options) {
121 return new WriteStream(template, options)
122}
123
124exports.open = open
125exports.openSync = openSync
126exports.mkdir = mkdir
127exports.mkdirSync = mkdirSync
128exports.writeFile = writeFile
129exports.writeFileSync = writeFileSync
130exports.createWriteStream = createWriteStream