Skip to content

Commit

Permalink
feat: embellish nextjs blog starter (#7258)
Browse files Browse the repository at this point in the history
* feat: embellish nextjs blog starter

* add default structure to clean template
  • Loading branch information
SimeonGriggs authored Jul 30, 2024
1 parent a9b9feb commit 0fc8216
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 266 deletions.
6 changes: 5 additions & 1 deletion packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,14 @@ export default async function initSanity(
}
const {chosen} = await getPackageManagerChoice(workDir, {interactive: false})
trace.log({step: 'selectPackageManager', selectedOption: chosen})
const packages = ['@sanity/vision@3', 'sanity@3', '@sanity/image-url@1', 'styled-components@6']
if (templateToUse === 'blog') {
packages.push('@sanity/icons')
}
await installNewPackages(
{
packageManager: chosen,
packages: ['@sanity/vision@3', 'sanity@3', '@sanity/image-url@1', 'styled-components@6'],
packages,
},
{
output: context.output,
Expand Down
10 changes: 5 additions & 5 deletions packages/@sanity/cli/src/actions/init-project/prompts/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export function promptForNextTemplate(prompt: CliPrompter): Promise<'clean' | 'b
message: 'Select project template to use',
type: 'list',
choices: [
{
value: 'clean',
name: 'Clean project with no predefined schema types',
},
{
value: 'blog',
name: 'Blog (schema)',
},
{
value: 'clean',
name: 'Clean project with no predefined schema types',
},
],
default: 'clean',
default: 'blog',
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import {structureTool} from 'sanity/structure'
// Go to https://www.sanity.io/docs/api-versioning to learn how API versioning works
import {apiVersion, dataset, projectId} from ${hasSrcFolder ? "'./src/sanity/env'" : "'./sanity/env'"}
import {schema} from ${hasSrcFolder ? "'./src/sanity/schema'" : "'./sanity/schema'"}
import {schema} from ${hasSrcFolder ? "'./src/sanity/schemaTypes'" : "'./sanity/schemaTypes'"}
import {structure} from ${hasSrcFolder ? "'./src/sanity/structure'" : "'./sanity/structure'"}
export default defineConfig({
basePath: ':basePath:',
projectId,
dataset,
// Add and edit the content schema in the './sanity/schema' folder
// Add and edit the content schema in the './sanity/schemaTypes' folder
schema,
plugins: [
structureTool(),
// Vision is a tool that lets you query your content with GROQ in the studio
structureTool({structure}),
// Vision is for querying with GROQ from inside the Studio
// https://www.sanity.io/docs/the-vision-plugin
visionTool({defaultApiVersion: apiVersion}),
],
Expand Down Expand Up @@ -59,7 +60,8 @@ export { metadata, viewport } from 'next-sanity/studio'
export default function StudioPage() {
return <NextStudio config={config} />
}`
}
`

// Format today's date like YYYY-MM-DD
const envTS = `export const apiVersion =
Expand Down Expand Up @@ -103,6 +105,54 @@ const schemaJS = `export const schema = {
}
`

const blogStructureTS = `import type {StructureResolver} from 'sanity/structure'
// https://www.sanity.io/docs/structure-builder-cheat-sheet
export const structure: StructureResolver = (S) =>
S.list()
.title('Blog')
.items([
S.documentTypeListItem('post').title('Posts'),
S.documentTypeListItem('category').title('Categories'),
S.documentTypeListItem('author').title('Authors'),
S.divider(),
...S.documentTypeListItems().filter(
(item) => item.getId() && !['post', 'category', 'author'].includes(item.getId()!),
),
])
`

const blogStructureJS = `// https://www.sanity.io/docs/structure-builder-cheat-sheet
export const structure = (S) =>
S.list()
.title('Blog')
.items([
S.documentTypeListItem('post').title('Posts'),
S.documentTypeListItem('category').title('Categories'),
S.documentTypeListItem('author').title('Authors'),
S.divider(),
...S.documentTypeListItems().filter(
(item) => item.getId() && !['post', 'category', 'author'].includes(item.getId()),
),
])
`

const structureTS = `import type {StructureResolver} from 'sanity/structure'
// https://www.sanity.io/docs/structure-builder-cheat-sheet
export const structure: StructureResolver = (S) =>
S.list()
.title('Content')
.items(S.documentTypeListItems())
`

const structureJS = `// https://www.sanity.io/docs/structure-builder-cheat-sheet
export const structure = (S) =>
S.list()
.title('Content')
.items(S.documentTypeListItems())
`

const client = `import { createClient } from 'next-sanity'
import { apiVersion, dataset, projectId } from '../env'
Expand Down Expand Up @@ -146,26 +196,26 @@ export const sanityFolder = (
useTypeScript: boolean,
template?: 'clean' | 'blog',
): FolderStructure => {
const isBlogTemplate = template === 'blog'

// Files used in both templates
const structure: FolderStructure = {
// eslint-disable-next-line no-nested-ternary
'schema.': useTypeScript
? isBlogTemplate
? blogSchemaTS
: schemaTS
: isBlogTemplate
? blogSchemaJS
: schemaJS,
'env.': useTypeScript ? envTS : envJS,
'lib': {
'client.': client,
'image.': useTypeScript ? imageTS : imageJS,
},
}

if (isBlogTemplate) {
structure.schemaTypes = blogSchemaFolder(useTypeScript)
if (template === 'blog') {
structure.schemaTypes = {
...blogSchemaFolder,
'index.': useTypeScript ? blogSchemaTS : blogSchemaJS,
}
structure['structure.'] = useTypeScript ? blogStructureTS : blogStructureJS
} else {
structure.schemaTypes = {
'index.': useTypeScript ? schemaTS : schemaJS,
}
structure['structure.'] = useTypeScript ? structureTS : structureJS
}

return structure
Expand Down
Loading

0 comments on commit 0fc8216

Please sign in to comment.