-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathglobal-environments.js
More file actions
358 lines (326 loc) · 13.6 KB
/
Copy pathglobal-environments.js
File metadata and controls
358 lines (326 loc) · 13.6 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { createSlice } from '@reduxjs/toolkit';
import { uuid } from 'utils/common/index';
import { environmentSchema } from '@usebruno/schema';
import { getDataTypeFromValue } from '@usebruno/common/utils';
import { cloneDeep } from 'lodash';
import { applyScriptEnvVars, getScriptModifiedKeys } from 'utils/environments';
const initialState = {
globalEnvironments: [],
activeGlobalEnvironmentUid: null,
globalEnvironmentDraft: null,
_scriptGlobalEnvBaseline: null
};
// Properties prefixed with `_` (e.g. `_scriptGlobalEnvBaseline`) are transient runtime state —
// never persisted to disk or included in exports.
export const globalEnvironmentsSlice = createSlice({
name: 'global-environments',
initialState,
reducers: {
updateGlobalEnvironments: (state, action) => {
const newEnvs = action.payload?.globalEnvironments || [];
const incomingActiveUid = action.payload?.activeGlobalEnvironmentUid ?? null;
const resolvedActiveUid = incomingActiveUid && newEnvs.some((e) => e?.uid === incomingActiveUid)
? incomingActiveUid
: null;
state.globalEnvironments = newEnvs;
state.activeGlobalEnvironmentUid = resolvedActiveUid;
},
_addGlobalEnvironment: (state, action) => {
const { name, uid, variables = [], color } = action.payload;
if (name?.length) {
state.globalEnvironments.push({
uid,
name,
variables,
color
});
}
},
_saveGlobalEnvironment: (state, action) => {
const { environmentUid: globalEnvironmentUid, variables } = action.payload;
if (globalEnvironmentUid) {
const environment = state.globalEnvironments.find((env) => env?.uid == globalEnvironmentUid);
if (environment) {
environment.variables = variables;
}
}
},
_renameGlobalEnvironment: (state, action) => {
const { environmentUid: globalEnvironmentUid, name } = action.payload;
if (globalEnvironmentUid) {
const environment = state.globalEnvironments.find((env) => env?.uid == globalEnvironmentUid);
if (environment) {
environment.name = name;
}
}
},
_copyGlobalEnvironment: (state, action) => {
const { name, uid, variables } = action.payload;
if (name?.length && uid) {
state.globalEnvironments.push({
uid,
name,
variables
});
}
},
_selectGlobalEnvironment: (state, action) => {
const { environmentUid: globalEnvironmentUid } = action.payload;
if (globalEnvironmentUid) {
const environment = state.globalEnvironments.find((env) => env?.uid == globalEnvironmentUid);
if (environment) {
state.activeGlobalEnvironmentUid = globalEnvironmentUid;
}
} else {
state.activeGlobalEnvironmentUid = null;
}
},
_deleteGlobalEnvironment: (state, action) => {
const { environmentUid: uid } = action.payload;
if (uid) {
state.globalEnvironments = state.globalEnvironments.filter((env) => env?.uid !== uid);
if (uid === state.activeGlobalEnvironmentUid) {
state.activeGlobalEnvironmentUid = null;
}
}
},
setGlobalEnvironmentDraft: (state, action) => {
const { environmentUid, variables } = action.payload;
state.globalEnvironmentDraft = { environmentUid, variables };
},
clearGlobalEnvironmentDraft: (state) => {
state.globalEnvironmentDraft = null;
},
_setScriptGlobalEnvBaseline: (state, action) => {
state._scriptGlobalEnvBaseline = action.payload;
},
_clearScriptGlobalEnvBaseline: (state) => {
state._scriptGlobalEnvBaseline = null;
},
_updateGlobalEnvironmentColor: (state, action) => {
const { environmentUid, color } = action.payload;
if (environmentUid) {
state.globalEnvironments = state.globalEnvironments.map((env) => env?.uid == environmentUid ? { ...env, color } : env);
}
}
}
});
export const {
updateGlobalEnvironments,
_addGlobalEnvironment,
_saveGlobalEnvironment,
_renameGlobalEnvironment,
_copyGlobalEnvironment,
_selectGlobalEnvironment,
_deleteGlobalEnvironment,
_updateGlobalEnvironmentColor,
setGlobalEnvironmentDraft,
clearGlobalEnvironmentDraft,
_setScriptGlobalEnvBaseline,
_clearScriptGlobalEnvBaseline
} = globalEnvironmentsSlice.actions;
const getWorkspaceContext = (state) => {
const workspaceUid = state.workspaces?.activeWorkspaceUid;
const workspace = state.workspaces?.workspaces?.find((w) => w.uid === workspaceUid);
return { workspaceUid, workspacePath: workspace?.pathname };
};
export const addGlobalEnvironment = ({ name, variables = [], color }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const uid = uuid();
const environment = { name, uid, variables };
const { ipcRenderer } = window;
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
environmentSchema
.validate(environment)
.then(() => ipcRenderer.invoke('renderer:create-global-environment', { name, uid, variables, color, workspaceUid, workspacePath }))
.then((result) => {
const finalUid = result?.uid || uid;
const finalName = result?.name || name;
const finalVariables = result?.variables || variables;
const finalColor = result?.color || color;
dispatch(_addGlobalEnvironment({ name: finalName, uid: finalUid, variables: finalVariables, color: finalColor }));
return finalUid;
})
.then((finalUid) => dispatch(selectGlobalEnvironment({ environmentUid: finalUid })))
.then(resolve)
.catch(reject);
});
};
export const copyGlobalEnvironment = ({ name, environmentUid: baseEnvUid }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
const globalEnvironments = state.globalEnvironments.globalEnvironments;
const baseEnv = globalEnvironments?.find((env) => env?.uid == baseEnvUid);
if (!baseEnv) {
return reject(new Error('Base environment not found'));
}
const uid = uuid();
const environment = { uid, name, variables: baseEnv.variables };
const { ipcRenderer } = window;
environmentSchema
.validate(environment)
.then(() => ipcRenderer.invoke('renderer:create-global-environment', { uid, name, variables: baseEnv.variables, workspaceUid, workspacePath }))
.then((result) => {
const finalUid = result?.uid || uid;
const finalName = result?.name || name;
const finalVariables = result?.variables || baseEnv.variables;
dispatch(_copyGlobalEnvironment({ name: finalName, uid: finalUid, variables: finalVariables }));
})
.then(resolve)
.catch(reject);
});
};
export const renameGlobalEnvironment = ({ name: newName, environmentUid }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
const globalEnvironments = state.globalEnvironments.globalEnvironments;
const environment = globalEnvironments?.find((env) => env?.uid == environmentUid);
if (!environment) {
return reject(new Error('Environment not found'));
}
environmentSchema
.validate(environment)
.then(() => ipcRenderer.invoke('renderer:rename-global-environment', { name: newName, environmentUid, workspaceUid, workspacePath }))
.then((result) => {
const resolvedUid = result?.uid || environmentUid;
dispatch(_renameGlobalEnvironment({ name: newName, environmentUid: resolvedUid }));
return ipcRenderer
.invoke('renderer:get-global-environments', { workspaceUid, workspacePath })
.then((data) => {
dispatch(updateGlobalEnvironments(data));
if (resolvedUid !== environmentUid) {
const currentState = getState();
const draft = currentState.globalEnvironments.globalEnvironmentDraft;
if (draft?.environmentUid === environmentUid) {
dispatch(setGlobalEnvironmentDraft({ environmentUid: resolvedUid, variables: draft.variables }));
}
}
return resolvedUid;
});
})
.then((resolvedUid) => dispatch(_selectGlobalEnvironment({ environmentUid: resolvedUid })))
.then(resolve)
.catch(reject);
});
};
export const saveGlobalEnvironment = ({ variables, environmentUid }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
const globalEnvironments = state.globalEnvironments.globalEnvironments;
let environment = globalEnvironments?.find((env) => env?.uid == environmentUid);
if (!environment) {
const activeUid = state.globalEnvironments?.activeGlobalEnvironmentUid;
const activeEnv = globalEnvironments?.find((env) => env?.uid == activeUid);
if (activeEnv) {
environment = activeEnv;
environmentUid = activeEnv.uid;
}
}
if (!environment) {
return reject(new Error('Environment not found'));
}
const environmentToSave = { ...environment, variables };
const { ipcRenderer } = window;
environmentSchema
.validate(environmentToSave)
.then(() => ipcRenderer.invoke('renderer:save-global-environment', {
environmentUid,
variables,
color: environment.color,
workspaceUid,
workspacePath
}))
.then(() => dispatch(_saveGlobalEnvironment({ environmentUid, variables })))
.then(resolve)
.catch(reject);
});
};
export const selectGlobalEnvironment = ({ environmentUid }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
ipcRenderer
.invoke('renderer:select-global-environment', { environmentUid, workspaceUid, workspacePath })
.then(() => dispatch(_selectGlobalEnvironment({ environmentUid })))
.then(resolve)
.catch(reject);
});
};
export const deleteGlobalEnvironment = ({ environmentUid }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
ipcRenderer
.invoke('renderer:delete-global-environment', { environmentUid, workspaceUid, workspacePath })
.then(() => dispatch(_deleteGlobalEnvironment({ environmentUid })))
.then(resolve)
.catch(reject);
});
};
export const globalEnvironmentsUpdateEvent = ({ globalEnvironmentVariables }) => (dispatch, getState) => {
if (!globalEnvironmentVariables) return;
const state = getState();
const globalEnvironments = state?.globalEnvironments?.globalEnvironments || [];
const environmentUid = state?.globalEnvironments?.activeGlobalEnvironmentUid;
const environment = globalEnvironments?.find((env) => env?.uid == environmentUid);
if (!environment || !environmentUid) return;
const draft = state?.globalEnvironments?.globalEnvironmentDraft;
if (draft && draft.environmentUid === environmentUid && draft.variables) {
const baseline = {};
environment.variables?.forEach((v) => {
if (v.enabled) baseline[v.name] = v.value;
});
dispatch(_setScriptGlobalEnvBaseline(baseline));
dispatch(_saveGlobalEnvironment({ environmentUid, variables: draft.variables }));
dispatch(clearGlobalEnvironmentDraft());
}
const updatedState = getState();
const updatedEnv = updatedState?.globalEnvironments?.globalEnvironments?.find((env) => env?.uid == environmentUid);
const baseline = updatedState?.globalEnvironments?._scriptGlobalEnvBaseline;
let variables = cloneDeep(updatedEnv?.variables || []);
variables = applyScriptEnvVars(variables, globalEnvironmentVariables, baseline, { skipKeys: ['__name__'] });
// Re-infer dataType only for vars the script actually modified — preserves draft-only type edits
// when a script does a structurally-equal no-op write.
const modifiedKeys = getScriptModifiedKeys(globalEnvironmentVariables, baseline, { skipKeys: ['__name__'] });
variables.forEach((v) => {
if (!modifiedKeys.has(v.name)) return;
const inferred = getDataTypeFromValue(globalEnvironmentVariables[v.name]);
if (inferred === 'string') {
delete v.dataType;
} else {
v.dataType = inferred;
}
});
dispatch(_saveGlobalEnvironment({ environmentUid, variables }));
const { ipcRenderer } = window;
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
environmentSchema
.validate({ ...environment, variables })
.then(() => ipcRenderer.invoke('renderer:save-global-environment', {
environmentUid,
variables,
color: environment.color,
workspaceUid,
workspacePath
}))
.catch((err) => console.error('Failed to persist global environment:', err));
};
export const updateGlobalEnvironmentColor = (environmentUid, color) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
ipcRenderer.invoke('renderer:update-global-environment-color', { environmentUid, color, workspaceUid, workspacePath })
.then(() => dispatch(_updateGlobalEnvironmentColor({ environmentUid, color })))
.then(resolve)
.catch(reject);
});
};
export default globalEnvironmentsSlice.reducer;