@@ -37,6 +37,37 @@ async function writeFileEnsureDir(filePath, data) {
37
37
await writeFile ( filePath , data ) ;
38
38
}
39
39
40
+ async function hasDockerfile ( templateDirectory : string ) : Promise < boolean > {
41
+ try {
42
+ const dockerfilePath = path . join (
43
+ templateDirectory ,
44
+ ".codesandbox" ,
45
+ "Dockerfile"
46
+ ) ;
47
+ await fs . access ( dockerfilePath ) ;
48
+ return true ;
49
+ } catch {
50
+ return false ;
51
+ }
52
+ }
53
+
54
+ async function removeDevcontainerFiles ( session : SandboxClient ) : Promise < void > {
55
+ try {
56
+ // Check if .devcontainer directory exists
57
+ const devcontainerPath = ".devcontainer" ;
58
+ try {
59
+ await session . fs . stat ( devcontainerPath ) ;
60
+ // If we reach here, the directory exists, so remove it
61
+ await session . fs . remove ( devcontainerPath , true ) ;
62
+ } catch {
63
+ // Directory doesn't exist, nothing to remove
64
+ }
65
+ } catch ( error ) {
66
+ // Log but don't fail the build if devcontainer cleanup fails
67
+ console . warn ( `Warning: Failed to remove .devcontainer files: ${ error } ` ) ;
68
+ }
69
+ }
70
+
40
71
function stripAnsiCodes ( str : string ) {
41
72
// Matches ESC [ params … finalChar
42
73
// \x1B = ESC
@@ -103,31 +134,34 @@ export const buildCommand: yargs.CommandModule<
103
134
// Validate ports parameter - ensure all values are valid numbers
104
135
if ( argv . ports && argv . ports . length > 0 ) {
105
136
const invalidPortsWithOriginal : string [ ] = [ ] ;
106
-
137
+
107
138
// Get the original arguments to show what the user actually typed
108
139
const originalArgs = process . argv ;
109
140
const portArgIndices : number [ ] = [ ] ;
110
-
141
+
111
142
// Find all --ports arguments in the original command
112
143
originalArgs . forEach ( ( arg , i ) => {
113
- if ( arg === ' --ports' && i + 1 < originalArgs . length ) {
144
+ if ( arg === " --ports" && i + 1 < originalArgs . length ) {
114
145
portArgIndices . push ( i + 1 ) ;
115
146
}
116
147
} ) ;
117
-
148
+
118
149
argv . ports . forEach ( ( port , i ) => {
119
- const isInvalid = ! Number . isInteger ( port ) ||
150
+ const isInvalid =
151
+ ! Number . isInteger ( port ) ||
120
152
port <= 0 ||
121
153
port > 65535 ||
122
154
! Number . isFinite ( port ) ;
123
-
155
+
124
156
if ( isInvalid ) {
125
157
// Try to get the original input, fallback to the parsed value
126
- const originalInput = portArgIndices [ i ] ? originalArgs [ portArgIndices [ i ] ] : String ( port ) ;
158
+ const originalInput = portArgIndices [ i ]
159
+ ? originalArgs [ portArgIndices [ i ] ]
160
+ : String ( port ) ;
127
161
invalidPortsWithOriginal . push ( originalInput ) ;
128
162
}
129
163
} ) ;
130
-
164
+
131
165
if ( invalidPortsWithOriginal . length > 0 ) {
132
166
throw new Error (
133
167
`Invalid port value(s): ${ invalidPortsWithOriginal . join (
@@ -140,7 +174,6 @@ export const buildCommand: yargs.CommandModule<
140
174
} ) ,
141
175
142
176
handler : async ( argv ) => {
143
-
144
177
const apiKey = getInferredApiKey ( ) ;
145
178
const api = new API ( { apiKey, instrumentation : instrumentedFetch } ) ;
146
179
const sdk = new CodeSandbox ( apiKey ) ;
@@ -294,6 +327,14 @@ export const buildCommand: yargs.CommandModule<
294
327
throw new Error ( `Failed to write files to sandbox: ${ error } ` ) ;
295
328
} ) ;
296
329
330
+ // Check if template has .codesandbox/Dockerfile and remove .devcontainer files if so
331
+ if ( await hasDockerfile ( argv . directory ) ) {
332
+ spinner . start (
333
+ updateSpinnerMessage ( index , "Configuring Docker file..." )
334
+ ) ;
335
+ await removeDevcontainerFiles ( session ) ;
336
+ }
337
+
297
338
// Dispose of the session after writing files to prevent reconnection
298
339
session . dispose ( ) ;
299
340
currentSession = null ;
0 commit comments