1
- /* eslint-disable prettier/prettier */
2
1
import fs from 'node:fs' ;
3
2
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 ) ;
3
+ import { fileURLToPath } from 'node:url' ;
4
+ import cloudflareConfigRaw from './hosting-config/_headers.txt?raw' ;
5
+ import netlifyConfigRaw from './hosting-config/netlify_toml.txt?raw' ;
6
+ import vercelConfigRaw from './hosting-config/vercel.json?raw' ;
7
+
8
+ const __filename = fileURLToPath ( import . meta. url ) ;
9
+ const __dirname = path . dirname ( __filename ) ;
10
+
11
+ export async function generateHostingConfig ( dest : string , provider : string ) {
12
+ const resolvedDest = path . resolve ( dest ) ;
13
+
14
+ if ( ! fs . existsSync ( resolvedDest ) ) {
15
+ console . log ( `Directory does not exist. Creating directory: ${ resolvedDest } ` ) ;
16
+ fs . mkdirSync ( resolvedDest , { recursive : true } ) ;
17
+ } else {
18
+ console . log ( `Directory already exists: ${ resolvedDest } ` ) ;
19
+ }
20
+
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 ) ;
28
30
}
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 ) ;
31
+ }
32
+
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 ) ;
34
45
}
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.' ) ;
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.' ) ;
48
55
}
56
+
57
+ fs . writeFileSync ( cloudflareConfigPath , cloudflareConfigRaw ) ;
58
+ } catch ( error ) {
59
+ console . error ( 'Failed to write Cloudflare config file:' , error ) ;
60
+ }
61
+ }
62
+
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 ) ;
49
73
} else {
50
- console . warn ( 'Template directory does not exist , skipping .gitignore copy.' ) ;
74
+ console . warn ( 'No .gitignore file found in template directory , skipping copy.' ) ;
51
75
}
52
- }
76
+ } else {
77
+ console . warn ( 'Template directory does not exist, skipping .gitignore copy.' ) ;
78
+ }
79
+ }
0 commit comments