Skip to content

Commit 8e7bc78

Browse files
Feat: Rename getBrowserStackData to getDevicesAndBrowsers and update related references
1 parent 68a026b commit 8e7bc78

File tree

6 files changed

+62
-53
lines changed

6 files changed

+62
-53
lines changed

src/lib/device-cache.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,46 @@ const URLS = {
1515
/**
1616
* Fetches and caches both BrowserStack datasets (live + app_live) with a shared TTL.
1717
*/
18-
export async function getBrowserStackData(
18+
export async function getDevicesAndBrowsers(
1919
type: "live" | "app_live",
2020
): Promise<any> {
2121
if (!fs.existsSync(CACHE_DIR)) {
2222
fs.mkdirSync(CACHE_DIR, { recursive: true });
2323
}
2424

2525
let cache: any = {};
26-
let shouldRefresh = true;
2726

2827
if (fs.existsSync(CACHE_FILE)) {
2928
const stats = fs.statSync(CACHE_FILE);
3029
if (Date.now() - stats.mtimeMs < TTL_MS) {
31-
cache = JSON.parse(fs.readFileSync(CACHE_FILE, "utf8"));
32-
shouldRefresh = false;
30+
try {
31+
cache = JSON.parse(fs.readFileSync(CACHE_FILE, "utf8"));
32+
return cache[type];
33+
} catch (error) {
34+
console.error("Error parsing cache file:", error);
35+
// Continue with fetching fresh data
36+
}
3337
}
3438
}
3539

36-
if (shouldRefresh) {
37-
const [liveRes, appLiveRes] = await Promise.all([
38-
fetch(URLS.live),
39-
fetch(URLS.app_live),
40-
]);
40+
const [liveRes, appLiveRes] = await Promise.all([
41+
fetch(URLS.live),
42+
fetch(URLS.app_live),
43+
]);
4144

42-
if (!liveRes.ok || !appLiveRes.ok) {
43-
throw new Error(
44-
`Failed to fetch data: live=${liveRes.statusText}, app_live=${appLiveRes.statusText}`,
45-
);
46-
}
45+
if (!liveRes.ok || !appLiveRes.ok) {
46+
throw new Error(
47+
`Failed to fetch configuration from BrowserStack for try catch around JSON parse.: live=${liveRes.statusText}, app_live=${appLiveRes.statusText}`,
48+
);
49+
}
4750

48-
const [liveData, appLiveData] = await Promise.all([
49-
liveRes.json(),
50-
appLiveRes.json(),
51-
]);
51+
const [liveData, appLiveData] = await Promise.all([
52+
liveRes.json(),
53+
appLiveRes.json(),
54+
]);
5255

53-
cache = { live: liveData, app_live: appLiveData };
54-
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache), "utf8");
55-
}
56+
cache = { live: liveData, app_live: appLiveData };
57+
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache), "utf8");
5658

5759
return cache[type];
5860
}

src/tools/applive-utils/start-session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import childProcess from "child_process";
22
import logger from "../../logger";
3-
import { getBrowserStackData } from "../../lib/device-cache";
3+
import { getDevicesAndBrowsers } from "../../lib/device-cache";
44
import { fuzzySearchDevices } from "./fuzzy-search";
55
import { sanitizeUrlParam } from "../../lib/utils";
66
import { uploadApp } from "./upload-app";
@@ -30,7 +30,7 @@ export async function startSession(args: StartSessionArgs): Promise<string> {
3030
const { appPath, desiredPlatform, desiredPhone } = args;
3131
let { desiredPlatformVersion } = args;
3232

33-
const data = await getBrowserStackData("app_live");
33+
const data = await getDevicesAndBrowsers("app_live");
3434

3535
const allDevices: DeviceEntry[] = data.mobile.flatMap((group: any) =>
3636
group.devices.map((dev: any) => ({ ...dev, os: group.os })),

src/tools/live-utils/desktop-filter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { getBrowserStackData } from "../../lib/device-cache";
1+
import { getDevicesAndBrowsers } from "../../lib/device-cache";
22
import { resolveVersion } from "./version-resolver";
33
import { customFuzzySearch } from "../../lib/fuzzy";
4-
import { DesktopArgs, DesktopEntry } from "./types";
4+
import { DesktopSearchArgs, DesktopEntry } from "./types";
55

6-
export async function filterDesktop(args: DesktopArgs): Promise<DesktopEntry> {
7-
const data = await getBrowserStackData("live");
6+
export async function filterDesktop(
7+
args: DesktopSearchArgs,
8+
): Promise<DesktopEntry> {
9+
const data = await getDevicesAndBrowsers("live");
810
const allEntries = getAllDesktopEntries(data);
911

1012
// Filter OS
@@ -114,7 +116,7 @@ function filterByOSVersion(
114116

115117
function addNotes(
116118
entry: DesktopEntry,
117-
args: DesktopArgs,
119+
args: DesktopSearchArgs,
118120
resolvedOS: string,
119121
resolvedBrowser: string,
120122
): void {

src/tools/live-utils/mobile-filter.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { getBrowserStackData } from "../../lib/device-cache";
1+
import { getDevicesAndBrowsers } from "../../lib/device-cache";
22
import { resolveVersion } from "./version-resolver";
33
import { customFuzzySearch } from "../../lib/fuzzy";
4-
import { MobileArgs, MobileEntry } from "./types";
4+
import { MobileSearchArgs, MobileEntry } from "./types";
55

66
// Extract all mobile entries from the data
77
function getAllMobileEntries(data: any): MobileEntry[] {
@@ -79,8 +79,10 @@ function createVersionNote(
7979
return "";
8080
}
8181

82-
export async function filterMobile(args: MobileArgs): Promise<MobileEntry> {
83-
const data = await getBrowserStackData("live");
82+
export async function filterMobile(
83+
args: MobileSearchArgs,
84+
): Promise<MobileEntry> {
85+
const data = await getDevicesAndBrowsers("live");
8486
const allEntries = getAllMobileEntries(data);
8587

8688
const osCandidates = filterByOS(allEntries, args.os);

src/tools/live-utils/start-session.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { sanitizeUrlParam } from "../../lib/utils";
21
import logger from "../../logger";
32
import childProcess from "child_process";
43
import { filterDesktop } from "./desktop-filter";
54
import { filterMobile } from "./mobile-filter";
65
import {
7-
DesktopArgs,
8-
MobileArgs,
6+
DesktopSearchArgs,
7+
MobileSearchArgs,
98
DesktopEntry,
109
MobileEntry,
1110
PlatformType,
@@ -33,35 +32,39 @@ async function prepareLocalTunnel(url: string): Promise<boolean> {
3332
* Entrypoint: detects platformType & delegates.
3433
*/
3534
export async function startBrowserSession(
36-
args: DesktopArgs | MobileArgs,
35+
args: DesktopSearchArgs | MobileSearchArgs,
3736
): Promise<string> {
3837
const entry =
3938
args.platformType === PlatformType.DESKTOP
40-
? await filterDesktop(args as DesktopArgs)
41-
: await filterMobile(args as MobileArgs);
39+
? await filterDesktop(args as DesktopSearchArgs)
40+
: await filterMobile(args as MobileSearchArgs);
4241

4342
const isLocal = await prepareLocalTunnel(args.url);
4443

4544
const url =
4645
args.platformType === PlatformType.DESKTOP
47-
? buildDesktopUrl(args as DesktopArgs, entry as DesktopEntry, isLocal)
48-
: buildMobileUrl(args as MobileArgs, entry as MobileEntry, isLocal);
46+
? buildDesktopUrl(
47+
args as DesktopSearchArgs,
48+
entry as DesktopEntry,
49+
isLocal,
50+
)
51+
: buildMobileUrl(args as MobileSearchArgs, entry as MobileEntry, isLocal);
4952

5053
openBrowser(url);
5154
return entry.notes ? `${url}, ${entry.notes}` : url;
5255
}
5356

5457
function buildDesktopUrl(
55-
args: DesktopArgs,
58+
args: DesktopSearchArgs,
5659
e: DesktopEntry,
5760
isLocal: boolean,
5861
): string {
5962
const params = new URLSearchParams({
60-
os: sanitizeUrlParam(e.os),
61-
os_version: sanitizeUrlParam(e.os_version),
62-
browser: sanitizeUrlParam(e.browser),
63-
browser_version: sanitizeUrlParam(e.browser_version),
64-
url: sanitizeUrlParam(args.url),
63+
os: e.os,
64+
os_version: e.os_version,
65+
browser: e.browser,
66+
browser_version: e.browser_version,
67+
url: args.url,
6568
scale_to_fit: "true",
6669
resolution: "responsive-mode",
6770
speed: "1",
@@ -72,7 +75,7 @@ function buildDesktopUrl(
7275
}
7376

7477
function buildMobileUrl(
75-
args: MobileArgs,
78+
args: MobileSearchArgs,
7679
d: MobileEntry,
7780
isLocal: boolean,
7881
): string {
@@ -84,11 +87,11 @@ function buildMobileUrl(
8487
const os = os_map[d.os as keyof typeof os_map] || d.os;
8588

8689
const params = new URLSearchParams({
87-
os: sanitizeUrlParam(os),
88-
os_version: sanitizeUrlParam(d.os_version),
90+
os: os,
91+
os_version: d.os_version,
8992
device: d.display_name,
90-
device_browser: sanitizeUrlParam(args.browser),
91-
url: sanitizeUrlParam(args.url),
93+
device_browser: args.browser,
94+
url: args.url,
9295
scale_to_fit: "true",
9396
speed: "1",
9497
local: isLocal ? "true" : "false",

src/tools/live-utils/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface DesktopArgs {
1+
export interface DesktopSearchArgs {
22
platformType: "desktop";
33
url: string;
44
os: string;
@@ -15,7 +15,7 @@ export interface DesktopEntry {
1515
notes?: string;
1616
}
1717

18-
export interface MobileArgs {
18+
export interface MobileSearchArgs {
1919
platformType: "mobile";
2020
url: string;
2121
os: string;

0 commit comments

Comments
 (0)