@fixprompt/cli 0.1.0 → 0.2.0
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/dist/cli.js +240 -4
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -86,12 +86,235 @@ function detectContext() {
|
|
|
86
86
|
return merged;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
// src/connect.ts
|
|
90
|
+
import { readFileSync, readdirSync, statSync } from "fs";
|
|
91
|
+
import { join } from "path";
|
|
92
|
+
import { spawnSync } from "child_process";
|
|
93
|
+
var DEFAULT_ENDPOINT = "https://geosloghub-production.up.railway.app";
|
|
94
|
+
var TOKEN_ENV_VAR_NAME = "FIXPROMPT_DEPLOY_TOKEN";
|
|
95
|
+
function fileExists(p) {
|
|
96
|
+
try {
|
|
97
|
+
return statSync(p).isFile();
|
|
98
|
+
} catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function dirExists(p) {
|
|
103
|
+
try {
|
|
104
|
+
return statSync(p).isDirectory();
|
|
105
|
+
} catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function detectProvider(cwd) {
|
|
110
|
+
if (fileExists(join(cwd, "app.json")) || fileExists(join(cwd, "app.config.js")) || fileExists(join(cwd, "app.config.ts")) || fileExists(join(cwd, "eas.json"))) {
|
|
111
|
+
return "eas";
|
|
112
|
+
}
|
|
113
|
+
if (fileExists(join(cwd, "vercel.json")) || fileExists(join(cwd, ".vercel", "project.json"))) {
|
|
114
|
+
return "vercel";
|
|
115
|
+
}
|
|
116
|
+
const wfDir = join(cwd, ".github", "workflows");
|
|
117
|
+
if (dirExists(wfDir)) {
|
|
118
|
+
try {
|
|
119
|
+
const files = readdirSync(wfDir);
|
|
120
|
+
if (files.some((f) => /\.ya?ml$/i.test(f))) return "github-actions";
|
|
121
|
+
} catch {
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
async function mintDeployToken(opts) {
|
|
127
|
+
const url = `${opts.endpoint.replace(/\/$/, "")}/admin/projects/${opts.projectId}/deploy-tokens`;
|
|
128
|
+
const res = await fetch(url, {
|
|
129
|
+
method: "POST",
|
|
130
|
+
headers: {
|
|
131
|
+
"x-internal-admin-token": opts.adminToken,
|
|
132
|
+
"Content-Type": "application/json"
|
|
133
|
+
},
|
|
134
|
+
body: JSON.stringify({ label: opts.label })
|
|
135
|
+
});
|
|
136
|
+
const text = await res.text();
|
|
137
|
+
if (!res.ok) {
|
|
138
|
+
throw new Error(`broker /admin/projects/${opts.projectId}/deploy-tokens responded ${res.status}: ${text.slice(0, 200)}`);
|
|
139
|
+
}
|
|
140
|
+
return JSON.parse(text);
|
|
141
|
+
}
|
|
142
|
+
function writeEas(token, env) {
|
|
143
|
+
console.log(`
|
|
144
|
+
\u2022 Writing ${TOKEN_ENV_VAR_NAME} to EAS env '${env}'\u2026`);
|
|
145
|
+
const useShell = process.platform === "win32";
|
|
146
|
+
const args = [
|
|
147
|
+
"eas-cli",
|
|
148
|
+
"env:create",
|
|
149
|
+
env,
|
|
150
|
+
"--name",
|
|
151
|
+
TOKEN_ENV_VAR_NAME,
|
|
152
|
+
"--value",
|
|
153
|
+
token,
|
|
154
|
+
"--type",
|
|
155
|
+
"plain",
|
|
156
|
+
"--visibility",
|
|
157
|
+
"plaintext",
|
|
158
|
+
"--non-interactive"
|
|
159
|
+
];
|
|
160
|
+
const r = spawnSync("npx", useShell ? args.map((a) => `"${a.replace(/"/g, '\\"')}"`) : args, {
|
|
161
|
+
stdio: "inherit",
|
|
162
|
+
shell: useShell
|
|
163
|
+
});
|
|
164
|
+
if (r.status !== 0) {
|
|
165
|
+
throw new Error(
|
|
166
|
+
`eas env:create exited ${r.status}. Common causes:
|
|
167
|
+
\u2022 Not logged in to EAS \u2014 run 'eas login'
|
|
168
|
+
\u2022 Wrong env name \u2014 pass --eas-env <production|preview|development>
|
|
169
|
+
\u2022 Variable already exists with a different value \u2014 delete it first or use the rotate flow.`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function writeGitHubActions(token, cwd) {
|
|
174
|
+
console.log(`
|
|
175
|
+
\u2022 Writing ${TOKEN_ENV_VAR_NAME} to GitHub Actions repo secrets\u2026`);
|
|
176
|
+
const useShell = process.platform === "win32";
|
|
177
|
+
const args = ["secret", "set", TOKEN_ENV_VAR_NAME, "--body", "-"];
|
|
178
|
+
const r = spawnSync("gh", useShell ? args.map((a) => `"${a.replace(/"/g, '\\"')}"`) : args, {
|
|
179
|
+
input: token,
|
|
180
|
+
cwd,
|
|
181
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
182
|
+
shell: useShell
|
|
183
|
+
});
|
|
184
|
+
if (r.status !== 0) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`gh secret set exited ${r.status}. Common causes:
|
|
187
|
+
\u2022 Not logged in to gh \u2014 run 'gh auth login'
|
|
188
|
+
\u2022 Wrong repo \u2014 run 'fixprompt connect' from the repo root, or set GH_REPO.`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async function writeVercel(token, target) {
|
|
193
|
+
const vt = process.env.VERCEL_TOKEN;
|
|
194
|
+
if (!vt) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
`Vercel provider needs VERCEL_TOKEN env. Generate one at
|
|
197
|
+
https://vercel.com/account/tokens
|
|
198
|
+
then re-run with that token exported.`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
let projectId = target.vercelProjectId;
|
|
202
|
+
if (!projectId) {
|
|
203
|
+
const pj = join(process.cwd(), ".vercel", "project.json");
|
|
204
|
+
if (!fileExists(pj)) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`Could not resolve Vercel project id. Either run from a 'vercel link'-ed
|
|
207
|
+
directory or pass --vercel-project-id <prj_\u2026>.`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
const parsed = JSON.parse(readFileSync(pj, "utf8"));
|
|
212
|
+
projectId = parsed.projectId;
|
|
213
|
+
} catch (e) {
|
|
214
|
+
throw new Error(`failed to read .vercel/project.json: ${e.message}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const targets = target.vercelTargets ?? ["production", "preview"];
|
|
218
|
+
console.log(`
|
|
219
|
+
\u2022 Writing ${TOKEN_ENV_VAR_NAME} to Vercel project ${projectId} (${targets.join(", ")})\u2026`);
|
|
220
|
+
const res = await fetch(`https://api.vercel.com/v10/projects/${projectId}/env`, {
|
|
221
|
+
method: "POST",
|
|
222
|
+
headers: {
|
|
223
|
+
Authorization: `Bearer ${vt}`,
|
|
224
|
+
"Content-Type": "application/json"
|
|
225
|
+
},
|
|
226
|
+
body: JSON.stringify({
|
|
227
|
+
key: TOKEN_ENV_VAR_NAME,
|
|
228
|
+
value: token,
|
|
229
|
+
type: "plain",
|
|
230
|
+
target: targets
|
|
231
|
+
})
|
|
232
|
+
});
|
|
233
|
+
const text = await res.text();
|
|
234
|
+
if (!res.ok) {
|
|
235
|
+
throw new Error(`Vercel /env responded ${res.status}: ${text.slice(0, 200)}`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
async function connect(args) {
|
|
239
|
+
const cwd = process.cwd();
|
|
240
|
+
const projectId = args.flags["project-id"] ?? process.env.FIXPROMPT_PROJECT_ID;
|
|
241
|
+
if (!projectId) {
|
|
242
|
+
console.error(
|
|
243
|
+
"Missing --project-id (or $FIXPROMPT_PROJECT_ID).\n Find it in the FixPrompt dashboard URL after picking your project."
|
|
244
|
+
);
|
|
245
|
+
process.exit(1);
|
|
246
|
+
}
|
|
247
|
+
const adminToken = args.flags["admin-token"] ?? process.env.FIXPROMPT_ADMIN_TOKEN ?? process.env.INTERNAL_ADMIN_TOKEN;
|
|
248
|
+
if (!adminToken) {
|
|
249
|
+
console.error(
|
|
250
|
+
"Missing --admin-token (or $FIXPROMPT_ADMIN_TOKEN / $INTERNAL_ADMIN_TOKEN).\n Per-user device-code auth lands in a later release; for now this is the\n broker-wide admin token (single-tenant). Treat it as a root secret."
|
|
251
|
+
);
|
|
252
|
+
process.exit(1);
|
|
253
|
+
}
|
|
254
|
+
const endpoint = (args.flags.endpoint ?? process.env.FIXPROMPT_ENDPOINT ?? DEFAULT_ENDPOINT).replace(/\/$/, "");
|
|
255
|
+
const explicit = args.flags.provider;
|
|
256
|
+
const provider = explicit ?? detectProvider(cwd);
|
|
257
|
+
if (!provider) {
|
|
258
|
+
console.error(
|
|
259
|
+
`Couldn't detect a provider from ${cwd}. Pass --provider <eas|vercel|github-actions>.
|
|
260
|
+
Detection looks for: app.json/eas.json (EAS), .vercel/project.json (Vercel),
|
|
261
|
+
.github/workflows/*.yml (GitHub Actions).`
|
|
262
|
+
);
|
|
263
|
+
process.exit(1);
|
|
264
|
+
}
|
|
265
|
+
const target = {
|
|
266
|
+
provider,
|
|
267
|
+
easEnv: args.flags["eas-env"] ?? "production",
|
|
268
|
+
vercelProjectId: args.flags["vercel-project-id"] ?? void 0,
|
|
269
|
+
vercelTargets: typeof args.flags["vercel-targets"] === "string" ? args.flags["vercel-targets"].split(",").map((t) => t.trim()) : void 0
|
|
270
|
+
};
|
|
271
|
+
const label = args.flags.label ?? `${provider}-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").slice(0, 19)}`;
|
|
272
|
+
console.log(`fixprompt connect`);
|
|
273
|
+
console.log(` provider : ${provider}`);
|
|
274
|
+
console.log(` project_id : ${projectId}`);
|
|
275
|
+
console.log(` endpoint : ${endpoint}`);
|
|
276
|
+
console.log(` label : ${label}`);
|
|
277
|
+
if (provider === "eas") console.log(` eas-env : ${target.easEnv}`);
|
|
278
|
+
console.log(`
|
|
279
|
+
\u2022 Minting deploy token via broker\u2026`);
|
|
280
|
+
const minted = await mintDeployToken({
|
|
281
|
+
endpoint,
|
|
282
|
+
adminToken,
|
|
283
|
+
projectId,
|
|
284
|
+
label
|
|
285
|
+
});
|
|
286
|
+
console.log(` \u2713 token_id ${minted.token_id} (value hidden \u2014 written to CI store only)`);
|
|
287
|
+
switch (provider) {
|
|
288
|
+
case "eas":
|
|
289
|
+
writeEas(minted.token, target.easEnv);
|
|
290
|
+
break;
|
|
291
|
+
case "github-actions":
|
|
292
|
+
writeGitHubActions(minted.token, cwd);
|
|
293
|
+
break;
|
|
294
|
+
case "vercel":
|
|
295
|
+
await writeVercel(minted.token, target);
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
console.log(`
|
|
299
|
+
\u2713 ${TOKEN_ENV_VAR_NAME} is now set on ${provider}.`);
|
|
300
|
+
console.log(` Next deploy can run with no shell paste:`);
|
|
301
|
+
if (provider === "eas") {
|
|
302
|
+
console.log(` npm run release:ota --branch=production --message="..."`);
|
|
303
|
+
} else if (provider === "github-actions") {
|
|
304
|
+
console.log(` Reference \${{ secrets.${TOKEN_ENV_VAR_NAME} }} in your workflow.`);
|
|
305
|
+
} else {
|
|
306
|
+
console.log(` The token is in your Vercel project env for the next deploy.`);
|
|
307
|
+
}
|
|
308
|
+
console.log(`
|
|
309
|
+
To revoke later: dashboard \u2192 Integrations \u2192 Deploy tokens \u2192 ${minted.token_id}.`);
|
|
310
|
+
}
|
|
311
|
+
|
|
89
312
|
// src/version.ts
|
|
90
313
|
var CLI_NAME = "@fixprompt/cli";
|
|
91
|
-
var CLI_VERSION = "0.
|
|
314
|
+
var CLI_VERSION = "0.2.0";
|
|
92
315
|
|
|
93
316
|
// src/cli.ts
|
|
94
|
-
var
|
|
317
|
+
var DEFAULT_ENDPOINT2 = "https://geosloghub-production.up.railway.app";
|
|
95
318
|
function parseArgs(argv) {
|
|
96
319
|
const flags = {};
|
|
97
320
|
const positional = [];
|
|
@@ -122,9 +345,19 @@ function helpText() {
|
|
|
122
345
|
return `${CLI_NAME} v${CLI_VERSION}
|
|
123
346
|
|
|
124
347
|
Usage:
|
|
348
|
+
fixprompt connect [options]
|
|
125
349
|
fixprompt deploy-start [options]
|
|
126
350
|
fixprompt help
|
|
127
351
|
|
|
352
|
+
connect options:
|
|
353
|
+
--project-id <uuid> FixPrompt project id (or $FIXPROMPT_PROJECT_ID)
|
|
354
|
+
--admin-token <fpa_\u2026> Broker admin token (or $FIXPROMPT_ADMIN_TOKEN)
|
|
355
|
+
--provider <name> eas | vercel | github-actions (auto-detected from cwd)
|
|
356
|
+
--eas-env <env> EAS env to write to (default: production)
|
|
357
|
+
--vercel-project-id <id> Override Vercel project id (default: .vercel/project.json)
|
|
358
|
+
--vercel-targets <list> Comma-separated (default: production,preview)
|
|
359
|
+
--label <text> Token label (default: <provider>-<timestamp>)
|
|
360
|
+
|
|
128
361
|
deploy-start auth (pick one):
|
|
129
362
|
--deploy-token <fpd_...> Deploy-only token from dashboard (recommended for CI)
|
|
130
363
|
(or $FIXPROMPT_DEPLOY_TOKEN env)
|
|
@@ -134,7 +367,7 @@ deploy-start auth (pick one):
|
|
|
134
367
|
deploy-start options:
|
|
135
368
|
--source <slug> LOGHUB_SOURCE (required only with --key; optional with --deploy-token)
|
|
136
369
|
(default: $LOGHUB_SOURCE or $FIXPROMPT_SOURCE env)
|
|
137
|
-
--endpoint <url> Broker URL (default: $FIXPROMPT_ENDPOINT or ${
|
|
370
|
+
--endpoint <url> Broker URL (default: $FIXPROMPT_ENDPOINT or ${DEFAULT_ENDPOINT2})
|
|
138
371
|
--status <state> in_progress | success | failed (default: in_progress)
|
|
139
372
|
--commit-sha <sha> Override auto-detected commit
|
|
140
373
|
--branch <name> Override auto-detected branch
|
|
@@ -161,7 +394,7 @@ function readKey(flags) {
|
|
|
161
394
|
return v || null;
|
|
162
395
|
}
|
|
163
396
|
function readEndpoint(flags) {
|
|
164
|
-
const v = flags.endpoint ?? process.env.FIXPROMPT_ENDPOINT ??
|
|
397
|
+
const v = flags.endpoint ?? process.env.FIXPROMPT_ENDPOINT ?? DEFAULT_ENDPOINT2;
|
|
165
398
|
return v.replace(/\/$/, "");
|
|
166
399
|
}
|
|
167
400
|
async function deployStart(args) {
|
|
@@ -231,6 +464,9 @@ async function deployStart(args) {
|
|
|
231
464
|
async function main() {
|
|
232
465
|
const args = parseArgs(process.argv.slice(2));
|
|
233
466
|
switch (args.command) {
|
|
467
|
+
case "connect":
|
|
468
|
+
await connect(args);
|
|
469
|
+
break;
|
|
234
470
|
case "deploy-start":
|
|
235
471
|
await deployStart(args);
|
|
236
472
|
break;
|
package/package.json
CHANGED