stack/packages/stack-cli/src/lib/app.ts
BilalG1 57149bd84b
Stack CLI (#1227)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Release Notes

* **New Features**
  * Added Stack CLI with authentication (login/logout) commands.
  * Added project management commands to list and create projects.
  * Added configuration management to pull and push project settings.
  * Added code execution capability to run JavaScript expressions.
  * Added initialization command for Stack Auth setup.

* **Tests**
  * Added comprehensive end-to-end test suite for CLI functionality.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-03-09 13:24:15 -07:00

35 lines
1.2 KiB
TypeScript

import { StackClientApp } from "@stackframe/js";
import type { CurrentInternalUser, AdminOwnedProject } from "@stackframe/js";
import { AuthError } from "./errors.js";
import { DEFAULT_PUBLISHABLE_CLIENT_KEY } from "./auth.js";
import type { SessionAuth, ProjectAuth } from "./auth.js";
export function getInternalApp(auth: SessionAuth): StackClientApp<true, "internal"> {
return new StackClientApp({
projectId: "internal",
publishableClientKey: DEFAULT_PUBLISHABLE_CLIENT_KEY,
baseUrl: auth.apiUrl,
tokenStore: {
accessToken: "",
refreshToken: auth.refreshToken,
},
noAutomaticPrefetch: true,
});
}
export async function getInternalUser(auth: SessionAuth): Promise<CurrentInternalUser> {
const app = getInternalApp(auth);
const user = await app.getUser({ or: "throw" });
return user as CurrentInternalUser;
}
export async function getAdminProject(auth: ProjectAuth): Promise<AdminOwnedProject> {
const user = await getInternalUser(auth);
const projects = await user.listOwnedProjects();
const project = projects.find((p) => p.id === auth.projectId);
if (!project) {
throw new AuthError(`Project '${auth.projectId}' not found. Make sure you own this project.`);
}
return project;
}