Skip to content

Commit b5c6df9

Browse files
christianalfonikodebots
andauthored
fix: Remove devcontainer folder when .codesandbox/Dockerfile identified (#196)
* When building a template we should identify if there is a .codesandbox/Dockerfile in the template, if so during writing of files to the Sandbox we should also delete the .devcontainer file already in the Sandbox. This is related to the CLI build command * chore: clean up --------- Co-authored-by: kodebots <[email protected]>
1 parent 93e65b3 commit b5c6df9

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

src/bin/commands/build.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@ async function writeFileEnsureDir(filePath, data) {
3737
await writeFile(filePath, data);
3838
}
3939

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+
4071
function stripAnsiCodes(str: string) {
4172
// Matches ESC [ params … finalChar
4273
// \x1B = ESC
@@ -103,31 +134,34 @@ export const buildCommand: yargs.CommandModule<
103134
// Validate ports parameter - ensure all values are valid numbers
104135
if (argv.ports && argv.ports.length > 0) {
105136
const invalidPortsWithOriginal: string[] = [];
106-
137+
107138
// Get the original arguments to show what the user actually typed
108139
const originalArgs = process.argv;
109140
const portArgIndices: number[] = [];
110-
141+
111142
// Find all --ports arguments in the original command
112143
originalArgs.forEach((arg, i) => {
113-
if (arg === '--ports' && i + 1 < originalArgs.length) {
144+
if (arg === "--ports" && i + 1 < originalArgs.length) {
114145
portArgIndices.push(i + 1);
115146
}
116147
});
117-
148+
118149
argv.ports.forEach((port, i) => {
119-
const isInvalid = !Number.isInteger(port) ||
150+
const isInvalid =
151+
!Number.isInteger(port) ||
120152
port <= 0 ||
121153
port > 65535 ||
122154
!Number.isFinite(port);
123-
155+
124156
if (isInvalid) {
125157
// 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);
127161
invalidPortsWithOriginal.push(originalInput);
128162
}
129163
});
130-
164+
131165
if (invalidPortsWithOriginal.length > 0) {
132166
throw new Error(
133167
`Invalid port value(s): ${invalidPortsWithOriginal.join(
@@ -140,7 +174,6 @@ export const buildCommand: yargs.CommandModule<
140174
}),
141175

142176
handler: async (argv) => {
143-
144177
const apiKey = getInferredApiKey();
145178
const api = new API({ apiKey, instrumentation: instrumentedFetch });
146179
const sdk = new CodeSandbox(apiKey);
@@ -294,6 +327,14 @@ export const buildCommand: yargs.CommandModule<
294327
throw new Error(`Failed to write files to sandbox: ${error}`);
295328
});
296329

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+
297338
// Dispose of the session after writing files to prevent reconnection
298339
session.dispose();
299340
currentSession = null;

0 commit comments

Comments
 (0)