Skip to content

Commit d1dd633

Browse files
committed
Refactor exported API types
1 parent e3adc4a commit d1dd633

File tree

6 files changed

+47
-88
lines changed

6 files changed

+47
-88
lines changed

src/config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import * as fs from 'fs/promises';
22
import * as os from 'os';
33
import * as path from 'path';
44
import packageJson from '../package.json' with { type: 'json' };
5+
import type { Config } from './core-types.js';
56

67
const defaultConfigFile = os.homedir() + '/.config/' + packageJson.name.split('/')[1] + '/config.json';
78

8-
interface Config {
9-
browsers: any[];
10-
}
11-
129
interface ReadResult {
1310
data: Config | null;
1411
configDir: string;

src/core-types.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export interface Browser {
2+
name: string;
3+
version: string;
4+
type: string;
5+
command: string;
6+
profile: string | boolean;
7+
processName?: string;
8+
tempDir?: string;
9+
neverStartFresh?: boolean;
10+
headless?: boolean;
11+
regex?: RegExp;
12+
}
13+
14+
export type BrowserInfo = Pick<Browser, 'name' | 'version' | 'type' | 'command'>;
15+
16+
export interface Config {
17+
browsers: Browser[];
18+
}
19+
20+
export interface LaunchOptions {
21+
browser: string;
22+
version?: string;
23+
proxy?: string;
24+
options?: string[];
25+
skipDefaults?: boolean;
26+
detached?: boolean;
27+
noProxy?: string | string[];
28+
headless?: boolean;
29+
prefs?: { [key: string]: any };
30+
profile?: string | null;
31+
tempDir?: string;
32+
}

src/create_profiles.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
import * as fs from 'fs/promises';
22
import * as path from 'path';
3+
import type { Browser } from './core-types.js';
34

4-
interface Browser {
5-
type: string;
6-
name: string;
7-
version: string;
8-
profile: string | boolean;
9-
}
10-
11-
/**
12-
* Create profiles for the given browsers
13-
* @param browsers Array of browsers
14-
* @param configDir Path to a directory, where the profiles should be put
15-
*/
165
async function createProfiles(browsers: Browser[], configDir: string): Promise<void> {
176
function dirName(name: string, version: string): string {
187
const dir = name + '-' + version;

src/detect.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@ import { darwinBrowsers } from './darwin/index.js';
33
import assign from 'lodash/assign.js';
44
import { Browsers } from './browsers.js';
55
import winDetectBrowsers from 'win-detect-browsers';
6+
import type { Browser } from './core-types.js';
67

78
const browsers = new Browsers();
89

9-
interface DetectedBrowser {
10-
type: string;
11-
name: string;
12-
command: string;
13-
version: string;
14-
profile?: string | boolean;
15-
[key: string]: any;
16-
}
17-
1810
/**
1911
* Detect all available browsers on Windows systems.
2012
* Returns an array of detected browsers.
2113
*/
22-
async function detectWindows(): Promise<DetectedBrowser[]> {
14+
async function detectWindows(): Promise<Browser[]> {
2315
const results = await winDetectBrowsers();
2416

2517
return results.map((browser) => {
@@ -131,7 +123,7 @@ async function checkUnix(commands: string[], regex: RegExp): Promise<{ version:
131123
* Detect all available web browsers.
132124
* Returns an array of available browsers.
133125
*/
134-
async function detect(): Promise<DetectedBrowser[]> {
126+
async function detect(): Promise<Browser[]> {
135127
if (process.platform === 'win32') {
136128
try {
137129
return await detectWindows();
@@ -177,12 +169,12 @@ async function detect(): Promise<DetectedBrowser[]> {
177169
name: browserPlatform.darwin || browserPlatform.type,
178170
command: command,
179171
version: version
180-
}) as DetectedBrowser;
172+
}) as Browser;
181173
})
182174
);
183175

184176
return results
185-
.filter((result): result is PromiseFulfilledResult<DetectedBrowser> => result.status === 'fulfilled')
177+
.filter((result): result is PromiseFulfilledResult<Browser> => result.status === 'fulfilled')
186178
.map(result => result.value);
187179
}
188180

src/index.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,11 @@ import { detect } from './detect.js';
55
import { runBrowser } from './run.js';
66
import { createProfiles } from './create_profiles.js';
77
import { BrowserInstance } from './instance.js';
8-
9-
interface Config {
10-
browsers: any[];
11-
}
12-
13-
interface LaunchOptions {
14-
browser: string;
15-
version?: string;
16-
proxy?: string;
17-
options?: string[];
18-
skipDefaults?: boolean;
19-
detached?: boolean;
20-
noProxy?: string | string[];
21-
headless?: boolean;
22-
prefs?: { [key: string]: any };
23-
profile?: string | null;
24-
}
25-
26-
interface Browser {
27-
name: string;
28-
version: string;
29-
type: string;
30-
command: string;
31-
}
8+
import type { Config, LaunchOptions, Browser, BrowserInfo } from './core-types.js';
329

3310
interface LaunchFunction {
3411
(uri: string, options: string | LaunchOptions): Promise<BrowserInstance>;
35-
browsers: any[];
12+
browsers: Browser[];
3613
}
3714

3815
/**
@@ -85,19 +62,19 @@ async function getLauncher(configFile?: string): Promise<LaunchFunction> {
8562
/**
8663
* Detect available browsers
8764
*/
88-
async function detectBrowsers(): Promise<Browser[]> {
65+
async function detectBrowsers(): Promise<BrowserInfo[]> {
8966
const browsers = await detect();
9067
return browsers.map((browser) => {
9168
return pick(browser, ['name', 'version', 'type', 'command']);
92-
}) as Browser[];
69+
}) as BrowserInfo[];
9370
}
9471

9572
/**
9673
* Detect the available browsers and build appropriate profiles if necessary
9774
*/
9875
async function buildConfig(configDir: string): Promise<Config> {
9976
const browsers = await detect();
100-
await createProfiles(browsers as any, configDir);
77+
await createProfiles(browsers, configDir);
10178
return { browsers };
10279
}
10380

@@ -127,5 +104,6 @@ async function updateBrowsers(configFile?: string): Promise<Config> {
127104
}
128105

129106
export { getLauncher, detectBrowsers, updateBrowsers };
130-
export type { Config, LaunchOptions, Browser, LaunchFunction };
107+
export type { Config, LaunchOptions, Browser, BrowserInfo } from './core-types.js';
108+
export type { LaunchFunction };
131109
export { BrowserInstance } from './instance.js';

src/run.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,7 @@ import * as path from 'path';
55
import assign from 'lodash/assign.js';
66
import { BrowserInstance } from './instance.js';
77
import headless from 'headless';
8-
9-
interface Browser {
10-
type: string;
11-
name: string;
12-
version: string;
13-
command: string;
14-
profile: string | boolean;
15-
processName?: string;
16-
tempDir?: string;
17-
neverStartFresh?: boolean;
18-
[key: string]: any;
19-
}
20-
21-
interface Config {
22-
browsers: Browser[];
23-
}
24-
25-
interface LaunchOptions {
26-
browser?: string;
27-
version?: string;
28-
proxy?: string;
29-
options?: string[];
30-
skipDefaults?: boolean;
31-
detached?: boolean;
32-
noProxy?: string | string[];
33-
headless?: boolean;
34-
prefs?: { [key: string]: any };
35-
profile?: string | null;
36-
tempDir?: string;
37-
}
8+
import type { Browser, Config, LaunchOptions } from './core-types.js';
389

3910
type SetupResult = { args: string[]; defaultArgs: string[] };
4011
type BrowserRunner = (uri: string, options: LaunchOptions) => Promise<BrowserInstance>;

0 commit comments

Comments
 (0)