Skip to content

Commit ffaaeba

Browse files
refactor: modify InstallState to be an enum (#33)
1 parent e22ae4d commit ffaaeba

File tree

3 files changed

+64
-49
lines changed

3 files changed

+64
-49
lines changed

dist/fiddle-core.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ export declare class Installer extends EventEmitter {
129129
* See {@link Installer.state} to get this value.
130130
* See Installer.on('state-changed') to watch for state changes.
131131
*/
132-
export declare type InstallState = 'missing' | 'downloading' | 'downloaded' | 'installing' | 'installed';
132+
export declare enum InstallState {
133+
missing = 'missing',
134+
downloading = 'downloading',
135+
downloaded = 'downloaded',
136+
installing = 'installing',
137+
installed = 'installed',
138+
}
133139

134140
export declare interface InstallStateEvent {
135141
version: string;

src/installer.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ type ProgressObject = { percent: number };
1919
* See {@link Installer.state} to get this value.
2020
* See Installer.on('state-changed') to watch for state changes.
2121
*/
22-
export type InstallState =
23-
| 'missing'
24-
| 'downloading'
25-
| 'downloaded'
26-
| 'installing'
27-
| 'installed';
22+
export enum InstallState {
23+
missing = 'missing',
24+
downloading = 'downloading',
25+
downloaded = 'downloaded',
26+
installing = 'installing',
27+
installed = 'installed',
28+
}
2829

2930
export interface InstallStateEvent {
3031
version: string;
@@ -79,14 +80,14 @@ export class Installer extends EventEmitter {
7980
}
8081

8182
public state(version: string): InstallState {
82-
return this.stateMap.get(version) || 'missing';
83+
return this.stateMap.get(version) || InstallState.missing;
8384
}
8485

8586
private setState(version: string, state: InstallState) {
8687
const d = debug('fiddle-core:Installer:setState');
8788
const oldState = this.state(version);
8889

89-
if (state === 'missing') {
90+
if (state === InstallState.missing) {
9091
this.stateMap.delete(version);
9192
} else {
9293
this.stateMap.set(version, state);
@@ -108,13 +109,13 @@ export class Installer extends EventEmitter {
108109
try {
109110
const versionFile = path.join(this.paths.electronInstall, 'version');
110111
const version = fs.readFileSync(versionFile, 'utf8');
111-
this.setState(version, 'installed');
112+
this.setState(version, InstallState.installed);
112113
} catch {
113114
// no current version
114115
}
115116

116117
if (this.installing) {
117-
this.setState(this.installing, 'installing');
118+
this.setState(this.installing, InstallState.installing);
118119
}
119120

120121
// already downloaded...
@@ -123,15 +124,15 @@ export class Installer extends EventEmitter {
123124
try {
124125
for (const file of fs.readdirSync(this.paths.electronDownloads)) {
125126
const match = reg.exec(file);
126-
if (match) this.setState(match[1], 'downloaded');
127+
if (match) this.setState(match[1], InstallState.downloaded);
127128
}
128129
} catch {
129130
// no download directory yet
130131
}
131132

132133
// being downloaded now...
133134
for (const version of this.downloading.keys()) {
134-
this.setState(version, 'downloading');
135+
this.setState(version, InstallState.downloading);
135136
}
136137
}
137138

@@ -187,7 +188,7 @@ export class Installer extends EventEmitter {
187188
}
188189

189190
if (isZipDeleted && isBinaryDeleted) {
190-
this.setState(version, 'missing');
191+
this.setState(version, InstallState.missing);
191192
} else {
192193
// Ideally the execution shouldn't reach this point
193194
console.warn(`Installer: Failed to remove version ${version}`);
@@ -197,7 +198,7 @@ export class Installer extends EventEmitter {
197198
/** The current Electron installation, if any. */
198199
public get installedVersion(): string | undefined {
199200
for (const [version, state] of this.stateMap)
200-
if (state === 'installed') return version;
201+
if (state === InstallState.installed) return version;
201202
}
202203

203204
private async download(
@@ -240,13 +241,13 @@ export class Installer extends EventEmitter {
240241
const zipFile = path.join(electronDownloads, getZipName(version));
241242

242243
const state = this.state(version);
243-
if (state === 'missing') {
244+
if (state === InstallState.missing) {
244245
d(`"${zipFile}" does not exist; downloading now`);
245-
this.setState(version, 'downloading');
246+
this.setState(version, InstallState.downloading);
246247
const tempFile = await this.download(version, opts);
247248
await fs.ensureDir(electronDownloads);
248249
await fs.move(tempFile, zipFile);
249-
this.setState(version, 'downloaded');
250+
this.setState(version, InstallState.downloaded);
250251
d(`"${zipFile}" downloaded`);
251252
} else {
252253
d(`"${zipFile}" exists; no need to download`);
@@ -293,7 +294,7 @@ export class Installer extends EventEmitter {
293294
d(`already installed`);
294295
} else {
295296
const zipFile = await this.ensureDownloaded(version, opts);
296-
this.setState(version, 'installing');
297+
this.setState(version, InstallState.installing);
297298
d(`installing from "${zipFile}"`);
298299
await fs.emptyDir(electronInstall);
299300
// FIXME(anyone) is there a less awful way to wrangle asar
@@ -307,8 +308,9 @@ export class Installer extends EventEmitter {
307308
// @ts-ignore: yes, I know noAsar isn't defined in process
308309
process.noAsar = noAsar; // eslint-disable-line
309310
}
310-
if (installedVersion) this.setState(installedVersion, 'downloaded');
311-
this.setState(version, 'installed');
311+
if (installedVersion)
312+
this.setState(installedVersion, InstallState.downloaded);
313+
this.setState(version, InstallState.installed);
312314
}
313315

314316
delete this.installing;

tests/installer.test.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import * as os from 'os';
33
import * as path from 'path';
44
import nock, { Scope } from 'nock';
55

6-
import { InstallStateEvent, Installer, Paths } from '../src/index';
6+
import {
7+
InstallStateEvent,
8+
Installer,
9+
Paths,
10+
InstallState,
11+
} from '../src/index';
712

813
describe('Installer', () => {
914
let tmpdir: string;
1015
let paths: Partial<Paths>;
1116
let nockScope: Scope;
1217
let installer: Installer;
18+
const { missing, downloading, downloaded, installing, installed } =
19+
InstallState;
1320
const version12 = '12.0.15' as const;
1421
const version13 = '13.1.7' as const;
1522
const version = version13;
@@ -65,7 +72,7 @@ describe('Installer', () => {
6572
const func = () => installer.remove(version);
6673
const { events } = await listenWhile(installer, func);
6774

68-
expect(installer.state(version)).toBe('missing');
75+
expect(installer.state(version)).toBe(missing);
6976

7077
return { events };
7178
}
@@ -77,15 +84,15 @@ describe('Installer', () => {
7784
};
7885

7986
// Version is already downloaded and present in local
80-
if (installer.state(version) !== 'missing') {
87+
if (installer.state(version) !== missing) {
8188
isDownloaded = true;
8289
}
8390
const func = () => installer.install(version, { progressCallback });
8491
const { events, result } = await listenWhile(installer, func);
8592
const exec = result as string;
8693

8794
expect(isDownloaded).toBe(true);
88-
expect(installer.state(version)).toBe('installed');
95+
expect(installer.state(version)).toBe(installed);
8996
expect(installer.installedVersion).toBe(version);
9097

9198
return { events, exec };
@@ -98,7 +105,7 @@ describe('Installer', () => {
98105
};
99106

100107
// Version is already downloaded and present in local
101-
if (installer.state(version) !== 'missing') {
108+
if (installer.state(version) !== missing) {
102109
isDownloaded = true;
103110
}
104111
const func = () =>
@@ -110,7 +117,7 @@ describe('Installer', () => {
110117

111118
expect(isDownloaded).toBe(true);
112119
expect(fs.existsSync(zipfile)).toBe(true);
113-
expect(installer.state(version)).toBe('downloaded');
120+
expect(installer.state(version)).toBe(downloaded);
114121

115122
return { events, zipfile };
116123
}
@@ -134,13 +141,13 @@ describe('Installer', () => {
134141
describe('ensureDownloaded()', () => {
135142
it('downloads the version if needed', async () => {
136143
// setup: version is not installed
137-
expect(installer.state(version)).toBe('missing');
144+
expect(installer.state(version)).toBe(missing);
138145

139146
// test that the zipfile was downloaded
140147
const { events } = await doDownload(installer, version);
141148
expect(events).toStrictEqual([
142-
{ version, state: 'downloading' },
143-
{ version, state: 'downloaded' },
149+
{ version, state: downloading },
150+
{ version, state: downloaded },
144151
]);
145152
});
146153

@@ -163,12 +170,12 @@ describe('Installer', () => {
163170
await doDownload(installer, version);
164171

165172
const { events } = await doRemove(installer, version);
166-
expect(events).toStrictEqual([{ version, state: 'missing' }]);
173+
expect(events).toStrictEqual([{ version, state: missing }]);
167174
});
168175

169176
it('does nothing if the version is missing', async () => {
170177
// setup: version is not installed
171-
expect(installer.state(version)).toBe('missing');
178+
expect(installer.state(version)).toBe(missing);
172179

173180
const { events } = await doRemove(installer, version);
174181
expect(events).toStrictEqual([]);
@@ -179,35 +186,35 @@ describe('Installer', () => {
179186
await doInstall(installer, version);
180187

181188
const { events } = await doRemove(installer, version);
182-
expect(events).toStrictEqual([{ version, state: 'missing' }]);
189+
expect(events).toStrictEqual([{ version, state: missing }]);
183190
expect(installer.installedVersion).toBe(undefined);
184191
});
185192
});
186193

187194
describe('install()', () => {
188195
it('downloads a version if necessary', async () => {
189196
// setup: version is not downloaded
190-
expect(installer.state(version)).toBe('missing');
197+
expect(installer.state(version)).toBe(missing);
191198
expect(installer.installedVersion).toBe(undefined);
192199

193200
const { events } = await doInstall(installer, version);
194201
expect(events).toStrictEqual([
195-
{ version, state: 'downloading' },
196-
{ version, state: 'downloaded' },
197-
{ version, state: 'installing' },
198-
{ version, state: 'installed' },
202+
{ version, state: downloading },
203+
{ version, state: downloaded },
204+
{ version, state: installing },
205+
{ version, state: installed },
199206
]);
200207
});
201208

202209
it('unzips a version if necessary', async () => {
203210
// setup: version is downloaded but not installed
204211
await doDownload(installer, version);
205-
expect(installer.state(version)).toBe('downloaded');
212+
expect(installer.state(version)).toBe(downloaded);
206213

207214
const { events } = await doInstall(installer, version);
208215
expect(events).toStrictEqual([
209-
{ version, state: 'installing' },
210-
{ version, state: 'installed' },
216+
{ version, state: installing },
217+
{ version, state: installed },
211218
]);
212219
});
213220

@@ -224,11 +231,11 @@ describe('Installer', () => {
224231
const { events } = await doInstall(installer, version13);
225232

226233
expect(events).toStrictEqual([
227-
{ version: version13, state: 'downloading' },
228-
{ version: version13, state: 'downloaded' },
229-
{ version: version13, state: 'installing' },
230-
{ version: version12, state: 'downloaded' },
231-
{ version: version13, state: 'installed' },
234+
{ version: version13, state: downloading },
235+
{ version: version13, state: downloaded },
236+
{ version: version13, state: installing },
237+
{ version: version12, state: downloaded },
238+
{ version: version13, state: installed },
232239
]);
233240
});
234241
});
@@ -248,16 +255,16 @@ describe('Installer', () => {
248255
describe('state()', () => {
249256
it("returns 'installed' if the version is installed", async () => {
250257
await doInstall(installer, version);
251-
expect(installer.state(version)).toBe('installed');
258+
expect(installer.state(version)).toBe(installed);
252259
});
253260

254261
it("returns 'downloaded' if the version is downloaded", async () => {
255262
await doDownload(installer, version);
256-
expect(installer.state(version)).toBe('downloaded');
263+
expect(installer.state(version)).toBe(downloaded);
257264
});
258265

259266
it("returns 'missing' if the version is not downloaded", () => {
260-
expect(installer.state(version)).toBe('missing');
267+
expect(installer.state(version)).toBe(missing);
261268
});
262269
});
263270
});

0 commit comments

Comments
 (0)