Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chromeProfile utility on finiky api #304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions config-api/src/chrome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fs from "fs"
import path from "path"

export function chromeFolder() {
const home = process.env.HOME;

if (!home) throw new Error("HOME env variable not set");

if (process.platform === "darwin")
return path.join(
home,
"Library",
"Application Support",
"Google",
"Chrome"
);
if (process.platform === "linux")
return path.join(home, ".config", "google-chrome");
if (process.platform === "win32")
return path.join(home, "AppData", "Local", "Google", "Chrome");

throw new Error("Unsupported platform");
}

type ChromeState = {
profile: {
info_cache: {
[key: string]: {
user_name: string
}
}
}
}

const chromeState: ChromeState = JSON.parse(fs.readFileSync(path.join(chromeFolder(), "Local State")).toString())

/**
* Utility function to get the profile id for a given email
*/
export function chromeProfile(email: string) {
const profile = Object.entries(chromeState.profile.info_cache).find(
([, profile]) => profile.user_name === email
);

if (!profile) throw new Error(`No profile found for ${email}`);

return profile[0];
}
2 changes: 2 additions & 0 deletions config-api/src/createAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import urlParse from "url-parse";
import { chromeProfile } from "./chrome";
import { ConfigAPI, UrlObject, Matcher, Options } from "./types";

declare const finickyInternalAPI: any;
Expand All @@ -22,6 +23,7 @@ export function createAPI(overrides: Partial<ConfigAPI> = {}): ConfigAPI {
getUrlParts: parseUrl,
matchHostnames,
matchDomains: matchHostnames,
chromeProfile,
...overrides,
};
}
Expand Down
2 changes: 2 additions & 0 deletions config-api/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { chromeProfile } from "./chrome";
import { parseUrl, matchHostnames } from "./createAPI";

/**
Expand Down Expand Up @@ -28,6 +29,7 @@ export type ConfigAPI = {
getKeys(): KeyOptions;
matchHostnames: typeof matchHostnames;
matchDomains: typeof matchHostnames;
chromeProfile: typeof chromeProfile;
};

/**
Expand Down
21 changes: 21 additions & 0 deletions config-api/test/chrome.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs';
import path from 'path';
import { chromeFolder, chromeProfile } from "../src/chrome";

test("no profile found", () => {
expect(() => chromeProfile("")).toThrow();
});

test("profile found", () => {
const localStateExists =
fs.lstatSync(path.join(chromeFolder(), "Local State")).isFile();

if (!localStateExists) return

const chromeState = JSON.parse(fs.readFileSync(path.join(chromeFolder(), "Local State")).toString())

for (const profile of Object.entries(chromeState.profile.info_cache)) {
const email = (profile[1] as { user_name: string }).user_name
expect(chromeProfile(email)).toBe(profile[0]);
}
});