vibedgames 0.5.0 → 0.5.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/LICENSE +21 -0
- package/dist/commands/completions.d.ts +1 -1
- package/dist/commands/completions.js +21 -20
- package/dist/commands/credits.d.ts +5 -5
- package/dist/commands/credits.js +43 -33
- package/dist/commands/deploy.d.ts +10 -10
- package/dist/commands/deploy.js +155 -139
- package/dist/commands/factory.d.ts +1 -1
- package/dist/commands/factory.js +18 -15
- package/dist/commands/fork.d.ts +14 -14
- package/dist/commands/fork.js +51 -46
- package/dist/commands/generate.js +318 -264
- package/dist/commands/init.d.ts +8 -8
- package/dist/commands/init.js +31 -24
- package/dist/commands/login.js +12 -12
- package/dist/commands/logout.js +2 -2
- package/dist/commands/new.d.ts +16 -16
- package/dist/commands/new.js +153 -142
- package/dist/commands/playtest.d.ts +5 -5
- package/dist/commands/playtest.js +103 -97
- package/dist/commands/update.d.ts +7 -7
- package/dist/commands/update.js +20 -17
- package/dist/commands/whoami.d.ts +5 -5
- package/dist/commands/whoami.js +8 -7
- package/dist/index.js +10 -10
- package/dist/lib/api.d.ts +4 -4
- package/dist/lib/api.js +6 -12
- package/dist/lib/archive.d.ts +4 -4
- package/dist/lib/archive.js +121 -104
- package/dist/lib/codex.d.ts +45 -15
- package/dist/lib/codex.js +175 -106
- package/dist/lib/config-file.d.ts +6 -6
- package/dist/lib/config-file.js +28 -26
- package/dist/lib/config.d.ts +8 -8
- package/dist/lib/config.js +31 -33
- package/dist/lib/manifest.d.ts +3 -3
- package/dist/lib/manifest.js +42 -38
- package/dist/lib/media-args.d.ts +7 -7
- package/dist/lib/media-args.js +113 -95
- package/dist/lib/media-download.d.ts +9 -9
- package/dist/lib/media-download.js +148 -134
- package/dist/lib/media-poll.d.ts +8 -8
- package/dist/lib/media-poll.js +32 -27
- package/dist/lib/media-upload.d.ts +2 -2
- package/dist/lib/media-upload.js +27 -27
- package/dist/lib/output.d.ts +27 -12
- package/dist/lib/output.js +26 -26
- package/dist/lib/package-manager.d.ts +3 -3
- package/dist/lib/package-manager.js +27 -22
- package/dist/lib/run.d.ts +4 -4
- package/dist/lib/run.js +7 -4
- package/dist/lib/strict-args.d.ts +4 -4
- package/dist/lib/strict-args.js +28 -18
- package/dist/lib/types.d.ts +5 -5
- package/dist/lib/types.js +9 -12
- package/dist/lib/update.d.ts +4 -4
- package/dist/lib/update.js +41 -32
- package/dist/lib/upload.d.ts +4 -4
- package/dist/lib/upload.js +14 -9
- package/package.json +16 -16
package/dist/commands/deploy.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { existsSync, readFileSync, rmSync } from "node:fs";
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
|
-
import
|
|
3
|
+
import path from "node:path";
|
|
4
4
|
import { defineCommand } from "citty";
|
|
5
|
-
import consola from "consola";
|
|
5
|
+
import { consola } from "consola";
|
|
6
6
|
import { createClient } from "../lib/api.js";
|
|
7
7
|
import { packSource } from "../lib/archive.js";
|
|
8
8
|
import { findProjectRoot, projectConfigPath, readProjectConfig, writeProjectConfig, SLUG_RE, } from "../lib/config-file.js";
|
|
@@ -13,98 +13,158 @@ import { isJsonString } from "../lib/types.js";
|
|
|
13
13
|
import { uploadAll } from "../lib/upload.js";
|
|
14
14
|
const deployArgs = {
|
|
15
15
|
dir: {
|
|
16
|
-
|
|
16
|
+
default: ".",
|
|
17
17
|
description: "Directory to deploy",
|
|
18
18
|
required: false,
|
|
19
|
-
|
|
19
|
+
type: "positional",
|
|
20
20
|
},
|
|
21
21
|
slug: {
|
|
22
|
-
type: "string",
|
|
23
22
|
description: "Override the slug (bypasses vibedgames.json)",
|
|
24
23
|
required: false,
|
|
24
|
+
type: "string",
|
|
25
25
|
},
|
|
26
26
|
source: {
|
|
27
|
-
type: "boolean",
|
|
28
|
-
description: "Also upload a forkable source archive, letting anyone logged in fork the project.",
|
|
29
27
|
default: false,
|
|
28
|
+
description: "Also upload a forkable source archive, letting anyone logged in fork the project.",
|
|
29
|
+
type: "boolean",
|
|
30
30
|
},
|
|
31
31
|
...outputArgs,
|
|
32
32
|
};
|
|
33
|
+
const formatBytes = (n) => {
|
|
34
|
+
if (n < 1024) {
|
|
35
|
+
return `${n} B`;
|
|
36
|
+
}
|
|
37
|
+
if (n < 1024 * 1024) {
|
|
38
|
+
return `${(n / 1024).toFixed(1)} KB`;
|
|
39
|
+
}
|
|
40
|
+
return `${(n / (1024 * 1024)).toFixed(1)} MB`;
|
|
41
|
+
};
|
|
42
|
+
/** Build-tool projects (package.json present): the root index.html is the
|
|
43
|
+
* source template, the playable game lives in the build output. Deploying the
|
|
44
|
+
* root uploads the whole source tree — wrong content and usually over the file
|
|
45
|
+
* caps — so prefer a build directory when one exists. Checked in popularity
|
|
46
|
+
* order: Vite/Rollup/Parcel → CRA/Preact → Next export/esbuild. */
|
|
47
|
+
const resolveDeployDir = (dir, say) => {
|
|
48
|
+
if (!existsSync(path.join(dir, "package.json"))) {
|
|
49
|
+
return dir;
|
|
50
|
+
}
|
|
51
|
+
for (const name of ["dist", "build", "out"]) {
|
|
52
|
+
if (existsSync(path.join(dir, name, "index.html"))) {
|
|
53
|
+
say(`Project root detected — deploying its build output ${name}/ instead.`);
|
|
54
|
+
return path.join(dir, name);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
consola.warn("This looks like an unbuilt project (package.json, no dist/build/out) — " +
|
|
58
|
+
"run the build first, or the deployed index.html will reference missing source files.");
|
|
59
|
+
return dir;
|
|
60
|
+
};
|
|
61
|
+
const promptForConfig = async (dir, say) => {
|
|
62
|
+
if (!process.stdin.isTTY) {
|
|
63
|
+
consola.error("No slug specified and no vibedgames.json found.\n" +
|
|
64
|
+
" Pass --slug <name> when running without a TTY (e.g. in CI or from a coding agent).\n" +
|
|
65
|
+
" Example: vg deploy ./dist --slug my-game");
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
// isJsonString: consola.prompt is typed to resolve a string but can resolve a
|
|
69
|
+
// cancel sentinel at runtime, and a symbol would throw inside RegExp.test.
|
|
70
|
+
const slug = await consola.prompt("Slug (e.g. pong):", { type: "text" });
|
|
71
|
+
if (!isJsonString(slug) || !SLUG_RE.test(slug)) {
|
|
72
|
+
consola.error("Invalid slug. Use lowercase letters, digits, hyphens.");
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
const name = await consola.prompt("Name (optional):", { type: "text" });
|
|
76
|
+
const config = {
|
|
77
|
+
name: isJsonString(name) && name.length > 0 ? name : undefined,
|
|
78
|
+
slug,
|
|
79
|
+
};
|
|
80
|
+
writeProjectConfig(dir, config);
|
|
81
|
+
say(`Wrote ${projectConfigPath(dir)}`);
|
|
82
|
+
return config;
|
|
83
|
+
};
|
|
84
|
+
const resolveDeployConfig = async (dir, slugOverride, say) => {
|
|
85
|
+
let config = null;
|
|
86
|
+
try {
|
|
87
|
+
config = readProjectConfig(dir);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
consola.error(error instanceof Error ? error.message : String(error));
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
if (slugOverride) {
|
|
94
|
+
config = { name: config?.name, slug: slugOverride };
|
|
95
|
+
}
|
|
96
|
+
config ??= await promptForConfig(dir, say);
|
|
97
|
+
if (!SLUG_RE.test(config.slug)) {
|
|
98
|
+
consola.error(`Invalid slug in vibedgames.json: ${config.slug}`);
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
return config;
|
|
102
|
+
};
|
|
103
|
+
const packForkableSource = async (dir, slug, say) => {
|
|
104
|
+
const root = findProjectRoot(dir);
|
|
105
|
+
if (!root) {
|
|
106
|
+
consola.warn("No vibedgames.json found above the deploy dir — skipping source upload.");
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
const archive = await packSource(root, path.join(tmpdir(), "vibedgames"));
|
|
111
|
+
say(`Source: ${archive.files.length} files, ${formatBytes(archive.bytes)} — forkable via \`vg fork ${slug}\``);
|
|
112
|
+
return archive;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
consola.error(`Source archive failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const uploadSourceArchive = async (archive, upload) => {
|
|
120
|
+
try {
|
|
121
|
+
const res = await fetch(upload.url, {
|
|
122
|
+
body: readFileSync(archive.path),
|
|
123
|
+
headers: upload.headers,
|
|
124
|
+
method: "PUT",
|
|
125
|
+
});
|
|
126
|
+
if (!res.ok) {
|
|
127
|
+
throw new Error(`source upload failed: ${res.status} ${res.statusText}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
rmSync(archive.path, { force: true });
|
|
132
|
+
consola.error(error instanceof Error ? error.message : String(error));
|
|
133
|
+
process.exit(1);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const finalizeDeployment = async (client, deploymentId) => {
|
|
137
|
+
try {
|
|
138
|
+
return await client.deploy.finalize({ deploymentId });
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
consola.error(error instanceof Error ? error.message : String(error));
|
|
142
|
+
process.exit(1);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
33
145
|
export const deployCommand = defineCommand({
|
|
146
|
+
args: deployArgs,
|
|
34
147
|
meta: {
|
|
35
|
-
name: "deploy",
|
|
36
148
|
description: "Deploy a game to vibedgames",
|
|
149
|
+
name: "deploy",
|
|
37
150
|
},
|
|
38
|
-
args: deployArgs,
|
|
39
151
|
run: async ({ args, rawArgs }) => {
|
|
40
152
|
assertKnownFlags(rawArgs, deployArgs);
|
|
41
153
|
// Progress narration shares stdout with the payload, so it has to go quiet
|
|
42
154
|
// whenever stdout is the result rather than a story about it.
|
|
43
155
|
const structured = isJsonOutput(args) || Boolean(args.field);
|
|
44
|
-
const say = structured
|
|
45
|
-
|
|
156
|
+
const say = structured
|
|
157
|
+
? () => {
|
|
158
|
+
/* empty */
|
|
159
|
+
}
|
|
160
|
+
: (message) => consola.info(message);
|
|
161
|
+
const dir = path.resolve(args.dir);
|
|
46
162
|
if (!existsSync(dir)) {
|
|
47
163
|
consola.error(`Directory does not exist: ${dir}`);
|
|
48
164
|
process.exit(1);
|
|
49
165
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
// the root uploads the whole source tree — wrong content and usually over
|
|
53
|
-
// the file caps — so prefer a build directory when one exists. Checked in
|
|
54
|
-
// popularity order: Vite/Rollup/Parcel → CRA/Preact → Next export/esbuild.
|
|
55
|
-
let deployDir = dir;
|
|
56
|
-
if (existsSync(join(dir, "package.json"))) {
|
|
57
|
-
for (const name of ["dist", "build", "out"]) {
|
|
58
|
-
if (existsSync(join(dir, name, "index.html"))) {
|
|
59
|
-
deployDir = join(dir, name);
|
|
60
|
-
say(`Project root detected — deploying its build output ${name}/ instead.`);
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (deployDir === dir) {
|
|
65
|
-
consola.warn("This looks like an unbuilt project (package.json, no dist/build/out) — " +
|
|
66
|
-
"run the build first, or the deployed index.html will reference missing source files.");
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
// ---- Resolve project config ---------------------------------------------
|
|
70
|
-
let config = null;
|
|
71
|
-
try {
|
|
72
|
-
config = readProjectConfig(dir);
|
|
73
|
-
}
|
|
74
|
-
catch (err) {
|
|
75
|
-
consola.error(err instanceof Error ? err.message : String(err));
|
|
76
|
-
process.exit(1);
|
|
77
|
-
}
|
|
78
|
-
if (args.slug) {
|
|
79
|
-
config = { slug: args.slug, name: config?.name };
|
|
80
|
-
}
|
|
81
|
-
if (!config) {
|
|
82
|
-
if (!process.stdin.isTTY) {
|
|
83
|
-
consola.error("No slug specified and no vibedgames.json found.\n" +
|
|
84
|
-
" Pass --slug <name> when running without a TTY (e.g. in CI or from a coding agent).\n" +
|
|
85
|
-
" Example: vg deploy ./dist --slug my-game");
|
|
86
|
-
process.exit(1);
|
|
87
|
-
}
|
|
88
|
-
// isJsonString: consola.prompt is typed to resolve a string but can
|
|
89
|
-
// resolve a cancel sentinel at runtime, and a symbol would throw
|
|
90
|
-
// inside RegExp.test.
|
|
91
|
-
const slug = await consola.prompt("Slug (e.g. pong):", { type: "text" });
|
|
92
|
-
if (!isJsonString(slug) || !SLUG_RE.test(slug)) {
|
|
93
|
-
consola.error("Invalid slug. Use lowercase letters, digits, hyphens.");
|
|
94
|
-
process.exit(1);
|
|
95
|
-
}
|
|
96
|
-
const name = await consola.prompt("Name (optional):", { type: "text" });
|
|
97
|
-
config = {
|
|
98
|
-
slug,
|
|
99
|
-
name: isJsonString(name) && name.length > 0 ? name : undefined,
|
|
100
|
-
};
|
|
101
|
-
writeProjectConfig(dir, config);
|
|
102
|
-
say(`Wrote ${projectConfigPath(dir)}`);
|
|
103
|
-
}
|
|
104
|
-
if (!SLUG_RE.test(config.slug)) {
|
|
105
|
-
consola.error(`Invalid slug in vibedgames.json: ${config.slug}`);
|
|
106
|
-
process.exit(1);
|
|
107
|
-
}
|
|
166
|
+
const deployDir = resolveDeployDir(dir, say);
|
|
167
|
+
const config = await resolveDeployConfig(dir, args.slug, say);
|
|
108
168
|
// ---- Walk files ---------------------------------------------------------
|
|
109
169
|
const manifest = buildManifest(deployDir);
|
|
110
170
|
if (manifest.length === 0) {
|
|
@@ -119,112 +179,75 @@ export const deployCommand = defineCommand({
|
|
|
119
179
|
const totalBytes = manifest.reduce((acc, f) => acc + f.size, 0);
|
|
120
180
|
say(`Deploying ${config.slug}: ${manifest.length} files, ${formatBytes(totalBytes)}`);
|
|
121
181
|
// ---- Pack forkable source (opt-in: source is a publish, --source) -------
|
|
122
|
-
|
|
123
|
-
if (args.source) {
|
|
124
|
-
const root = findProjectRoot(dir);
|
|
125
|
-
if (!root) {
|
|
126
|
-
consola.warn("No vibedgames.json found above the deploy dir — skipping source upload.");
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
try {
|
|
130
|
-
sourceArchive = await packSource(root, join(tmpdir(), "vibedgames"));
|
|
131
|
-
say(`Source: ${sourceArchive.files.length} files, ${formatBytes(sourceArchive.bytes)} — forkable via \`vg fork ${config.slug}\``);
|
|
132
|
-
}
|
|
133
|
-
catch (err) {
|
|
134
|
-
consola.error(`Source archive failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
135
|
-
process.exit(1);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
182
|
+
const sourceArchive = args.source ? await packForkableSource(dir, config.slug, say) : null;
|
|
139
183
|
// ---- Create deployment --------------------------------------------------
|
|
140
184
|
const client = createClient();
|
|
141
185
|
const started = Date.now();
|
|
142
186
|
let created;
|
|
143
187
|
try {
|
|
144
188
|
created = await client.deploy.create({
|
|
145
|
-
slug: config.slug,
|
|
146
|
-
name: config.name,
|
|
147
189
|
files: manifest.map((f) => ({
|
|
190
|
+
contentType: f.contentType,
|
|
148
191
|
path: f.path,
|
|
149
|
-
size: f.size,
|
|
150
192
|
sha256: f.sha256,
|
|
151
|
-
|
|
193
|
+
size: f.size,
|
|
152
194
|
})),
|
|
195
|
+
name: config.name,
|
|
196
|
+
slug: config.slug,
|
|
153
197
|
source: sourceArchive
|
|
154
|
-
? {
|
|
198
|
+
? { bytes: sourceArchive.bytes, sha256: sourceArchive.sha256 }
|
|
155
199
|
: undefined,
|
|
156
200
|
});
|
|
157
201
|
}
|
|
158
|
-
catch (
|
|
159
|
-
if (sourceArchive)
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (sourceArchive) {
|
|
160
204
|
rmSync(sourceArchive.path, { force: true });
|
|
161
|
-
|
|
205
|
+
}
|
|
206
|
+
consola.error(error instanceof Error ? error.message : String(error));
|
|
162
207
|
process.exit(1);
|
|
163
208
|
}
|
|
164
209
|
// ---- Upload to R2 -------------------------------------------------------
|
|
165
|
-
if (!structured)
|
|
210
|
+
if (!structured) {
|
|
166
211
|
consola.start(`Uploading ${created.uploads.length} files`);
|
|
212
|
+
}
|
|
167
213
|
try {
|
|
168
214
|
await uploadAll({
|
|
169
215
|
files: manifest,
|
|
170
|
-
uploads: created.uploads,
|
|
171
216
|
onProgress: (done, total) => {
|
|
172
217
|
if (done === total || done % 10 === 0) {
|
|
173
218
|
say(`Uploaded ${done}/${total}`);
|
|
174
219
|
}
|
|
175
220
|
},
|
|
221
|
+
uploads: created.uploads,
|
|
176
222
|
});
|
|
177
223
|
}
|
|
178
|
-
catch (
|
|
179
|
-
if (sourceArchive)
|
|
224
|
+
catch (error) {
|
|
225
|
+
if (sourceArchive) {
|
|
180
226
|
rmSync(sourceArchive.path, { force: true });
|
|
181
|
-
|
|
227
|
+
}
|
|
228
|
+
consola.error(error instanceof Error ? error.message : String(error));
|
|
182
229
|
process.exit(1);
|
|
183
230
|
}
|
|
184
231
|
// ---- Upload source archive (best-effort companion to the bundle) -------
|
|
185
|
-
if (sourceArchive
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
method: "PUT",
|
|
189
|
-
headers: created.sourceUpload.headers,
|
|
190
|
-
body: readFileSync(sourceArchive.path),
|
|
191
|
-
});
|
|
192
|
-
if (!res.ok) {
|
|
193
|
-
throw new Error(`source upload failed: ${res.status} ${res.statusText}`);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
catch (err) {
|
|
197
|
-
rmSync(sourceArchive.path, { force: true });
|
|
198
|
-
consola.error(err instanceof Error ? err.message : String(err));
|
|
199
|
-
process.exit(1);
|
|
232
|
+
if (sourceArchive) {
|
|
233
|
+
if (created.sourceUpload) {
|
|
234
|
+
await uploadSourceArchive(sourceArchive, created.sourceUpload);
|
|
200
235
|
}
|
|
201
236
|
rmSync(sourceArchive.path, { force: true });
|
|
202
237
|
}
|
|
203
|
-
else if (sourceArchive) {
|
|
204
|
-
rmSync(sourceArchive.path, { force: true });
|
|
205
|
-
}
|
|
206
238
|
// ---- Finalize -----------------------------------------------------------
|
|
207
|
-
|
|
208
|
-
try {
|
|
209
|
-
finalized = await client.deploy.finalize({
|
|
210
|
-
deploymentId: created.deploymentId,
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
catch (err) {
|
|
214
|
-
consola.error(err instanceof Error ? err.message : String(err));
|
|
215
|
-
process.exit(1);
|
|
216
|
-
}
|
|
239
|
+
const finalized = await finalizeDeployment(client, created.deploymentId);
|
|
217
240
|
const elapsedMs = Date.now() - started;
|
|
218
241
|
if (writeStructured({
|
|
219
|
-
|
|
220
|
-
url: finalized.url,
|
|
242
|
+
bytes: totalBytes,
|
|
221
243
|
deployment_id: created.deploymentId,
|
|
244
|
+
elapsed_ms: elapsedMs,
|
|
222
245
|
files: manifest.length,
|
|
223
|
-
|
|
246
|
+
slug: config.slug,
|
|
224
247
|
source: sourceArchive
|
|
225
|
-
? {
|
|
248
|
+
? { bytes: sourceArchive.bytes, files: sourceArchive.files.length }
|
|
226
249
|
: null,
|
|
227
|
-
|
|
250
|
+
url: finalized.url,
|
|
228
251
|
}, args)) {
|
|
229
252
|
return;
|
|
230
253
|
}
|
|
@@ -232,10 +255,3 @@ export const deployCommand = defineCommand({
|
|
|
232
255
|
consola.log(` ${finalized.url}`);
|
|
233
256
|
},
|
|
234
257
|
});
|
|
235
|
-
function formatBytes(n) {
|
|
236
|
-
if (n < 1024)
|
|
237
|
-
return `${n} B`;
|
|
238
|
-
if (n < 1024 * 1024)
|
|
239
|
-
return `${(n / 1024).toFixed(1)} KB`;
|
|
240
|
-
return `${(n / (1024 * 1024)).toFixed(1)} MB`;
|
|
241
|
-
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/** Resolve (installing on first use) and exec the factory. Never returns. */
|
|
2
|
-
export declare
|
|
2
|
+
export declare const runFactory: (args: string[]) => never;
|
|
3
3
|
export declare const factoryCommand: import("citty").CommandDef<import("citty").ArgsDef>;
|
package/dist/commands/factory.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
|
-
import
|
|
3
|
+
import path from "node:path";
|
|
4
4
|
import { defineCommand } from "citty";
|
|
5
|
-
import consola from "consola";
|
|
5
|
+
import { consola } from "consola";
|
|
6
6
|
/**
|
|
7
7
|
* `vg factory` — the autonomous game factory, shipped as an optional plugin.
|
|
8
8
|
* The vg CLI carries none of it: the factory lives in a per-platform npm
|
|
@@ -14,31 +14,34 @@ import consola from "consola";
|
|
|
14
14
|
*/
|
|
15
15
|
const PKG = `@vibedgames/factory-${process.platform}-${process.arch}`;
|
|
16
16
|
const BIN = process.platform === "win32" ? "vg-factory.exe" : "vg-factory";
|
|
17
|
-
|
|
17
|
+
const npmGlobalRoot = () => {
|
|
18
18
|
try {
|
|
19
|
-
const res = spawnSync("npm", ["root", "-g"], { encoding: "
|
|
20
|
-
if (res.status !== 0 || res.error !== undefined)
|
|
19
|
+
const res = spawnSync("npm", ["root", "-g"], { encoding: "utf-8", timeout: 30_000 });
|
|
20
|
+
if (res.status !== 0 || res.error !== undefined) {
|
|
21
21
|
return null;
|
|
22
|
+
}
|
|
22
23
|
const root = res.stdout.trim();
|
|
23
24
|
return root || null;
|
|
24
25
|
}
|
|
25
26
|
catch {
|
|
26
27
|
return null;
|
|
27
28
|
}
|
|
28
|
-
}
|
|
29
|
+
};
|
|
29
30
|
/** The installed factory binary, or null. VG_FACTORY_BIN overrides (dev). */
|
|
30
|
-
|
|
31
|
+
const findBinary = () => {
|
|
31
32
|
const override = process.env.VG_FACTORY_BIN;
|
|
32
|
-
if (override)
|
|
33
|
+
if (override) {
|
|
33
34
|
return existsSync(override) ? override : null;
|
|
35
|
+
}
|
|
34
36
|
const root = npmGlobalRoot();
|
|
35
|
-
if (!root)
|
|
37
|
+
if (!root) {
|
|
36
38
|
return null;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
}
|
|
40
|
+
const bin = path.join(root, PKG, "bin", BIN);
|
|
41
|
+
return existsSync(bin) ? bin : null;
|
|
42
|
+
};
|
|
40
43
|
/** Resolve (installing on first use) and exec the factory. Never returns. */
|
|
41
|
-
export
|
|
44
|
+
export const runFactory = (args) => {
|
|
42
45
|
let bin = findBinary();
|
|
43
46
|
if (!bin) {
|
|
44
47
|
consola.start(`Installing the factory (${PKG})…`);
|
|
@@ -56,11 +59,11 @@ export function runFactory(args) {
|
|
|
56
59
|
}
|
|
57
60
|
const result = spawnSync(bin, args, { stdio: "inherit" });
|
|
58
61
|
process.exit(result.status ?? 1);
|
|
59
|
-
}
|
|
62
|
+
};
|
|
60
63
|
export const factoryCommand = defineCommand({
|
|
61
64
|
meta: {
|
|
62
|
-
name: "factory",
|
|
63
65
|
description: "Run the vibedgames factory — an autonomous agent that builds a browser game and evolves it like a studio (optional plugin; installs on first use). All arguments are passed through: `vg factory` opens the dashboard, `vg factory start <slug>` resumes a game.",
|
|
66
|
+
name: "factory",
|
|
64
67
|
},
|
|
65
68
|
run: ({ rawArgs }) => {
|
|
66
69
|
// Normally unreachable (index.ts routes `vg factory` before citty), but
|
package/dist/commands/fork.d.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
export declare const forkCommand: import("citty").CommandDef<{
|
|
2
|
+
readonly field: {
|
|
3
|
+
readonly description: "Print a single value from the JSON result, e.g. --field request_id or --field images[0].url. Prints scalars bare for shell capture.";
|
|
4
|
+
readonly type: "string";
|
|
5
|
+
};
|
|
2
6
|
readonly json: {
|
|
3
|
-
readonly type: "boolean";
|
|
4
7
|
readonly description: "Print structured JSON to stdout.";
|
|
8
|
+
readonly type: "boolean";
|
|
5
9
|
};
|
|
6
|
-
readonly
|
|
10
|
+
readonly force: {
|
|
11
|
+
readonly default: false;
|
|
12
|
+
readonly description: "Overwrite the target directory if it exists.";
|
|
13
|
+
readonly type: "boolean";
|
|
14
|
+
};
|
|
15
|
+
readonly name: {
|
|
16
|
+
readonly description: "Display name for the fork (defaults to the target slug).";
|
|
7
17
|
readonly type: "string";
|
|
8
|
-
readonly description: "Print a single value from the JSON result, e.g. --field request_id or --field images[0].url. Prints scalars bare for shell capture.";
|
|
9
18
|
};
|
|
10
19
|
readonly slug: {
|
|
11
|
-
readonly type: "positional";
|
|
12
20
|
readonly description: "Slug of the project to fork (e.g. bomberman).";
|
|
13
21
|
readonly required: true;
|
|
22
|
+
readonly type: "positional";
|
|
14
23
|
};
|
|
15
24
|
readonly target: {
|
|
16
|
-
readonly type: "positional";
|
|
17
25
|
readonly description: "New slug + directory for the fork. Defaults to <slug>-fork.";
|
|
18
26
|
readonly required: false;
|
|
19
|
-
|
|
20
|
-
readonly name: {
|
|
21
|
-
readonly type: "string";
|
|
22
|
-
readonly description: "Display name for the fork (defaults to the target slug).";
|
|
23
|
-
};
|
|
24
|
-
readonly force: {
|
|
25
|
-
readonly type: "boolean";
|
|
26
|
-
readonly description: "Overwrite the target directory if it exists.";
|
|
27
|
-
readonly default: false;
|
|
27
|
+
readonly type: "positional";
|
|
28
28
|
};
|
|
29
29
|
}>;
|
package/dist/commands/fork.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
|
-
import
|
|
3
|
+
import path from "node:path";
|
|
4
4
|
import { defineCommand } from "citty";
|
|
5
|
-
import consola from "consola";
|
|
5
|
+
import { consola } from "consola";
|
|
6
6
|
import { createClient } from "../lib/api.js";
|
|
7
7
|
import { extractSource } from "../lib/archive.js";
|
|
8
8
|
import { SLUG_RE } from "../lib/config-file.js";
|
|
@@ -10,33 +10,53 @@ import { isJsonOutput, outputArgs, writeStructured } from "../lib/output.js";
|
|
|
10
10
|
import { assertKnownFlags } from "../lib/strict-args.js";
|
|
11
11
|
import { isJsonObject } from "../lib/types.js";
|
|
12
12
|
const forkArgs = {
|
|
13
|
+
force: {
|
|
14
|
+
default: false,
|
|
15
|
+
description: "Overwrite the target directory if it exists.",
|
|
16
|
+
type: "boolean",
|
|
17
|
+
},
|
|
18
|
+
name: {
|
|
19
|
+
description: "Display name for the fork (defaults to the target slug).",
|
|
20
|
+
type: "string",
|
|
21
|
+
},
|
|
13
22
|
slug: {
|
|
14
|
-
type: "positional",
|
|
15
23
|
description: "Slug of the project to fork (e.g. bomberman).",
|
|
16
24
|
required: true,
|
|
25
|
+
type: "positional",
|
|
17
26
|
},
|
|
18
27
|
target: {
|
|
19
|
-
type: "positional",
|
|
20
28
|
description: "New slug + directory for the fork. Defaults to <slug>-fork.",
|
|
21
29
|
required: false,
|
|
22
|
-
|
|
23
|
-
name: {
|
|
24
|
-
type: "string",
|
|
25
|
-
description: "Display name for the fork (defaults to the target slug).",
|
|
26
|
-
},
|
|
27
|
-
force: {
|
|
28
|
-
type: "boolean",
|
|
29
|
-
description: "Overwrite the target directory if it exists.",
|
|
30
|
-
default: false,
|
|
30
|
+
type: "positional",
|
|
31
31
|
},
|
|
32
32
|
...outputArgs,
|
|
33
33
|
};
|
|
34
|
+
const rewritePackageName = (dir, slug) => {
|
|
35
|
+
const pkgPath = path.resolve(dir, "package.json");
|
|
36
|
+
if (!existsSync(pkgPath)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
41
|
+
if (!isJsonObject(pkg)) {
|
|
42
|
+
throw new Error("package.json is not a JSON object");
|
|
43
|
+
}
|
|
44
|
+
pkg.name = slug;
|
|
45
|
+
delete pkg.repository;
|
|
46
|
+
delete pkg.bugs;
|
|
47
|
+
delete pkg.homepage;
|
|
48
|
+
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
consola.warn(`Could not rewrite package.json name: ${error instanceof Error ? error.message : String(error)}`);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
34
54
|
export const forkCommand = defineCommand({
|
|
55
|
+
args: forkArgs,
|
|
35
56
|
meta: {
|
|
36
|
-
name: "fork",
|
|
37
57
|
description: "Fork another project's source so you can build on it. Downloads the source a project shipped with `vg deploy`, extracts it locally, and rewrites it to a new slug.",
|
|
58
|
+
name: "fork",
|
|
38
59
|
},
|
|
39
|
-
args: forkArgs,
|
|
40
60
|
run: async ({ args, rawArgs }) => {
|
|
41
61
|
assertKnownFlags(rawArgs, forkArgs);
|
|
42
62
|
const source = args.slug.trim().toLowerCase();
|
|
@@ -45,7 +65,7 @@ export const forkCommand = defineCommand({
|
|
|
45
65
|
consola.error(`Invalid target slug "${target}". Use lowercase letters, digits, and hyphens.`);
|
|
46
66
|
process.exit(1);
|
|
47
67
|
}
|
|
48
|
-
const dir = resolve(process.cwd(), target);
|
|
68
|
+
const dir = path.resolve(process.cwd(), target);
|
|
49
69
|
if (existsSync(dir)) {
|
|
50
70
|
if (!args.force) {
|
|
51
71
|
consola.error(`Directory ${dir} already exists. Pass --force to overwrite, or pick another target.`);
|
|
@@ -53,42 +73,45 @@ export const forkCommand = defineCommand({
|
|
|
53
73
|
}
|
|
54
74
|
// --force means replace, not merge — clear stale files first so the fork
|
|
55
75
|
// isn't a dirty mix of the archive and whatever was already there.
|
|
56
|
-
rmSync(dir, {
|
|
76
|
+
rmSync(dir, { force: true, recursive: true });
|
|
57
77
|
}
|
|
58
78
|
const client = createClient();
|
|
59
79
|
// ---- Resolve + download source -----------------------------------------
|
|
60
|
-
if (!isJsonOutput(args) && !args.field)
|
|
80
|
+
if (!isJsonOutput(args) && !args.field) {
|
|
61
81
|
consola.start(`Forking ${source}`);
|
|
82
|
+
}
|
|
62
83
|
let src;
|
|
63
84
|
try {
|
|
64
85
|
src = await client.deploy.getSource({ slug: source });
|
|
65
86
|
}
|
|
66
|
-
catch (
|
|
67
|
-
consola.error(
|
|
87
|
+
catch (error) {
|
|
88
|
+
consola.error(error instanceof Error ? error.message : String(error));
|
|
68
89
|
process.exit(1);
|
|
69
90
|
}
|
|
70
|
-
const tmp = join(tmpdir(), "vibedgames", `fork-${process.pid}.tgz`);
|
|
71
|
-
mkdirSync(join(tmpdir(), "vibedgames"), { recursive: true });
|
|
91
|
+
const tmp = path.join(tmpdir(), "vibedgames", `fork-${process.pid}.tgz`);
|
|
92
|
+
mkdirSync(path.join(tmpdir(), "vibedgames"), { recursive: true });
|
|
72
93
|
try {
|
|
73
94
|
const res = await fetch(src.url);
|
|
74
|
-
if (!res.ok)
|
|
95
|
+
if (!res.ok) {
|
|
75
96
|
throw new Error(`download failed: ${res.status} ${res.statusText}`);
|
|
97
|
+
}
|
|
76
98
|
writeFileSync(tmp, Buffer.from(await res.arrayBuffer()));
|
|
77
99
|
await extractSource(tmp, dir);
|
|
78
100
|
}
|
|
79
|
-
catch (
|
|
80
|
-
consola.error(
|
|
101
|
+
catch (error) {
|
|
102
|
+
consola.error(error instanceof Error ? error.message : String(error));
|
|
81
103
|
process.exit(1);
|
|
82
104
|
}
|
|
83
105
|
finally {
|
|
84
106
|
rmSync(tmp, { force: true });
|
|
85
107
|
}
|
|
86
108
|
// ---- Re-slug the fork (clean slate — no provenance recorded) ------------
|
|
87
|
-
const name = args.name ?? target.
|
|
88
|
-
writeFileSync(resolve(dir, "vibedgames.json"), `${JSON.stringify({ slug: target
|
|
109
|
+
const name = args.name ?? target.replaceAll("-", " ");
|
|
110
|
+
writeFileSync(path.resolve(dir, "vibedgames.json"), `${JSON.stringify({ name, slug: target }, null, 2)}\n`);
|
|
89
111
|
rewritePackageName(dir, target);
|
|
90
|
-
if (writeStructured({
|
|
112
|
+
if (writeStructured({ dir, forkedFrom: source, slug: target }, args)) {
|
|
91
113
|
return;
|
|
114
|
+
}
|
|
92
115
|
consola.success(`Forked ${source} → ${target}`);
|
|
93
116
|
consola.log(` ${dir}`);
|
|
94
117
|
consola.log("");
|
|
@@ -99,21 +122,3 @@ export const forkCommand = defineCommand({
|
|
|
99
122
|
consola.log(` # then: npm run build && vg deploy ./dist → ${target}.vibedgames.com`);
|
|
100
123
|
},
|
|
101
124
|
});
|
|
102
|
-
function rewritePackageName(dir, slug) {
|
|
103
|
-
const pkgPath = resolve(dir, "package.json");
|
|
104
|
-
if (!existsSync(pkgPath))
|
|
105
|
-
return;
|
|
106
|
-
try {
|
|
107
|
-
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
108
|
-
if (!isJsonObject(pkg))
|
|
109
|
-
throw new Error("package.json is not a JSON object");
|
|
110
|
-
pkg.name = slug;
|
|
111
|
-
delete pkg.repository;
|
|
112
|
-
delete pkg.bugs;
|
|
113
|
-
delete pkg.homepage;
|
|
114
|
-
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
115
|
-
}
|
|
116
|
-
catch (err) {
|
|
117
|
-
consola.warn(`Could not rewrite package.json name: ${err instanceof Error ? err.message : String(err)}`);
|
|
118
|
-
}
|
|
119
|
-
}
|