Skip to content

Commit

Permalink
refactor: Improve version checking, feature flag caching, and login p…
Browse files Browse the repository at this point in the history
…rocess
  • Loading branch information
JohnPhamous committed Feb 15, 2025
1 parent 85aab26 commit 4ac99c4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/checkVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function checkProductionCLIVersion() {
// Fetch from network
try {
const response = await fetch(
"https://raw.githubusercontent.com/sfcompute/cli/refs/heads/main/package.json",
"https://raw.githubusercontent.com/sfcompute/cli/refs/heads/main/package.json"
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
Expand All @@ -78,7 +78,7 @@ async function checkProductionCLIVersion() {
await writeCache(data.version);
return data.version;
} catch (error) {
console.error("boba failed to check latest CLI version:", error);
console.error("failed to check latest CLI version:", error);
return null;
}
}
Expand All @@ -103,7 +103,7 @@ export async function checkVersion() {

if (isPatchUpdate) {
console.log(
chalk.cyan(`Automatically upgrading ${version}${latestVersion}`),
chalk.cyan(`Automatically upgrading ${version}${latestVersion}`)
);
try {
execSync("sf upgrade", { stdio: "inherit" });
Expand Down Expand Up @@ -131,7 +131,7 @@ Run 'sf upgrade' to update to the latest version
padding: 1,
borderColor: "yellow",
borderStyle: "round",
}),
})
);
}
}
21 changes: 15 additions & 6 deletions src/helpers/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface FeatureFlagCache {
[key: string]: CachedFeatureFlag;
}

const ONE_DAY_MS = 24 * 60 * 60 * 1000;
const FEATURE_FLAG_CACHE_TTL = 60 * 60 * 1000;

function getFeatureFlagCachePath(): string {
const configDir = join(homedir(), ".sfcompute");
Expand All @@ -24,8 +24,8 @@ export async function saveFeatureFlags(flags: FeatureFlagCache): Promise<void> {
try {
await Deno.mkdir(configDir, { recursive: true });
await Deno.writeTextFile(cachePath, JSON.stringify(flags, null, 2));
} catch (error) {
console.error("boba error saving feature flags:", error);
} catch {
// Silent error
}
}

Expand All @@ -42,7 +42,7 @@ export async function loadFeatureFlags(): Promise<FeatureFlagCache> {

export async function getCachedFeatureFlag(
feature: string,
accountId: string,
accountId: string
): Promise<CachedFeatureFlag | null> {
const cache = await loadFeatureFlags();
const key = `${accountId}:${feature}`;
Expand All @@ -65,15 +65,24 @@ export async function getCachedFeatureFlag(
export async function cacheFeatureFlag(
feature: string,
accountId: string,
value: boolean,
value: boolean
): Promise<void> {
const cache = await loadFeatureFlags();
const key = `${accountId}:${feature}`;

cache[key] = {
value,
expiresAt: Date.now() + ONE_DAY_MS,
expiresAt: Date.now() + FEATURE_FLAG_CACHE_TTL,
};

await saveFeatureFlags(cache);
}

export async function clearFeatureFlags(): Promise<void> {
const cachePath = getFeatureFlagCachePath();
try {
await Deno.remove(cachePath);
} catch {
// Silent error if file doesn't exist
}
}
19 changes: 16 additions & 3 deletions src/lib/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { getWebAppUrl } from "../helpers/urls.ts";
// where the fetch API in Bun isn't passing the body
// through redirects correctly
import axios from "axios";
import { clearFeatureFlags } from "../helpers/feature-flags.ts";
import { getLoggedInAccountId } from "./me.ts";

export function registerLogin(program: Command) {
program
Expand All @@ -30,13 +32,24 @@ export function registerLogin(program: Command) {
clearScreen();
console.log(`\n\n Click here to login:\n ${url}\n\n`);
console.log(
` Do these numbers match your browser window?\n ${validation}\n\n`,
` Do these numbers match your browser window?\n ${validation}\n\n`
);

const checkSession = async () => {
const session = await getSession({ token: result.token });
if (session?.token) {
await saveConfig({ auth_token: session.token });
let accountId: undefined | string;

try {
accountId = await getLoggedInAccountId();
} catch {
// No-op
}
await saveConfig({
auth_token: session.token,
account_id: accountId,
});
await clearFeatureFlags();
spinner.succeed("Logged in successfully");
process.exit(0);
} else {
Expand All @@ -60,7 +73,7 @@ async function createSession({ validation }: { validation: string }) {
"Content-Type": "application/json",
},
maxRedirects: 5,
},
}
);

return response.data as {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function registerMe(program: Command) {
});
}

async function getLoggedInAccountId() {
export async function getLoggedInAccountId() {
const loggedIn = await isLoggedIn();
if (!loggedIn) {
logLoginMessageAndQuit();
Expand Down

0 comments on commit 4ac99c4

Please sign in to comment.