Skip to content

Commit d094590

Browse files
authored
feat(sandbox): validating manifest options (#394)
* feat(sandbox): validating manifest, specifically option object and showing steps to correct * feat(sandbox): move validation to a better place -> manifest.ts * feat(sandbox): reset sandbox file * feat(sandbox): remove empty string check and mark todo in a duplicate code * feat(sandbox): add red color to error message * feat(helper): add logic also to manifest-helper * feat(helper): catch expected empty cases * feat(helper): fix tests * feat(helper): fix and add tests * feat(helper): make manifest-helper to a package and consume it inside vite helper * feat(helper): add concurrent building process for manifest helper * feat(helper): remove comment and adjust check for when manifest is not defined to remove it from query params * feat(helper): switch dependency to the package * feat(helper): add check for empty options * 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 e2ef9e9 commit d094590

File tree

14 files changed

+227
-113
lines changed

14 files changed

+227
-113
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"walkdir": "^0.4.1"
3737
},
3838
"devDependencies": {
39+
"@storyblok/manifest-helper": "workspace:*",
3940
"@types/execa": "2.0.0",
4041
"@types/fs-extra": "11.0.4",
4142
"@types/prompts": "2.4.9",

packages/cli/src/commands/deploy/helper.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import {
1414
StoryClientType,
1515
StoryblokClient,
1616
} from '../../storyblok/storyblok-client'
17-
import { getErrorMessage } from '@storyblok/manifest-helper/src/utils'
1817
import {
1918
load,
2019
Manifest,
2120
MANIFEST_FILE_NAME,
2221
ManifestOption,
23-
} from '@storyblok/manifest-helper/src/manifest'
22+
getErrorMessage,
23+
} from '@storyblok/manifest-helper'
2424

2525
const packageNameMessage =
2626
'How would you like to call the deployed field-plugin?\n (Lowercase alphanumeric and dash are allowed.)'
@@ -256,7 +256,7 @@ export const confirmOptionsUpdate = async (
256256
}
257257

258258
export const printManifestOptions = (options: ManifestOption[] | undefined) => {
259-
if (options?.length === 0) {
259+
if (options?.length === 0 || options === undefined) {
260260
return
261261
}
262262

@@ -351,10 +351,8 @@ export const loadManifest = (): Manifest | undefined => {
351351

352352
return load()
353353
} catch (err) {
354-
console.log(bold(red('[ERROR]')), `Error while loading the manifest file`)
355354
console.log(`path: ${MANIFEST_FILE_NAME}`)
356-
console.log(`error: ${getErrorMessage(err)}`)
357-
355+
console.log(red(`${bold('[ERROR]:')} ${getErrorMessage(err)}`))
358356
return undefined
359357
}
360358
}

packages/cli/src/storyblok/storyblok-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { red } from 'kleur/colors'
22
import type { Response } from 'node-fetch'
3-
import { ManifestOption } from '@storyblok/manifest-helper/src/manifest'
3+
import { ManifestOption } from '@storyblok/manifest-helper'
44

55
export type FieldType = {
66
id: number

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
6-
"devDependencies": {
7-
"typescript": "^5.3.3"
8-
},
96
"dependencies": {
7+
"@storyblok/manifest-helper": "workspace:*",
108
"kleur": "4.1.5"
9+
},
10+
"devDependencies": {
11+
"@storyblok/manifest-helper": "workspace:*",
12+
"typescript": "^5.3.3"
1113
}
1214
}

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

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+
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import * as querystring from 'querystring'
2-
import { bold } from './utils/text'
2+
import { red, bold } from './utils/text'
33
import { arrows } from './utils/arrows'
4-
import { load, manifestExists, MANIFEST_FILE_NAME } from './manifest'
5-
import type { Manifest } from './manifest'
4+
import {
5+
load,
6+
manifestExists,
7+
MANIFEST_FILE_NAME,
8+
Manifest,
9+
} from '@storyblok/manifest-helper'
610

711
export const SANDBOX_BASE_URL = `https://plugin-sandbox.storyblok.com/field-plugin`
812

913
export type SandboxQueryParams = {
1014
url: string
11-
manifest: Manifest | null
15+
manifest: Manifest | undefined
1216
}
1317

1418
export const buildQueryString = (params: SandboxQueryParams) => {
1519
const queryParams: querystring.ParsedUrlQueryInput = {
1620
url: params.url,
1721
}
1822

19-
if (params.manifest !== null) {
23+
if (params.manifest !== undefined) {
2024
queryParams.manifest = JSON.stringify(params.manifest)
2125
}
2226

@@ -36,14 +40,12 @@ export const generateSandboxUrl = (fieldPluginUrl: string) => {
3640
return `${SANDBOX_BASE_URL}?${queryString}`
3741
}
3842

39-
const loadManifest = (): Manifest | null => {
40-
if (!manifestExists()) return null
41-
43+
const loadManifest = (): Manifest | undefined => {
4244
try {
4345
return load()
4446
} catch (err) {
4547
displayManifestErrorLoading(err as Error)
46-
return null
48+
return undefined
4749
}
4850
}
4951

@@ -58,4 +60,4 @@ const displayManifestChecking = () => {
5860
}
5961

6062
const displayManifestErrorLoading = (err: Error) =>
61-
console.log(`${arrows.red} ${bold(`${err.message}`)}`)
63+
console.log(`${arrows.red} ${red(`${err.message}`)}`)

packages/field-plugin/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
"check:types": "tsc --noEmit",
4141
"dev": "vite build --watch",
4242
"test": "jest",
43-
"build": "unbuild && yarn build:helpers",
44-
"build:helpers": "cd helpers && unbuild"
43+
"build": "yarn build:manifest-helper && unbuild && yarn build:helpers",
44+
"build:helpers": "cd helpers && unbuild",
45+
"build:manifest-helper": "cd ../manifest-helper && yarn build"
4546
},
4647
"devDependencies": {
4748
"@types/core-js": "2.5.8",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
entries: ['./src/index.ts'],
5+
declaration: true,
6+
rollup: {
7+
emitCJS: true,
8+
},
9+
failOnWarn: false,
10+
})

packages/manifest-helper/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
"name": "@storyblok/manifest-helper",
33
"version": "0.0.1",
44
"type": "module",
5+
"types": "./dist/index.d.ts",
6+
"main": "./dist/index.cjs",
7+
"exports": {
8+
".": {
9+
"import": "./dist/index.mjs",
10+
"require": "./dist/index.cjs"
11+
}
12+
},
513
"license": "MIT",
614
"scripts": {
715
"test": "vitest run",
8-
"watch": "vitest watch"
16+
"watch": "vitest watch",
17+
"build": "unbuild"
918
},
1019
"devDependencies": {
20+
"unbuild": "2.0.0",
1121
"vite": "^5.1.3",
1222
"vitest": "1.3.0"
1323
}

0 commit comments

Comments
 (0)