forked from supabase/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.ts
More file actions
89 lines (75 loc) · 2.77 KB
/
Copy pathpaths.ts
File metadata and controls
89 lines (75 loc) · 2.77 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
81
82
83
84
85
86
87
88
89
import { Effect, FileSystem, Path } from "effect";
export interface ProjectPaths {
readonly projectRoot: string;
readonly supabaseDir: string;
readonly configPath: string;
readonly envPath: string;
readonly envLocalPath: string;
}
const findConfigInRoot = Effect.fnUntraced(function* (root: string) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const supabaseDir = path.join(root, "supabase");
const jsonPath = path.join(supabaseDir, "config.json");
const tomlPath = path.join(supabaseDir, "config.toml");
const jsonExists = yield* fs.exists(jsonPath);
const tomlExists = yield* fs.exists(tomlPath);
if (!jsonExists && !tomlExists) {
return null;
}
return {
projectRoot: root,
supabaseDir,
configPath: jsonExists ? jsonPath : tomlPath,
envPath: path.join(supabaseDir, ".env"),
envLocalPath: path.join(supabaseDir, ".env.local"),
} satisfies ProjectPaths;
});
export interface FindProjectPathsOptions {
/**
* When `false`, only `cwd` itself is checked for `supabase/config.{json,toml}` —
* no ancestor climb. Go's own resolution never searches twice: an explicit
* `--workdir`/`SUPABASE_WORKDIR` is used exactly as given (`ChangeWorkDir`,
* `apps/cli-go/internal/utils/misc.go:231-247`), and once `os.Chdir`'d there,
* `config.toml` is read as a plain relative path with no further ancestor
* search (`NewPathBuilder`, `pkg/config/utils.go:43-48`). Ancestor climbing in
* Go only ever happens once, as the *default* when workdir is unset
* (`getProjectRoot`, `internal/utils/misc.go:209-224`).
*
* Callers that already hold an authoritative, Go-equivalent project root
* (e.g. the legacy `stop`/`status` ports' `cliConfig.workdir`, which mirrors
* `ChangeWorkDir`'s own explicit-vs-default resolution) should pass `false`
* here to avoid a second, un-Go-like ancestor search that could otherwise
* pick up an unrelated ancestor project's config.
*
* Defaults to `true` (the original ancestor-search behavior), so existing
* callers are unaffected.
*/
readonly search?: boolean;
}
export const findProjectPaths = Effect.fnUntraced(function* (
cwd: string,
options?: FindProjectPathsOptions,
) {
const path = yield* Path.Path;
const start = path.resolve(cwd);
if (options?.search === false) {
return yield* findConfigInRoot(start);
}
let current = start;
while (true) {
const match = yield* findConfigInRoot(current);
if (match !== null) {
return match;
}
const parent = path.dirname(current);
if (parent === current) {
return null;
}
current = parent;
}
});
export const findProjectRoot = Effect.fnUntraced(function* (cwd: string) {
const paths = yield* findProjectPaths(cwd);
return paths?.projectRoot ?? null;
});