Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions .github/workflows/protect-sample-data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Protect Sample Data

on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'apps/sanity/sample-data.tar.gz'
- '.github/workflows/protect-sample-data.yml'

# Allows triggering this workflow from GitHub UI
workflow_dispatch:

permissions:
contents: read
pull-requests: write

jobs:
check-sample-data:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check sample data modifications 🔒
id: check-sample-data
run: |
# Get the list of files changed in the PR
CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }})

# Check if sample-data.tar.gz was modified
if echo "$CHANGED_FILES" | grep -q "apps/sanity/sample-data.tar.gz"; then
# Get the author of the PR
PR_AUTHOR="${{ github.event.pull_request.user.login }}"
# Get the repository owner
REPO_OWNER="${{ github.repository_owner }}"

if [ "$PR_AUTHOR" != "$REPO_OWNER" ]; then
echo "::error::Sample data file modification requires approval from the repository owner."
echo "::error::Please contact @$REPO_OWNER for approval."
exit 1
else
echo "::warning::Sample data file modified by repository owner. Proceeding with caution."
fi
fi

- name: Comment on PR 💬
if: failure()
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `⚠️ **Sample Data Protection**\n\nThis PR modifies the sample data file (\`apps/sanity/sample-data.tar.gz\`). For security reasons, only the repository owner can modify this file.\n\nPlease contact @${{ github.repository_owner }} for approval.`
})
188 changes: 188 additions & 0 deletions apps/sanity/config/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { spawn } from 'child_process';
import { copyFileSync, existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
const rootDir = join(__dirname, '..');
const webDir = join(rootDir, '..', 'web');

async function main() {
try {
console.log('🚀 Initializing Sanity project...');

// Run sanity init
console.log('\n📦 Running Sanity initialization...');
console.log('Please follow the prompts to set up your Sanity project...');

// Run Sanity init with full interactivity but capture stdout
const sanityInit = spawn('npx', ['sanity', 'init', '--bare'], {
stdio: ['inherit', 'pipe', 'inherit'],
cwd: rootDir,
env: {
...process.env,
FORCE_COLOR: '1',
CI: 'false',
TERM: process.env.TERM || 'xterm-256color',
},
});

let output = '';
sanityInit.stdout.on('data', (data) => {
const text = data.toString();
output = text;

// Remove the first line of the project selection prompt
if (text.includes('Create new project') && !text.includes('(Use arrow keys)')) {
let cleanedText = text.replace(

Check warning on line 37 in apps/sanity/config/init.ts

View check run for this annotation

codefactor.io / CodeFactor

apps/sanity/config/init.ts#L37

'cleanedText' is never reassigned. Use 'const' instead. (prefer-const)
'\x1B[32m?\x1B[39m \x1B[1mCreate a new project or select an existing one\x1B[22m\x1B[0m \x1B[0m\n',
'',
);
console.log(cleanedText);
return;
}

if (text.includes('Select dataset to use') && !text.includes('(Use arrow keys)')) {
let cleanedText = text.replace(

Check warning on line 46 in apps/sanity/config/init.ts

View check run for this annotation

codefactor.io / CodeFactor

apps/sanity/config/init.ts#L46

'cleanedText' is never reassigned. Use 'const' instead. (prefer-const)
'\x1B[32m?\x1B[39m \x1B[1mSelect dataset to use\x1B[22m\x1B[0m \x1B[0m\n',
'',
);
console.log(cleanedText);
return;
}

console.log(text);
});

await new Promise((resolve, reject) => {
sanityInit.on('close', (code) => {
if (code === 0) {
resolve(true);
} else {
reject(new Error(`Sanity init failed with code ${code}`));
}
});
});

console.log('\n✅ Sanity initialization complete!');

// Extract project details from output
const projectIdMatch = output.match(/Project ID:\s*\x1B\[36m([^\s]+)\x1B\[39m/);

Check warning on line 70 in apps/sanity/config/init.ts

View check run for this annotation

codefactor.io / CodeFactor

apps/sanity/config/init.ts#L70

Unexpected control character(s) in regular expression: \x1b, \x1b. (no-control-regex)
const datasetMatch = output.match(/Dataset:\s*\x1B\[36m([^\s]+)\x1B\[39m/);

Check warning on line 71 in apps/sanity/config/init.ts

View check run for this annotation

codefactor.io / CodeFactor

apps/sanity/config/init.ts#L71

Unexpected control character(s) in regular expression: \x1b, \x1b. (no-control-regex)

if (!projectIdMatch || !datasetMatch) {
throw new Error('Could not find project ID or dataset in Sanity CLI output');
}

const projectId = projectIdMatch[1].trim();
const dataset = datasetMatch[1].trim();

console.log(`Project ID: ${projectId}`);
console.log(`Dataset: ${dataset}`);

// Update environment files
console.log('\n📝 Setting up environment variables...');

// Sanity app environment files
const sanityEnvExamplePath = join(rootDir, '.env.example');
const sanityEnvLocalPath = join(rootDir, '.env.local');

// Web app environment files
const webEnvExamplePath = join(webDir, '.env.example');
const webEnvLocalPath = join(webDir, '.env.local');

// Sanity app environment setup
if (existsSync(sanityEnvExamplePath)) {
copyFileSync(sanityEnvExamplePath, sanityEnvLocalPath);
console.log('✓ Copied Sanity .env.example to .env.local');
} else {
console.log('⚠️ No Sanity .env.example found, creating new environment file');
}

// Web app environment setup
if (existsSync(webEnvExamplePath)) {
copyFileSync(webEnvExamplePath, webEnvLocalPath);
console.log('✓ Copied Web .env.example to .env.local');
} else {
console.log('⚠️ No Web .env.example found, creating new environment file');
}

// Update Sanity app environment file
let sanityEnvLocalContent = readFileSync(sanityEnvLocalPath, 'utf-8');

// Update Sanity variables
sanityEnvLocalContent = sanityEnvLocalContent
.replace(
/SANITY_STUDIO_SANITY_PROJECT_ID=.*\n?/,
`SANITY_STUDIO_SANITY_PROJECT_ID=${projectId}\n`,
)
.replace(
/SANITY_STUDIO_SANITY_DATASET=.*\n?/,
`SANITY_STUDIO_SANITY_DATASET=${dataset}\n`,
);

writeFileSync(sanityEnvLocalPath, sanityEnvLocalContent.trim() + '\n');

// Update Web app environment file
let webEnvLocalContent = readFileSync(webEnvLocalPath, 'utf-8');

// Update Web variables
webEnvLocalContent = webEnvLocalContent
.replace(
/NEXT_PUBLIC_SANITY_PROJECT_ID=.*\n?/,
`NEXT_PUBLIC_SANITY_PROJECT_ID=${projectId}\n`,
)
.replace(/NEXT_PUBLIC_SANITY_DATASET=.*\n?/, `NEXT_PUBLIC_SANITY_DATASET=${dataset}\n`);

writeFileSync(webEnvLocalPath, webEnvLocalContent.trim() + '\n');

console.log('\n✅ Environment files updated successfully!');

// Check for sample data
const sampleDataPath = join(rootDir, 'sample-data.tar.gz');
if (existsSync(sampleDataPath)) {
console.log('\n📦 Found sample data! Would you like to import it?');
console.log('This will populate your Sanity dataset with sample content.');

// Prompt user for import
const { execSync } = await import('child_process');
const readline = await import('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const answer = await new Promise<string>((resolve) => {
rl.question('Import sample data? (y/N): ', resolve);
});
rl.close();

if (answer.toLowerCase() === 'y') {
console.log('\n📥 Importing sample data...');
try {
execSync(`npx sanity dataset import sample-data.tar.gz ${dataset}`, {
stdio: 'inherit',
cwd: rootDir,
});
console.log('✅ Sample data imported successfully!');
} catch (error) {
console.error('❌ Error importing sample data:', error);
}
}
}

console.log('\n🎉 Sanity project initialized successfully!');
console.log('\nNext steps:');
console.log('1. Start the development server: pnpm g:dev');
console.log('2. Open the Sanity Studio: http://localhost:3333');
console.log('3. Open the Next.js app: http://localhost:3000');
if (existsSync(sampleDataPath)) {
console.log('4. Optionally import sample data using the command above');
}
} catch (error) {
console.error('❌ Error during initialization:', error);
process.exit(1);
}
}

main();
1 change: 1 addition & 0 deletions apps/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"extract-schema": "npx sanity schema extract",
"generate:blocks": "tsx features/modular-content-blocks/scripts/generate-sanity-block-schema-imports.ts",
"generate:types": "npx sanity typegen generate",
"init": "tsx config/init.ts",
"lint": "pnpm eslint . && pnpm prettier",
"lint:fix": "pnpm eslint . --fix && pnpm prettier:fix",
"postinstall": "pnpm turbo generate:types",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"g:ci-check": "pnpm install && pnpm g:typecheck && pnpm g:lint && pnpm g:test:unit && pnpm g:build",
"g:dev": "turbo run dev --color",
"g:generate:types": "turbo run generate:types",
"g:init": "cd apps/sanity && pnpm run init",
"g:lint-staged-files": "lint-staged --allow-empty",
"g:lint": "TIMING=1 turbo run lint --color",
"g:lint:fix": "turbo run lint:fix --color",
Expand Down
Loading