Skip to content

Commit 72f6e72

Browse files
committed
improve type safety
1 parent 94301f6 commit 72f6e72

4 files changed

Lines changed: 34 additions & 30 deletions

File tree

packages/cli/src/install.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ interface Args {
1010
config: ParkUIConfig
1111
}
1212

13-
export const installRegistryItem = ({ item, config }: Args) =>
13+
export const installRegistryItem = ({ item: { files, pandaConfig }, config }: Args) =>
1414
Effect.all([
1515
createFiles({
16-
files: item.files,
16+
files,
1717
config,
1818
}),
19-
updatePandaConfig(item.panda),
19+
updatePandaConfig(pandaConfig),
2020
])
2121

2222
interface CreateFilesArgs {

packages/cli/src/panda.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
SyntaxKind,
1010
} from 'ts-morph'
1111
import { PandaConfigInvalid, PandaConfigNotFound } from './error'
12-
import type { PandaConfiguration } from './schema'
12+
import type { JsonValue, PandaConfig } from './schema'
1313

14-
export const updatePandaConfig = ({ imports = [], config = {} }: PandaConfiguration = {}) => {
14+
export const updatePandaConfig = ({ imports = [], extension = {} }: PandaConfig = {}) => {
1515
return getConfigPath().pipe(
1616
Effect.flatMap((configPath) =>
1717
pipe(
@@ -77,7 +77,9 @@ export const updatePandaConfig = ({ imports = [], config = {} }: PandaConfigurat
7777
catch: () => PandaConfigInvalid,
7878
}),
7979
),
80-
Effect.tap((configObj) => Effect.sync(() => mergeObjectLiteral(configObj, config))),
80+
Effect.tap((objLiteral) =>
81+
Effect.sync(() => mergeObjectLiteral(objLiteral, extension)),
82+
),
8183
Effect.tap(() => sourceFile.organizeImports()),
8284
Effect.flatMap(() =>
8385
Effect.tryPromise({
@@ -111,10 +113,10 @@ const getConfigPath = () =>
111113
),
112114
)
113115

114-
const mergeObjectLiteral = (
115-
objLiteral: ObjectLiteralExpression,
116-
update: Record<string, unknown>,
117-
) => {
116+
const mergeObjectLiteral = (objLiteral: ObjectLiteralExpression, update: JsonValue) => {
117+
if (typeof update !== 'object' || update === null || Array.isArray(update)) {
118+
throw new Error('update must be an object')
119+
}
118120
for (const [key, value] of Object.entries(update)) {
119121
let prop = objLiteral.getProperty(key) as PropertyAssignment | undefined
120122

@@ -129,7 +131,7 @@ const mergeObjectLiteral = (
129131
prop
130132
.setInitializer('{}')
131133
.getInitializerIfKind(SyntaxKind.ObjectLiteralExpression)) as ObjectLiteralExpression
132-
mergeObjectLiteral(nested, value as Record<string, unknown>)
134+
mergeObjectLiteral(nested, value)
133135
} else {
134136
if (prop) {
135137
const existingObj = prop.getInitializerIfKind(SyntaxKind.ObjectLiteralExpression)

packages/cli/src/schema.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ const moduleDeclaration = z.discriminatedUnion('type', [
3131
])
3232
export type ModuleDeclaration = z.infer<typeof moduleDeclaration>
3333

34-
type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }
34+
export type JsonValue =
35+
| string
36+
| number
37+
| boolean
38+
| null
39+
| JsonValue[]
40+
| { [key: string]: JsonValue }
3541

3642
const jsonValue: z.ZodType<JsonValue> = z.lazy(() =>
3743
z.union([
@@ -45,7 +51,6 @@ const jsonValue: z.ZodType<JsonValue> = z.lazy(() =>
4551
)
4652

4753
const indexFile = z.object({
48-
fileName: z.string(),
4954
exports: z.array(moduleDeclaration).optional(),
5055
imports: z.array(moduleDeclaration).optional(),
5156
})
@@ -60,14 +65,10 @@ const registryFile = z.object({
6065
export type RegistryFile = z.infer<typeof registryFile>
6166

6267
const pandaConfig = z.object({
63-
theme: jsonValue.optional(),
64-
})
65-
66-
const pandaConfiguration = z.object({
67-
config: pandaConfig.optional(),
68+
extension: jsonValue.optional(),
6869
imports: z.array(moduleDeclaration).optional(),
6970
})
70-
export type PandaConfiguration = z.infer<typeof pandaConfiguration>
71+
export type PandaConfig = z.infer<typeof pandaConfig>
7172

7273
export const registryItem = z.object({
7374
id: z.string(),
@@ -78,7 +79,7 @@ export const registryItem = z.object({
7879
devDependencies: z.array(z.string()).optional(),
7980
registryDependencies: z.array(z.string()).optional(),
8081
files: z.array(registryFile).optional(),
81-
panda: pandaConfiguration.optional(),
82+
pandaConfig: pandaConfig.optional(),
8283
categories: z.array(z.string()).optional(),
8384
})
8485
export type RegistryItem = z.infer<typeof registryItem>

website/public/schema/registry-item.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,23 @@
192192
},
193193
"description": "The main payload of the registry item. This is an array of files that are part of the registry item"
194194
},
195-
"panda": {
195+
"pandaConfig": {
196196
"type": "object",
197197
"properties": {
198-
"config": {
199-
"type": "object",
200-
"properties": {
201-
"theme": {
202-
"$ref": "#/definitions/JsonValue",
203-
"description": "The theme configuration for the panda item"
204-
}
198+
"extension": {
199+
"$ref": "#/definitions/JsonValue",
200+
"description": "The theme configuration for the panda item"
201+
},
202+
"imports": {
203+
"type": "array",
204+
"items": {
205+
"$ref": "#/definitions/ModuleDeclaration"
205206
},
206-
"additionalProperties": false
207+
"description": "The module imports required for the panda configuration"
207208
}
208209
},
209210
"additionalProperties": false,
210-
"description": "The panda configuration for the registry item. This is an object with a config property."
211+
"description": "The panda configuration for the registry item."
211212
},
212213
"categories": {
213214
"type": "array",

0 commit comments

Comments
 (0)