-
Notifications
You must be signed in to change notification settings - Fork 722
/
Copy pathsettings-execution.tsx
372 lines (338 loc) · 11 KB
/
settings-execution.tsx
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import * as React from 'react';
import {
Button,
ButtonGroup,
Callout,
Checkbox,
FormGroup,
InputGroup,
Radio,
RadioGroup,
} from '@blueprintjs/core';
import { observer } from 'mobx-react';
import { GlobalSetting, IPackageManager } from '../../interfaces';
import { AppState } from '../state';
export enum SettingItemType {
EnvVars = GlobalSetting.environmentVariables,
Flags = GlobalSetting.executionFlags,
}
interface ExecutionSettingsProps {
appState: AppState;
}
interface ExecutionSettingsState {
[setting: string]: Record<string, string>;
}
/**
* Settings content to manage execution-related preferences.
*
* @class ExecutionSettings
* @extends {React.Component<ExecutionSettingsProps, ExecutionSettingsState>}
*/
export const ExecutionSettings = observer(
class ExecutionSettings extends React.Component<
ExecutionSettingsProps,
ExecutionSettingsState
> {
constructor(props: ExecutionSettingsProps) {
super(props);
this.state = {
executionFlags: Object.assign(
{},
...props.appState.executionFlags.map((flag, idx) => {
return { [idx]: flag };
}),
),
environmentVariables: Object.assign(
{},
...props.appState.environmentVariables.map((envVar, idx) => {
return { [idx]: envVar };
}),
),
};
this.handleDeleteDataChange = this.handleDeleteDataChange.bind(this);
this.handleElectronLoggingChange = this.handleElectronLoggingChange.bind(
this,
);
this.handleSettingsItemChange = this.handleSettingsItemChange.bind(this);
this.addNewSettingsItem = this.addNewSettingsItem.bind(this);
}
public componentDidMount() {
const { environmentVariables, executionFlags } = this.state;
if (Object.keys(executionFlags).length === 0) {
this.addNewSettingsItem(SettingItemType.Flags);
}
if (Object.keys(environmentVariables).length === 0) {
this.addNewSettingsItem(SettingItemType.EnvVars);
}
}
public componentDidUpdate() {
const { appState } = this.props;
for (const type of Object.values(SettingItemType)) {
const values = Object.values(this.state[type]);
appState[type] = values.filter((v) => v !== '');
}
}
/**
* Handles a change on whether or not the user data dir should be deleted
* after a run.
*
* @param {React.FormEvent<HTMLInputElement>} event
*/
public handleDeleteDataChange(event: React.FormEvent<HTMLInputElement>) {
const { checked } = event.currentTarget;
this.props.appState.isKeepingUserDataDirs = checked;
}
/**
* Handles a change on whether or not electron should log more things
*
* @param {React.FormEvent<HTMLInputElement>} event
*/
public handleElectronLoggingChange(
event: React.FormEvent<HTMLInputElement>,
) {
const { checked } = event.currentTarget;
this.props.appState.isEnablingElectronLogging = checked;
}
/**
* Handles a change in the execution flags or environment variables
* run with the Electron executable.
*
* @param {React.ChangeEvent<HTMLInputElement>} event
* @param {SettingItemType} type
*/
public handleSettingsItemChange(
event: React.ChangeEvent<HTMLInputElement>,
type: SettingItemType,
) {
const { name, value } = event.currentTarget;
this.setState((prevState) => ({
[type]: {
...prevState[type],
[name]: value,
},
}));
}
/**
* Adds a new settings item input field.
*
* @param {SettingItemType} type
*/
private addNewSettingsItem(type: SettingItemType) {
const array = Object.entries(this.state[type]);
this.setState((prevState) => ({
[type as any]: {
...prevState[type],
[array.length]: '',
},
}));
}
/**
* Handle a change to the package manager used to install modules when running
* Fiddles;
*
* @param {React.FormEvent<HTMLInputElement>} event
*/
private handlePMChange = (event: React.FormEvent<HTMLInputElement>) => {
const { appState } = this.props;
const { value } = event.currentTarget;
appState.packageManager = value as IPackageManager;
};
public renderDeleteItem(idx: string, type: SettingItemType): JSX.Element {
const updated = this.state[type];
const removeFn = () => {
if (Object.keys(updated).length === 1) {
updated[idx] = '';
} else {
delete updated[idx];
}
this.setState({ [type]: updated });
};
return (
<Button
icon="cross"
disabled={Object.keys(updated).length === 1}
onClick={removeFn}
/>
);
}
private renderEnvironmentVariables() {
const { environmentVariables } = this.state;
const varsArray = Object.entries(environmentVariables);
const type = SettingItemType.EnvVars;
return (
<FormGroup>
<p>
Electron allows starting the executable with{' '}
<a href="https://www.electronjs.org/docs/api/environment-variables">
user-provided environment variables
</a>
, such as{' '}
<code>
NODE_OPTIONS="--no-warnings --max-old-space-size=2048"
</code>
. Those can be added here to run when you start your Fiddles.
</p>
<br />
{varsArray.map(([idx, envVar]) => {
return (
<InputGroup
aria-label={'Set user-provided environment variables'}
placeholder='NODE_OPTIONS="--no-warnings --max-old-space-size=2048"'
value={envVar}
name={idx}
key={idx}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
this.handleSettingsItemChange(e, type)
}
rightElement={this.renderDeleteItem(idx, type)}
/>
);
})}
</FormGroup>
);
}
private renderExecutionFlags() {
const { executionFlags } = this.state;
const flagsArray = Object.entries(executionFlags);
const type = SettingItemType.Flags;
return (
<FormGroup>
<p>
Electron allows starting the executable with{' '}
<a href="https://www.electronjs.org/docs/api/command-line-switches">
user-provided flags
</a>
, such as <code>--js-flags=--expose-gc</code>. Those can be added to
run when you start your Fiddles.
</p>
<br />
{flagsArray.map(([idx, flag]) => {
return (
<InputGroup
aria-label={'Set user-provided flags'}
placeholder="--js-flags=--expose-gc"
value={flag}
name={idx}
key={idx}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
this.handleSettingsItemChange(e, type)
}
rightElement={this.renderDeleteItem(idx, type)}
/>
);
})}
</FormGroup>
);
}
public render() {
const {
isKeepingUserDataDirs,
isEnablingElectronLogging,
} = this.props.appState;
return (
<div>
<h1>Execution</h1>
<Callout>
These advanced settings control how Electron Fiddle executes your
fiddles.
</Callout>
<br />
<Callout>
<FormGroup>
<p>
Whenever Electron runs, it creates a user data directory for
cookies, the cache, and various other things that it needs to
keep around. Since fiddles are usually just run once, we delete
this directory after your fiddle exits. Enable this setting to
keep the user data directories around.
</p>
<Checkbox
checked={isKeepingUserDataDirs}
label="Do not delete user data directories."
onChange={this.handleDeleteDataChange}
/>
</FormGroup>
</Callout>
<br />
<Callout>
<FormGroup>
<p>
There are some flags that Electron uses to log extra information
both internally and through Chromium. Enable this option to make
Fiddle produce those logs. Enabling advanced Electron logging
will set the <code>ELECTRON_ENABLE_LOGGING</code>,{' '}
<code>ELECTRON_DEBUG_NOTIFICATION</code>, and{' '}
<code>ELECTRON_ENABLE_STACK_DUMPING</code> environment variables
to true. See{' '}
<a href="https://www.electronjs.org/docs/api/environment-variables">
documentation
</a>{' '}
for more information about what they do.
</p>
<Checkbox
checked={isEnablingElectronLogging}
label="Enable advanced Electron logging."
onChange={this.handleElectronLoggingChange}
/>
</FormGroup>
</Callout>
<br />
<Callout>
{this.renderExecutionFlags()}
<ButtonGroup>
<Button
onClick={() => this.addNewSettingsItem(SettingItemType.Flags)}
>
Add New Flag
</Button>
</ButtonGroup>
</Callout>
<br />
<Callout>
{this.renderEnvironmentVariables()}
<ButtonGroup>
<Button
onClick={() => this.addNewSettingsItem(SettingItemType.EnvVars)}
>
Add New Variable
</Button>
</ButtonGroup>
</Callout>
<br />
<Callout>
<FormGroup>
<span style={{ marginRight: 4 }}>
Electron Fiddle will install packages if you specify them. It
uses{' '}
<a
href="https://www.npmjs.com/"
target="_blank"
rel="noreferrer"
>
npm
</a>{' '}
as its package manager by default, but{' '}
<a
href="https://classic.yarnpkg.com/lang/en/"
target="_blank"
rel="noreferrer"
>
Yarn
</a>{' '}
is also available.
</span>
<RadioGroup
onChange={this.handlePMChange}
selectedValue={this.props.appState.packageManager}
inline={true}
>
<Radio label="npm" value="npm" />
<Radio label="yarn" value="yarn" />
</RadioGroup>
</FormGroup>
</Callout>
</div>
);
}
},
);