@fluid-app/fluid-cli 0.1.64 → 0.1.66
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 +68 -6
- package/dist/bin/fluid.mjs +822 -101
- package/dist/bin/fluid.mjs.map +1 -1
- package/dist/index.d.mts +73 -17
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +29 -281
- package/dist/index.mjs.map +1 -1
- package/dist/token-BUGqpr-U.mjs +2975 -0
- package/dist/token-BUGqpr-U.mjs.map +1 -0
- package/package.json +3 -1
- package/dist/token-6SEVoGMw.mjs +0 -604
- package/dist/token-6SEVoGMw.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluid-app/fluid-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.66",
|
|
4
4
|
"description": "Core CLI for Fluid Commerce — auth, config, and plugin system",
|
|
5
5
|
"bin": {
|
|
6
6
|
"fluid": "./dist/bin/fluid.mjs"
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@napi-rs/keyring": "^2.1.0",
|
|
26
27
|
"chalk": "^5.3.0",
|
|
27
28
|
"commander": "^12.0.0",
|
|
28
29
|
"ora": "^8.0.0",
|
|
@@ -30,6 +31,7 @@
|
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@fluid-app/api-client-core": "0.1.0",
|
|
34
|
+
"@fluid-app/auth": "0.1.0",
|
|
33
35
|
"@fluid-app/typescript-config": "0.0.0",
|
|
34
36
|
"@types/node": "24.10.12",
|
|
35
37
|
"@types/prompts": "^2.4.9",
|
package/dist/token-6SEVoGMw.mjs
DELETED
|
@@ -1,604 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { randomBytes } from "node:crypto";
|
|
4
|
-
import { homedir } from "node:os";
|
|
5
|
-
//#region src/catalog/annotations.ts
|
|
6
|
-
const ANNOTATION = Symbol.for("@fluid-app/fluid-cli.command-annotation");
|
|
7
|
-
/**
|
|
8
|
-
* Record the endpoints a command calls and whether it writes, so
|
|
9
|
-
* `fluid commands --json` can list them. Returns the command for chaining.
|
|
10
|
-
*/
|
|
11
|
-
function describeCommand(command, annotation) {
|
|
12
|
-
const target = command;
|
|
13
|
-
target[ANNOTATION] = {
|
|
14
|
-
...target[ANNOTATION],
|
|
15
|
-
...annotation
|
|
16
|
-
};
|
|
17
|
-
return command;
|
|
18
|
-
}
|
|
19
|
-
function readAnnotation(command) {
|
|
20
|
-
return command[ANNOTATION] ?? null;
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
|
-
//#region src/catalog/walk.ts
|
|
24
|
-
function isHidden(command) {
|
|
25
|
-
return command._hidden === true;
|
|
26
|
-
}
|
|
27
|
-
function isRunnable(command) {
|
|
28
|
-
return command._actionHandler != null;
|
|
29
|
-
}
|
|
30
|
-
function visibleSubcommands(command) {
|
|
31
|
-
return command.commands.filter((sub) => !isHidden(sub));
|
|
32
|
-
}
|
|
33
|
-
function visibleOptions(command) {
|
|
34
|
-
return command.options.filter((option) => !option.hidden);
|
|
35
|
-
}
|
|
36
|
-
function describeOption(option) {
|
|
37
|
-
return {
|
|
38
|
-
flags: option.flags,
|
|
39
|
-
long: option.long ?? null,
|
|
40
|
-
short: option.short ?? null,
|
|
41
|
-
description: option.description,
|
|
42
|
-
takesValue: option.required,
|
|
43
|
-
optionalValue: option.optional,
|
|
44
|
-
required: option.mandatory,
|
|
45
|
-
defaultValue: option.defaultValue
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
function describeArgument(argument) {
|
|
49
|
-
return {
|
|
50
|
-
name: argument.name(),
|
|
51
|
-
description: argument.description,
|
|
52
|
-
required: argument.required,
|
|
53
|
-
variadic: argument.variadic
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function inferWrites(command, declared) {
|
|
57
|
-
if (declared !== void 0) return declared;
|
|
58
|
-
return command.options.some((option) => option.long === "--yes") ? true : null;
|
|
59
|
-
}
|
|
60
|
-
function walkCommands(root) {
|
|
61
|
-
const entries = [];
|
|
62
|
-
function visit(command, path, ancestors) {
|
|
63
|
-
const annotation = readAnnotation(command);
|
|
64
|
-
const subcommands = visibleSubcommands(command);
|
|
65
|
-
entries.push({
|
|
66
|
-
command: path.join(" "),
|
|
67
|
-
path,
|
|
68
|
-
description: command.description(),
|
|
69
|
-
aliases: command.aliases(),
|
|
70
|
-
arguments: command.registeredArguments.map(describeArgument),
|
|
71
|
-
options: visibleOptions(command).map(describeOption),
|
|
72
|
-
inheritsOptionsFrom: ancestors.filter((ancestor) => visibleOptions(ancestor).some((option) => option.long !== "--version")).map((ancestor) => commandPath(ancestor).join(" ")),
|
|
73
|
-
subcommands: subcommands.map((sub) => sub.name()),
|
|
74
|
-
runnable: isRunnable(command),
|
|
75
|
-
writes: inferWrites(command, annotation?.writes),
|
|
76
|
-
endpoints: annotation?.endpoints ?? null
|
|
77
|
-
});
|
|
78
|
-
for (const sub of subcommands) visit(sub, [...path, sub.name()], [...ancestors, command]);
|
|
79
|
-
}
|
|
80
|
-
visit(root, [root.name()], []);
|
|
81
|
-
return entries;
|
|
82
|
-
}
|
|
83
|
-
function rootCommand(command) {
|
|
84
|
-
let root = command;
|
|
85
|
-
while (root.parent) root = root.parent;
|
|
86
|
-
return root;
|
|
87
|
-
}
|
|
88
|
-
function commandPath(command) {
|
|
89
|
-
const path = [];
|
|
90
|
-
for (let c = command; c; c = c.parent) path.unshift(c.name());
|
|
91
|
-
return path;
|
|
92
|
-
}
|
|
93
|
-
/** Keep the entries at or under `prefix`, matching names or aliases. */
|
|
94
|
-
function filterCatalog(entries, root, prefix) {
|
|
95
|
-
let target = root;
|
|
96
|
-
for (const word of prefix) {
|
|
97
|
-
const next = visibleSubcommands(target).find((sub) => sub.name() === word || sub.aliases().includes(word));
|
|
98
|
-
if (!next) return [];
|
|
99
|
-
target = next;
|
|
100
|
-
}
|
|
101
|
-
const base = commandPath(target);
|
|
102
|
-
return entries.filter((entry) => base.every((word, index) => entry.path[index] === word));
|
|
103
|
-
}
|
|
104
|
-
//#endregion
|
|
105
|
-
//#region src/utils/result.ts
|
|
106
|
-
function success(value) {
|
|
107
|
-
return {
|
|
108
|
-
success: true,
|
|
109
|
-
value
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
function failure(error) {
|
|
113
|
-
return {
|
|
114
|
-
success: false,
|
|
115
|
-
error
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
function isSuccess(result) {
|
|
119
|
-
return result.success === true;
|
|
120
|
-
}
|
|
121
|
-
function isFailure(result) {
|
|
122
|
-
return result.success === false;
|
|
123
|
-
}
|
|
124
|
-
function tryCatch(fn) {
|
|
125
|
-
try {
|
|
126
|
-
return success(fn());
|
|
127
|
-
} catch (error) {
|
|
128
|
-
return failure(error instanceof Error ? error : new Error(String(error)));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
async function tryCatchAsync(fn) {
|
|
132
|
-
try {
|
|
133
|
-
return success(await fn());
|
|
134
|
-
} catch (error) {
|
|
135
|
-
return failure(error instanceof Error ? error : new Error(String(error)));
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
function unwrap(result) {
|
|
139
|
-
if (isSuccess(result)) return result.value;
|
|
140
|
-
if (result.error instanceof Error) throw result.error;
|
|
141
|
-
throw new Error(typeof result.error === "object" && result.error !== null ? JSON.stringify(result.error) : String(result.error));
|
|
142
|
-
}
|
|
143
|
-
function unwrapOr(result, defaultValue) {
|
|
144
|
-
if (isSuccess(result)) return result.value;
|
|
145
|
-
return defaultValue;
|
|
146
|
-
}
|
|
147
|
-
function mapResult(result, fn) {
|
|
148
|
-
if (isSuccess(result)) return success(fn(result.value));
|
|
149
|
-
return result;
|
|
150
|
-
}
|
|
151
|
-
function mapError(result, fn) {
|
|
152
|
-
if (isFailure(result)) return failure(fn(result.error));
|
|
153
|
-
return result;
|
|
154
|
-
}
|
|
155
|
-
function isError(value) {
|
|
156
|
-
return value instanceof Error;
|
|
157
|
-
}
|
|
158
|
-
function isNodeError(value) {
|
|
159
|
-
return value instanceof Error && "code" in value;
|
|
160
|
-
}
|
|
161
|
-
function getErrorMessage(error) {
|
|
162
|
-
if (error instanceof Error) return error.message;
|
|
163
|
-
if (typeof error === "string") return error;
|
|
164
|
-
return String(error);
|
|
165
|
-
}
|
|
166
|
-
//#endregion
|
|
167
|
-
//#region src/auth/fluid-api.ts
|
|
168
|
-
const API_TIMEOUT_MS = 1e4;
|
|
169
|
-
function getFluidApiBase() {
|
|
170
|
-
return process.env["FLUID_API_BASE"] ?? "https://api.fluid.app";
|
|
171
|
-
}
|
|
172
|
-
function makeSignal() {
|
|
173
|
-
const controller = new AbortController();
|
|
174
|
-
const timer = setTimeout(() => controller.abort(), API_TIMEOUT_MS);
|
|
175
|
-
return {
|
|
176
|
-
signal: controller.signal,
|
|
177
|
-
cleanup: () => clearTimeout(timer)
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
const FLUID_API_ERROR = {
|
|
181
|
-
INVALID_TOKEN: {
|
|
182
|
-
code: "INVALID_TOKEN",
|
|
183
|
-
message: "Token is invalid or expired"
|
|
184
|
-
},
|
|
185
|
-
API_UNREACHABLE: {
|
|
186
|
-
code: "API_UNREACHABLE",
|
|
187
|
-
message: "Could not reach the Fluid API"
|
|
188
|
-
},
|
|
189
|
-
UUID_NOT_FOUND: {
|
|
190
|
-
code: "UUID_NOT_FOUND",
|
|
191
|
-
message: "Verification session not found"
|
|
192
|
-
},
|
|
193
|
-
CODE_EXPIRED: {
|
|
194
|
-
code: "CODE_EXPIRED",
|
|
195
|
-
message: "Verification code has expired — please request a new one"
|
|
196
|
-
},
|
|
197
|
-
INVALID_CODE: {
|
|
198
|
-
code: "INVALID_CODE",
|
|
199
|
-
message: "Invalid verification code"
|
|
200
|
-
},
|
|
201
|
-
COMPANY_NOT_FOUND: {
|
|
202
|
-
code: "COMPANY_NOT_FOUND",
|
|
203
|
-
message: "Company not found"
|
|
204
|
-
},
|
|
205
|
-
SWITCH_FAILED: {
|
|
206
|
-
code: "SWITCH_FAILED",
|
|
207
|
-
message: "Failed to switch company"
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
function createApiError(template, details) {
|
|
211
|
-
return {
|
|
212
|
-
code: template.code,
|
|
213
|
-
message: template.message,
|
|
214
|
-
details
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Validate a token against the Fluid API and return company info
|
|
219
|
-
*/
|
|
220
|
-
async function validateToken(token) {
|
|
221
|
-
const { signal, cleanup } = makeSignal();
|
|
222
|
-
try {
|
|
223
|
-
const response = await fetch(`${getFluidApiBase()}/api/company/v1/companies/me`, {
|
|
224
|
-
method: "GET",
|
|
225
|
-
headers: { Authorization: `Bearer ${token}` },
|
|
226
|
-
signal
|
|
227
|
-
});
|
|
228
|
-
if (response.ok) {
|
|
229
|
-
const name = (await response.json())?.data?.company?.name;
|
|
230
|
-
if (!name) return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, "Unexpected response shape from /companies/me"));
|
|
231
|
-
return success({ name });
|
|
232
|
-
}
|
|
233
|
-
if (response.status === 401 || response.status === 403) return failure(createApiError(FLUID_API_ERROR.INVALID_TOKEN, `HTTP ${response.status}: Check that your token is valid and non-expired.`));
|
|
234
|
-
const body = await response.text();
|
|
235
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, `HTTP ${response.status}: ${body}`));
|
|
236
|
-
} catch (err) {
|
|
237
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
238
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, message));
|
|
239
|
-
} finally {
|
|
240
|
-
cleanup();
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Send a multi-factor authentication code to the given email address.
|
|
245
|
-
* Always returns 201 from the API (anti-enumeration).
|
|
246
|
-
*/
|
|
247
|
-
async function sendMfa(email) {
|
|
248
|
-
const { signal, cleanup } = makeSignal();
|
|
249
|
-
try {
|
|
250
|
-
const response = await fetch(`${getFluidApiBase()}/api/authentication/send_mfa`, {
|
|
251
|
-
method: "POST",
|
|
252
|
-
headers: { "Content-Type": "application/json" },
|
|
253
|
-
body: JSON.stringify({ email }),
|
|
254
|
-
signal
|
|
255
|
-
});
|
|
256
|
-
if (response.status === 201) {
|
|
257
|
-
const data = await response.json();
|
|
258
|
-
const uuid = data.multi_factor_authentication?.uuid;
|
|
259
|
-
const expiresAt = data.multi_factor_authentication?.verification_code_expires_at;
|
|
260
|
-
if (!uuid || !expiresAt) return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, "Unexpected response shape from /send_mfa"));
|
|
261
|
-
return success({
|
|
262
|
-
uuid,
|
|
263
|
-
expiresAt
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
const body = await response.text();
|
|
267
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, `HTTP ${response.status}: ${body}`));
|
|
268
|
-
} catch (err) {
|
|
269
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
270
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, message));
|
|
271
|
-
} finally {
|
|
272
|
-
cleanup();
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Confirm a multi-factor authentication code and retrieve company JWTs.
|
|
277
|
-
*/
|
|
278
|
-
async function confirmMfa(uuid, code) {
|
|
279
|
-
const { signal, cleanup } = makeSignal();
|
|
280
|
-
try {
|
|
281
|
-
const response = await fetch(`${getFluidApiBase()}/api/authentication/confirm_mfa`, {
|
|
282
|
-
method: "POST",
|
|
283
|
-
headers: { "Content-Type": "application/json" },
|
|
284
|
-
body: JSON.stringify({
|
|
285
|
-
uuid,
|
|
286
|
-
multi_factor_authentication: { verification_code: code }
|
|
287
|
-
}),
|
|
288
|
-
signal
|
|
289
|
-
});
|
|
290
|
-
if (response.ok) {
|
|
291
|
-
const data = await response.json();
|
|
292
|
-
if (!data.authenticated) return failure(createApiError(FLUID_API_ERROR.INVALID_CODE, "Authentication was not confirmed by the server"));
|
|
293
|
-
const companies = Array.isArray(data.companies) ? data.companies : [];
|
|
294
|
-
return success({
|
|
295
|
-
authType: data.auth_type ?? "",
|
|
296
|
-
companies: companies.map((c) => ({
|
|
297
|
-
id: c.id,
|
|
298
|
-
name: c.name,
|
|
299
|
-
shopName: c.shop_name,
|
|
300
|
-
jwt: c.jwt
|
|
301
|
-
}))
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
if (response.status === 404) return failure(createApiError(FLUID_API_ERROR.UUID_NOT_FOUND));
|
|
305
|
-
if (response.status === 410) return failure(createApiError(FLUID_API_ERROR.CODE_EXPIRED));
|
|
306
|
-
if (response.status === 422) return failure(createApiError(FLUID_API_ERROR.INVALID_CODE));
|
|
307
|
-
const body = await response.text();
|
|
308
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, `HTTP ${response.status}: ${body}`));
|
|
309
|
-
} catch (err) {
|
|
310
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
311
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, message));
|
|
312
|
-
} finally {
|
|
313
|
-
cleanup();
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Fetch all companies the authenticated user has access to.
|
|
318
|
-
*/
|
|
319
|
-
async function fetchUserCompanies(token, baseUrl) {
|
|
320
|
-
const { signal, cleanup } = makeSignal();
|
|
321
|
-
const apiBase = baseUrl ?? getFluidApiBase();
|
|
322
|
-
try {
|
|
323
|
-
const response = await fetch(`${apiBase}/api/me`, {
|
|
324
|
-
method: "GET",
|
|
325
|
-
headers: { Authorization: `Bearer ${token}` },
|
|
326
|
-
signal
|
|
327
|
-
});
|
|
328
|
-
if (response.ok) {
|
|
329
|
-
const data = await response.json();
|
|
330
|
-
return success((Array.isArray(data.companies) ? data.companies : []).map((c) => ({
|
|
331
|
-
id: c.id,
|
|
332
|
-
name: c.name
|
|
333
|
-
})));
|
|
334
|
-
}
|
|
335
|
-
if (response.status === 401 || response.status === 403) return failure(createApiError(FLUID_API_ERROR.INVALID_TOKEN, `HTTP ${response.status}`));
|
|
336
|
-
const body = await response.text();
|
|
337
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, `HTTP ${response.status}: ${body}`));
|
|
338
|
-
} catch (err) {
|
|
339
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
340
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, message));
|
|
341
|
-
} finally {
|
|
342
|
-
cleanup();
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Switch to a different company and receive a new JWT.
|
|
347
|
-
*
|
|
348
|
-
* Response shape (from @fluid-app/auth SwitchCompanyResponse):
|
|
349
|
-
* { company: { id, name, fluid_shop, jwt }, meta: { request_id, timestamp } }
|
|
350
|
-
*/
|
|
351
|
-
async function switchCompany(token, companyId, baseUrl) {
|
|
352
|
-
const { signal, cleanup } = makeSignal();
|
|
353
|
-
const apiBase = baseUrl ?? getFluidApiBase();
|
|
354
|
-
try {
|
|
355
|
-
const response = await fetch(`${apiBase}/api/authentication/company/${companyId}/switch`, {
|
|
356
|
-
method: "PUT",
|
|
357
|
-
headers: {
|
|
358
|
-
Authorization: `Bearer ${token}`,
|
|
359
|
-
"Content-Type": "application/json"
|
|
360
|
-
},
|
|
361
|
-
signal
|
|
362
|
-
});
|
|
363
|
-
if (response.ok) {
|
|
364
|
-
const company = (await response.json()).company;
|
|
365
|
-
if (!company?.jwt) return failure(createApiError(FLUID_API_ERROR.SWITCH_FAILED, "Unexpected response shape from /switch"));
|
|
366
|
-
return success({
|
|
367
|
-
companyId: company.id,
|
|
368
|
-
companyName: company.name,
|
|
369
|
-
fluidShop: company.fluid_shop,
|
|
370
|
-
jwt: company.jwt
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
if (response.status === 404) return failure(createApiError(FLUID_API_ERROR.COMPANY_NOT_FOUND, `Company ${companyId} not found`));
|
|
374
|
-
if (response.status === 401 || response.status === 403) return failure(createApiError(FLUID_API_ERROR.INVALID_TOKEN, `HTTP ${response.status}`));
|
|
375
|
-
const body = await response.text();
|
|
376
|
-
return failure(createApiError(FLUID_API_ERROR.SWITCH_FAILED, `HTTP ${response.status}: ${body}`));
|
|
377
|
-
} catch (err) {
|
|
378
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
379
|
-
return failure(createApiError(FLUID_API_ERROR.API_UNREACHABLE, message));
|
|
380
|
-
} finally {
|
|
381
|
-
cleanup();
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
//#endregion
|
|
385
|
-
//#region src/config/types.ts
|
|
386
|
-
function createDefaultConfig() {
|
|
387
|
-
return {
|
|
388
|
-
activeProfile: null,
|
|
389
|
-
profiles: {},
|
|
390
|
-
plugins: {},
|
|
391
|
-
enabledPlugins: null
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
//#endregion
|
|
395
|
-
//#region src/config/paths.ts
|
|
396
|
-
/**
|
|
397
|
-
* XDG-compliant config directory resolution
|
|
398
|
-
*
|
|
399
|
-
* Priority:
|
|
400
|
-
* 1. FLUID_CONFIG_DIR env var (explicit override)
|
|
401
|
-
* 2. ~/.fluid/ on macOS (convention for CLI tools)
|
|
402
|
-
* 3. %APPDATA%/fluid/ on Windows
|
|
403
|
-
* 4. $XDG_CONFIG_HOME/fluid/ on Linux (XDG spec)
|
|
404
|
-
* 5. ~/.config/fluid/ fallback on Linux
|
|
405
|
-
*/
|
|
406
|
-
function getConfigDir() {
|
|
407
|
-
const envOverride = process.env["FLUID_CONFIG_DIR"];
|
|
408
|
-
if (envOverride) return envOverride;
|
|
409
|
-
if (process.platform === "darwin") return join(homedir(), ".fluid");
|
|
410
|
-
if (process.platform === "win32") return join(process.env["APPDATA"] ?? join(homedir(), "AppData", "Roaming"), "fluid");
|
|
411
|
-
const xdgConfig = process.env["XDG_CONFIG_HOME"];
|
|
412
|
-
if (xdgConfig) return join(xdgConfig, "fluid");
|
|
413
|
-
return join(homedir(), ".config", "fluid");
|
|
414
|
-
}
|
|
415
|
-
function getConfigFilePath() {
|
|
416
|
-
return join(getConfigDir(), "config.json");
|
|
417
|
-
}
|
|
418
|
-
//#endregion
|
|
419
|
-
//#region src/config/config.ts
|
|
420
|
-
/**
|
|
421
|
-
* Read/write config.json with atomic writes and safe defaults
|
|
422
|
-
*/
|
|
423
|
-
function readConfig() {
|
|
424
|
-
const configPath = getConfigFilePath();
|
|
425
|
-
if (!existsSync(configPath)) return createDefaultConfig();
|
|
426
|
-
const raw = readFileSync(configPath, "utf8");
|
|
427
|
-
let parsed;
|
|
428
|
-
try {
|
|
429
|
-
parsed = JSON.parse(raw);
|
|
430
|
-
} catch {
|
|
431
|
-
return createDefaultConfig();
|
|
432
|
-
}
|
|
433
|
-
return withDurableWorkspaceAliases({
|
|
434
|
-
...createDefaultConfig(),
|
|
435
|
-
...parsed
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
function writeConfig(config) {
|
|
439
|
-
const configPath = getConfigFilePath();
|
|
440
|
-
const dir = dirname(configPath);
|
|
441
|
-
mkdirSync(dir, {
|
|
442
|
-
recursive: true,
|
|
443
|
-
mode: 448
|
|
444
|
-
});
|
|
445
|
-
const tmpPath = join(dir, `.fluid-config-${randomBytes(6).toString("hex")}.tmp`);
|
|
446
|
-
try {
|
|
447
|
-
writeFileSync(tmpPath, JSON.stringify(withDurableWorkspaceAliases(config), null, 2) + "\n", {
|
|
448
|
-
encoding: "utf8",
|
|
449
|
-
mode: 384
|
|
450
|
-
});
|
|
451
|
-
renameSync(tmpPath, configPath);
|
|
452
|
-
} catch (err) {
|
|
453
|
-
try {
|
|
454
|
-
unlinkSync(tmpPath);
|
|
455
|
-
} catch {}
|
|
456
|
-
throw err;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
function workspaceSlug(value) {
|
|
460
|
-
return value.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
461
|
-
}
|
|
462
|
-
function storefrontSlug(value) {
|
|
463
|
-
return workspaceSlug((value.trim().replace(/^[a-z][a-z0-9+.-]*:\/\//i, "").split(/[/?#]/, 1)[0]?.replace(/:\d+$/, "") ?? "").replace(/\.fluid\.app$/i, ""));
|
|
464
|
-
}
|
|
465
|
-
/** Preserve rename history even when `fluid logout` clears the profiles that
|
|
466
|
-
* originally carried it. Derivation on read migrates existing config files;
|
|
467
|
-
* normalization on every write makes the migration durable before profiles
|
|
468
|
-
* are removed. */
|
|
469
|
-
function withDurableWorkspaceAliases(config) {
|
|
470
|
-
const aliases = {};
|
|
471
|
-
const add = (storefront, candidates) => {
|
|
472
|
-
const key = storefrontSlug(storefront);
|
|
473
|
-
if (!key) return;
|
|
474
|
-
const values = new Set(aliases[key] ?? []);
|
|
475
|
-
for (const candidate of candidates) {
|
|
476
|
-
const slug = workspaceSlug(candidate);
|
|
477
|
-
if (slug) values.add(slug);
|
|
478
|
-
}
|
|
479
|
-
if (values.size > 0) aliases[key] = [...values];
|
|
480
|
-
};
|
|
481
|
-
for (const [storefront, candidates] of Object.entries(config.workspaceAliasesByFluidShop ?? {})) {
|
|
482
|
-
if (!Array.isArray(candidates)) continue;
|
|
483
|
-
add(storefront, candidates.filter((candidate) => typeof candidate === "string"));
|
|
484
|
-
}
|
|
485
|
-
for (const profile of Object.values(config.profiles)) if (profile.fluidShop) add(profile.fluidShop, [profile.name, profile.companyName]);
|
|
486
|
-
return Object.keys(aliases).length > 0 || config.workspaceAliasesByFluidShop !== void 0 ? {
|
|
487
|
-
...config,
|
|
488
|
-
workspaceAliasesByFluidShop: aliases
|
|
489
|
-
} : config;
|
|
490
|
-
}
|
|
491
|
-
function updateConfig(updater) {
|
|
492
|
-
const updated = updater(readConfig());
|
|
493
|
-
writeConfig(updated);
|
|
494
|
-
return updated;
|
|
495
|
-
}
|
|
496
|
-
//#endregion
|
|
497
|
-
//#region src/config/project-config.ts
|
|
498
|
-
/**
|
|
499
|
-
* Project-level config (.fluidrc) reader.
|
|
500
|
-
*
|
|
501
|
-
* Walks upward from a starting directory to find a `.fluidrc` file.
|
|
502
|
-
* The file is JSON with a `profile` field that maps to a profile name
|
|
503
|
-
* in `~/.fluid/config.json`.
|
|
504
|
-
*
|
|
505
|
-
* This allows portal repos to pin themselves to a specific CLI profile
|
|
506
|
-
* so that `fluid portal pull` / `push` always use the correct company
|
|
507
|
-
* credentials, regardless of which profile is globally active.
|
|
508
|
-
*/
|
|
509
|
-
const RC_FILENAME = ".fluidrc";
|
|
510
|
-
/** Cached result keyed by the starting directory. */
|
|
511
|
-
let cachedResult;
|
|
512
|
-
/**
|
|
513
|
-
* Walk upward from `startDir` looking for a `.fluidrc` file.
|
|
514
|
-
*
|
|
515
|
-
* Returns the parsed config when found, or `null` if no valid `.fluidrc`
|
|
516
|
-
* exists in any ancestor directory.
|
|
517
|
-
*
|
|
518
|
-
* The result is cached per starting directory for the lifetime of the
|
|
519
|
-
* process — CLI commands are short-lived so stale-cache is not a concern.
|
|
520
|
-
*/
|
|
521
|
-
function findProjectConfig(startDir) {
|
|
522
|
-
if (cachedResult && cachedResult.dir === startDir) return cachedResult.config;
|
|
523
|
-
let dir = startDir;
|
|
524
|
-
while (true) {
|
|
525
|
-
const rcPath = join(dir, RC_FILENAME);
|
|
526
|
-
if (existsSync(rcPath)) {
|
|
527
|
-
try {
|
|
528
|
-
const raw = readFileSync(rcPath, "utf8");
|
|
529
|
-
const parsed = JSON.parse(raw);
|
|
530
|
-
if (typeof parsed.profile === "string" && parsed.profile.length > 0) {
|
|
531
|
-
const config = { profile: parsed.profile };
|
|
532
|
-
cachedResult = {
|
|
533
|
-
dir: startDir,
|
|
534
|
-
config
|
|
535
|
-
};
|
|
536
|
-
return config;
|
|
537
|
-
}
|
|
538
|
-
} catch {}
|
|
539
|
-
cachedResult = {
|
|
540
|
-
dir: startDir,
|
|
541
|
-
config: null
|
|
542
|
-
};
|
|
543
|
-
return null;
|
|
544
|
-
}
|
|
545
|
-
const parent = dirname(dir);
|
|
546
|
-
if (parent === dir) break;
|
|
547
|
-
dir = parent;
|
|
548
|
-
}
|
|
549
|
-
cachedResult = {
|
|
550
|
-
dir: startDir,
|
|
551
|
-
config: null
|
|
552
|
-
};
|
|
553
|
-
return null;
|
|
554
|
-
}
|
|
555
|
-
//#endregion
|
|
556
|
-
//#region src/auth/token.ts
|
|
557
|
-
/**
|
|
558
|
-
* Token storage and retrieval from config
|
|
559
|
-
*/
|
|
560
|
-
/**
|
|
561
|
-
* Get the auth token for a named profile, or the active profile when omitted.
|
|
562
|
-
*
|
|
563
|
-
* Resolution order (when `profileName` is omitted — mirrors `resolveToken`
|
|
564
|
-
* in domain/context.ts):
|
|
565
|
-
* 1. `FLUID_TOKEN` / `FLUID_API_TOKEN` environment variables
|
|
566
|
-
* 2. Project-level `.fluidrc` profile (walks upward from cwd)
|
|
567
|
-
* 3. Global active profile from `~/.fluid/config.json`
|
|
568
|
-
*
|
|
569
|
-
* When `profileName` is provided explicitly (e.g. via `--profile` flag),
|
|
570
|
-
* it takes precedence over the environment variables, `.fluidrc`, and the
|
|
571
|
-
* active profile.
|
|
572
|
-
*/
|
|
573
|
-
function getAuthToken(profileName) {
|
|
574
|
-
const config = readConfig();
|
|
575
|
-
if (profileName) return config.profiles[profileName]?.token ?? null;
|
|
576
|
-
const envToken = process.env["FLUID_TOKEN"] ?? process.env["FLUID_API_TOKEN"];
|
|
577
|
-
if (envToken) return envToken;
|
|
578
|
-
const projectConfig = findProjectConfig(process.cwd());
|
|
579
|
-
if (projectConfig?.profile) {
|
|
580
|
-
const profile = config.profiles[projectConfig.profile];
|
|
581
|
-
if (profile) return profile.token;
|
|
582
|
-
}
|
|
583
|
-
if (!config.activeProfile) return null;
|
|
584
|
-
return config.profiles[config.activeProfile]?.token ?? null;
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* Get the active profile, or null if not logged in
|
|
588
|
-
*/
|
|
589
|
-
function getActiveProfile() {
|
|
590
|
-
const config = readConfig();
|
|
591
|
-
if (!config.activeProfile) return null;
|
|
592
|
-
return config.profiles[config.activeProfile] ?? null;
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* List all stored profile names
|
|
596
|
-
*/
|
|
597
|
-
function listProfileNames() {
|
|
598
|
-
const config = readConfig();
|
|
599
|
-
return Object.keys(config.profiles);
|
|
600
|
-
}
|
|
601
|
-
//#endregion
|
|
602
|
-
export { unwrapOr as A, isSuccess as C, tryCatch as D, success as E, walkCommands as F, describeCommand as I, rootCommand as M, visibleOptions as N, tryCatchAsync as O, visibleSubcommands as P, isNodeError as S, mapResult as T, validateToken as _, readConfig as a, isError as b, getConfigDir as c, FLUID_API_ERROR as d, confirmMfa as f, switchCompany as g, sendMfa as h, findProjectConfig as i, filterCatalog as j, unwrap as k, getConfigFilePath as l, getFluidApiBase as m, getAuthToken as n, updateConfig as o, fetchUserCompanies as p, listProfileNames as r, writeConfig as s, getActiveProfile as t, createDefaultConfig as u, failure as v, mapError as w, isFailure as x, getErrorMessage as y };
|
|
603
|
-
|
|
604
|
-
//# sourceMappingURL=token-6SEVoGMw.mjs.map
|