@@ -9,107 +9,44 @@ if (!E2B_API_KEY) {
9
9
}
10
10
11
11
export async function createSandbox ( { files } : { files : z . infer < typeof benchifyFileSchema > } ) {
12
- const sandbox = await Sandbox . create ( 'vite-template' , { apiKey : E2B_API_KEY } ) ;
13
-
14
- // Debug: Log template files before writing anything
15
- try {
16
- console . log ( "TEMPLATE VERIFICATION:" ) ;
17
- const { stdout : templateFiles } = await sandbox . commands . run ( 'ls -la /app' , { cwd : '/app' } ) ;
18
- console . log ( "Template files in /app:" , templateFiles ) ;
19
-
20
- const { stdout : templatePkgJson } = await sandbox . commands . run ( 'cat /app/package.json' , { cwd : '/app' } ) ;
21
- console . log ( "Template package.json:" , templatePkgJson ) ;
22
- } catch ( error ) {
23
- console . error ( "Error checking template:" , error ) ;
12
+ // Create sandbox from the improved template
13
+ const sandbox = await Sandbox . create ( 'vite-support' , { apiKey : E2B_API_KEY } ) ;
14
+ console . log ( `Sandbox created: ${ sandbox . sandboxId } ` ) ;
15
+
16
+ // Check if the user provided CSS files with old Tailwind syntax
17
+ const cssFiles = files . filter ( file => file . path . endsWith ( '.css' ) ) ;
18
+ for ( const cssFile of cssFiles ) {
19
+ // If the file contains @tailwind directives, replace with the new v4 syntax
20
+ if ( cssFile . content . includes ( '@tailwind' ) ) {
21
+ console . log ( `Updating Tailwind v4 syntax in ${ cssFile . path } ` ) ;
22
+ cssFile . content = '@import "tailwindcss";' ;
23
+ }
24
24
}
25
25
26
- // Find AI-generated package.json to extract dependencies
27
- const aiPackageJsonFile = files . find ( file => file . path === 'package.json' ) ;
28
-
29
- // Filter out package.json from files to write (we'll handle it separately)
30
- const filesToWrite = files
31
- . filter ( file => file . path !== 'package.json' )
32
- . map ( file => ( {
33
- path : `/app/${ file . path } ` ,
34
- data : file . content
35
- } ) ) ;
36
-
37
- // Write all files to the sandbox EXCEPT package.json
38
- await sandbox . files . write ( filesToWrite ) ;
39
-
40
- console . log ( "sandbox created" , sandbox . sandboxId ) ;
41
-
42
- // Debug: Verify files after writing
43
- try {
44
- console . log ( "AFTER WRITING FILES:" ) ;
45
- const { stdout : rootContents } = await sandbox . commands . run ( 'ls -la /app' , { cwd : '/app' } ) ;
46
- console . log ( "Files in /app:" , rootContents ) ;
47
-
48
- const { stdout : packageJson } = await sandbox . commands . run ( 'cat /app/package.json' , { cwd : '/app' } ) ;
49
- console . log ( "Current package.json:" , packageJson ) ;
50
-
51
- const { stdout : scriptsList } = await sandbox . commands . run ( 'npm run' , { cwd : '/app' } ) ;
52
- console . log ( "Available npm scripts:" , scriptsList ) ;
53
- } catch ( error ) {
54
- console . error ( "Error in debug commands:" , error ) ;
26
+ // Check if the user provided a postcss.config.js and if it needs to be updated for Tailwind v4
27
+ const postcssFile = files . find ( file => file . path === 'postcss.config.js' ) ;
28
+ if ( postcssFile && postcssFile . content . includes ( 'tailwindcss' ) ) {
29
+ // Fix postcss config to use @tailwindcss /postcss
30
+ const fixedContent = postcssFile . content . replace ( / [ ' " ] t a i l w i n d c s s [ ' " ] / , '"@tailwindcss/postcss"' ) ;
31
+ // Update the file with fixed content
32
+ postcssFile . content = fixedContent ;
55
33
}
56
34
57
- // Process dependencies if AI provided a package.json
58
- if ( aiPackageJsonFile ) {
59
- try {
60
- const aiPackageJson = JSON . parse ( aiPackageJsonFile . content ) ;
61
- const dependencies = aiPackageJson . dependencies || { } ;
62
- const devDependencies = aiPackageJson . devDependencies || { } ;
35
+ // Write files directly to the working directory (/app)
36
+ const filesToWrite = files . map ( file => ( {
37
+ path : `/app/${ file . path } ` ,
38
+ data : file . content
39
+ } ) ) ;
63
40
64
- // Filter out pre-installed dependencies
65
- const preInstalled = [
66
- 'react' , 'react-dom' , '@tailwindcss/vite' , 'tailwindcss' ,
67
- '@types/react' , '@types/react-dom' , '@vitejs/plugin-react' ,
68
- 'typescript' , 'vite' , 'postcss' , 'autoprefixer'
69
- ] ;
70
-
71
- // Get new deps that need to be installed
72
- const newDeps = Object . keys ( dependencies ) . filter ( dep => ! preInstalled . includes ( dep ) ) ;
73
- const newDevDeps = Object . keys ( devDependencies ) . filter ( dep => ! preInstalled . includes ( dep ) ) ;
74
-
75
- // Install only new dependencies if any exist
76
- if ( newDeps . length > 0 ) {
77
- console . log ( "Installing new dependencies:" , newDeps . join ( ", " ) ) ;
78
- await sandbox . commands . run ( `cd /app && npm install --legacy-peer-deps ${ newDeps . join ( ' ' ) } ` ) ;
79
- }
80
-
81
- if ( newDevDeps . length > 0 ) {
82
- console . log ( "Installing new dev dependencies:" , newDevDeps . join ( ", " ) ) ;
83
- await sandbox . commands . run ( `cd /app && npm install --legacy-peer-deps --save-dev ${ newDevDeps . join ( ' ' ) } ` ) ;
84
- }
85
- } catch ( error ) {
86
- console . error ( "Error parsing package.json:" , error ) ;
87
- }
88
- }
89
-
90
- // Fix permissions with sudo before starting
91
- try {
92
- await sandbox . commands . run ( 'sudo rm -rf /app/node_modules/.vite' , { cwd : '/app' } ) ;
93
- await sandbox . commands . run ( 'sudo mkdir -p /app/node_modules/.vite' , { cwd : '/app' } ) ;
94
- await sandbox . commands . run ( 'sudo chmod -R 777 /app/node_modules/.vite' , { cwd : '/app' } ) ;
95
- } catch ( error ) {
96
- console . error ( "Error fixing permissions:" , error ) ;
97
- }
41
+ await sandbox . files . write ( filesToWrite ) ;
98
42
99
- // Run the Vite app
100
- try {
101
- await sandbox . commands . run ( 'npm run dev -- --host' , {
102
- cwd : '/app' ,
103
- timeoutMs : 0 ,
104
- } ) ;
105
- } catch ( error ) {
106
- console . error ( "Error running Vite:" , error ) ;
107
- }
43
+ const previewUrl = `https://${ sandbox . getHost ( 5173 ) } ` ;
44
+ console . log ( 'Preview URL: ' , previewUrl ) ;
108
45
109
46
return {
110
47
sbxId : sandbox . sandboxId ,
111
- template : 'vite-template ' ,
112
- url : `https:// ${ sandbox . getHost ( 5173 ) } `
48
+ template : 'vite-support ' ,
49
+ url : previewUrl
113
50
} ;
114
51
}
115
52
0 commit comments