UNPKG

2.35 kBMarkdownView Raw
1# exit [![Build Status](https://secure.travis-ci.org/cowboy/node-exit.png?branch=master)](http://travis-ci.org/cowboy/node-exit)
2
3A replacement for process.exit that ensures stdio are fully drained before exiting.
4
5To make a long story short, if `process.exit` is called on Windows, script output is often truncated when pipe-redirecting `stdout` or `stderr`. This module attempts to work around this issue by waiting until those streams have been completely drained before actually calling `process.exit`.
6
7See [Node.js issue #3584](https://github.com/joyent/node/issues/3584) for further reference.
8
9Tested in OS X 10.8, Windows 7 on Node.js 0.8.25 and 0.10.18.
10
11Based on some code by [@vladikoff](https://github.com/vladikoff).
12
13## Getting Started
14Install the module with: `npm install exit`
15
16```javascript
17var exit = require('exit');
18
19// These lines should appear in the output, EVEN ON WINDOWS.
20console.log("omg");
21console.error("yay");
22
23// process.exit(5);
24exit(5);
25
26// These lines shouldn't appear in the output.
27console.log("wtf");
28console.error("bro");
29```
30
31## Don't believe me? Try it for yourself.
32
33In Windows, clone the repo and cd to the `test\fixtures` directory. The only difference between [log.js](test/fixtures/log.js) and [log-broken.js](test/fixtures/log-broken.js) is that the former uses `exit` while the latter calls `process.exit` directly.
34
35This test was done using cmd.exe, but you can see the same results using `| grep "std"` in either PowerShell or git-bash.
36
37```
38C:\node-exit\test\fixtures>node log.js 0 10 stdout stderr 2>&1 | find "std"
39stdout 0
40stderr 0
41stdout 1
42stderr 1
43stdout 2
44stderr 2
45stdout 3
46stderr 3
47stdout 4
48stderr 4
49stdout 5
50stderr 5
51stdout 6
52stderr 6
53stdout 7
54stderr 7
55stdout 8
56stderr 8
57stdout 9
58stderr 9
59
60C:\node-exit\test\fixtures>node log-broken.js 0 10 stdout stderr 2>&1 | find "std"
61
62C:\node-exit\test\fixtures>
63```
64
65## Contributing
66In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
67
68## Release History
692013-11-26 - v0.1.2 - Fixed a bug with hanging processes.
702013-09-26 - v0.1.1 - Fixed some bugs. It seems to actually work now!
712013-09-20 - v0.1.0 - Initial release.
72
73## License
74Copyright (c) 2013 "Cowboy" Ben Alman
75Licensed under the MIT license.