Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bucketco/cli",
"version": "1.0.0",
"version": "1.0.1",
"packageManager": "yarn@4.1.1",
"description": "CLI for Bucket service",
"main": "./dist/index.js",
Expand Down
34 changes: 27 additions & 7 deletions packages/cli/stores/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ const defaultConfig: Config = {
typesOutput: [{ path: DEFAULT_TYPES_OUTPUT, format: "react" }],
};

const moduleRoot = fileURLToPath(import.meta.url).substring(
0,
fileURLToPath(import.meta.url).lastIndexOf("cli") + 3,
);

// Helper to normalize typesOutput to array format
export function normalizeTypesOutput(
output?: string | TypesOutput[],
Expand All @@ -59,6 +64,7 @@ class ConfigStore {
protected config: Config = { ...defaultConfig };
protected configPath: string | undefined;
protected projectPath: string | undefined;
protected clientVersion: string | undefined;
protected validateConfig: ValidateFunction | undefined;

async initialize() {
Expand All @@ -69,11 +75,7 @@ class ConfigStore {
protected async createValidator() {
try {
// Using current config store file, resolve the schema.json path
const filePath = fileURLToPath(import.meta.url);
const schemaPath = join(
filePath.substring(0, filePath.lastIndexOf("cli") + 3),
"schema.json",
);
const schemaPath = join(moduleRoot, "schema.json");
const content = await readFile(schemaPath, "utf-8");
const parsed = parseJSON(content) as unknown as Config;
const ajv = new Ajv();
Expand All @@ -89,11 +91,25 @@ class ConfigStore {
return;
}

// Load the client version from the module's package.json metadata
try {
const packageJSONPath = await findUp("package.json");
const moduleMetadata = await readFile(
join(moduleRoot, "package.json"),
"utf-8",
);
const moduleMetadataParsed = parseJSON(moduleMetadata) as unknown as {
version: string;
};
this.clientVersion = moduleMetadataParsed.version;
Comment on lines +96 to +103
Copy link
Contributor

Choose a reason for hiding this comment

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

this is probably fine, but as a point of reference this is what the SDKs do: https://github.com/bucketco/bucket-javascript-sdk/blob/main/packages/browser-sdk/src/config.ts#L1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't unfortunately do that without getting the "importing json is experimental warning" because the package uses type "module"

Copy link
Contributor

Choose a reason for hiding this comment

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

gotcha 👍

} catch {
// Should not be the case, but ignore if no package.json is found
}

try {
const projectMetadataPath = await findUp("package.json");
this.configPath = await findUp(CONFIG_FILE_NAME);
this.projectPath = dirname(
this.configPath ?? packageJSONPath ?? process.cwd(),
this.configPath ?? projectMetadataPath ?? process.cwd(),
);

if (!this.configPath) return;
Expand Down Expand Up @@ -167,6 +183,10 @@ class ConfigStore {
return this.configPath;
}

getClientVersion() {
return this.clientVersion;
}

getProjectPath() {
return this.projectPath ?? process.cwd();
}
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import open from "open";
import { authStore } from "../stores/auth.js";
import { configStore } from "../stores/config.js";

import {
CLIENT_VERSION_HEADER_NAME,
CLIENT_VERSION_HEADER_VALUE,
} from "./constants.js";
import { ParamType } from "./types.js";
import { errorUrl, loginUrl, successUrl } from "./urls.js";

Expand Down Expand Up @@ -153,6 +157,9 @@ export async function authRequest<T = Record<string, unknown>>(
headers: {
...options?.headers,
Authorization: `Bearer ${token}`,
[CLIENT_VERSION_HEADER_NAME]: CLIENT_VERSION_HEADER_VALUE(
configStore.getClientVersion() ?? "unknown",
),
},
});

Expand Down
4 changes: 4 additions & 0 deletions packages/cli/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os from "node:os";
import { join } from "node:path";

export const CLIENT_VERSION_HEADER_NAME = "bucket-sdk-version";
export const CLIENT_VERSION_HEADER_VALUE = (version: string) =>
`cli/${version}`;

export const CONFIG_FILE_NAME = "bucket.config.json";
export const AUTH_FILE = join(os.homedir(), ".bucket-auth");
export const SCHEMA_URL = `https://unpkg.com/@bucketco/cli@latest/schema.json`;
Expand Down