Repository navigation
Expand file tree
/
Copy pathpath-discovery.ts
More file actions
77 lines (68 loc) · 2.16 KB
/
Copy pathpath-discovery.ts
File metadata and controls
77 lines (68 loc) · 2.16 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fsSync from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import type { LoadConfigOptions } from '../config-schema.js';
import { expandHome } from '../env.js';
import { mcporterConfigCandidates } from '../paths.js';
export interface ResolvedConfigPath {
path: string;
explicit: boolean;
}
export async function listConfigLayerPaths(
options: LoadConfigOptions = {},
rootDir: string = process.cwd()
): Promise<string[]> {
const explicitPath = options.configPath ?? process.env.MCPORTER_CONFIG;
if (explicitPath) {
return [path.resolve(expandHome(explicitPath.trim()))];
}
const paths: string[] = [];
const homeCandidates = homeConfigCandidates();
const existingHome = homeCandidates.find((candidate) => pathExists(candidate));
if (existingHome) {
paths.push(existingHome);
}
const projectPath = path.resolve(rootDir, 'config', 'mcporter.json');
if (pathExists(projectPath)) {
paths.push(projectPath);
}
return paths;
}
export function resolveConfigPath(configPath: string | undefined, rootDir: string): ResolvedConfigPath {
if (configPath) {
return { path: path.resolve(configPath), explicit: true };
}
const envConfig = process.env.MCPORTER_CONFIG;
if (envConfig && envConfig.trim().length > 0) {
return { path: path.resolve(expandHome(envConfig.trim())), explicit: true };
}
const projectPath = path.resolve(rootDir, 'config', 'mcporter.json');
if (pathExists(projectPath)) {
return { path: projectPath, explicit: false };
}
const homeCandidates = homeConfigCandidates();
const existingHome = homeCandidates.find((candidate) => pathExists(candidate));
if (existingHome) {
return { path: existingHome, explicit: false };
}
return { path: projectPath, explicit: false };
}
export function homeConfigCandidates(): string[] {
return mcporterConfigCandidates();
}
export function pathExists(filePath: string): boolean {
try {
fsSync.accessSync(filePath);
return true;
} catch {
return false;
}
}
export async function pathExistsAsync(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}