Version 1
[yaffs-website] / node_modules / temp / README.md
1 node-temp
2 =========
3
4 Temporary files, directories, and streams for Node.js.
5
6 Handles generating a unique file/directory name under the appropriate
7 system temporary directory, changing the file to an appropriate mode,
8 and supports automatic removal (if asked)
9
10 `temp` has a similar API to the `fs` module.
11
12 Node.js Compatibility
13 ---------------------
14
15 Supports v0.10.0+.
16
17 [![Build Status](https://travis-ci.org/bruce/node-temp.png)](https://travis-ci.org/bruce/node-temp)
18
19 Please let me know if you have problems running it on a later version of Node.js or
20 have platform-specific problems.
21
22 Installation
23 ------------
24
25 Install it using [npm](http://github.com/isaacs/npm):
26
27     $ npm install temp
28
29 Or get it directly from:
30 http://github.com/bruce/node-temp
31
32 Synopsis
33 --------
34
35 You can create temporary files with `open` and `openSync`, temporary
36 directories with `mkdir` and `mkdirSync`, or you can get a unique name
37 in the system temporary directory with `path`.
38
39 Working copies of the following examples can be found under the
40 `examples` directory.
41
42 ### Temporary Files
43
44 To create a temporary file use `open` or `openSync`, passing
45 them an optional prefix, suffix, or both (see below for details on
46 affixes). The object passed to the callback (or returned) has
47 `path` and `fd` keys:
48
49 ```javascript
50 { path: "/path/to/file",
51 , fd: theFileDescriptor
52 }
53 ```
54
55 In this example we write to a temporary file and call out to `grep` and
56 `wc -l` to determine the number of time `foo` occurs in the text.  The
57 temporary file is chmod'd `0600` and cleaned up automatically when the
58 process at exit (because `temp.track()` is called):
59
60 ```javascript
61 var temp = require('temp'),
62     fs   = require('fs'),
63     util  = require('util'),
64     exec = require('child_process').exec;
65
66 // Automatically track and cleanup files at exit
67 temp.track();
68
69 // Fake data
70 var myData = "foo\nbar\nfoo\nbaz";
71
72 // Process the data (note: error handling omitted)
73 temp.open('myprefix', function(err, info) {
74   if (!err) {
75     fs.write(info.fd, myData);
76     fs.close(info.fd, function(err) {
77       exec("grep foo '" + info.path + "' | wc -l", function(err, stdout) {
78         util.puts(stdout.trim());
79       });
80     });
81   }
82 });
83 ```
84
85 ### Want Cleanup? Make sure you ask for it.
86
87 As noted in the example above, if you want temp to track the files and
88 directories it creates and handle removing those files and directories
89 on exit, you must call `track()`. The `track()` function is chainable,
90 and it's recommended that you call it when requiring the module.
91
92 ```javascript
93 var temp = require("temp").track();
94 ```
95
96 Why is this necessary? In pre-0.6 versions of temp, tracking was
97 automatic. While this works great for scripts and
98 [Grunt tasks](http://gruntjs.com/), it's not so great for long-running
99 server processes. Since that's arguably what Node.js is _for_, you
100 have to opt-in to tracking.
101
102 But it's easy.
103
104 #### Cleanup anytime
105
106 When tracking, you can run `cleanup()` and `cleanupSync()` anytime
107 (`cleanupSync()` will be run for you on process exit). An object will
108 be returned (or passed to the callback) with cleanup counts and
109 the file/directory tracking lists will be reset.
110
111 ```javascript
112 > temp.cleanupSync();
113 { files: 1,
114   dirs:  0 }
115 ```
116
117 ```javascript
118 > temp.cleanup(function(err, stats) {
119     console.log(stats);
120   });
121 { files: 1,
122   dirs:  0 }
123 ```
124
125 Note: If you're not tracking, an error ("not tracking") will be passed
126 to the callback.
127
128 ### Temporary Directories
129
130 To create a temporary directory, use `mkdir` or `mkdirSync`, passing
131 it an optional prefix, suffix, or both (see below for details on affixes).
132
133 In this example we create a temporary directory, write to a file
134 within it, call out to an external program to create a PDF, and read
135 the result.  While the external process creates a lot of additional
136 files, the temporary directory is removed automatically at exit (because
137 `temp.track()` is called):
138
139 ```javascript
140 var temp = require('temp'),
141     fs   = require('fs'),
142     util = require('util'),
143     path = require('path'),
144     exec = require('child_process').exec;
145
146 // Automatically track and cleanup files at exit
147 temp.track();
148
149 // For use with ConTeXt, http://wiki.contextgarden.net
150 var myData = "\\starttext\nHello World\n\\stoptext";
151
152 temp.mkdir('pdfcreator', function(err, dirPath) {
153   var inputPath = path.join(dirPath, 'input.tex')
154   fs.writeFile(inputPath, myData, function(err) {
155     if (err) throw err;
156     process.chdir(dirPath);
157     exec("texexec '" + inputPath + "'", function(err) {
158       if (err) throw err;
159       fs.readFile(path.join(dirPath, 'input.pdf'), function(err, data) {
160         if (err) throw err;
161         sys.print(data);
162       });
163     });
164   });
165 });
166 ```
167
168 ### Temporary Streams
169
170 To create a temporary WriteStream, use 'createWriteStream', which sits
171 on top of `fs.createWriteStream`. The return value is a
172 `fs.WriteStream` whose `path` is registered for removal when
173 `temp.cleanup` is called (because `temp.track()` is called).
174
175 ```javascript
176 var temp = require('temp');
177
178 // Automatically track and cleanup files at exit
179 temp.track();
180
181 var stream = temp.createWriteStream();
182 stream.write("Some data");
183 // Maybe do some other things
184 stream.end();
185 ```
186
187 ### Affixes
188
189 You can provide custom prefixes and suffixes when creating temporary
190 files and directories. If you provide a string, it is used as the prefix
191 for the temporary name. If you provide an object with `prefix`,
192 `suffix` and `dir` keys, they are used for the temporary name.
193
194 Here are some examples:
195
196 * `"aprefix"`: A simple prefix, prepended to the filename; this is
197   shorthand for:
198 * `{prefix: "aprefix"}`: A simple prefix, prepended to the filename
199 * `{suffix: ".asuffix"}`: A suffix, appended to the filename
200   (especially useful when the file needs to be named with specific
201   extension for use with an external program).
202 * `{prefix: "myprefix", suffix: "mysuffix"}`: Customize both affixes
203 * `{dir: path.join(os.tmpDir(), "myapp")}`: default prefix and suffix
204   within a new temporary directory.
205 * `null`: Use the defaults for files and directories (prefixes `"f-"`
206   and `"d-"`, respectively, no suffixes).
207
208 In this simple example we read a `pdf`, write it to a temporary file with
209 a `.pdf` extension, and close it.
210
211 ```javascript
212 var fs   = require('fs'),
213     temp = require('temp');
214
215 fs.readFile('/path/to/source.pdf', function(err, data) {
216   temp.open({suffix: '.pdf'}, function(err, info) {
217     if (err) throw err;
218     fs.write(info.fd, contents);
219     fs.close(info.fd, function(err) {
220       if (err) throw err;
221       // Do something with the file
222     });
223   });
224 });
225 ```
226
227 ### Just a path, please
228
229 If you just want a unique name in your temporary directory, use
230 `path`:
231
232 ```javascript
233 var fs = require('fs');
234 var tempName = temp.path({suffix: '.pdf'});
235 // Do something with tempName
236 ```
237
238 Note: The file isn't created for you, and the mode is not changed  -- and it
239 will not be removed automatically at exit.  If you use `path`, it's
240 all up to you.
241
242 Using it with Grunt
243 -------------------
244
245 If you want to use the module with [Grunt](http://gruntjs.com/), make sure you
246 use `async()` in your Gruntfile:
247
248 ```javascript
249 module.exports = function (grunt) {
250   var temp = require("temp");
251   temp.track(); // Cleanup files, please
252   grunt.registerTask("temptest", "Testing temp", function() {
253
254     var done = this.async(); // Don't forget this!
255
256     grunt.log.writeln("About to write a file...");
257     temp.open('tempfile', function(err, info) {
258       // File writing shenanigans here
259       grunt.log.writeln("Wrote a file!")
260
261       done(); // REALLY don't forget this!
262
263     });
264   });
265 };
266 ```
267
268 For more information, see the [Grunt FAQ](http://gruntjs.com/frequently-asked-questions#why-doesn-t-my-asynchronous-task-complete).
269
270 Testing
271 -------
272
273 ```sh
274 $ npm test
275 ```
276
277 Contributing
278 ------------
279
280 You can find the repository at:
281 http://github.com/bruce/node-temp
282
283 Issues/Feature Requests can be submitted at:
284 http://github.com/bruce/node-temp/issues
285
286 I'd really like to hear your feedback, and I'd love to receive your
287 pull-requests!
288
289 Copyright
290 ---------
291
292 Copyright (c) 2010-2014 Bruce Williams. This software is licensed
293 under the MIT License, see LICENSE for details.