Skip to content

Commit 9fb08cb

Browse files
kinfuykinfuy
authored andcommitted
fix: detail
1 parent a5ce3dc commit 9fb08cb

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"prettier-ci": "prettier --check '**/*.{ts,json,md,yml}'"
2121
},
2222
"peerDependencies": {
23-
"typescript": "^4",
24-
"vite": "^2 || ^3 || ^4"
23+
"vite": "^4"
2524
},
2625
"devDependencies": {
2726
"@types/cross-spawn": "^6.0.2",

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ import { bindShortcuts, CLIShortcut } from './shortcuts';
33

44
let started = false;
55

6-
export function shortcutsPlugin(shortcutsOptions: CLIShortcut[]): Plugin {
6+
export interface ShortcutsOptions {
7+
/**
8+
* ctrl+c or ctrl+d
9+
* @description default use process.exit(1) exit
10+
*/
11+
dealWithctrl?: boolean | ((server:ViteDevServer)=>void)
12+
shortcuts: CLIShortcut[];
13+
}
14+
15+
export function shortcutsPlugin(shortcutsOptions?: ShortcutsOptions): Plugin {
716
return {
817
name: 'shortcuts',
918
apply: 'serve',
1019
configureServer: (server: ViteDevServer) => {
1120
return () => {
1221
if (started) return;
13-
bindShortcuts(server, { print: true, customShortcuts: shortcutsOptions });
22+
bindShortcuts(server, shortcutsOptions);
1423
started = true;
1524
};
1625
},

src/shortcuts.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
11
import colors from 'picocolors';
22
import type { ViteDevServer } from 'vite';
3+
import { ShortcutsOptions } from '.';
34

45
export function isDefined<T>(value: T | undefined | null): value is T {
56
return value != null;
67
}
78

8-
export type BindShortcutsOptions = {
9-
/**
10-
* Print a one line hint to the terminal.
11-
*/
12-
print?: boolean;
13-
customShortcuts?: (CLIShortcut | undefined | null)[];
14-
};
15-
169
export type CLIShortcut = {
1710
key: string;
1811
description: string;
1912
action(server: ViteDevServer): void | Promise<void>;
2013
};
2114

22-
export function bindShortcuts(server: ViteDevServer, opts?: BindShortcutsOptions): void {
15+
export function bindShortcuts(server: ViteDevServer, opts?: ShortcutsOptions): void {
2316
if (!server.httpServer || !process.stdin.isTTY || process.env.CI) {
2417
return;
2518
}
2619

27-
const shortcuts = (opts?.customShortcuts ?? []).filter(isDefined).concat(BASE_SHORTCUTS);
20+
const shortcuts = (opts?.shortcuts ?? []).filter(isDefined).concat(BASE_SHORTCUTS);
2821

2922
let actionRunning = false;
3023

3124
const onInput = async (input: string) => {
3225
// ctrl+c or ctrl+d
3326
if (input === '\x03' || input === '\x04') {
34-
process.emit('SIGTERM')
35-
// process.exit(1);
27+
if (opts?.dealWithctrl) {
28+
if (typeof opts.dealWithctrl === 'function') opts.dealWithctrl(server);
29+
else process.exit(1)
30+
}
3631
}
3732

3833
if (actionRunning) return;

0 commit comments

Comments
 (0)