Skip to content

Commit

Permalink
feat(cli): slim down remote test template (#8224)
Browse files Browse the repository at this point in the history
  • Loading branch information
RostiMelk authored Jan 13, 2025
1 parent 78318f3 commit 41580c4
Show file tree
Hide file tree
Showing 56 changed files with 65 additions and 4,744 deletions.
1 change: 0 additions & 1 deletion packages/@sanity/cli/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# package specific
/templates
/test/test-template
20 changes: 11 additions & 9 deletions packages/@sanity/cli/src/util/remoteTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,17 @@ export async function applyEnvVariables(
value: string,
useQuotes: boolean,
) => {
const pattern = varRegex instanceof RegExp ? varRegex : new RegExp(`${varRegex}=.*$`, 'gm')
const match = content.match(pattern)
if (!match) return content

const varName = match[0].split('=')[0]
return content.replace(
new RegExp(`${varName}=.*$`, 'gm'),
`${varName}=${useQuotes ? `"${value}"` : value}`,
)
const varPattern = typeof varRegex === 'string' ? varRegex : varRegex.source
const pattern = new RegExp(`.*${varPattern}=.*$`, 'gm')
const matches = content.matchAll(pattern)
return Array.from(matches).reduce((updatedContent, match) => {
if (!match[0]) return updatedContent
const varName = match[0].split('=')[0].trim()
return updatedContent.replace(
new RegExp(`${varName}=.*$`, 'gm'),
`${varName}=${useQuotes ? `"${value}"` : value}`,
)
}, content)
}

let envContent = templateContent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NEXT_PUBLIC_SANITY_PROJECT_ID=
SANITY_STUDIO_PROJECT_ID=

NEXT_PUBLIC_SANITY_DATASET=
SANITY_STUDIO_DATASET=
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test template solely for the purpose of testing the `init --template` command when using remote templates.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {type NextConfig} from 'next'

const nextConfig: NextConfig = {
/* config options here */
}

export default nextConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test-template",
"version": "1.0.0",
"main": "package.json",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"next": "^15",
"next-sanity": "^9",
"sanity": "^3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {defineCliConfig} from 'sanity/cli'

export default defineCliConfig({
api: {
projectId: process.env.SANITY_STUDIO_PROJECT_ID,
dataset: process.env.SANITY_STUDIO_DATASET,
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'

export default defineConfig({
name: 'sanity-template-astro-clean',
title: 'Sanity Astro Starter',
projectId: process.env.SANITY_STUDIO_PROJECT_ID,
dataset: process.env.SANITY_STUDIO_DATASET,
plugins: [structureTool()],
schema: {
types: [],
},
})
12 changes: 7 additions & 5 deletions packages/@sanity/cli/test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describeCliTest('CLI: `sanity init v3`', () => {
'--dataset',
testRunArgs.dataset,
'--template',
'sanity-io/sanity/packages/@sanity/cli/test/test-template',
'sanity-io/sanity/packages/@sanity/cli/test/__fixtures__/remote-template',
'--output-path',
`${baseTestPath}/${outpath}`,
'--package-manager',
Expand Down Expand Up @@ -145,7 +145,7 @@ describeCliTest('CLI: `sanity init v3`', () => {
'--dataset',
testRunArgs.dataset,
'--template',
'https://github.com/sanity-io/sanity/tree/next/packages/@sanity/cli/test/test-template',
'https://github.com/sanity-io/sanity/tree/next/packages/@sanity/cli/test/__fixtures__/remote-template',
'--output-path',
`${baseTestPath}/${outpath}`,
'--package-manager',
Expand Down Expand Up @@ -183,7 +183,7 @@ describeCliTest('CLI: `sanity init v3`', () => {
'--dataset',
testRunArgs.dataset,
'--template',
'sanity-io/sanity/packages/@sanity/cli/test/test-template',
'sanity-io/sanity/packages/@sanity/cli/test/__fixtures__/remote-template',
'--output-path',
`${baseTestPath}/${outpath}`,
'--package-manager',
Expand All @@ -192,8 +192,10 @@ describeCliTest('CLI: `sanity init v3`', () => {

const envContent = await fs.readFile(path.join(baseTestPath, outpath, '.env.local'), 'utf-8')

expect(envContent).toContain(`SANITY_PROJECT_ID=${cliProjectId}`)
expect(envContent).toContain(`SANITY_DATASET=${testRunArgs.dataset}`)
expect(envContent).toContain(`NEXT_PUBLIC_SANITY_PROJECT_ID=${cliProjectId}`)
expect(envContent).toContain(`SANITY_STUDIO_PROJECT_ID=${cliProjectId}`)
expect(envContent).toContain(`NEXT_PUBLIC_SANITY_DATASET=${testRunArgs.dataset}`)
expect(envContent).toContain(`SANITY_STUDIO_DATASET=${testRunArgs.dataset}`)
})

testConcurrent('fails with invalid repository format', async () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/@sanity/cli/test/test-template/.env.local.example

This file was deleted.

2 changes: 0 additions & 2 deletions packages/@sanity/cli/test/test-template/.eslintignore

This file was deleted.

4 changes: 0 additions & 4 deletions packages/@sanity/cli/test/test-template/.eslintrc

This file was deleted.

42 changes: 0 additions & 42 deletions packages/@sanity/cli/test/test-template/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions packages/@sanity/cli/test/test-template/.prettierignore

This file was deleted.

4 changes: 0 additions & 4 deletions packages/@sanity/cli/test/test-template/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions packages/@sanity/cli/test/test-template/app/(blog)/actions.ts

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions packages/@sanity/cli/test/test-template/app/(blog)/avatar.tsx

This file was deleted.

31 changes: 0 additions & 31 deletions packages/@sanity/cli/test/test-template/app/(blog)/cover-image.tsx

This file was deleted.

5 changes: 0 additions & 5 deletions packages/@sanity/cli/test/test-template/app/(blog)/date.tsx

This file was deleted.

Loading

0 comments on commit 41580c4

Please sign in to comment.