Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed May 23, 2024
1 parent 6766c53 commit d5aae73
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 77 deletions.
18 changes: 0 additions & 18 deletions packages/@sanity/manifest/src/schema/v1.ts

This file was deleted.

101 changes: 101 additions & 0 deletions packages/@sanity/manifest/src/schema/v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import z from 'zod'

export const manifestV1Deprecation = z.object({
reason: z.string(),
})

export const manifestV1TypeValidationRule = z.object({
flag: z.literal('type'),
constraint: z.string(), // xxx make me precise
})

// TOOD: Constraints
export const manifestV1UriValidationRule = z.object({
flag: z.literal('uri'),
})

// TODO
// export const manifestV1ValidationRule = z.any()
export const manifestV1ValidationRule = z.union([
manifestV1TypeValidationRule,
manifestV1UriValidationRule,
// TODO: Remove
z.any(),
])

export const manifestV1ValidationGroup = z.object({
rules: z.array(manifestV1ValidationRule),
message: z.string().optional(),
level: z.union([z.literal('error'), z.literal('warning'), z.literal('info')]).optional(),
})

export type ManifestV1ValidationGroup = z.infer<typeof manifestV1ValidationGroup>

export const manifestV1Reference = z.object({
type: z.string(),
})

export type ManifestV1Reference = z.infer<typeof manifestV1Reference>

export const manifestV1ReferenceGroup = z.array(manifestV1Reference)

export type ManifestV1ReferenceGroup = z.infer<typeof manifestV1ReferenceGroup>

const _base = z.object({
type: z.string(),
name: z.string(),
title: z.string().optional(),
description: z.string().optional(),
deprecated: manifestV1Deprecation.optional(),
readOnly: z.boolean().optional(), // xxx
hidden: z.boolean().optional(), // xxx
validation: z.array(manifestV1ValidationGroup).optional(),
to: manifestV1ReferenceGroup.optional(),
of: z.any(),
preview: z
.object({
select: z.record(z.string(), z.string()),
})
.optional(),
})

const manifestV1TypeBase: z.ZodType<ManifestV1Type> = _base.extend({
fields: z.array(z.lazy(() => manifestV1Field)).optional(),
})

export const manifestV1Field = manifestV1TypeBase

export type ManifestV1Field = z.infer<typeof manifestV1Field>

// export const ManifestV1TypeSchema = ManifestV1TypeBaseSchema.extend({
// readOnly: z.boolean().optional(),
// hidden: z.boolean().optional(),
// preview: z
// .object({
// select: z.record(z.string(), z.string()),
// })
// .optional(),
// })

export type ManifestV1Type = z.infer<typeof _base> & {
fields?: ManifestV1Field[]
}

export const manifestV1Schema = z.array(manifestV1TypeBase)

export const ManifestSchema = z.object({manifestVersion: z.number()})

export const manifestV1Workspace = z.object({
name: z.string(),
dataset: z.string(),
schema: z.union([manifestV1Schema, z.string()]), // xxx don't actually want string here, but allows us to replace with filename
})

export type ManifestV1Workspace = z.infer<typeof manifestV1Workspace>

export const manifestV1 = ManifestSchema.extend({
createdAt: z.date(),
workspaces: z.array(manifestV1Workspace),
})

export type ManifestV1 = z.infer<typeof manifestV1>
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ const extractManifest: CliCommandAction = async (_args, context) => {

spinner.succeed(`Extracted manifest to ${chalk.cyan(path)}`)
} catch (err) {
console.error('[ERR]', err)
// trace.error(err)
spinner.fail('Failed to extract manifest')
throw err
// throw err
}
}

Expand All @@ -125,7 +126,8 @@ async function externalizeSchema(
): Promise<ManifestV1Workspace> {
const schemaString = JSON.stringify(workspace.schema, null, 2)
const hash = createHash('sha1').update(schemaString).digest('hex')
const filename = `${hash.slice(0, 8)}${SCHEMA_FILENAME_SUFFIX}`
// const filename = `${hash.slice(0, 8)}${SCHEMA_FILENAME_SUFFIX}`
const filename = `${workspace.name}${SCHEMA_FILENAME_SUFFIX}`

await writeFile(join(staticPath, filename), schemaString)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {type CliCommandDefinition} from '@sanity/cli'

// xxx tmp
import mod from '../../actions/schema/extractAction'

const description = 'Extracts a JSON representation of a Sanity schema within a Studio context.'

const helpText = `
Expand All @@ -23,9 +26,10 @@ const extractSchemaCommand: CliCommandDefinition = {
description,
helpText,
action: async (args, context) => {
const mod = await import('../../actions/schema/extractAction')

return mod.default(args, context)
// const mod = await import('../../actions/schema/extractAction')
//
// return mod.default(args, context)
return mod(args, context)
},
} satisfies CliCommandDefinition

Expand Down
Loading

0 comments on commit d5aae73

Please sign in to comment.