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

fix: [ENG-1319] allow user to set explicit end time, round up end and duration to the nearest hour #70

Merged
merged 13 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 8 additions & 0 deletions .cursor/rules/cli.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
description: Describes the CLI project
globs:
---
- This is a CLI program written in Deno using the `commander` library and `@commander-js/extra-typings`.

- When using node globals like `console.log`, `setTimeout`, `process`, etc, import them from the corresponding explicit `node:console`, `node:timers`, and `node:process`, etc.
- Where possible, use the `openapi-typescript`/`openapi-fetch` client instead of writing raw fetch calls.
3 changes: 3 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
},
"fmt": {
"exclude": ["src/schema.ts"]
},
"compilerOptions": {
"lib": ["deno.ns"]
}
}
10 changes: 6 additions & 4 deletions src/checkVersion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boxen from "boxen";
import chalk from "chalk";
import { execSync } from "node:child_process";
import * as console from "node:console";
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { join } from "node:path";
Expand Down Expand Up @@ -69,12 +70,13 @@ 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}`);
}
const data = await response.json();
// deno-lint-ignore no-explicit-any -- Deno has narrower types for fetch responses, but we know this code works atm.
const data = await response.json() as any;
await writeCache(data.version);
return data.version;
} catch (error) {
Expand Down Expand Up @@ -103,7 +105,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 +133,7 @@ Run 'sf upgrade' to update to the latest version
padding: 1,
borderColor: "yellow",
borderStyle: "round",
})
}),
);
}
}
1 change: 1 addition & 0 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { unlinkSync } from "node:fs";
import * as console from "node:console";
import { homedir } from "node:os";
import { join } from "node:path";
import process from "node:process";
Expand Down
1 change: 1 addition & 0 deletions src/helpers/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import process from "node:process";
import * as console from "node:console";
import { getCommandBase } from "./command.ts";
import { clearAuthFromConfig } from "./config.ts";

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +65,7 @@ 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}`;
Expand Down
19 changes: 9 additions & 10 deletions src/helpers/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,16 @@ export function centsToDollars(cents: Cents): number {
export function dollarsToCents(dollars: number): Cents {
return Math.ceil(dollars * 100);
}

export function parseStartDate(startDate: string): Date | "NOW" | null {
export function parseStartDateOrNow(startDate?: string): Date | "NOW" {
if (!startDate) return "NOW";
const nowRe = /\b(?:"|')?[nN][oO][wW](?:"|')?\b/;
if (nowRe.test(startDate)) {
return "NOW";
}

if (nowRe.test(startDate)) return "NOW";
const chronoDate = chrono.parseDate(startDate);
if (!chronoDate) {
return null;
}
return chronoDate ?? "NOW";
}

return chronoDate;
export function parseStartDate(startDate?: string | Date): Date {
if (startDate instanceof Date) return startDate;
const result = parseStartDateOrNow(startDate);
return result === "NOW" ? new Date() : result;
Comment on lines +112 to +115
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world this would be called like coerceStartDateToDate but this is a pedantic nit

}
1 change: 0 additions & 1 deletion src/helpers/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const apiPaths: Record<string, Path<IdParams | never>> = {
me: "/v0/me",
ping: "/v0/ping",

orders_create: "/v0/orders",
orders_list: "/v0/orders",
orders_get: ({ id }: IdParams): string => `/v0/orders/${id}`,
orders_cancel: ({ id }: IdParams): string => `/v0/orders/${id}`,
Expand Down
1 change: 1 addition & 0 deletions src/helpers/waitingForOrder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ora from "ora";
import { setTimeout } from "node:timers";
import { logAndQuit } from "./errors.ts";
import { getOrder } from "./fetchers.ts";

Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bun

import { Command } from "@commander-js/extra-typings";
import * as console from "node:console";
import os from "node:os";
import process from "node:process";
import pkg from "../package.json" with { type: "json" };
Expand Down Expand Up @@ -64,7 +65,8 @@ const main = async () => {
},
});

const data = await response.json();
// deno-lint-ignore no-explicit-any -- Deno has narrower types for fetch responses, but we know this code works atm.
const data = await response.json() as any;
if (data.id) {
exchangeAccountId = data.id;
saveConfig({ ...config, account_id: data.id });
Expand Down
1 change: 1 addition & 0 deletions src/lib/balance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chalk from "chalk";
import Table from "cli-table3";
import * as console from "node:console";
import type { Command } from "@commander-js/extra-typings";
import { apiClient } from "../apiClient.ts";
import { isLoggedIn } from "../helpers/config.ts";
Expand Down
Loading
Loading