Skip to content

Commit 2c062e9

Browse files
feat(cli): move hosting configuration generation to dedicated file
moved the `generateHostingConfig` function to a new file with improved imports for hosting provider configuration files (`vercel.json`, `netlify.toml`, `_headers`). updated the function to work with the new imports and streamlined configuration generation. this change reduces clutter in the main implementation file and improves modularity by isolating hosting-related logic. closes #234
1 parent 5de42a3 commit 2c062e9

File tree

6 files changed

+57
-73
lines changed

6 files changed

+57
-73
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint-disable prettier/prettier */
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import cloudflareConfigFile from './hosting-config/_headers?raw';
5+
import netlifyConfigFile from './hosting-config/netlify.toml?raw';
6+
import vercelConfigFile from './hosting-config/vercel.json?raw';
7+
8+
export async function generateHostingConfig(dest: string, providers: string[]) {
9+
const resolvedDest = path.resolve(dest);
10+
11+
if (!fs.existsSync(resolvedDest)) {
12+
console.log(`Directory does not exist. Creating directory: ${resolvedDest}`);
13+
fs.mkdirSync(resolvedDest, { recursive: true });
14+
} else {
15+
console.log(`Directory already exists: ${resolvedDest}`);
16+
}
17+
18+
if (providers.includes('Vercel')) {
19+
const vercelConfigPath = path.join(resolvedDest, 'vercel.json');
20+
console.log('Writing Vercel config file to:', vercelConfigPath);
21+
fs.writeFileSync(vercelConfigPath, vercelConfigFile);
22+
}
23+
24+
if (providers.includes('Netlify')) {
25+
const netlifyConfigPath = path.join(resolvedDest, 'netlify.toml');
26+
console.log('Writing Netlify config file to:', netlifyConfigPath);
27+
fs.writeFileSync(netlifyConfigPath, netlifyConfigFile);
28+
}
29+
30+
if (providers.includes('Cloudflare')) {
31+
const cloudflareConfigPath = path.join(resolvedDest, '_headers');
32+
console.log('Writing Cloudflare config file to:', cloudflareConfigPath);
33+
fs.writeFileSync(cloudflareConfigPath, cloudflareConfigFile);
34+
}
35+
36+
const templateDir = path.resolve(__dirname, '_template');
37+
console.log('Looking for template directory at:', templateDir);
38+
39+
if (fs.existsSync(templateDir)) {
40+
const gitignoreTemplatePath = path.join(templateDir, '.gitignore');
41+
42+
if (fs.existsSync(gitignoreTemplatePath)) {
43+
const gitignoreDestPath = path.join(resolvedDest, '.gitignore');
44+
console.log('Copying .gitignore to:', gitignoreDestPath);
45+
fs.copyFileSync(gitignoreTemplatePath, gitignoreDestPath);
46+
} else {
47+
console.warn('No .gitignore file found in template directory, skipping copy.');
48+
}
49+
} else {
50+
console.warn('Template directory does not exist, skipping .gitignore copy.');
51+
}
52+
}
File renamed without changes.

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

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { generateProjectName } from '../../utils/project.js';
1010
import { assertNotCanceled } from '../../utils/tasks.js';
1111
import { updateWorkspaceVersions } from '../../utils/workspace-version.js';
1212
import { setupEnterpriseConfig } from './enterprise.js';
13+
import { generateHostingConfig } from './generate-hosting-config.js';
1314
import { initGitRepo } from './git.js';
1415
import { installAndStart } from './install-start.js';
1516
import { DEFAULT_VALUES, type CreateOptions } from './options.js';
@@ -337,76 +338,3 @@ function verifyFlags(flags: CreateOptions) {
337338
throw new Error('Cannot start project without installing dependencies.');
338339
}
339340
}
340-
341-
async function generateHostingConfig(dest: string, providers: string[]) {
342-
const resolvedDest = path.resolve(dest);
343-
344-
if (!fs.existsSync(resolvedDest)) {
345-
console.log(`Directory does not exist. Creating directory: ${resolvedDest}`);
346-
fs.mkdirSync(resolvedDest, { recursive: true });
347-
} else {
348-
console.log(`Directory already exists: ${resolvedDest}`);
349-
}
350-
351-
const templateDir = path.resolve(__dirname, '_template');
352-
console.log('Looking for template directory at:', templateDir);
353-
354-
if (!fs.existsSync(templateDir)) {
355-
console.error('Template directory does not exist at:', templateDir);
356-
} else {
357-
console.log('Template directory found at:', templateDir);
358-
}
359-
360-
if (providers.includes('Vercel')) {
361-
const vercelConfigPath = path.join(resolvedDest, 'vercel.json');
362-
console.log('Vercel config file will be written to:', vercelConfigPath);
363-
fs.writeFileSync(
364-
vercelConfigPath,
365-
JSON.stringify(
366-
{
367-
headers: [{ source: '/(.*)', headers: [{ key: 'Access-Control-Allow-Origin', value: '*' }] }],
368-
},
369-
null,
370-
2,
371-
),
372-
);
373-
}
374-
375-
if (providers.includes('Netlify')) {
376-
const netlifyConfigPath = path.join(resolvedDest, 'netlify.toml');
377-
console.log('Netlify config file will be written to:', netlifyConfigPath);
378-
fs.writeFileSync(
379-
netlifyConfigPath,
380-
`[build]
381-
publish = "build"
382-
command = "npm run build"
383-
384-
[[headers]]
385-
for = "/*"
386-
[headers.values]
387-
Access-Control-Allow-Origin = "*"`,
388-
);
389-
}
390-
391-
if (providers.includes('Cloudflare')) {
392-
const cloudflareConfigPath = path.join(resolvedDest, '_headers');
393-
console.log('Cloudflare config file will be written to:', cloudflareConfigPath);
394-
fs.writeFileSync(
395-
cloudflareConfigPath,
396-
`/*
397-
Access-Control-Allow-Origin: *`,
398-
);
399-
}
400-
401-
if (fs.existsSync(templateDir)) {
402-
const gitignoreTemplatePath = path.join(templateDir, '.gitignore');
403-
404-
if (fs.existsSync(gitignoreTemplatePath)) {
405-
const gitignoreDestPath = path.join(resolvedDest, '.gitignore');
406-
console.log('Copying .gitignore to:', gitignoreDestPath);
407-
fs.copyFileSync(gitignoreTemplatePath, gitignoreDestPath);
408-
} else {
409-
console.warn('No .gitignore file found in template directory, skipping copy.');
410-
}
411-
}
412-
}

packages/cli/src/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*?raw' {
2+
const content: string;
3+
export default content;
4+
}

0 commit comments

Comments
 (0)