Skip to content

Commit f113602

Browse files
authored
feat(helper): watching config files for changes (#396)
* feat(helper): make manifest-helper to a package and consume it inside vite helper * feat(helper): watching config files for changes * feat(helper): splitting printing functions * feat(helper): add check file name and directory level * feat(helper): update consistent names * feat(helper): remove comment
1 parent 546a875 commit f113602

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

packages/field-plugin/helpers/vite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": "0.0.0",
55
"type": "module",
66
"dependencies": {
7+
"@storyblok/manifest-helper": "workspace:*",
78
"kleur": "4.1.5"
89
},
910
"devDependencies": {

packages/field-plugin/helpers/vite/src/plugins.ts

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
import type { PluginOption } from 'vite'
1+
import type { PluginOption, ViteDevServer } from 'vite'
22
import { generateSandboxUrl } from './sandbox'
33
import { bold, green } from './utils/text'
44
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+
}
519

620
export function printProd(): PluginOption {
721
return {
@@ -28,13 +42,21 @@ export function printDev(): PluginOption {
2842
configureServer(server) {
2943
// Overrides the message that Vite prints out when the server is started. To reduce complexity, it does not include color
3044
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]
3658

37-
console.log(`
59+
console.log(`
3860
${arrows.green} ${bold(
3961
'Partner Portal',
4062
)}: https://app.storyblok.com/#/partner/fields
@@ -44,14 +66,29 @@ export function printDev(): PluginOption {
4466
4567
${arrows.green} ${bold('Local')}: ${localUrl}
4668
${arrows.green} ${bold('Network')}: ${networkUrl}
47-
48-
See the plugin in action on
49-
50-
${arrows.green} ${bold('Sandbox')}: ${generateSandboxUrl(localUrl)}
5169
`)
52-
}
53-
},
70+
}
71+
72+
function printSandboxUrl(server: ViteDevServer) {
73+
if (!server.resolvedUrls) {
74+
return
5475
}
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)}`)
5581
}
5682

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

Comments
 (0)