forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
50 lines (45 loc) · 2.07 KB
/
utils.ts
File metadata and controls
50 lines (45 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as path from 'path';
import { IWorkspaceService } from '../common/application/types';
import { IConfigurationService } from '../common/types';
import { IDataScienceFileSystem } from './types';
export async function calculateWorkingDirectory(
configService: IConfigurationService,
workspace: IWorkspaceService,
fs: IDataScienceFileSystem
): Promise<string | undefined> {
let workingDir: string | undefined;
// For a local launch calculate the working directory that we should switch into
const settings = configService.getSettings(undefined);
const fileRoot = settings.datascience.notebookFileRoot;
// If we don't have a workspace open the notebookFileRoot seems to often have a random location in it (we use ${workspaceRoot} as default)
// so only do this setting if we actually have a valid workspace open
if (fileRoot && workspace.hasWorkspaceFolders) {
const workspaceFolderPath = workspace.workspaceFolders![0].uri.fsPath;
if (path.isAbsolute(fileRoot)) {
if (await fs.localDirectoryExists(fileRoot)) {
// User setting is absolute and exists, use it
workingDir = fileRoot;
} else {
// User setting is absolute and doesn't exist, use workspace
workingDir = workspaceFolderPath;
}
} else if (!fileRoot.includes('${')) {
// fileRoot is a relative path, combine it with the workspace folder
const combinedPath = path.join(workspaceFolderPath, fileRoot);
if (await fs.localDirectoryExists(combinedPath)) {
// combined path exists, use it
workingDir = combinedPath;
} else {
// Combined path doesn't exist, use workspace
workingDir = workspaceFolderPath;
}
} else {
// fileRoot is a variable that hasn't been expanded
workingDir = fileRoot;
}
}
return workingDir;
}