-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocal-state.ts
More file actions
221 lines (188 loc) · 6.67 KB
/
Copy pathlocal-state.ts
File metadata and controls
221 lines (188 loc) · 6.67 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { mkdir, readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import type { AuthProviderId } from "../types/auth";
import type { GitRepositoryConnection } from "../types/project";
export interface LocalState {
auth: {
provider: AuthProviderId;
userId: string;
workspaceId: string;
} | null;
project: {
rememberedByWorkspace: Record<string, RememberedProjectState>;
lastResolved: RememberedProjectState | null;
repositoryConnectionsByProject: Record<string, GitRepositoryConnection>;
};
branch: {
active: string;
};
app: {
selectedByProject: Record<string, SelectedAppState>;
knownLiveDeploymentByProject: Record<string, Record<string, string>>;
};
}
export interface SelectedAppState {
id: string;
name: string;
}
export interface RememberedProjectState {
id: string;
name: string;
workspaceId: string;
}
const DEFAULT_STATE: LocalState = {
auth: null,
project: {
rememberedByWorkspace: {},
lastResolved: null,
repositoryConnectionsByProject: {},
},
branch: {
active: "preview",
},
app: {
selectedByProject: {},
knownLiveDeploymentByProject: {},
},
};
export const DEFAULT_STATE_FILE_NAME = "state.json";
export function resolveLocalStateFilePath(stateDir: string): string {
return path.join(stateDir, DEFAULT_STATE_FILE_NAME);
}
export class LocalStateStore {
private readonly stateFilePath: string;
constructor(stateDir: string, private readonly signal?: AbortSignal) {
this.stateFilePath = resolveLocalStateFilePath(stateDir);
}
async read(): Promise<LocalState> {
this.signal?.throwIfAborted();
try {
const raw = await readFile(this.stateFilePath, { encoding: "utf8", signal: this.signal });
const parsed = JSON.parse(raw) as Partial<LocalState>;
return {
auth: parsed.auth ?? structuredClone(DEFAULT_STATE.auth),
project: {
rememberedByWorkspace: parsed.project?.rememberedByWorkspace ?? {},
lastResolved: parsed.project?.lastResolved ?? null,
repositoryConnectionsByProject: parsed.project?.repositoryConnectionsByProject ?? {},
},
branch: {
active: parsed.branch?.active ?? DEFAULT_STATE.branch.active,
},
app: {
selectedByProject: parsed.app?.selectedByProject ?? {},
knownLiveDeploymentByProject: parsed.app?.knownLiveDeploymentByProject ?? {},
},
};
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
return structuredClone(DEFAULT_STATE);
}
throw error;
}
}
async write(state: LocalState): Promise<void> {
this.signal?.throwIfAborted();
// mkdir does not accept AbortSignal; check before the filesystem boundary.
await mkdir(path.dirname(this.stateFilePath), { recursive: true });
this.signal?.throwIfAborted();
await writeFile(this.stateFilePath, `${JSON.stringify(state, null, 2)}\n`, { encoding: "utf8" });
this.signal?.throwIfAborted();
}
async setAuthSession(session: NonNullable<LocalState["auth"]>): Promise<LocalState> {
const state = await this.read();
state.auth = session;
await this.write(state);
return state;
}
async clearAuthSession(): Promise<LocalState> {
const state = await this.read();
state.auth = null;
await this.write(state);
return state;
}
async setActiveBranch(active: string): Promise<LocalState> {
const state = await this.read();
state.branch.active = active;
await this.write(state);
return state;
}
async readRememberedProject(workspaceId: string): Promise<RememberedProjectState | null> {
const state = await this.read();
return state.project.rememberedByWorkspace[workspaceId] ?? null;
}
async readLastResolvedProject(): Promise<RememberedProjectState | null> {
const state = await this.read();
return state.project.lastResolved;
}
async setRememberedProject(project: RememberedProjectState): Promise<LocalState> {
const state = await this.read();
state.project.rememberedByWorkspace[project.workspaceId] = project;
state.project.lastResolved = project;
await this.write(state);
return state;
}
async readRepositoryConnection(projectId: string): Promise<GitRepositoryConnection | null> {
const state = await this.read();
return state.project.repositoryConnectionsByProject[projectId] ?? null;
}
async setRepositoryConnection(
projectId: string,
connection: GitRepositoryConnection,
): Promise<LocalState> {
const state = await this.read();
state.project.repositoryConnectionsByProject[projectId] = connection;
await this.write(state);
return state;
}
async clearRepositoryConnection(projectId: string): Promise<LocalState> {
const state = await this.read();
delete state.project.repositoryConnectionsByProject[projectId];
await this.write(state);
return state;
}
async readSelectedApp(projectId: string): Promise<SelectedAppState | null> {
const state = await this.read();
return state.app.selectedByProject[projectId] ?? null;
}
async setSelectedApp(projectId: string, app: SelectedAppState): Promise<LocalState> {
const state = await this.read();
state.app.selectedByProject[projectId] = app;
await this.write(state);
return state;
}
async clearSelectedApp(projectId: string, appId: string): Promise<LocalState> {
const state = await this.read();
const selectedApp = state.app.selectedByProject[projectId];
if (!selectedApp || selectedApp.id !== appId) {
return state;
}
delete state.app.selectedByProject[projectId];
await this.write(state);
return state;
}
async readKnownLiveDeployment(projectId: string, appId: string): Promise<string | null> {
const state = await this.read();
return state.app.knownLiveDeploymentByProject[projectId]?.[appId] ?? null;
}
async setKnownLiveDeployment(projectId: string, appId: string, deploymentId: string): Promise<LocalState> {
const state = await this.read();
state.app.knownLiveDeploymentByProject[projectId] ??= {};
state.app.knownLiveDeploymentByProject[projectId][appId] = deploymentId;
await this.write(state);
return state;
}
async clearKnownLiveDeployment(projectId: string, appId: string): Promise<LocalState> {
const state = await this.read();
const projectDeployments = state.app.knownLiveDeploymentByProject[projectId];
if (!projectDeployments || !(appId in projectDeployments)) {
return state;
}
delete projectDeployments[appId];
if (Object.keys(projectDeployments).length === 0) {
delete state.app.knownLiveDeploymentByProject[projectId];
}
await this.write(state);
return state;
}
}