Skip to content

Commit 3a63148

Browse files
Copilotchrmarti
andcommitted
Refactor workspace folder derivation logic into utility function
Co-authored-by: chrmarti <[email protected]>
1 parent 959ad6f commit 3a63148

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

src/spec-node/devContainersSpecCLI.ts

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ const defaultDefaultUserEnvProbe: UserEnvProbe = 'loginInteractiveShell';
5050

5151
const mountRegex = /^type=(bind|volume),source=([^,]+),target=([^,]+)(?:,external=(true|false))?$/;
5252

53+
/**
54+
* Derives the workspace folder from either the provided workspace folder argument or the config path.
55+
* When config is in a .devcontainer directory, the workspace folder is the parent directory.
56+
* Otherwise, it's the directory containing the config file.
57+
*/
58+
function deriveWorkspaceFolder(workspaceFolderArg: string | undefined, configParam: string | undefined): string {
59+
if (workspaceFolderArg) {
60+
return path.resolve(process.cwd(), workspaceFolderArg);
61+
}
62+
if (configParam) {
63+
const resolvedConfig = path.resolve(process.cwd(), configParam);
64+
const configDir = path.dirname(resolvedConfig);
65+
// If config is in a .devcontainer directory, go up one level to get the workspace folder
66+
if (path.basename(configDir) === '.devcontainer') {
67+
return path.dirname(configDir);
68+
}
69+
return configDir;
70+
}
71+
// This should never be reached due to validation, but provide a fallback
72+
return process.cwd();
73+
}
74+
5375
(async () => {
5476

5577
const packageFolder = path.join(__dirname, '..', '..');
@@ -580,24 +602,7 @@ async function doBuild({
580602
await Promise.all(disposables.map(d => d()));
581603
};
582604
try {
583-
// If workspace-folder is not provided, derive it from config path
584-
// When config is in .devcontainer/devcontainer.json, we need to go up two levels
585-
// When config is in devcontainer.json, we need to go up one level
586-
let workspaceFolder: string;
587-
if (workspaceFolderArg) {
588-
workspaceFolder = path.resolve(process.cwd(), workspaceFolderArg);
589-
} else if (configParam) {
590-
const resolvedConfig = path.resolve(process.cwd(), configParam);
591-
const configDir = path.dirname(resolvedConfig);
592-
// If config is in a .devcontainer directory, go up one more level
593-
if (path.basename(configDir) === '.devcontainer') {
594-
workspaceFolder = path.dirname(configDir);
595-
} else {
596-
workspaceFolder = configDir;
597-
}
598-
} else {
599-
workspaceFolder = process.cwd();
600-
}
605+
const workspaceFolder = deriveWorkspaceFolder(workspaceFolderArg, configParam);
601606
const configFile: URI | undefined = configParam ? URI.file(path.resolve(process.cwd(), configParam)) : undefined;
602607
const overrideConfigFile: URI | undefined = /* overrideConfig ? URI.file(path.resolve(process.cwd(), overrideConfig)) : */ undefined;
603608
const addCacheFroms = addCacheFrom ? (Array.isArray(addCacheFrom) ? addCacheFrom as string[] : [addCacheFrom]) : [];
@@ -1168,24 +1173,7 @@ async function outdated({
11681173
};
11691174
let output: Log | undefined;
11701175
try {
1171-
// If workspace-folder is not provided, derive it from config path
1172-
// When config is in .devcontainer/devcontainer.json, we need to go up two levels
1173-
// When config is in devcontainer.json, we need to go up one level
1174-
let workspaceFolder: string;
1175-
if (workspaceFolderArg) {
1176-
workspaceFolder = path.resolve(process.cwd(), workspaceFolderArg);
1177-
} else if (configParam) {
1178-
const resolvedConfig = path.resolve(process.cwd(), configParam);
1179-
const configDir = path.dirname(resolvedConfig);
1180-
// If config is in a .devcontainer directory, go up one more level
1181-
if (path.basename(configDir) === '.devcontainer') {
1182-
workspaceFolder = path.dirname(configDir);
1183-
} else {
1184-
workspaceFolder = configDir;
1185-
}
1186-
} else {
1187-
workspaceFolder = process.cwd();
1188-
}
1176+
const workspaceFolder = deriveWorkspaceFolder(workspaceFolderArg, configParam);
11891177
const configFile = configParam ? URI.file(path.resolve(process.cwd(), configParam)) : undefined;
11901178
const cliHost = await getCLIHost(workspaceFolder, loadNativeModule, logFormat === 'text');
11911179
const extensionPath = path.join(__dirname, '..', '..');

0 commit comments

Comments
 (0)