vibedgames 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dist/commands/deploy.d.ts +13 -0
- package/dist/commands/deploy.js +136 -0
- package/dist/commands/list.d.ts +1 -0
- package/dist/commands/list.js +23 -0
- package/dist/commands/login.d.ts +1 -0
- package/dist/commands/login.js +49 -0
- package/dist/commands/logout.d.ts +1 -0
- package/dist/commands/logout.js +13 -0
- package/dist/commands/rm.d.ts +12 -0
- package/dist/commands/rm.js +35 -0
- package/dist/commands/whoami.d.ts +1 -0
- package/dist/commands/whoami.js +32 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +20 -0
- package/dist/lib/api.d.ts +6 -0
- package/dist/lib/api.js +32 -0
- package/dist/lib/config-file.d.ts +7 -0
- package/dist/lib/config-file.js +24 -0
- package/dist/lib/config.d.ts +10 -0
- package/dist/lib/config.js +45 -0
- package/dist/lib/manifest.d.ts +10 -0
- package/dist/lib/manifest.js +85 -0
- package/dist/lib/upload.d.ts +16 -0
- package/dist/lib/upload.js +36 -0
- package/dist/prompt-E6QYABXJ.js +754 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# vibedgames (CLI)
|
|
2
|
+
|
|
3
|
+
CLI tool for deploying games to vibedgames.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm i -g vibedgames
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Commands
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
vg login # authenticate via browser
|
|
15
|
+
vg logout # clear credentials
|
|
16
|
+
vg whoami # show current user
|
|
17
|
+
vg deploy [dir] # deploy a game directory
|
|
18
|
+
vg deploy ./dist --slug my-game
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## How deploy works
|
|
22
|
+
|
|
23
|
+
1. Walks the directory, hashes files, validates `index.html` exists
|
|
24
|
+
2. Calls `deploy.create` API — gets presigned R2 upload URLs
|
|
25
|
+
3. Uploads files to R2 with bounded concurrency
|
|
26
|
+
4. Calls `deploy.finalize` — game goes live at `{slug}.vibedgames.com`
|
|
27
|
+
|
|
28
|
+
## Auth
|
|
29
|
+
|
|
30
|
+
Uses a polling flow: CLI generates a code, opens the browser, polls until the user confirms. Token stored at `~/.config/vg/auth.json`.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { defineCommand } from "citty";
|
|
4
|
+
import consola from "consola";
|
|
5
|
+
import { createClient } from "../lib/api.js";
|
|
6
|
+
import { projectConfigPath, readProjectConfig, writeProjectConfig, } from "../lib/config-file.js";
|
|
7
|
+
import { buildManifest } from "../lib/manifest.js";
|
|
8
|
+
import { uploadAll } from "../lib/upload.js";
|
|
9
|
+
const SLUG_RE = /^[a-z0-9][a-z0-9-]*[a-z0-9]$/;
|
|
10
|
+
export const deployCommand = defineCommand({
|
|
11
|
+
meta: {
|
|
12
|
+
name: "deploy",
|
|
13
|
+
description: "Deploy a game to vibedgames",
|
|
14
|
+
},
|
|
15
|
+
args: {
|
|
16
|
+
dir: {
|
|
17
|
+
type: "positional",
|
|
18
|
+
description: "Directory to deploy",
|
|
19
|
+
required: false,
|
|
20
|
+
default: ".",
|
|
21
|
+
},
|
|
22
|
+
slug: {
|
|
23
|
+
type: "string",
|
|
24
|
+
description: "Override the slug (bypasses vibedgames.json)",
|
|
25
|
+
required: false,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
run: async ({ args }) => {
|
|
29
|
+
const dir = resolve(args.dir);
|
|
30
|
+
if (!existsSync(dir)) {
|
|
31
|
+
consola.error(`Directory does not exist: ${dir}`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
// ---- Resolve project config ---------------------------------------------
|
|
35
|
+
let config = null;
|
|
36
|
+
try {
|
|
37
|
+
config = readProjectConfig(dir);
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
consola.error(err instanceof Error ? err.message : String(err));
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
if (args.slug) {
|
|
44
|
+
config = { slug: args.slug, name: config?.name };
|
|
45
|
+
}
|
|
46
|
+
if (!config) {
|
|
47
|
+
const slug = await consola.prompt("Slug (e.g. pong):", { type: "text" });
|
|
48
|
+
if (typeof slug !== "string" || !SLUG_RE.test(slug)) {
|
|
49
|
+
consola.error("Invalid slug. Use lowercase letters, digits, hyphens.");
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
const name = await consola.prompt("Name (optional):", { type: "text" });
|
|
53
|
+
config = {
|
|
54
|
+
slug,
|
|
55
|
+
name: typeof name === "string" && name.length > 0 ? name : undefined,
|
|
56
|
+
};
|
|
57
|
+
writeProjectConfig(dir, config);
|
|
58
|
+
consola.info(`Wrote ${projectConfigPath(dir)}`);
|
|
59
|
+
}
|
|
60
|
+
if (!SLUG_RE.test(config.slug)) {
|
|
61
|
+
consola.error(`Invalid slug in vibedgames.json: ${config.slug}`);
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
// ---- Walk files ---------------------------------------------------------
|
|
65
|
+
const manifest = buildManifest(dir);
|
|
66
|
+
if (manifest.length === 0) {
|
|
67
|
+
consola.error(`No files found in ${dir}`);
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
const hasIndex = manifest.some((f) => f.path === "index.html");
|
|
71
|
+
if (!hasIndex) {
|
|
72
|
+
consola.error("Deployment must contain an index.html at the root.");
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
const totalBytes = manifest.reduce((acc, f) => acc + f.size, 0);
|
|
76
|
+
consola.info(`Deploying ${config.slug}: ${manifest.length} files, ${formatBytes(totalBytes)}`);
|
|
77
|
+
// ---- Create deployment --------------------------------------------------
|
|
78
|
+
const client = createClient();
|
|
79
|
+
const started = Date.now();
|
|
80
|
+
let created;
|
|
81
|
+
try {
|
|
82
|
+
created = await client.deploy.create.mutate({
|
|
83
|
+
slug: config.slug,
|
|
84
|
+
name: config.name,
|
|
85
|
+
files: manifest.map((f) => ({
|
|
86
|
+
path: f.path,
|
|
87
|
+
size: f.size,
|
|
88
|
+
sha256: f.sha256,
|
|
89
|
+
contentType: f.contentType,
|
|
90
|
+
})),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
consola.error(err instanceof Error ? err.message : String(err));
|
|
95
|
+
process.exit(1);
|
|
96
|
+
}
|
|
97
|
+
// ---- Upload to R2 -------------------------------------------------------
|
|
98
|
+
consola.start(`Uploading ${created.uploads.length} files`);
|
|
99
|
+
try {
|
|
100
|
+
await uploadAll({
|
|
101
|
+
files: manifest,
|
|
102
|
+
uploads: created.uploads,
|
|
103
|
+
onProgress: (done, total) => {
|
|
104
|
+
if (done === total || done % 10 === 0) {
|
|
105
|
+
consola.info(`Uploaded ${done}/${total}`);
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
consola.error(err instanceof Error ? err.message : String(err));
|
|
112
|
+
process.exit(1);
|
|
113
|
+
}
|
|
114
|
+
// ---- Finalize -----------------------------------------------------------
|
|
115
|
+
let finalized;
|
|
116
|
+
try {
|
|
117
|
+
finalized = await client.deploy.finalize.mutate({
|
|
118
|
+
deploymentId: created.deploymentId,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
consola.error(err instanceof Error ? err.message : String(err));
|
|
123
|
+
process.exit(1);
|
|
124
|
+
}
|
|
125
|
+
const elapsed = ((Date.now() - started) / 1000).toFixed(1);
|
|
126
|
+
consola.success(`Deployed ${config.slug} in ${elapsed}s`);
|
|
127
|
+
consola.log(` ${finalized.url}`);
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
function formatBytes(n) {
|
|
131
|
+
if (n < 1024)
|
|
132
|
+
return `${n} B`;
|
|
133
|
+
if (n < 1024 * 1024)
|
|
134
|
+
return `${(n / 1024).toFixed(1)} KB`;
|
|
135
|
+
return `${(n / (1024 * 1024)).toFixed(1)} MB`;
|
|
136
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const listCommand: import("citty").CommandDef<import("citty").ArgsDef>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineCommand } from "citty";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { createClient } from "../lib/api.js";
|
|
4
|
+
export const listCommand = defineCommand({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "list",
|
|
7
|
+
description: "List your games",
|
|
8
|
+
},
|
|
9
|
+
run: async () => {
|
|
10
|
+
const client = createClient();
|
|
11
|
+
const { builds } = await client.localGame.listBuilds.query({ limit: 50 });
|
|
12
|
+
if (builds.length === 0) {
|
|
13
|
+
consola.info("No games found");
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
for (const build of builds) {
|
|
17
|
+
const fileCount = build.gameBuildFiles.length;
|
|
18
|
+
const updated = new Date(build.updatedAt).toLocaleDateString();
|
|
19
|
+
const title = build.title ?? "(untitled)";
|
|
20
|
+
consola.log(` ${build.id.slice(0, 8)} ${title} ${fileCount} files ${updated}`);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const loginCommand: import("citty").CommandDef<import("citty").ArgsDef>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { exec } from "node:child_process";
|
|
2
|
+
import { defineCommand } from "citty";
|
|
3
|
+
import consola from "consola";
|
|
4
|
+
import { createPublicClient } from "../lib/api.js";
|
|
5
|
+
import { getBaseUrl, saveConfig } from "../lib/config.js";
|
|
6
|
+
const POLL_INTERVAL_MS = 2_000;
|
|
7
|
+
const MAX_POLLS = 150; // 5 min at 2s intervals
|
|
8
|
+
function openBrowser(url) {
|
|
9
|
+
const cmd = process.platform === "darwin"
|
|
10
|
+
? "open"
|
|
11
|
+
: process.platform === "win32"
|
|
12
|
+
? "start"
|
|
13
|
+
: "xdg-open";
|
|
14
|
+
exec(`${cmd} "${url}"`);
|
|
15
|
+
}
|
|
16
|
+
function sleep(ms) {
|
|
17
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
18
|
+
}
|
|
19
|
+
export const loginCommand = defineCommand({
|
|
20
|
+
meta: {
|
|
21
|
+
name: "login",
|
|
22
|
+
description: "Authenticate with vibedgames",
|
|
23
|
+
},
|
|
24
|
+
run: async () => {
|
|
25
|
+
const baseUrl = getBaseUrl();
|
|
26
|
+
const client = createPublicClient(baseUrl);
|
|
27
|
+
const { code } = await client.cliAuth.create.mutate();
|
|
28
|
+
consola.box(`Code: ${code}`);
|
|
29
|
+
consola.info("Opening browser to complete authentication...");
|
|
30
|
+
const authUrl = `${baseUrl}/auth/cli?code=${code}`;
|
|
31
|
+
openBrowser(authUrl);
|
|
32
|
+
consola.start("Waiting for confirmation...");
|
|
33
|
+
for (let i = 0; i < MAX_POLLS; i++) {
|
|
34
|
+
await sleep(POLL_INTERVAL_MS);
|
|
35
|
+
const result = await client.cliAuth.poll.query({ code });
|
|
36
|
+
if (result.status === "confirmed") {
|
|
37
|
+
saveConfig({ token: result.token, baseUrl });
|
|
38
|
+
consola.success("Logged in successfully");
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (result.status === "expired") {
|
|
42
|
+
consola.error("Code expired. Run `vg login` to try again.");
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
consola.error("Timed out waiting for confirmation.");
|
|
47
|
+
process.exit(1);
|
|
48
|
+
},
|
|
49
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const logoutCommand: import("citty").CommandDef<import("citty").ArgsDef>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineCommand } from "citty";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { clearConfig } from "../lib/config.js";
|
|
4
|
+
export const logoutCommand = defineCommand({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "logout",
|
|
7
|
+
description: "Log out of vibedgames",
|
|
8
|
+
},
|
|
9
|
+
run: () => {
|
|
10
|
+
clearConfig();
|
|
11
|
+
consola.success("Logged out");
|
|
12
|
+
},
|
|
13
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { defineCommand } from "citty";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { createClient } from "../lib/api.js";
|
|
4
|
+
export const rmCommand = defineCommand({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "rm",
|
|
7
|
+
description: "Remove a game",
|
|
8
|
+
},
|
|
9
|
+
args: {
|
|
10
|
+
id: {
|
|
11
|
+
type: "positional",
|
|
12
|
+
description: "Game build ID to remove",
|
|
13
|
+
required: true,
|
|
14
|
+
},
|
|
15
|
+
force: {
|
|
16
|
+
type: "boolean",
|
|
17
|
+
description: "Skip confirmation",
|
|
18
|
+
default: false,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
run: async ({ args }) => {
|
|
22
|
+
if (!args.force) {
|
|
23
|
+
const confirmed = await consola.prompt(`Remove game ${args.id}?`, {
|
|
24
|
+
type: "confirm",
|
|
25
|
+
});
|
|
26
|
+
if (!confirmed) {
|
|
27
|
+
consola.info("Cancelled");
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const client = createClient();
|
|
32
|
+
await client.localGame.deleteBuild.mutate({ buildId: args.id });
|
|
33
|
+
consola.success(`Removed ${args.id}`);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const whoamiCommand: import("citty").CommandDef<import("citty").ArgsDef>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { defineCommand } from "citty";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { getBaseUrl, getToken } from "../lib/config.js";
|
|
4
|
+
export const whoamiCommand = defineCommand({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "whoami",
|
|
7
|
+
description: "Show the currently authenticated user",
|
|
8
|
+
},
|
|
9
|
+
run: async () => {
|
|
10
|
+
const token = getToken();
|
|
11
|
+
if (!token) {
|
|
12
|
+
consola.warn("Not logged in. Run `vg login` to authenticate.");
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
const baseUrl = getBaseUrl();
|
|
16
|
+
const res = await fetch(`${baseUrl}/api/auth/get-session`, {
|
|
17
|
+
headers: {
|
|
18
|
+
Authorization: `Bearer ${token}`,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
if (!res.ok) {
|
|
22
|
+
consola.error("Failed to fetch session. Try `vg login` again.");
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
const data = (await res.json());
|
|
26
|
+
if (!data?.user) {
|
|
27
|
+
consola.warn("Session expired. Run `vg login` to re-authenticate.");
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
consola.log(`${data.user.name} (${data.user.email})`);
|
|
31
|
+
},
|
|
32
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { defineCommand, runMain } from "citty";
|
|
3
|
+
import { deployCommand } from "./commands/deploy.js";
|
|
4
|
+
import { loginCommand } from "./commands/login.js";
|
|
5
|
+
import { logoutCommand } from "./commands/logout.js";
|
|
6
|
+
import { whoamiCommand } from "./commands/whoami.js";
|
|
7
|
+
const main = defineCommand({
|
|
8
|
+
meta: {
|
|
9
|
+
name: "vg",
|
|
10
|
+
version: "0.0.1",
|
|
11
|
+
description: "vibedgames CLI",
|
|
12
|
+
},
|
|
13
|
+
subCommands: {
|
|
14
|
+
login: loginCommand,
|
|
15
|
+
logout: logoutCommand,
|
|
16
|
+
deploy: deployCommand,
|
|
17
|
+
whoami: whoamiCommand,
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
runMain(main);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AppRouter } from "@repo/api";
|
|
2
|
+
import type { TRPCClient } from "@trpc/client";
|
|
3
|
+
/** Authenticated client — requires a saved session token. */
|
|
4
|
+
export declare function createClient(): TRPCClient<AppRouter>;
|
|
5
|
+
/** Unauthenticated client — for login flow. */
|
|
6
|
+
export declare function createPublicClient(baseUrl: string): TRPCClient<AppRouter>;
|
package/dist/lib/api.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createTRPCClient, httpBatchLink } from "@trpc/client";
|
|
2
|
+
import superjson from "superjson";
|
|
3
|
+
import { getBaseUrl, getToken } from "./config.js";
|
|
4
|
+
/** Authenticated client — requires a saved session token. */
|
|
5
|
+
export function createClient() {
|
|
6
|
+
const token = getToken();
|
|
7
|
+
if (!token) {
|
|
8
|
+
throw new Error("Not logged in. Run `vg login` to authenticate.");
|
|
9
|
+
}
|
|
10
|
+
return createTRPCClient({
|
|
11
|
+
links: [
|
|
12
|
+
httpBatchLink({
|
|
13
|
+
url: `${getBaseUrl()}/api/trpc`,
|
|
14
|
+
transformer: superjson,
|
|
15
|
+
headers: () => ({
|
|
16
|
+
Authorization: `Bearer ${token}`,
|
|
17
|
+
}),
|
|
18
|
+
}),
|
|
19
|
+
],
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/** Unauthenticated client — for login flow. */
|
|
23
|
+
export function createPublicClient(baseUrl) {
|
|
24
|
+
return createTRPCClient({
|
|
25
|
+
links: [
|
|
26
|
+
httpBatchLink({
|
|
27
|
+
url: `${baseUrl}/api/trpc`,
|
|
28
|
+
transformer: superjson,
|
|
29
|
+
}),
|
|
30
|
+
],
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type ProjectConfig = {
|
|
2
|
+
slug: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function readProjectConfig(dir: string): ProjectConfig | null;
|
|
6
|
+
export declare function writeProjectConfig(dir: string, config: ProjectConfig): void;
|
|
7
|
+
export declare function projectConfigPath(dir: string): string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
const FILENAME = "vibedgames.json";
|
|
4
|
+
export function readProjectConfig(dir) {
|
|
5
|
+
const path = join(dir, FILENAME);
|
|
6
|
+
if (!existsSync(path))
|
|
7
|
+
return null;
|
|
8
|
+
const raw = readFileSync(path, "utf-8");
|
|
9
|
+
const parsed = JSON.parse(raw);
|
|
10
|
+
if (!parsed ||
|
|
11
|
+
typeof parsed !== "object" ||
|
|
12
|
+
!("slug" in parsed) ||
|
|
13
|
+
typeof parsed.slug !== "string") {
|
|
14
|
+
throw new Error(`${FILENAME} is malformed — missing "slug".`);
|
|
15
|
+
}
|
|
16
|
+
return parsed;
|
|
17
|
+
}
|
|
18
|
+
export function writeProjectConfig(dir, config) {
|
|
19
|
+
const path = join(dir, FILENAME);
|
|
20
|
+
writeFileSync(path, JSON.stringify(config, null, 2) + "\n");
|
|
21
|
+
}
|
|
22
|
+
export function projectConfigPath(dir) {
|
|
23
|
+
return join(dir, FILENAME);
|
|
24
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type Config = {
|
|
2
|
+
token: string;
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function getConfig(): Config | null;
|
|
6
|
+
export declare function saveConfig(config: Config): void;
|
|
7
|
+
export declare function clearConfig(): void;
|
|
8
|
+
export declare function getBaseUrl(): string;
|
|
9
|
+
export declare function getToken(): string | null;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, unlinkSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
function getConfigDir() {
|
|
5
|
+
const xdg = process.env.XDG_CONFIG_HOME;
|
|
6
|
+
const base = xdg ?? join(homedir(), ".config");
|
|
7
|
+
return join(base, "vg");
|
|
8
|
+
}
|
|
9
|
+
function getConfigPath() {
|
|
10
|
+
return join(getConfigDir(), "auth.json");
|
|
11
|
+
}
|
|
12
|
+
export function getConfig() {
|
|
13
|
+
const path = getConfigPath();
|
|
14
|
+
if (!existsSync(path))
|
|
15
|
+
return null;
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function saveConfig(config) {
|
|
24
|
+
const dir = getConfigDir();
|
|
25
|
+
if (!existsSync(dir)) {
|
|
26
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
27
|
+
}
|
|
28
|
+
writeFileSync(getConfigPath(), JSON.stringify(config, null, 2), {
|
|
29
|
+
mode: 0o600,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export function clearConfig() {
|
|
33
|
+
const path = getConfigPath();
|
|
34
|
+
if (existsSync(path)) {
|
|
35
|
+
unlinkSync(path);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function getBaseUrl() {
|
|
39
|
+
return (process.env.VG_API_URL ??
|
|
40
|
+
getConfig()?.baseUrl ??
|
|
41
|
+
"https://vibedgames.com");
|
|
42
|
+
}
|
|
43
|
+
export function getToken() {
|
|
44
|
+
return getConfig()?.token ?? null;
|
|
45
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type ManifestFile = {
|
|
2
|
+
/** forward-slash relative path, e.g. "assets/sprite.png" */
|
|
3
|
+
path: string;
|
|
4
|
+
/** absolute filesystem path */
|
|
5
|
+
absolutePath: string;
|
|
6
|
+
size: number;
|
|
7
|
+
sha256: string;
|
|
8
|
+
contentType: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function buildManifest(rootDir: string): ManifestFile[];
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import { join, relative, sep } from "node:path";
|
|
4
|
+
const IGNORED_TOP_LEVEL = new Set([
|
|
5
|
+
"node_modules",
|
|
6
|
+
".git",
|
|
7
|
+
".turbo",
|
|
8
|
+
".cache",
|
|
9
|
+
".DS_Store",
|
|
10
|
+
"vibedgames.json",
|
|
11
|
+
]);
|
|
12
|
+
const CONTENT_TYPES = {
|
|
13
|
+
html: "text/html; charset=utf-8",
|
|
14
|
+
htm: "text/html; charset=utf-8",
|
|
15
|
+
js: "application/javascript; charset=utf-8",
|
|
16
|
+
mjs: "application/javascript; charset=utf-8",
|
|
17
|
+
cjs: "application/javascript; charset=utf-8",
|
|
18
|
+
css: "text/css; charset=utf-8",
|
|
19
|
+
json: "application/json; charset=utf-8",
|
|
20
|
+
map: "application/json; charset=utf-8",
|
|
21
|
+
svg: "image/svg+xml",
|
|
22
|
+
png: "image/png",
|
|
23
|
+
jpg: "image/jpeg",
|
|
24
|
+
jpeg: "image/jpeg",
|
|
25
|
+
gif: "image/gif",
|
|
26
|
+
webp: "image/webp",
|
|
27
|
+
avif: "image/avif",
|
|
28
|
+
ico: "image/x-icon",
|
|
29
|
+
wasm: "application/wasm",
|
|
30
|
+
mp3: "audio/mpeg",
|
|
31
|
+
ogg: "audio/ogg",
|
|
32
|
+
wav: "audio/wav",
|
|
33
|
+
m4a: "audio/mp4",
|
|
34
|
+
mp4: "video/mp4",
|
|
35
|
+
webm: "video/webm",
|
|
36
|
+
ttf: "font/ttf",
|
|
37
|
+
otf: "font/otf",
|
|
38
|
+
woff: "font/woff",
|
|
39
|
+
woff2: "font/woff2",
|
|
40
|
+
txt: "text/plain; charset=utf-8",
|
|
41
|
+
xml: "application/xml",
|
|
42
|
+
};
|
|
43
|
+
function contentTypeForPath(path) {
|
|
44
|
+
const ext = path.split(".").pop()?.toLowerCase() ?? "";
|
|
45
|
+
return CONTENT_TYPES[ext] ?? "application/octet-stream";
|
|
46
|
+
}
|
|
47
|
+
function shouldIgnore(relPath) {
|
|
48
|
+
const top = relPath.split(sep)[0] ?? "";
|
|
49
|
+
if (IGNORED_TOP_LEVEL.has(top))
|
|
50
|
+
return true;
|
|
51
|
+
// skip hidden files anywhere in the tree
|
|
52
|
+
if (relPath.split(sep).some((seg) => seg.startsWith(".")))
|
|
53
|
+
return true;
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
export function buildManifest(rootDir) {
|
|
57
|
+
const files = [];
|
|
58
|
+
const walk = (dir) => {
|
|
59
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
60
|
+
const abs = join(dir, entry.name);
|
|
61
|
+
const rel = relative(rootDir, abs);
|
|
62
|
+
if (shouldIgnore(rel))
|
|
63
|
+
continue;
|
|
64
|
+
if (entry.isDirectory()) {
|
|
65
|
+
walk(abs);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (!entry.isFile())
|
|
69
|
+
continue;
|
|
70
|
+
const stat = statSync(abs);
|
|
71
|
+
const buf = readFileSync(abs);
|
|
72
|
+
const sha256 = createHash("sha256").update(buf).digest("hex");
|
|
73
|
+
const posixPath = rel.split(sep).join("/");
|
|
74
|
+
files.push({
|
|
75
|
+
path: posixPath,
|
|
76
|
+
absolutePath: abs,
|
|
77
|
+
size: stat.size,
|
|
78
|
+
sha256,
|
|
79
|
+
contentType: contentTypeForPath(posixPath),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
walk(rootDir);
|
|
84
|
+
return files;
|
|
85
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ManifestFile } from "./manifest.js";
|
|
2
|
+
export type UploadTarget = {
|
|
3
|
+
path: string;
|
|
4
|
+
url: string;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Upload all files to their presigned PUT URLs with bounded concurrency.
|
|
9
|
+
* Throws on the first non-2xx response.
|
|
10
|
+
*/
|
|
11
|
+
export declare function uploadAll({ files, uploads, concurrency, onProgress, }: {
|
|
12
|
+
files: ManifestFile[];
|
|
13
|
+
uploads: UploadTarget[];
|
|
14
|
+
concurrency?: number;
|
|
15
|
+
onProgress?: (done: number, total: number) => void;
|
|
16
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* Upload all files to their presigned PUT URLs with bounded concurrency.
|
|
4
|
+
* Throws on the first non-2xx response.
|
|
5
|
+
*/
|
|
6
|
+
export async function uploadAll({ files, uploads, concurrency = 6, onProgress, }) {
|
|
7
|
+
const byPath = new Map(files.map((f) => [f.path, f]));
|
|
8
|
+
let done = 0;
|
|
9
|
+
let cursor = 0;
|
|
10
|
+
async function worker() {
|
|
11
|
+
while (true) {
|
|
12
|
+
const idx = cursor++;
|
|
13
|
+
if (idx >= uploads.length)
|
|
14
|
+
return;
|
|
15
|
+
const upload = uploads[idx];
|
|
16
|
+
const file = byPath.get(upload.path);
|
|
17
|
+
if (!file) {
|
|
18
|
+
throw new Error(`Missing file for upload target: ${upload.path}`);
|
|
19
|
+
}
|
|
20
|
+
const body = readFileSync(file.absolutePath);
|
|
21
|
+
const res = await fetch(upload.url, {
|
|
22
|
+
method: "PUT",
|
|
23
|
+
headers: upload.headers,
|
|
24
|
+
body,
|
|
25
|
+
});
|
|
26
|
+
if (!res.ok) {
|
|
27
|
+
const text = await res.text().catch(() => "");
|
|
28
|
+
throw new Error(`Upload failed for ${upload.path}: ${res.status} ${res.statusText} ${text}`);
|
|
29
|
+
}
|
|
30
|
+
done++;
|
|
31
|
+
onProgress?.(done, uploads.length);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const workers = Array.from({ length: Math.min(concurrency, uploads.length) }, () => worker());
|
|
35
|
+
await Promise.all(workers);
|
|
36
|
+
}
|