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
19 changes: 14 additions & 5 deletions src/commands/gxgames/api/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ const LinkSchema = z.object({
export type GxGamesLink = z.infer<typeof LinkSchema>;

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

async read(): Promise<GxGamesLink> {
const cache = await Cache.initLazy(this.ctx, { type: "infer" });
const dir = await cache.getSubDirPath(this.ctx, CACHE_SUBDIR);
const dir = await this.cacheDir();
try {
const raw = await this.ctx.fs.readFile(
this.ctx.path.join(dir, LINK_FILENAME),
Expand All @@ -50,11 +52,18 @@ export class LinkStorage {

async write(link: GxGamesLink): Promise<void> {
// TODO: later, we may want to store this as part of the "manifest" file instead under a "tools.gxgames" key.
const cache = await Cache.initLazy(this.ctx, { type: "infer" });
const dir = await cache.getSubDirPath(this.ctx, CACHE_SUBDIR);
const dir = await this.cacheDir();
await this.ctx.fs.writeFile(
this.ctx.path.join(dir, LINK_FILENAME),
JSON.stringify(link, 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);
}
}
30 changes: 17 additions & 13 deletions src/commands/gxgames/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
*/

import { buildCommand, buildRouteMap } from "@stricli/core";
import { parseProjectPath } from "~/project";

const projectParam = {
brief: "Path to the project .yyp file (defaults to current directory)",
placeholder: "project",
parse: parseProjectPath,
optional: true as const,
};

const gxgamesLinkCommand = buildCommand({
loader: async () => import("./commands/link-impl"),
parameters: {
positional: { kind: "tuple", parameters: [] },
positional: { kind: "tuple", parameters: [projectParam] },
flags: {
studioid: {
kind: "parsed",
Expand All @@ -43,17 +51,13 @@ const gxgamesLinkCommand = buildCommand({
const gxgamesUploadCommand = buildCommand({
loader: async () => import("./commands/upload-impl"),
parameters: {
positional: {
kind: "tuple",
parameters: [
{
brief: "Path to the zip file to upload",
placeholder: "file",
parse: String,
},
],
},
positional: { kind: "tuple", parameters: [projectParam] },
flags: {
file: {
kind: "parsed",
parse: String,
brief: "Path to the zip file to upload",
},
version: {
kind: "parsed",
parse: String,
Expand All @@ -73,7 +77,7 @@ const gxgamesUploadCommand = buildCommand({
const gxgamesMetaCommand = buildCommand({
loader: async () => import("./commands/meta-impl"),
parameters: {
positional: { kind: "tuple", parameters: [] },
positional: { kind: "tuple", parameters: [projectParam] },
flags: {
title: {
kind: "parsed",
Expand Down Expand Up @@ -128,7 +132,7 @@ const gxgamesMetaCommand = buildCommand({
const gxgamesPublishCommand = buildCommand({
loader: async () => import("./commands/publish-impl"),
parameters: {
positional: { kind: "tuple", parameters: [] },
positional: { kind: "tuple", parameters: [projectParam] },
},
docs: {
brief: "Make the game public on GX.Games",
Expand Down
5 changes: 4 additions & 1 deletion src/commands/gxgames/commands/link-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import * as p from "@clack/prompts";
import type { Context } from "~/context";
import { KnownError } from "~/error";
import type { ProjectPath } from "~/project";

import { createAuthManager } from "../auth";
import { getApiClient } from "../api";
Expand All @@ -25,6 +26,7 @@ import { LinkStorage } from "../api";
export default async function (
this: Context,
flags: { studioid?: string; gameid?: string },
project?: ProjectPath,
): Promise<void> {
let studioId = flags.studioid;
let gameId = flags.gameid;
Expand Down Expand Up @@ -97,6 +99,7 @@ export default async function (
}
}

await new LinkStorage(this).write({ studioId, gameId });
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}`);
}
10 changes: 8 additions & 2 deletions src/commands/gxgames/commands/meta-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import type { Context } from "~/context";
import { KnownError } from "~/error";
import type { ProjectPath } from "~/project";

import { LinkStorage } from "../api";
import { createAuthManager } from "../auth";
Expand All @@ -35,8 +36,13 @@ interface MetaFlags {
graphic?: string;
}

export default async function (this: Context, flags: MetaFlags): Promise<void> {
const link = await new LinkStorage(this).read();
export default async function (
this: Context,
flags: MetaFlags,
project?: ProjectPath,
): 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 gameRes = await api.getGameDetails(link.gameId);
Expand Down
5 changes: 4 additions & 1 deletion src/commands/gxgames/commands/publish-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import type { Context } from "~/context";
import * as p from "@clack/prompts";
import { KnownError } from "~/error";
import type { ProjectPath } from "~/project";

import { LinkStorage } from "../api";
import { createAuthManager } from "../auth";
Expand All @@ -25,8 +26,10 @@ import { getApiClient } from "../api";
export default async function (
this: Context,
_flags: Record<never, never>,
project?: ProjectPath,
): Promise<void> {
const link = await new LinkStorage(this).read();
const projectDir = project ? this.path.dirname(project) : undefined;
const link = await new LinkStorage(this, projectDir).read();
const api = getApiClient(this, createAuthManager(this));

const publishLog = this.makeTaskLogger("Publishing game");
Expand Down
12 changes: 7 additions & 5 deletions src/commands/gxgames/commands/upload-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import * as p from "@clack/prompts";
import { getApiClient } from "../api";
import { createAuthManager } from "../auth";
import { KnownError } from "~/error";
import type { ProjectPath } from "~/project";

import { LinkStorage } from "../api";

export default async function (
this: Context,
flags: { version?: string },
file: string,
flags: { file: string; version?: string },
project?: ProjectPath,
): Promise<void> {
const link = await new LinkStorage(this).read();
const projectDir = project ? this.path.dirname(project) : undefined;
const link = await new LinkStorage(this, projectDir).read();

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

Expand Down Expand Up @@ -60,11 +62,11 @@ export default async function (
}

const uploadLog = this.makeTaskLogger("Uploading bundle");
const fileBuffer = await this.fs.readFile(file);
const fileBuffer = await this.fs.readFile(flags.file);
const res = await api.uploadGameBundle(
link.gameId,
{ version },
{ file: new File([fileBuffer], this.path.basename(file)) },
{ file: new File([fileBuffer], this.path.basename(flags.file)) },
);
if (!res.success) {
throw new KnownError(res.errors);
Expand Down
Loading