Skip to content

Commit ac47667

Browse files
feat(scaffolding): add hosting config files for common providers
add configuration files for Vercel, Netlify, and Cloudflare Pages to simplify deployment and address CORS header issues in TutorialKit. This improves the setup experience by automating build commands and headers for common hosting providers, reducing manual configuration steps. close #234
1 parent e1e9160 commit ac47667

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

_headers

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
Cross-Origin-Embedder-Policy: require-corp
3+
Cross-Origin-Opener-Policy: same-origin

netlify.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[headers]]
2+
for = "/*"
3+
[headers.values]
4+
Cross-Origin-Embedder-Policy = "require-corp"
5+
Cross-Origin-Opener-Policy = "same-origin"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"demo:build": "pnpm run build && pnpm run --filter=demo.tutorialkit.dev build",
1818
"lint": "eslint \"{packages,docs,extensions,integration}/**/*\"",
1919
"test": "pnpm run --stream --filter='@tutorialkit/*' test --run",
20-
"test:e2e": "pnpm run --filter='./e2e' test"
20+
"test:e2e": "pnpm run --filter='./e2e' test",
21+
"postbuild": "cp _headers ./dist/"
2122
},
2223
"license": "MIT",
2324
"packageManager": "[email protected]",

packages/cli/src/commands/create/index.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ async function _createTutorial(flags: CreateOptions): Promise<undefined> {
107107
const dest = await getTutorialDirectory(tutorialName, flags);
108108
const resolvedDest = path.resolve(process.cwd(), dest);
109109

110+
const providers = await prompts.multiselect({
111+
message: 'Select hosting providers for automatic configuration (use space to select):',
112+
options: [
113+
{ value: 'Vercel', label: 'Vercel' },
114+
{ value: 'Netlify', label: 'Netlify' },
115+
{ value: 'Cloudflare', label: 'Cloudflare' },
116+
],
117+
initialValues: [],
118+
});
119+
120+
assertNotCanceled(providers);
121+
prompts.log.info(`Configuring for: ${providers.join(', ')}`);
122+
123+
await generateHostingConfig(resolvedDest, providers);
124+
125+
await copyTemplate(resolvedDest, flags);
126+
updatePackageJson(resolvedDest, tutorialName, flags);
127+
110128
prompts.log.info(`Scaffolding tutorial in ${chalk.blue(resolvedDest)}`);
111129

112130
if (fs.existsSync(resolvedDest) && !flags.force) {
@@ -319,3 +337,40 @@ function verifyFlags(flags: CreateOptions) {
319337
throw new Error('Cannot start project without installing dependencies.');
320338
}
321339
}
340+
341+
function generateHostingConfig(dest: string, providers: string[]) {
342+
if (providers.includes('Vercel')) {
343+
fs.writeFileSync(
344+
path.join(dest, 'vercel.json'),
345+
JSON.stringify(
346+
{
347+
headers: [{ source: '/(.*)', headers: [{ key: 'Access-Control-Allow-Origin', value: '*' }] }],
348+
},
349+
null,
350+
2,
351+
),
352+
);
353+
}
354+
355+
if (providers.includes('Netlify')) {
356+
fs.writeFileSync(
357+
path.join(dest, 'netlify.toml'),
358+
`[build]
359+
publish = "build"
360+
command = "npm run build"
361+
362+
[[headers]]
363+
for = "/*"
364+
[headers.values]
365+
Access-Control-Allow-Origin = "*"`,
366+
);
367+
}
368+
369+
if (providers.includes('Cloudflare')) {
370+
fs.writeFileSync(
371+
path.join(dest, '_headers'),
372+
`/*
373+
Access-Control-Allow-Origin: *`,
374+
);
375+
}
376+
}

vercel.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"headers": [
3+
{
4+
"source": "/(.*)",
5+
"headers": [
6+
{
7+
"key": "Cross-Origin-Embedder-Policy",
8+
"value": "require-crop"
9+
},
10+
{
11+
"key": "Cross-Origin-Embedder-Policy",
12+
"value": "same-origin"
13+
}
14+
]
15+
}
16+
]
17+
}

0 commit comments

Comments
 (0)