1
- import type { PluginOption } from 'vite'
1
+ import type { PluginOption , ViteDevServer } from 'vite'
2
2
import { generateSandboxUrl } from './sandbox'
3
3
import { bold , green } from './utils/text'
4
4
import { arrows } from './utils/arrows'
5
+ import { MANIFEST_FILE_NAME } from '@storyblok/manifest-helper'
6
+ import path from 'path'
7
+
8
+ export function watchConfigFile ( ) : PluginOption {
9
+ return {
10
+ name : 'storyblok-field-plugin-watch-config-file' ,
11
+ handleHotUpdate ( { file, server } ) {
12
+ // NOTE: This condition checks if the file is on the same directory level as where the command has been executed and checks the file name
13
+ if ( isFileInSameLevel ( file ) && getFileName ( file ) === MANIFEST_FILE_NAME ) {
14
+ printSandboxUrl ( server )
15
+ }
16
+ } ,
17
+ }
18
+ }
5
19
6
20
export function printProd ( ) : PluginOption {
7
21
return {
@@ -28,13 +42,21 @@ export function printDev(): PluginOption {
28
42
configureServer ( server ) {
29
43
// Overrides the message that Vite prints out when the server is started. To reduce complexity, it does not include color
30
44
server . printUrls = ( ) => {
31
- if ( ! server . resolvedUrls ) {
32
- return
33
- }
34
- const localUrl = server . resolvedUrls . local [ 0 ]
35
- const networkUrl = server . resolvedUrls . network [ 0 ]
45
+ printServerUrls ( server )
46
+ printSandboxUrl ( server )
47
+ }
48
+ } ,
49
+ }
50
+ }
51
+
52
+ function printServerUrls ( server : ViteDevServer ) {
53
+ if ( ! server . resolvedUrls ) {
54
+ return
55
+ }
56
+ const localUrl = server . resolvedUrls . local [ 0 ]
57
+ const networkUrl = server . resolvedUrls . network [ 0 ]
36
58
37
- console . log ( `
59
+ console . log ( `
38
60
${ arrows . green } ${ bold (
39
61
'Partner Portal' ,
40
62
) } : https://app.storyblok.com/#/partner/fields
@@ -44,14 +66,29 @@ export function printDev(): PluginOption {
44
66
45
67
${ arrows . green } ${ bold ( 'Local' ) } : ${ localUrl }
46
68
${ arrows . green } ${ bold ( 'Network' ) } : ${ networkUrl }
47
-
48
- See the plugin in action on
49
-
50
- ${ arrows . green } ${ bold ( 'Sandbox' ) } : ${ generateSandboxUrl ( localUrl ) }
51
69
` )
52
- }
53
- } ,
70
+ }
71
+
72
+ function printSandboxUrl ( server : ViteDevServer ) {
73
+ if ( ! server . resolvedUrls ) {
74
+ return
54
75
}
76
+ const localUrl = server . resolvedUrls . local [ 0 ]
77
+
78
+ console . log ( ` See the plugin in action on
79
+
80
+ ${ arrows . green } ${ bold ( 'Sandbox' ) } : ${ generateSandboxUrl ( localUrl ) } ` )
55
81
}
56
82
57
- export const plugins = [ printProd ( ) , printDev ( ) ]
83
+ export const plugins = [ printProd ( ) , printDev ( ) , watchConfigFile ( ) ]
84
+
85
+ function getFileName ( file : string ) {
86
+ return path . basename ( file )
87
+ }
88
+
89
+ function isFileInSameLevel ( file : string ) : boolean {
90
+ const currentDir = process . cwd ( )
91
+ const filePath = path . resolve ( currentDir , file )
92
+ const fileDir = path . dirname ( filePath )
93
+ return currentDir === fileDir
94
+ }
0 commit comments