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
4 changes: 2 additions & 2 deletions src/commands/gxgames/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ class Auth {
}
}

export function createAuthManager(ctx: Context) {
export function createAuthManager(ctx: Context, projectDir?: string) {
return new Auth({
fetch: ctx.fetch,
http: ctx.http,
open: ctx.open,
makeTaskLogger: ctx.makeTaskLogger,
storage: new AuthStorage(ctx),
storage: new AuthStorage(ctx, projectDir),
});
}
23 changes: 14 additions & 9 deletions src/commands/gxgames/auth/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ const AuthSchema = z.object({
export type GxGamesAuth = z.infer<typeof AuthSchema>;

export class AuthStorage {
constructor(private readonly ctx: Context) {}
constructor(
private readonly ctx: Context,
private readonly projectDir?: string,
) {}

async read(): Promise<GxGamesAuth | undefined> {
const cache = await Cache.initLazy(this.ctx, { type: "infer" });
const dir = await cache.getSubDirPath(this.ctx, CACHE_SUBDIR, {
preferShared: true,
});
const dir = await this.cacheDir();
try {
const raw = await this.ctx.fs.readFile(
this.ctx.path.join(dir, AUTH_FILENAME),
Expand All @@ -49,13 +49,18 @@ export class AuthStorage {
}

async write(auth: GxGamesAuth): Promise<void> {
const cache = await Cache.initLazy(this.ctx, { type: "infer" });
const dir = await cache.getSubDirPath(this.ctx, CACHE_SUBDIR, {
preferShared: true,
});
const dir = await this.cacheDir();
await this.ctx.fs.writeFile(
this.ctx.path.join(dir, AUTH_FILENAME),
JSON.stringify(auth, null, 2),
);
}

private async cacheDir(): Promise<string> {
const cache = await Cache.initLazy(this.ctx, {
type: "infer",
projectDir: this.projectDir,
});
return cache.getSubDirPath(this.ctx, CACHE_SUBDIR, { preferShared: true });
}
}
4 changes: 2 additions & 2 deletions src/commands/gxgames/commands/link-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export default async function (
): Promise<void> {
let studioId = flags.studioid;
let gameId = flags.gameid;
const projectDir = project ? this.path.dirname(project) : undefined;

if (!studioId || !gameId) {
const api = getApiClient(this, createAuthManager(this));
const api = getApiClient(this, createAuthManager(this, projectDir));

if (!studioId) {
const studiosRes = await api.getUserStudios({ pageSize: 999 });
Expand Down Expand Up @@ -99,7 +100,6 @@ export default async function (
}
}

const projectDir = project ? this.path.dirname(project) : undefined;
await new LinkStorage(this, projectDir).write({ studioId, gameId });
p.log.success(`Linked to studio ${studioId}, game ${gameId}`);
}
2 changes: 1 addition & 1 deletion src/commands/gxgames/commands/meta-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default async function (
): Promise<void> {
const projectDir = project ? this.path.dirname(project) : undefined;
const link = await new LinkStorage(this, projectDir).read();
const api = getApiClient(this, createAuthManager(this));
const api = getApiClient(this, createAuthManager(this, projectDir));

const gameRes = await api.getGameDetails(link.gameId);
if (!gameRes.success) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/gxgames/commands/publish-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default async function (
): Promise<void> {
const projectDir = project ? this.path.dirname(project) : undefined;
const link = await new LinkStorage(this, projectDir).read();
const api = getApiClient(this, createAuthManager(this));
const api = getApiClient(this, createAuthManager(this, projectDir));

const publishLog = this.makeTaskLogger("Publishing game");
const res = await api.publishGame(link.gameId);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/gxgames/commands/upload-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default async function (
const projectDir = project ? this.path.dirname(project) : undefined;
const link = await new LinkStorage(this, projectDir).read();

const api = getApiClient(this, createAuthManager(this));
const api = getApiClient(this, createAuthManager(this, projectDir));

const gamesRes = await api.getUserGames({
studioId: [link.studioId],
Expand Down
Loading