@sandro-sikic/maker 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -0
- package/index.js +33 -2
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ export function run(
|
|
|
14
14
|
opts?: { maxLines?: number },
|
|
15
15
|
): Promise<RunResult>;
|
|
16
16
|
|
|
17
|
+
export function onExit(cb: () => void | Promise<void>): () => void;
|
|
18
|
+
|
|
17
19
|
// runtime-forwarded objects (exported as values in JS)
|
|
18
20
|
export const prompt: typeof import('@inquirer/prompts');
|
|
19
21
|
export const spinner: typeof import('ora');
|
package/index.js
CHANGED
|
@@ -94,9 +94,40 @@ function run(command, opts = {}) {
|
|
|
94
94
|
});
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Register a callback to run when the process receives SIGINT (Ctrl+C).
|
|
99
|
+
* - Accepts a function (may be async).
|
|
100
|
+
* - Returns a disposer function that removes the listener.
|
|
101
|
+
*/
|
|
102
|
+
function onExit(cb) {
|
|
103
|
+
if (typeof cb !== 'function') {
|
|
104
|
+
throw new TypeError('onExit requires a callback function');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let called = false;
|
|
108
|
+
const handler = async () => {
|
|
109
|
+
if (called) return;
|
|
110
|
+
called = true;
|
|
111
|
+
try {
|
|
112
|
+
await Promise.resolve(cb());
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error('onExit callback error:', err);
|
|
115
|
+
} finally {
|
|
116
|
+
// ensure we exit after callback finishes (0 = success)
|
|
117
|
+
process.exit(0);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
process.on('SIGINT', handler);
|
|
122
|
+
|
|
123
|
+
// return a function that removes the listener in case the caller wants to cancel
|
|
124
|
+
return () => process.off('SIGINT', handler);
|
|
125
|
+
}
|
|
126
|
+
|
|
97
127
|
module.exports = {
|
|
98
128
|
init,
|
|
99
129
|
run,
|
|
100
|
-
|
|
101
|
-
|
|
130
|
+
onExit,
|
|
131
|
+
prompt: inquirer?.default || inquirer,
|
|
132
|
+
spinner: ora?.default || ora,
|
|
102
133
|
};
|