-
Notifications
You must be signed in to change notification settings - Fork 13
/
deno_workspace.ts
62 lines (51 loc) · 1.66 KB
/
deno_workspace.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
import { parseYaml, YAMLError, extname } from "../../deps.ts";
import { DENO_WORKSPACE_FILES } from "../const.ts";
import { DenoWorkspace } from "../interfaces.ts";
import { getFirstExistingPath, getFileContent } from "../utils/file.ts";
import {
WorkspaceNotFoundError,
WorkspaceFileIsMalformed,
} from "../utils/DenoXErrors.ts";
async function loadDenoWorkspace(): Promise<DenoWorkspace> {
try {
const denoWorkspaceFilePath = await getFirstExistingPath(
DENO_WORKSPACE_FILES,
);
if (extname(denoWorkspaceFilePath) === ".ts") {
const appendFileScheme = Deno.build.os == "windows" ? 'file:///' : '';
return await _loadTSWorkspace(appendFileScheme + denoWorkspaceFilePath);
}
return await _loadYAMLWorkspace(denoWorkspaceFilePath) as DenoWorkspace;
} catch (e) {
throw _handleLoadDenoWorkspaceErrors(e);
}
}
async function _loadTSWorkspace(
denoWorkspaceFilePath: string,
): Promise<DenoWorkspace> {
const { workspace } = await import(denoWorkspaceFilePath) as {
workspace: DenoWorkspace;
};
return workspace;
}
async function _loadYAMLWorkspace(
denoWorkspaceFilePath: string,
): Promise<DenoWorkspace> {
const denoWorkspaceFileContent = await getFileContent(
denoWorkspaceFilePath,
);
return parseYaml(denoWorkspaceFileContent) as DenoWorkspace;
}
function _handleLoadDenoWorkspaceErrors(e: unknown): Error {
if (e instanceof Deno.errors.NotFound) {
return new WorkspaceNotFoundError();
}
if (
e instanceof YAMLError || e instanceof ReferenceError ||
e instanceof TypeError
) {
return new WorkspaceFileIsMalformed(e.message);
}
return e as Error;
}
export { loadDenoWorkspace };