-
-
Notifications
You must be signed in to change notification settings - Fork 853
Expand file tree
/
Copy pathfind-config.ts
More file actions
80 lines (70 loc) · 2.55 KB
/
Copy pathfind-config.ts
File metadata and controls
80 lines (70 loc) · 2.55 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
78
79
80
import { buildError, isString, normalizePath, result } from '@utils';
import type { CompilerSystem, Diagnostic } from '../declarations';
/**
* An object containing the {@link CompilerSystem} used to find the configuration file, as well as the location on disk
* to search for a Stencil configuration
*/
export type FindConfigOptions = {
sys: CompilerSystem;
configPath?: string | null;
};
/**
* The results of attempting to find a Stencil configuration file on disk
*/
export type FindConfigResults = {
configPath: string;
rootDir: string;
};
/**
* Attempt to find a Stencil configuration file on the file system
* @param opts the options needed to find the configuration file
* @returns the results of attempting to find a configuration file on disk
*/
export const findConfig = async (opts: FindConfigOptions): Promise<result.Result<FindConfigResults, Diagnostic[]>> => {
const sys = opts.sys;
const cwd = sys.getCurrentDirectory();
const rootDir = normalizePath(cwd);
let configPath = opts.configPath;
if (isString(configPath)) {
if (!sys.platformPath.isAbsolute(configPath)) {
// passed in a custom stencil config location,
// but it's relative, so prefix the cwd
configPath = normalizePath(sys.platformPath.join(cwd, configPath));
} else {
// config path already an absolute path, we're good here
configPath = normalizePath(configPath);
}
} else {
// nothing was passed in, use the current working directory
configPath = rootDir;
}
const results: FindConfigResults = {
configPath,
rootDir: normalizePath(cwd),
};
const stat = await sys.stat(configPath);
if (stat.error) {
const diagnostics: Diagnostic[] = [];
const diagnostic = buildError(diagnostics);
diagnostic.absFilePath = configPath;
diagnostic.header = `Invalid config path`;
diagnostic.messageText = `Config path "${configPath}" not found`;
return result.err(diagnostics);
}
if (stat.isFile) {
results.configPath = configPath;
results.rootDir = sys.platformPath.dirname(configPath);
} else if (stat.isDirectory) {
// this is only a directory, so let's make some assumptions
for (const configName of ['stencil.config.ts', 'stencil.config.js']) {
const testConfigFilePath = sys.platformPath.join(configPath, configName);
const stat = await sys.stat(testConfigFilePath);
if (stat.isFile) {
results.configPath = testConfigFilePath;
results.rootDir = sys.platformPath.dirname(testConfigFilePath);
break;
}
}
}
return result.ok(results);
};