|
1 | 1 | import fs from 'node:fs';
|
2 | 2 | import path from 'node:path';
|
3 |
| -import { fileURLToPath } from 'node:url'; |
| 3 | +import * as prompts from '@clack/prompts'; |
| 4 | +import chalk from 'chalk'; |
| 5 | +import { warnLabel } from 'src/utils/messages.js'; |
| 6 | +import { runTask } from 'src/utils/tasks.js'; |
4 | 7 | import cloudflareConfigRaw from './hosting-config/_headers.txt?raw';
|
5 | 8 | import netlifyConfigRaw from './hosting-config/netlify_toml.txt?raw';
|
6 | 9 | import vercelConfigRaw from './hosting-config/vercel.json?raw';
|
7 | 10 |
|
8 |
| -const __filename = fileURLToPath(import.meta.url); |
9 |
| -const __dirname = path.dirname(__filename); |
| 11 | +export async function generateHostingConfig(dest: string, provider: string, flags: { dryRun: boolean }) { |
| 12 | + prompts.log.info(`${chalk.blue('Hosting Configuration')} Setting up configuration for ${provider}`); |
10 | 13 |
|
11 |
| -export async function generateHostingConfig(dest: string, provider: string) { |
12 | 14 | const resolvedDest = path.resolve(dest);
|
13 | 15 |
|
14 | 16 | if (!fs.existsSync(resolvedDest)) {
|
15 |
| - console.log(`Directory does not exist. Creating directory: ${resolvedDest}`); |
16 | 17 | fs.mkdirSync(resolvedDest, { recursive: true });
|
17 |
| - } else { |
18 |
| - console.log(`Directory already exists: ${resolvedDest}`); |
19 | 18 | }
|
20 | 19 |
|
21 |
| - if (provider.includes('Vercel')) { |
22 |
| - const vercelConfigPath = path.join(resolvedDest, 'vercel.json'); |
23 |
| - console.log('Writing Vercel config file to:', vercelConfigPath); |
24 |
| - |
25 |
| - try { |
26 |
| - const vercelConfig = typeof vercelConfigRaw === 'string' ? JSON.parse(vercelConfigRaw) : vercelConfigRaw; |
27 |
| - fs.writeFileSync(vercelConfigPath, JSON.stringify(vercelConfig, null, 2)); |
28 |
| - } catch (error) { |
29 |
| - console.error('Failed to write Vercel config file:', error); |
30 |
| - } |
31 |
| - } |
| 20 | + let config; |
| 21 | + let filename; |
32 | 22 |
|
33 |
| - if (provider.includes('Netlify')) { |
34 |
| - const netlifyConfigPath = path.join(resolvedDest, 'netlify.toml'); |
35 |
| - console.log('Writing Netlify config file to:', netlifyConfigPath); |
36 |
| - |
37 |
| - try { |
38 |
| - if (typeof netlifyConfigRaw !== 'string') { |
39 |
| - throw new Error('Netlify config must be a string.'); |
40 |
| - } |
41 |
| - |
42 |
| - fs.writeFileSync(netlifyConfigPath, netlifyConfigRaw); |
43 |
| - } catch (error) { |
44 |
| - console.error('Failed to write Netlify config file:', error); |
45 |
| - } |
46 |
| - } |
47 |
| - |
48 |
| - if (provider.includes('Cloudflare')) { |
49 |
| - const cloudflareConfigPath = path.join(resolvedDest, '_headers'); |
50 |
| - console.log('Writing Cloudflare config file to:', cloudflareConfigPath); |
51 |
| - |
52 |
| - try { |
53 |
| - if (typeof cloudflareConfigRaw !== 'string') { |
54 |
| - throw new Error('Cloudflare config must be a string.'); |
55 |
| - } |
56 |
| - |
57 |
| - fs.writeFileSync(cloudflareConfigPath, cloudflareConfigRaw); |
58 |
| - } catch (error) { |
59 |
| - console.error('Failed to write Cloudflare config file:', error); |
60 |
| - } |
| 23 | + if (provider.includes('Vercel')) { |
| 24 | + config = typeof vercelConfigRaw === 'string' ? vercelConfigRaw : JSON.stringify(vercelConfigRaw, null, 2); |
| 25 | + filename = 'vercel.json'; |
| 26 | + } else if (provider.includes('Netlify')) { |
| 27 | + config = netlifyConfigRaw; |
| 28 | + filename = 'netlify.toml'; |
| 29 | + } else if (provider.includes('Cloudflare')) { |
| 30 | + config = cloudflareConfigRaw; |
| 31 | + filename = '_headers'; |
61 | 32 | }
|
62 | 33 |
|
63 |
| - const templateDir = path.resolve(__dirname, '_template'); |
64 |
| - console.log('Looking for template directory at:', templateDir); |
65 |
| - |
66 |
| - if (fs.existsSync(templateDir)) { |
67 |
| - const gitignoreTemplatePath = path.join(templateDir, '.gitignore'); |
68 |
| - |
69 |
| - if (fs.existsSync(gitignoreTemplatePath)) { |
70 |
| - const gitignoreDestPath = path.join(resolvedDest, '.gitignore'); |
71 |
| - console.log('Copying .gitignore to:', gitignoreDestPath); |
72 |
| - fs.copyFileSync(gitignoreTemplatePath, gitignoreDestPath); |
73 |
| - } else { |
74 |
| - console.warn('No .gitignore file found in template directory, skipping copy.'); |
75 |
| - } |
| 34 | + if (config && filename) { |
| 35 | + await runTask({ |
| 36 | + title: `Create hosting files for ${provider}`, |
| 37 | + dryRun: flags.dryRun, |
| 38 | + dryRunMessage: `${warnLabel('DRY RUN')} Skipped hosting provider config creation`, |
| 39 | + task: async () => { |
| 40 | + const filepath = path.join(resolvedDest, filename); |
| 41 | + fs.writeFileSync(filepath, config); |
| 42 | + return `Added ${filepath}`; |
| 43 | + }, |
| 44 | + }); |
76 | 45 | } else {
|
77 |
| - console.warn('Template directory does not exist, skipping .gitignore copy.'); |
| 46 | + prompts.log.message( |
| 47 | + `${chalk.blue('hosting provider config [skip]')} You can configure hosting provider settings manually later. For more information see https://tutorialkit.dev/guides/deployment/#headers-configuration` |
| 48 | + ); |
78 | 49 | }
|
79 | 50 | }
|
0 commit comments