Comparing version
@@ -16,2 +16,3 @@ "use strict"; | ||
checkForUpdate: function () { return (0, meta_tools_1.checkForUpdate)(toolbox); }, | ||
onAbort: meta_tools_1.onAbort, | ||
}; | ||
@@ -18,0 +19,0 @@ toolbox.meta = meta; |
@@ -39,3 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.checkForUpdate = exports.commandInfo = exports.getVersion = exports.getPackageJSON = void 0; | ||
exports.onAbort = exports.checkForUpdate = exports.commandInfo = exports.getVersion = exports.getPackageJSON = void 0; | ||
var utils_1 = require("./utils"); | ||
@@ -123,2 +123,36 @@ /** | ||
exports.checkForUpdate = checkForUpdate; | ||
/** | ||
* Executes the given callback when a termination signal is received. | ||
* If callback returns a promise, it will wait for promise to resolve before aborting. | ||
* | ||
* @param callback Callback function for handling process termination | ||
*/ | ||
function onAbort(callback) { | ||
var _this = this; | ||
var signals = ['SIGINT', 'SIGQUIT', 'SIGTERM', 'SIGHUP', 'SIGBREAK']; | ||
signals.forEach(function (signal) { | ||
process.on(signal, function () { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, Promise.resolve()]; | ||
case 1: | ||
_a.sent(); | ||
signals.forEach(function (removeSignal) { | ||
// Remove listeners to prevent calling it multiple times | ||
process.removeAllListeners(removeSignal); | ||
// Add empty listeners to prevent terminating while onAbort callback is running | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
process.on(removeSignal, function () { }); | ||
}); | ||
return [4 /*yield*/, callback(signal)]; | ||
case 2: | ||
_a.sent(); | ||
process.exit(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}); | ||
} | ||
exports.onAbort = onAbort; | ||
//# sourceMappingURL=meta-tools.js.map |
@@ -0,1 +1,2 @@ | ||
import { onAbort } from '../toolbox/meta-tools'; | ||
import { GluegunToolbox } from '../domain/toolbox'; | ||
@@ -9,2 +10,3 @@ import { PackageJSON } from '../toolbox/meta-types'; | ||
checkForUpdate: () => Promise<boolean | string>; | ||
onAbort: typeof onAbort; | ||
} | ||
@@ -11,0 +13,0 @@ /** |
import { GluegunToolbox } from '../domain/toolbox'; | ||
import { PackageJSON } from './meta-types'; | ||
import { PackageJSON, AbortSignals } from './meta-types'; | ||
/** | ||
@@ -27,1 +27,8 @@ * Finds the currently running CLI package.json | ||
export declare function checkForUpdate(toolbox: GluegunToolbox): Promise<false | string>; | ||
/** | ||
* Executes the given callback when a termination signal is received. | ||
* If callback returns a promise, it will wait for promise to resolve before aborting. | ||
* | ||
* @param callback Callback function for handling process termination | ||
*/ | ||
export declare function onAbort(callback: (signal: AbortSignals) => void | Promise<void>): void; |
@@ -37,1 +37,2 @@ export interface PackageJSON { | ||
} | ||
export declare type AbortSignals = 'SIGINT' | 'SIGQUIT' | 'SIGTERM' | 'SIGHUP' | 'SIGBREAK'; |
@@ -49,1 +49,11 @@ Provides functions for accessing information about the currently running CLI. You can access this on the Gluegun toolbox. | ||
``` | ||
## onAbort | ||
Executes the given callback when a [termination signal](https://nodejs.org/api/process.html#process_signal_events) is received. These signals are `SIGINT`, `SIGQUIT`, `SIGTERM`, `SIGHUP`, `SIGBREAK`. If callback returns a promise, it will wait for promise to resolve before aborting. | ||
```js | ||
toolbox.meta.onAbort((signal) => { | ||
console.log('Received termination signal', signal) | ||
}) | ||
``` |
{ | ||
"name": "gluegun", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "A delightful toolkit for building Node-powered CLIs.", | ||
@@ -5,0 +5,0 @@ "repository": "infinitered/gluegun", |
@@ -114,3 +114,2 @@ [](https://www.npmjs.org/package/gluegun) | ||
- [Solidarity](https://github.com/infinitered/solidarity) - audits your system dependencies so you can develop in peace | ||
- [AWS Amplify CLI](https://github.com/aws-amplify/amplify-cli) - A CLI toolchain for simplifying serverless web and mobile development | ||
- [Sara Vieira's Fiddly](https://github.com/SaraVieira/fiddly) - Create beautiful and simple HTML pages from your Readme.md files - [https://fiddly.netlify.com](https://fiddly.netlify.com) | ||
@@ -120,2 +119,4 @@ - [Graph CLI](https://github.com/graphprotocol/graph-cli) - CLI for building and managing subgraphs that index data from Ethereum and IPFS - [https://thegraph.com/explorer](https://thegraph.com/explorer) | ||
*Additionally, the first versions of the [AWS Amplify CLI](https://github.com/aws-amplify/amplify-cli) (a CLI toolchain for simplifying serverless web and mobile development) used Gluegun. They've since integrated Gluegun's functionality into their CLI in a bespoke way, but you can still see Gluegun patterns in their CLI.* | ||
# What's under the hood? | ||
@@ -122,0 +123,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
388912
0.87%4681
0.97%147
0.68%