pcific 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/SKILL.md +127 -0
- package/dist/api/catalog.js +637 -0
- package/dist/api/run.js +960 -0
- package/dist/api/search.js +171 -0
- package/dist/api/shortcuts.js +799 -0
- package/dist/bin.js +648 -0
- package/dist/commands/auth.js +513 -0
- package/dist/commands/doctor.js +410 -0
- package/dist/commands/init.js +306 -0
- package/dist/commands/sync.js +214 -0
- package/dist/detect.js +214 -0
- package/dist/env.js +339 -0
- package/dist/goLive.js +629 -0
- package/dist/keyLink.js +305 -0
- package/dist/mcp/server.js +206 -0
- package/dist/mcp/tools.js +689 -0
- package/dist/python/template.js +793 -0
- package/dist/schema/discover.js +324 -0
- package/dist/schema/helpers.js +396 -0
- package/dist/schema/prismaToJsonSchema.js +197 -0
- package/dist/schema/push.js +570 -0
- package/dist/schema/tsToJsonSchema.js +405 -0
- package/dist/schema/zodToJsonSchema.js +442 -0
- package/dist/templates/openapi.json +1 -0
- package/dist/templates/sdk/auth.ts +139 -0
- package/dist/templates/sdk/client.ts +1059 -0
- package/dist/templates/sdk/errors.ts +76 -0
- package/dist/templates/sdk/generated-types.ts +5746 -0
- package/dist/templates/sdk/http.ts +359 -0
- package/dist/templates/sdk/index.ts +84 -0
- package/dist/templates/sdk/json.ts +24 -0
- package/dist/templates/sdk/kit.ts +516 -0
- package/dist/templates/sdk/multipart.ts +99 -0
- package/dist/templates/sdk/sse.ts +131 -0
- package/dist/templates/sdk/verify.ts +766 -0
- package/dist/templates/sdk-js/auth.d.ts +20 -0
- package/dist/templates/sdk-js/auth.js +85 -0
- package/dist/templates/sdk-js/client.d.ts +324 -0
- package/dist/templates/sdk-js/client.js +607 -0
- package/dist/templates/sdk-js/errors.d.ts +51 -0
- package/dist/templates/sdk-js/errors.js +48 -0
- package/dist/templates/sdk-js/generated-types.d.ts +5736 -0
- package/dist/templates/sdk-js/generated-types.js +5 -0
- package/dist/templates/sdk-js/http.d.ts +50 -0
- package/dist/templates/sdk-js/http.js +263 -0
- package/dist/templates/sdk-js/index.d.ts +9 -0
- package/dist/templates/sdk-js/index.js +7 -0
- package/dist/templates/sdk-js/json.d.ts +11 -0
- package/dist/templates/sdk-js/json.js +16 -0
- package/dist/templates/sdk-js/kit.d.ts +220 -0
- package/dist/templates/sdk-js/kit.js +208 -0
- package/dist/templates/sdk-js/multipart.d.ts +23 -0
- package/dist/templates/sdk-js/multipart.js +61 -0
- package/dist/templates/sdk-js/sse.d.ts +12 -0
- package/dist/templates/sdk-js/sse.js +99 -0
- package/dist/templates/sdk-js/verify.d.ts +114 -0
- package/dist/templates/sdk-js/verify.js +506 -0
- package/dist/templates.js +925 -0
- package/dist/ui.js +399 -0
- package/dist/webhooks/events.js +171 -0
- package/dist/webhooks/listen.js +772 -0
- package/package.json +32 -4
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { detectProject } from "../detect.js";
|
|
4
|
+
import { CliError, DEFAULT_BASE_URL, ensureEnvGitignored, normalizeBaseUrl, readEnvFile, readMergedProjectEnv, resolveCliConfig, writeEnvKeysAtomically, } from "../env.js";
|
|
5
|
+
import { collectKeyLink, openBrowser, startKeyLink, waitForKeyLink, } from "../keyLink.js";
|
|
6
|
+
import { redactSecrets } from "../ui.js";
|
|
7
|
+
const CLI_USER_AGENT = "pcific-cli/0.1.0";
|
|
8
|
+
function isJsonObject(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
function isString(value) {
|
|
12
|
+
return typeof value === "string";
|
|
13
|
+
}
|
|
14
|
+
function isNumber(value) {
|
|
15
|
+
return typeof value === "number";
|
|
16
|
+
}
|
|
17
|
+
function asNonEmptyString(value) {
|
|
18
|
+
return isString(value) && value.length > 0 ? value : undefined;
|
|
19
|
+
}
|
|
20
|
+
function asFiniteNumber(value) {
|
|
21
|
+
return isNumber(value) && Number.isFinite(value) ? value : undefined;
|
|
22
|
+
}
|
|
23
|
+
function parseJsonValue(rawText) {
|
|
24
|
+
if (!rawText)
|
|
25
|
+
return null;
|
|
26
|
+
try {
|
|
27
|
+
// SAFETY: JSON.parse output conforms to the recursive CliJsonValue data model.
|
|
28
|
+
return JSON.parse(rawText);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function isStreamTty(stream) {
|
|
35
|
+
return "isTTY" in stream && stream.isTTY === true;
|
|
36
|
+
}
|
|
37
|
+
function parseWhoamiBody(raw) {
|
|
38
|
+
if (!isJsonObject(raw)) {
|
|
39
|
+
return { scopes: [] };
|
|
40
|
+
}
|
|
41
|
+
const obj = raw;
|
|
42
|
+
const businessRaw = isJsonObject(obj.business) ? obj.business : undefined;
|
|
43
|
+
const marketplaceRaw = isJsonObject(obj.marketplace) ? obj.marketplace : null;
|
|
44
|
+
const dailyCapRaw = isJsonObject(obj.dailyCap) ? obj.dailyCap : null;
|
|
45
|
+
const rawScopes = Array.isArray(obj.scopes) ? obj.scopes : Array.isArray(obj.can) ? obj.can : [];
|
|
46
|
+
const scopes = [];
|
|
47
|
+
for (const item of rawScopes) {
|
|
48
|
+
if (isString(item) && item.length > 0) {
|
|
49
|
+
scopes.push(item);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
id: asNonEmptyString(obj.id),
|
|
54
|
+
name: asNonEmptyString(obj.name),
|
|
55
|
+
kind: asNonEmptyString(obj.kind),
|
|
56
|
+
prefix: asNonEmptyString(obj.prefix),
|
|
57
|
+
business: businessRaw
|
|
58
|
+
? { id: asNonEmptyString(businessRaw.id), name: asNonEmptyString(businessRaw.name) }
|
|
59
|
+
: undefined,
|
|
60
|
+
marketplace: marketplaceRaw
|
|
61
|
+
? { id: asNonEmptyString(marketplaceRaw.id), name: asNonEmptyString(marketplaceRaw.name) }
|
|
62
|
+
: null,
|
|
63
|
+
scopes,
|
|
64
|
+
expiresAt: obj.expiresAt === null ? null : asNonEmptyString(obj.expiresAt),
|
|
65
|
+
dailyCap: dailyCapRaw
|
|
66
|
+
? { limit: asFiniteNumber(dailyCapRaw.limit), usedToday: asFiniteNumber(dailyCapRaw.usedToday) }
|
|
67
|
+
: null,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function toJsonIdentity(identity) {
|
|
71
|
+
const obj = {
|
|
72
|
+
scopes: identity.scopes,
|
|
73
|
+
};
|
|
74
|
+
if (identity.id !== undefined)
|
|
75
|
+
obj.id = identity.id;
|
|
76
|
+
if (identity.name !== undefined)
|
|
77
|
+
obj.name = identity.name;
|
|
78
|
+
if (identity.kind !== undefined)
|
|
79
|
+
obj.kind = identity.kind;
|
|
80
|
+
if (identity.prefix !== undefined)
|
|
81
|
+
obj.prefix = identity.prefix;
|
|
82
|
+
if (identity.business) {
|
|
83
|
+
const biz = {};
|
|
84
|
+
if (identity.business.id !== undefined)
|
|
85
|
+
biz.id = identity.business.id;
|
|
86
|
+
if (identity.business.name !== undefined)
|
|
87
|
+
biz.name = identity.business.name;
|
|
88
|
+
obj.business = biz;
|
|
89
|
+
}
|
|
90
|
+
if (identity.marketplace === null) {
|
|
91
|
+
obj.marketplace = null;
|
|
92
|
+
}
|
|
93
|
+
else if (identity.marketplace) {
|
|
94
|
+
const mkt = {};
|
|
95
|
+
if (identity.marketplace.id !== undefined)
|
|
96
|
+
mkt.id = identity.marketplace.id;
|
|
97
|
+
if (identity.marketplace.name !== undefined)
|
|
98
|
+
mkt.name = identity.marketplace.name;
|
|
99
|
+
obj.marketplace = mkt;
|
|
100
|
+
}
|
|
101
|
+
if (identity.expiresAt !== undefined)
|
|
102
|
+
obj.expiresAt = identity.expiresAt;
|
|
103
|
+
if (identity.dailyCap === null) {
|
|
104
|
+
obj.dailyCap = null;
|
|
105
|
+
}
|
|
106
|
+
else if (identity.dailyCap) {
|
|
107
|
+
const cap = {};
|
|
108
|
+
if (identity.dailyCap.limit !== undefined)
|
|
109
|
+
cap.limit = identity.dailyCap.limit;
|
|
110
|
+
if (identity.dailyCap.usedToday !== undefined)
|
|
111
|
+
cap.usedToday = identity.dailyCap.usedToday;
|
|
112
|
+
obj.dailyCap = cap;
|
|
113
|
+
}
|
|
114
|
+
return obj;
|
|
115
|
+
}
|
|
116
|
+
export async function fetchKeyIdentity(options) {
|
|
117
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
118
|
+
let response;
|
|
119
|
+
try {
|
|
120
|
+
response = await fetchImpl(`${options.baseUrl}/api/v1/me`, {
|
|
121
|
+
method: "GET",
|
|
122
|
+
headers: {
|
|
123
|
+
accept: "application/json",
|
|
124
|
+
authorization: `Bearer ${options.testKey}`,
|
|
125
|
+
"user-agent": CLI_USER_AGENT,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
catch (failure) {
|
|
130
|
+
const message = failure instanceof Error ? failure.message : "Network error.";
|
|
131
|
+
return {
|
|
132
|
+
ok: false,
|
|
133
|
+
status: 0,
|
|
134
|
+
code: "NETWORK_ERROR",
|
|
135
|
+
error: redactSecrets(`Could not reach ${options.baseUrl}/api/v1/me: ${message}`),
|
|
136
|
+
tryThis: "Check your internet connection or --base-url, then try again.",
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const rawText = await response.text();
|
|
140
|
+
const parsed = parseJsonValue(rawText);
|
|
141
|
+
if (!response.ok) {
|
|
142
|
+
const body = isJsonObject(parsed) ? parsed : {};
|
|
143
|
+
const code = asNonEmptyString(body.code) ?? `HTTP_${response.status}`;
|
|
144
|
+
const error = redactSecrets(asNonEmptyString(body.error) ?? `PCIFIC returned HTTP ${response.status} from /api/v1/me.`);
|
|
145
|
+
const tryThis = redactSecrets(asNonEmptyString(body.tryThis) ??
|
|
146
|
+
(response.status === 401
|
|
147
|
+
? "Run npx pcific login --reauth to link a fresh test key into .env."
|
|
148
|
+
: "Run npx pcific doctor to inspect your local configuration."));
|
|
149
|
+
return { ok: false, status: response.status, code, error, tryThis };
|
|
150
|
+
}
|
|
151
|
+
return { ok: true, identity: parseWhoamiBody(parsed) };
|
|
152
|
+
}
|
|
153
|
+
function formatIdentitySummary(identity, configuredShopId) {
|
|
154
|
+
const lines = [];
|
|
155
|
+
if (identity.business?.name) {
|
|
156
|
+
const idPart = identity.business.id ? ` (${identity.business.id})` : "";
|
|
157
|
+
lines.push(`Business: ${identity.business.name}${idPart}`);
|
|
158
|
+
}
|
|
159
|
+
const keyLabel = [identity.name, identity.prefix ? `${identity.prefix}...` : undefined]
|
|
160
|
+
.filter(Boolean)
|
|
161
|
+
.join(" ");
|
|
162
|
+
if (keyLabel) {
|
|
163
|
+
lines.push(`Key: ${keyLabel}${identity.kind ? ` [${identity.kind}]` : ""}`);
|
|
164
|
+
}
|
|
165
|
+
if (identity.marketplace?.name || identity.marketplace?.id) {
|
|
166
|
+
const shopName = identity.marketplace.name ?? "Shop";
|
|
167
|
+
const shopId = identity.marketplace.id ? ` (${identity.marketplace.id})` : "";
|
|
168
|
+
lines.push(`Shop: ${shopName}${shopId}`);
|
|
169
|
+
}
|
|
170
|
+
else if (configuredShopId) {
|
|
171
|
+
lines.push(`Shop: All shops (default in .env: ${configuredShopId})`);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
lines.push("Shop: All shops (business-wide)");
|
|
175
|
+
}
|
|
176
|
+
if (identity.scopes.length > 0) {
|
|
177
|
+
lines.push(`Scopes: ${identity.scopes.join(", ")}`);
|
|
178
|
+
}
|
|
179
|
+
if (identity.dailyCap &&
|
|
180
|
+
identity.dailyCap.usedToday !== undefined &&
|
|
181
|
+
identity.dailyCap.limit !== undefined) {
|
|
182
|
+
lines.push(`Test cap: ${identity.dailyCap.usedToday}/${identity.dailyCap.limit} AI requests today`);
|
|
183
|
+
}
|
|
184
|
+
if (identity.expiresAt) {
|
|
185
|
+
lines.push(`Expires: ${identity.expiresAt}`);
|
|
186
|
+
}
|
|
187
|
+
return lines;
|
|
188
|
+
}
|
|
189
|
+
export async function runLogin(options) {
|
|
190
|
+
const { cwd, ui } = options;
|
|
191
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
192
|
+
const openBrowserFn = options.openBrowser ?? openBrowser;
|
|
193
|
+
ui.header("Login", "Connect this project to your PCIFIC Business account");
|
|
194
|
+
const config = resolveCliConfig(cwd, {
|
|
195
|
+
baseUrl: options.baseUrl,
|
|
196
|
+
env: options.env,
|
|
197
|
+
allowReplaceDotEnvLiveKey: Boolean(options.reauth),
|
|
198
|
+
});
|
|
199
|
+
const project = detectProject(cwd);
|
|
200
|
+
const gitignore = ensureEnvGitignored(cwd);
|
|
201
|
+
if (gitignore.status === "added") {
|
|
202
|
+
ui.ok("Added .env to .gitignore.");
|
|
203
|
+
}
|
|
204
|
+
else if (gitignore.status === "created") {
|
|
205
|
+
ui.ok("Created .gitignore with .env.");
|
|
206
|
+
}
|
|
207
|
+
if (config.testKey && !options.reauth) {
|
|
208
|
+
const checkSpin = ui.spinner("Checking existing PCIFIC_TEST_KEY");
|
|
209
|
+
const me = await fetchKeyIdentity({
|
|
210
|
+
baseUrl: config.baseUrl,
|
|
211
|
+
testKey: config.testKey,
|
|
212
|
+
fetch: fetchImpl,
|
|
213
|
+
});
|
|
214
|
+
const shopMatches = !options.shop || (me.ok && me.identity.marketplace?.name === options.shop);
|
|
215
|
+
if (me.ok && shopMatches) {
|
|
216
|
+
checkSpin.succeed(`Already logged in${me.identity.business?.name ? ` as ${me.identity.business.name}` : ""}.`);
|
|
217
|
+
if (ui.json) {
|
|
218
|
+
ui.emitJson({
|
|
219
|
+
command: "login",
|
|
220
|
+
ok: true,
|
|
221
|
+
status: "reused",
|
|
222
|
+
baseUrl: config.baseUrl,
|
|
223
|
+
marketplaceId: config.marketplaceId ?? null,
|
|
224
|
+
identity: toJsonIdentity(me.identity),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
const lines = formatIdentitySummary(me.identity, config.marketplaceId);
|
|
229
|
+
lines.push("", "Run npx pcific login --reauth to link a new test key or switch shops.");
|
|
230
|
+
ui.box("PCIFIC account", lines);
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
exitCode: 0,
|
|
234
|
+
status: "reused",
|
|
235
|
+
baseUrl: config.baseUrl,
|
|
236
|
+
marketplaceId: config.marketplaceId,
|
|
237
|
+
identity: me.identity,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
if (me.ok && options.shop) {
|
|
241
|
+
checkSpin.warn(`Existing PCIFIC_TEST_KEY is for ${me.identity.marketplace?.name ?? "all shops"}, not "${options.shop}"; starting a fresh login.`);
|
|
242
|
+
}
|
|
243
|
+
else if (!me.ok) {
|
|
244
|
+
checkSpin.warn(`Existing PCIFIC_TEST_KEY could not be verified (${me.error}); starting a fresh login.`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const startSpin = ui.spinner("Starting browser sign-in connection");
|
|
248
|
+
let started;
|
|
249
|
+
try {
|
|
250
|
+
started = await startKeyLink({
|
|
251
|
+
baseUrl: config.baseUrl,
|
|
252
|
+
suggestedName: project.projectName,
|
|
253
|
+
shop: options.shop,
|
|
254
|
+
retryCommand: "pcific login",
|
|
255
|
+
fetch: fetchImpl,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
catch (failure) {
|
|
259
|
+
startSpin.fail("Could not start browser sign-in connection.");
|
|
260
|
+
throw failure;
|
|
261
|
+
}
|
|
262
|
+
startSpin.succeed("Sign-in connection started.");
|
|
263
|
+
const boxLines = [
|
|
264
|
+
`Confirmation code: ${started.confirmationCode}`,
|
|
265
|
+
`Approval link: ${started.approvalUrl}`,
|
|
266
|
+
"",
|
|
267
|
+
"Open the link (sign in if asked), type the code, review permissions, and click Generate.",
|
|
268
|
+
];
|
|
269
|
+
if (started.shopNote) {
|
|
270
|
+
boxLines.push("", started.shopNote);
|
|
271
|
+
}
|
|
272
|
+
if (ui.json) {
|
|
273
|
+
const errStream = options.stderr ?? process.stderr;
|
|
274
|
+
errStream.write(`${boxLines.join("\n")}\n`);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
ui.box("Approve PCIFIC login in your browser", boxLines);
|
|
278
|
+
}
|
|
279
|
+
if (ui.interactive) {
|
|
280
|
+
const opened = await openBrowserFn(started.approvalUrl);
|
|
281
|
+
if (opened) {
|
|
282
|
+
ui.info("Opened approval link in your default browser.");
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const waitSpin = ui.spinner("Waiting for approval in browser");
|
|
286
|
+
let waitResult;
|
|
287
|
+
try {
|
|
288
|
+
waitResult = await waitForKeyLink({
|
|
289
|
+
baseUrl: config.baseUrl,
|
|
290
|
+
id: started.id,
|
|
291
|
+
pollToken: started.pollToken,
|
|
292
|
+
maxRounds: options.maxWaitRounds ?? 12,
|
|
293
|
+
waitSeconds: options.waitSeconds ?? 25,
|
|
294
|
+
retryCommand: "pcific login",
|
|
295
|
+
fetch: fetchImpl,
|
|
296
|
+
onTick: ({ round, maxRounds }) => {
|
|
297
|
+
waitSpin.update(`Waiting for approval (round ${round}/${maxRounds})`);
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
catch (failure) {
|
|
302
|
+
waitSpin.fail("Waiting for approval failed.");
|
|
303
|
+
throw failure;
|
|
304
|
+
}
|
|
305
|
+
if (waitResult.timedOut) {
|
|
306
|
+
waitSpin.fail("Timed out waiting for browser approval.");
|
|
307
|
+
throw new CliError("Timed out waiting for the key connection to be approved.", {
|
|
308
|
+
code: "KEY_LINK_TIMEOUT",
|
|
309
|
+
tryThis: "Run npx pcific login again and click Generate within 5 minutes.",
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
if (waitResult.status !== "ready") {
|
|
313
|
+
waitSpin.fail(`Sign-in ended with status "${waitResult.status}".`);
|
|
314
|
+
throw new CliError(`Key connection ended with status "${waitResult.status}".`, {
|
|
315
|
+
code: `KEY_LINK_${waitResult.status.toUpperCase()}`,
|
|
316
|
+
tryThis: "Run npx pcific login to start a fresh browser sign-in.",
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
waitSpin.succeed("Approved. Writing test key privately into .env.");
|
|
320
|
+
const collected = await collectKeyLink({
|
|
321
|
+
baseUrl: config.baseUrl,
|
|
322
|
+
id: started.id,
|
|
323
|
+
pollToken: started.pollToken,
|
|
324
|
+
cwd,
|
|
325
|
+
retryCommand: "pcific login",
|
|
326
|
+
fetch: fetchImpl,
|
|
327
|
+
});
|
|
328
|
+
// Always verify the newly collected .env key (not a shadowing .env.local or process.env key)
|
|
329
|
+
const freshDotEnv = readEnvFile(cwd, { includeLocal: false });
|
|
330
|
+
const newlyLinkedKey = freshDotEnv.PCIFIC_TEST_KEY;
|
|
331
|
+
const shadowedBy = [];
|
|
332
|
+
const mergedWithLocal = readEnvFile(cwd, { includeLocal: true });
|
|
333
|
+
const procEnv = options.env ?? process.env;
|
|
334
|
+
if (mergedWithLocal.PCIFIC_TEST_KEY && mergedWithLocal.PCIFIC_TEST_KEY !== newlyLinkedKey) {
|
|
335
|
+
shadowedBy.push(".env.local");
|
|
336
|
+
}
|
|
337
|
+
if (procEnv.PCIFIC_TEST_KEY && procEnv.PCIFIC_TEST_KEY.trim().length > 0) {
|
|
338
|
+
shadowedBy.push("process.env");
|
|
339
|
+
}
|
|
340
|
+
let identity;
|
|
341
|
+
if (newlyLinkedKey) {
|
|
342
|
+
const verified = await fetchKeyIdentity({
|
|
343
|
+
baseUrl: config.baseUrl,
|
|
344
|
+
testKey: newlyLinkedKey,
|
|
345
|
+
fetch: fetchImpl,
|
|
346
|
+
});
|
|
347
|
+
if (verified.ok) {
|
|
348
|
+
identity = verified.identity;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (ui.json) {
|
|
352
|
+
ui.emitJson({
|
|
353
|
+
command: "login",
|
|
354
|
+
ok: true,
|
|
355
|
+
status: "linked",
|
|
356
|
+
baseUrl: config.baseUrl,
|
|
357
|
+
envPath: ".env",
|
|
358
|
+
marketplaceId: collected.marketplaceId ?? null,
|
|
359
|
+
shadowedBy,
|
|
360
|
+
identity: identity ? toJsonIdentity(identity) : null,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
const lines = identity ? formatIdentitySummary(identity, collected.marketplaceId) : [];
|
|
365
|
+
lines.push("Saved: PCIFIC_TEST_KEY written privately into .env (mode 0600, never printed)");
|
|
366
|
+
if (shadowedBy.length > 0) {
|
|
367
|
+
lines.push(`Warning: PCIFIC_TEST_KEY is also set in ${shadowedBy.join(" and ")}, which overrides .env.`);
|
|
368
|
+
}
|
|
369
|
+
ui.box("Signed in to PCIFIC", lines);
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
exitCode: 0,
|
|
373
|
+
status: "linked",
|
|
374
|
+
baseUrl: config.baseUrl,
|
|
375
|
+
marketplaceId: collected.marketplaceId,
|
|
376
|
+
shadowedBy,
|
|
377
|
+
identity,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
export function runLogout(options) {
|
|
381
|
+
const { cwd, ui } = options;
|
|
382
|
+
const mergedBefore = readMergedProjectEnv(cwd, options.env);
|
|
383
|
+
const rawBaseUrl = options.baseUrl?.trim() || mergedBefore.PCIFIC_BASE_URL?.trim() || DEFAULT_BASE_URL;
|
|
384
|
+
const baseUrl = normalizeBaseUrl(rawBaseUrl);
|
|
385
|
+
const envPath = path.join(cwd, ".env");
|
|
386
|
+
const envFileExisted = existsSync(envPath);
|
|
387
|
+
const removedKeys = [];
|
|
388
|
+
if (envFileExisted) {
|
|
389
|
+
const existingDotEnv = readEnvFile(cwd, { includeLocal: false });
|
|
390
|
+
if (existingDotEnv.PCIFIC_TEST_KEY !== undefined) {
|
|
391
|
+
removedKeys.push("PCIFIC_TEST_KEY");
|
|
392
|
+
}
|
|
393
|
+
if (existingDotEnv.PCIFIC_MARKETPLACE_ID !== undefined) {
|
|
394
|
+
removedKeys.push("PCIFIC_MARKETPLACE_ID");
|
|
395
|
+
}
|
|
396
|
+
if (removedKeys.length > 0) {
|
|
397
|
+
writeEnvKeysAtomically(cwd, {
|
|
398
|
+
PCIFIC_TEST_KEY: null,
|
|
399
|
+
PCIFIC_MARKETPLACE_ID: null,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const remainingSources = [];
|
|
404
|
+
const mergedFilesAfter = readEnvFile(cwd, { includeLocal: true });
|
|
405
|
+
const procEnv = options.env ?? process.env;
|
|
406
|
+
if (mergedFilesAfter.PCIFIC_TEST_KEY) {
|
|
407
|
+
remainingSources.push(".env.local");
|
|
408
|
+
}
|
|
409
|
+
if (procEnv.PCIFIC_TEST_KEY && procEnv.PCIFIC_TEST_KEY.trim().length > 0) {
|
|
410
|
+
remainingSources.push("process.env");
|
|
411
|
+
}
|
|
412
|
+
if (ui.json) {
|
|
413
|
+
ui.emitJson({
|
|
414
|
+
command: "logout",
|
|
415
|
+
ok: true,
|
|
416
|
+
removedKeys,
|
|
417
|
+
envFileExisted,
|
|
418
|
+
remainingSources,
|
|
419
|
+
revokeUrl: `${baseUrl}/dashboard/keys`,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
ui.header("Logout", "Disconnect local PCIFIC test credentials");
|
|
424
|
+
if (removedKeys.length > 0) {
|
|
425
|
+
ui.ok(`Removed ${removedKeys.join(" and ")} from .env.`);
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
ui.info("No PCIFIC_TEST_KEY or PCIFIC_MARKETPLACE_ID found in .env.");
|
|
429
|
+
}
|
|
430
|
+
if (remainingSources.length > 0) {
|
|
431
|
+
ui.warn(`PCIFIC_TEST_KEY is still set in ${remainingSources.join(" and ")}. Remove or unset it there to fully disconnect.`);
|
|
432
|
+
}
|
|
433
|
+
ui.info(`To revoke the key on PCIFIC too, open ${baseUrl}/dashboard/keys.`);
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
exitCode: 0,
|
|
437
|
+
removedKeys,
|
|
438
|
+
envFileExisted,
|
|
439
|
+
remainingSources,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
export async function runWhoami(options) {
|
|
443
|
+
const { cwd, ui } = options;
|
|
444
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
445
|
+
const stdout = options.stdout ?? process.stdout;
|
|
446
|
+
const isTty = isStreamTty(stdout);
|
|
447
|
+
const useJson = Boolean(options.explicitJson) || (!options.explicitPlain && !isTty);
|
|
448
|
+
const emitFailure = (code, message, tryThis) => {
|
|
449
|
+
const cleanMessage = redactSecrets(message);
|
|
450
|
+
const cleanTryThis = redactSecrets(tryThis);
|
|
451
|
+
if (useJson) {
|
|
452
|
+
stdout.write(`${JSON.stringify({ ok: false, code, error: cleanMessage, tryThis: cleanTryThis })}\n`);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
ui.error(cleanMessage, cleanTryThis);
|
|
456
|
+
}
|
|
457
|
+
return {
|
|
458
|
+
exitCode: 1,
|
|
459
|
+
authenticated: false,
|
|
460
|
+
baseUrl: DEFAULT_BASE_URL,
|
|
461
|
+
};
|
|
462
|
+
};
|
|
463
|
+
let config;
|
|
464
|
+
try {
|
|
465
|
+
config = resolveCliConfig(cwd, { baseUrl: options.baseUrl, env: options.env });
|
|
466
|
+
}
|
|
467
|
+
catch (failure) {
|
|
468
|
+
if (failure instanceof CliError) {
|
|
469
|
+
return emitFailure(failure.code, failure.message, failure.tryThis);
|
|
470
|
+
}
|
|
471
|
+
return emitFailure("CONFIG_ERROR", failure instanceof Error ? failure.message : "Invalid configuration.", "Check .env and run npx pcific doctor.");
|
|
472
|
+
}
|
|
473
|
+
if (!config.testKey) {
|
|
474
|
+
return emitFailure("NOT_LOGGED_IN", "Not signed in to PCIFIC in this project (no PCIFIC_TEST_KEY found).", "Run npx pcific login to connect a test key through your browser.");
|
|
475
|
+
}
|
|
476
|
+
const result = await fetchKeyIdentity({
|
|
477
|
+
baseUrl: config.baseUrl,
|
|
478
|
+
testKey: config.testKey,
|
|
479
|
+
fetch: fetchImpl,
|
|
480
|
+
});
|
|
481
|
+
if (!result.ok) {
|
|
482
|
+
return emitFailure(result.code, result.error, result.tryThis);
|
|
483
|
+
}
|
|
484
|
+
const payload = {
|
|
485
|
+
command: "whoami",
|
|
486
|
+
ok: true,
|
|
487
|
+
authenticated: true,
|
|
488
|
+
baseUrl: config.baseUrl,
|
|
489
|
+
keySource: config.testKeySource ?? null,
|
|
490
|
+
marketplaceId: config.marketplaceId ?? null,
|
|
491
|
+
identity: toJsonIdentity(result.identity),
|
|
492
|
+
};
|
|
493
|
+
if (useJson) {
|
|
494
|
+
const formatted = isTty && !options.explicitJson ? JSON.stringify(payload, null, 2) : JSON.stringify(payload);
|
|
495
|
+
stdout.write(`${redactSecrets(formatted)}\n`);
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
ui.header("Whoami", "Active PCIFIC account and key identity");
|
|
499
|
+
const lines = formatIdentitySummary(result.identity, config.marketplaceId);
|
|
500
|
+
if (config.testKeySource) {
|
|
501
|
+
lines.push(`Source: ${config.testKeySource}`);
|
|
502
|
+
}
|
|
503
|
+
ui.box("Signed in", lines);
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
exitCode: 0,
|
|
507
|
+
authenticated: true,
|
|
508
|
+
baseUrl: config.baseUrl,
|
|
509
|
+
keySource: config.testKeySource,
|
|
510
|
+
marketplaceId: config.marketplaceId,
|
|
511
|
+
identity: result.identity,
|
|
512
|
+
};
|
|
513
|
+
}
|