File tree Expand file tree Collapse file tree 3 files changed +19
-16
lines changed Expand file tree Collapse file tree 3 files changed +19
-16
lines changed Original file line number Diff line number Diff line change 20
20
"prettier-ci" : " prettier --check '**/*.{ts,json,md,yml}'"
21
21
},
22
22
"peerDependencies" : {
23
- "typescript" : " ^4" ,
24
- "vite" : " ^2 || ^3 || ^4"
23
+ "vite" : " ^4"
25
24
},
26
25
"devDependencies" : {
27
26
"@types/cross-spawn" : " ^6.0.2" ,
Original file line number Diff line number Diff line change @@ -3,14 +3,23 @@ import { bindShortcuts, CLIShortcut } from './shortcuts';
3
3
4
4
let started = false ;
5
5
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 {
7
16
return {
8
17
name : 'shortcuts' ,
9
18
apply : 'serve' ,
10
19
configureServer : ( server : ViteDevServer ) => {
11
20
return ( ) => {
12
21
if ( started ) return ;
13
- bindShortcuts ( server , { print : true , customShortcuts : shortcutsOptions } ) ;
22
+ bindShortcuts ( server , shortcutsOptions ) ;
14
23
started = true ;
15
24
} ;
16
25
} ,
Original file line number Diff line number Diff line change 1
1
import colors from 'picocolors' ;
2
2
import type { ViteDevServer } from 'vite' ;
3
+ import { ShortcutsOptions } from '.' ;
3
4
4
5
export function isDefined < T > ( value : T | undefined | null ) : value is T {
5
6
return value != null ;
6
7
}
7
8
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
-
16
9
export type CLIShortcut = {
17
10
key : string ;
18
11
description : string ;
19
12
action ( server : ViteDevServer ) : void | Promise < void > ;
20
13
} ;
21
14
22
- export function bindShortcuts ( server : ViteDevServer , opts ?: BindShortcutsOptions ) : void {
15
+ export function bindShortcuts ( server : ViteDevServer , opts ?: ShortcutsOptions ) : void {
23
16
if ( ! server . httpServer || ! process . stdin . isTTY || process . env . CI ) {
24
17
return ;
25
18
}
26
19
27
- const shortcuts = ( opts ?. customShortcuts ?? [ ] ) . filter ( isDefined ) . concat ( BASE_SHORTCUTS ) ;
20
+ const shortcuts = ( opts ?. shortcuts ?? [ ] ) . filter ( isDefined ) . concat ( BASE_SHORTCUTS ) ;
28
21
29
22
let actionRunning = false ;
30
23
31
24
const onInput = async ( input : string ) => {
32
25
// ctrl+c or ctrl+d
33
26
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
+ }
36
31
}
37
32
38
33
if ( actionRunning ) return ;
You can’t perform that action at this time.
0 commit comments