├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /LICENSE: -------------------------------------------------------------------------------- 1 | The ISC License 2 | 3 | Copyright (c) Isaac Z. Schlueter and Contributors 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR 15 | IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # exit-code 2 | 3 | `process.exitCode` behavior back-ported from io.js and Node.js 0.12+ 4 | 5 | ## USAGE 6 | 7 | ```javascript 8 | require('exit-code') 9 | 10 | process.exitCode = 2 11 | 12 | // do some other stuff 13 | // when the process exits, it'll do it with a code of 2 14 | ``` 15 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | if (process.version.match(/^v(0\.12\.|[^0])/)) 2 | return 3 | 4 | var code = 0 5 | Object.defineProperty(process, 'exitCode', { 6 | set: function (c) { 7 | if (typeof c !== 'number') 8 | throw new TypeError('exitCode must be a number') 9 | code = c 10 | if (exiting && code) 11 | process.exit(code) 12 | }, 13 | get: function () { 14 | return code 15 | }, 16 | enumerable: true, configurable: true 17 | }) 18 | 19 | var exiting = false 20 | process.on('exit', function (c) { 21 | exiting = true 22 | if (code && !c) 23 | process.exit(code) 24 | }) 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "exit-code", 3 | "version": "1.0.2", 4 | "description": "`process.exitCode` behavior back-ported from io.js and Node.js 0.12+", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/isaacs/exit-code.git" 12 | }, 13 | "author": "Isaac Z. Schlueter (http://blog.izs.me/)", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/isaacs/exit-code/issues" 17 | }, 18 | "homepage": "https://github.com/isaacs/exit-code#readme" 19 | } 20 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | if (!isNaN(process.argv[2])) { 2 | require('./') 3 | process.exitCode = +process.argv[2] 4 | setTimeout(function() { 5 | process.exit() 6 | }) 7 | return 8 | } 9 | 10 | if (!isNaN(process.argv[3])) { 11 | require('./') 12 | setTimeout(function () { 13 | process.on('exit', function () { 14 | process.exitCode = +process.argv[3] 15 | }) 16 | }) 17 | return 18 | } 19 | 20 | var test = require('tap').test 21 | var spawn = require('child_process').spawn 22 | var node = process.execPath 23 | var codes = [0, 1, 2, 3, 4, 5] 24 | 25 | test('immediate exit', function (t) { 26 | t.plan(codes.length) 27 | codes.forEach(function (want) { 28 | spawn(node, [__filename, want]).on('close', function (found) { 29 | t.equal(found, want, 'immediate exit ' + want) 30 | }) 31 | }) 32 | }) 33 | 34 | test('deferred exit', function (t) { 35 | t.plan(codes.length) 36 | codes.forEach(function (want) { 37 | spawn(node, [__filename, 'x', want]).on('close', function (found) { 38 | t.equal(found, want, 'deferred exit ' + want) 39 | }) 40 | }) 41 | }) 42 | --------------------------------------------------------------------------------