@freeticket/cli 0.9.0 → 0.10.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/README.md +29 -12
- package/dist/index.js +1371 -622
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// src/index.ts
|
|
4
|
-
import { Command } from "commander";
|
|
5
|
-
|
|
6
3
|
// package.json
|
|
7
4
|
var package_default = {
|
|
8
5
|
name: "@freeticket/cli",
|
|
9
|
-
version: "0.
|
|
6
|
+
version: "0.10.0",
|
|
10
7
|
description: "Official FreeTicket CLI \u2014 consumes the B2B REST API for events, sales, tickets, memberships, venues, staff, and reports.",
|
|
11
8
|
keywords: [
|
|
12
9
|
"freeticket",
|
|
@@ -69,6 +66,91 @@ var package_default = {
|
|
|
69
66
|
}
|
|
70
67
|
};
|
|
71
68
|
|
|
69
|
+
// src/lib/banner.ts
|
|
70
|
+
import { Chalk } from "chalk";
|
|
71
|
+
var YELLOW = "#FFD500";
|
|
72
|
+
var c = new Chalk({ level: 3 });
|
|
73
|
+
var ART = String.raw`
|
|
74
|
+
___ ___ ___ ___ _____ ___ ___ _ _____ _____
|
|
75
|
+
| __| _ \ __| __|_ _|_ _/ __| |/ / __|_ _|
|
|
76
|
+
| _|| / _|| _| | | | | (__| ' <| _| | |
|
|
77
|
+
|_| |_|_\___|___| |_| |___\___|_|\_\___| |_|
|
|
78
|
+
`;
|
|
79
|
+
function banner() {
|
|
80
|
+
return [
|
|
81
|
+
c.hex(YELLOW)(ART),
|
|
82
|
+
c.hex(YELLOW)(" \u25CF ") + c.hex(YELLOW).bold("FreeTicket") + c.hex(YELLOW)(" \u2014 events, tickets, and sales from your terminal"),
|
|
83
|
+
""
|
|
84
|
+
].join("\n");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/lib/update-check.ts
|
|
88
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
89
|
+
import { homedir } from "os";
|
|
90
|
+
import { dirname, join } from "path";
|
|
91
|
+
import chalk from "chalk";
|
|
92
|
+
var CACHE_PATH = join(homedir(), ".freeticket", "update-check.json");
|
|
93
|
+
var REGISTRY = "https://registry.npmjs.org/@freeticket/cli/latest";
|
|
94
|
+
var ONE_DAY = 24 * 60 * 60 * 1e3;
|
|
95
|
+
var FETCH_TIMEOUT = 1500;
|
|
96
|
+
async function notifyUpdate(current) {
|
|
97
|
+
if (!process.stderr.isTTY || process.env.FT_NO_UPDATE_CHECK || process.env.CI) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const latest = await getLatest();
|
|
102
|
+
if (latest && isNewer(latest, current)) {
|
|
103
|
+
process.stderr.write(
|
|
104
|
+
`
|
|
105
|
+
${chalk.yellow(`\u26A0 Update available: @freeticket/cli ${current} \u2192 ${latest}`)}
|
|
106
|
+
${chalk.dim(" npm i -g @freeticket/cli@latest \xB7 or use npx @freeticket/cli@latest")}
|
|
107
|
+
|
|
108
|
+
`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
} catch {
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async function getLatest() {
|
|
115
|
+
const cached = readCache();
|
|
116
|
+
if (cached && Date.now() - cached.at < ONE_DAY) return cached.latest;
|
|
117
|
+
const res = await fetch(REGISTRY, {
|
|
118
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT),
|
|
119
|
+
headers: { Accept: "application/vnd.npm.install-v1+json" }
|
|
120
|
+
});
|
|
121
|
+
if (!res.ok) throw new Error(`registry ${res.status}`);
|
|
122
|
+
const latest = (await res.json()).version ?? null;
|
|
123
|
+
if (latest) writeCache({ at: Date.now(), latest });
|
|
124
|
+
return latest;
|
|
125
|
+
}
|
|
126
|
+
function readCache() {
|
|
127
|
+
if (!existsSync(CACHE_PATH)) return null;
|
|
128
|
+
try {
|
|
129
|
+
return JSON.parse(readFileSync(CACHE_PATH, "utf8"));
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function writeCache(v) {
|
|
135
|
+
try {
|
|
136
|
+
mkdirSync(dirname(CACHE_PATH), { recursive: true });
|
|
137
|
+
writeFileSync(CACHE_PATH, JSON.stringify(v));
|
|
138
|
+
} catch {
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function isNewer(a, b) {
|
|
142
|
+
const parse = (v) => {
|
|
143
|
+
const [maj, min, pat] = (v.replace(/^v/, "").split("-")[0] ?? "").split(".").map((n) => Number.parseInt(n, 10) || 0);
|
|
144
|
+
return [maj ?? 0, min ?? 0, pat ?? 0];
|
|
145
|
+
};
|
|
146
|
+
const [aM, aN, aP] = parse(a);
|
|
147
|
+
const [bM, bN, bP] = parse(b);
|
|
148
|
+
return aM > bM || aM === bM && (aN > bN || aN === bN && aP > bP);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/program.ts
|
|
152
|
+
import { Command } from "commander";
|
|
153
|
+
|
|
72
154
|
// src/client/client.gen.ts
|
|
73
155
|
import { createClient, createConfig } from "@hey-api/client-fetch";
|
|
74
156
|
var client = createClient(createConfig({
|
|
@@ -1144,169 +1226,83 @@ var deleteApiKeysId = (options) => {
|
|
|
1144
1226
|
...options
|
|
1145
1227
|
});
|
|
1146
1228
|
};
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
import chalk2 from "chalk";
|
|
1150
|
-
|
|
1151
|
-
// src/admin-client/client.gen.ts
|
|
1152
|
-
import { createClient as createClient2, createConfig as createConfig2 } from "@hey-api/client-fetch";
|
|
1153
|
-
var client2 = createClient2(createConfig2({
|
|
1154
|
-
baseUrl: "https://admin.appfreeticket.com/api/admin"
|
|
1155
|
-
}));
|
|
1156
|
-
|
|
1157
|
-
// src/admin-client/sdk.gen.ts
|
|
1158
|
-
var getMe2 = (options) => {
|
|
1159
|
-
return (options?.client ?? client2).get({
|
|
1229
|
+
var getContentVideos = (options) => {
|
|
1230
|
+
return (options?.client ?? client).get({
|
|
1160
1231
|
security: [
|
|
1161
1232
|
{
|
|
1162
|
-
|
|
1163
|
-
name: "better-auth.session_token",
|
|
1233
|
+
name: "Authorization",
|
|
1164
1234
|
type: "apiKey"
|
|
1165
|
-
}
|
|
1166
|
-
],
|
|
1167
|
-
url: "/me",
|
|
1168
|
-
...options
|
|
1169
|
-
});
|
|
1170
|
-
};
|
|
1171
|
-
var getWorkspaces = (options) => {
|
|
1172
|
-
return (options?.client ?? client2).get({
|
|
1173
|
-
security: [
|
|
1235
|
+
},
|
|
1174
1236
|
{
|
|
1175
|
-
|
|
1176
|
-
name: "better-auth.session_token",
|
|
1237
|
+
name: "x-api-key",
|
|
1177
1238
|
type: "apiKey"
|
|
1178
1239
|
}
|
|
1179
1240
|
],
|
|
1180
|
-
url: "/
|
|
1241
|
+
url: "/content/videos",
|
|
1181
1242
|
...options
|
|
1182
1243
|
});
|
|
1183
1244
|
};
|
|
1184
|
-
var
|
|
1185
|
-
return (options
|
|
1245
|
+
var getContentPosts = (options) => {
|
|
1246
|
+
return (options?.client ?? client).get({
|
|
1186
1247
|
security: [
|
|
1187
1248
|
{
|
|
1188
|
-
|
|
1189
|
-
name: "better-auth.session_token",
|
|
1249
|
+
name: "Authorization",
|
|
1190
1250
|
type: "apiKey"
|
|
1191
|
-
}
|
|
1192
|
-
],
|
|
1193
|
-
url: "/workspaces",
|
|
1194
|
-
...options,
|
|
1195
|
-
headers: {
|
|
1196
|
-
"Content-Type": "application/json",
|
|
1197
|
-
...options?.headers
|
|
1198
|
-
}
|
|
1199
|
-
});
|
|
1200
|
-
};
|
|
1201
|
-
var getWorkspacesId = (options) => {
|
|
1202
|
-
return (options.client ?? client2).get({
|
|
1203
|
-
security: [
|
|
1251
|
+
},
|
|
1204
1252
|
{
|
|
1205
|
-
|
|
1206
|
-
name: "better-auth.session_token",
|
|
1253
|
+
name: "x-api-key",
|
|
1207
1254
|
type: "apiKey"
|
|
1208
1255
|
}
|
|
1209
1256
|
],
|
|
1210
|
-
url: "/
|
|
1257
|
+
url: "/content/posts",
|
|
1211
1258
|
...options
|
|
1212
1259
|
});
|
|
1213
1260
|
};
|
|
1214
|
-
var
|
|
1215
|
-
return (options
|
|
1216
|
-
security: [
|
|
1217
|
-
{
|
|
1218
|
-
in: "cookie",
|
|
1219
|
-
name: "better-auth.session_token",
|
|
1220
|
-
type: "apiKey"
|
|
1221
|
-
}
|
|
1222
|
-
],
|
|
1223
|
-
url: "/workspaces/{id}",
|
|
1224
|
-
...options,
|
|
1225
|
-
headers: {
|
|
1226
|
-
"Content-Type": "application/json",
|
|
1227
|
-
...options?.headers
|
|
1228
|
-
}
|
|
1229
|
-
});
|
|
1230
|
-
};
|
|
1231
|
-
var postWorkspacesIdPlan = (options) => {
|
|
1232
|
-
return (options.client ?? client2).post({
|
|
1261
|
+
var getContentLives = (options) => {
|
|
1262
|
+
return (options?.client ?? client).get({
|
|
1233
1263
|
security: [
|
|
1234
1264
|
{
|
|
1235
|
-
|
|
1236
|
-
name: "better-auth.session_token",
|
|
1265
|
+
name: "Authorization",
|
|
1237
1266
|
type: "apiKey"
|
|
1238
|
-
}
|
|
1239
|
-
],
|
|
1240
|
-
url: "/workspaces/{id}/plan",
|
|
1241
|
-
...options,
|
|
1242
|
-
headers: {
|
|
1243
|
-
"Content-Type": "application/json",
|
|
1244
|
-
...options?.headers
|
|
1245
|
-
}
|
|
1246
|
-
});
|
|
1247
|
-
};
|
|
1248
|
-
var postWorkspacesIdSuspend = (options) => {
|
|
1249
|
-
return (options.client ?? client2).post({
|
|
1250
|
-
security: [
|
|
1267
|
+
},
|
|
1251
1268
|
{
|
|
1252
|
-
|
|
1253
|
-
name: "better-auth.session_token",
|
|
1269
|
+
name: "x-api-key",
|
|
1254
1270
|
type: "apiKey"
|
|
1255
1271
|
}
|
|
1256
1272
|
],
|
|
1257
|
-
url: "/
|
|
1273
|
+
url: "/content/lives",
|
|
1258
1274
|
...options
|
|
1259
1275
|
});
|
|
1260
1276
|
};
|
|
1261
|
-
var
|
|
1262
|
-
return (options.client ??
|
|
1277
|
+
var getContentLivesId = (options) => {
|
|
1278
|
+
return (options.client ?? client).get({
|
|
1263
1279
|
security: [
|
|
1264
1280
|
{
|
|
1265
|
-
|
|
1266
|
-
name: "better-auth.session_token",
|
|
1281
|
+
name: "Authorization",
|
|
1267
1282
|
type: "apiKey"
|
|
1268
|
-
}
|
|
1269
|
-
],
|
|
1270
|
-
url: "/workspaces/{id}/restore",
|
|
1271
|
-
...options
|
|
1272
|
-
});
|
|
1273
|
-
};
|
|
1274
|
-
var getUsers = (options) => {
|
|
1275
|
-
return (options?.client ?? client2).get({
|
|
1276
|
-
security: [
|
|
1283
|
+
},
|
|
1277
1284
|
{
|
|
1278
|
-
|
|
1279
|
-
name: "better-auth.session_token",
|
|
1285
|
+
name: "x-api-key",
|
|
1280
1286
|
type: "apiKey"
|
|
1281
1287
|
}
|
|
1282
1288
|
],
|
|
1283
|
-
url: "/
|
|
1289
|
+
url: "/content/lives/{id}",
|
|
1284
1290
|
...options
|
|
1285
1291
|
});
|
|
1286
1292
|
};
|
|
1287
|
-
var
|
|
1288
|
-
return (options.client ??
|
|
1293
|
+
var postContentPlaybackToken = (options) => {
|
|
1294
|
+
return (options.client ?? client).post({
|
|
1289
1295
|
security: [
|
|
1290
1296
|
{
|
|
1291
|
-
|
|
1292
|
-
name: "better-auth.session_token",
|
|
1297
|
+
name: "Authorization",
|
|
1293
1298
|
type: "apiKey"
|
|
1294
|
-
}
|
|
1295
|
-
],
|
|
1296
|
-
url: "/users/{id}",
|
|
1297
|
-
...options
|
|
1298
|
-
});
|
|
1299
|
-
};
|
|
1300
|
-
var patchUsersId = (options) => {
|
|
1301
|
-
return (options.client ?? client2).patch({
|
|
1302
|
-
security: [
|
|
1299
|
+
},
|
|
1303
1300
|
{
|
|
1304
|
-
|
|
1305
|
-
name: "better-auth.session_token",
|
|
1301
|
+
name: "x-api-key",
|
|
1306
1302
|
type: "apiKey"
|
|
1307
1303
|
}
|
|
1308
1304
|
],
|
|
1309
|
-
url: "/
|
|
1305
|
+
url: "/content/playback-token",
|
|
1310
1306
|
...options,
|
|
1311
1307
|
headers: {
|
|
1312
1308
|
"Content-Type": "application/json",
|
|
@@ -1314,22 +1310,880 @@ var patchUsersId = (options) => {
|
|
|
1314
1310
|
}
|
|
1315
1311
|
});
|
|
1316
1312
|
};
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1313
|
+
|
|
1314
|
+
// src/commands/account.ts
|
|
1315
|
+
import chalk4 from "chalk";
|
|
1316
|
+
|
|
1317
|
+
// src/admin-client/client.gen.ts
|
|
1318
|
+
import { createClient as createClient2, createConfig as createConfig2 } from "@hey-api/client-fetch";
|
|
1319
|
+
var client2 = createClient2(createConfig2({
|
|
1320
|
+
baseUrl: "https://admin.appfreeticket.com/api/admin"
|
|
1321
|
+
}));
|
|
1322
|
+
|
|
1323
|
+
// src/lib/config.ts
|
|
1324
|
+
import {
|
|
1325
|
+
chmodSync,
|
|
1326
|
+
existsSync as existsSync2,
|
|
1327
|
+
mkdirSync as mkdirSync2,
|
|
1328
|
+
readFileSync as readFileSync2,
|
|
1329
|
+
renameSync,
|
|
1330
|
+
writeFileSync as writeFileSync2
|
|
1331
|
+
} from "fs";
|
|
1332
|
+
import { homedir as homedir2 } from "os";
|
|
1333
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
1334
|
+
var CONFIG_DIR = join2(homedir2(), ".freeticket");
|
|
1335
|
+
var CONFIG_PATH = join2(CONFIG_DIR, "config.json");
|
|
1336
|
+
var DEFAULT_API_URL = "https://admin.appfreeticket.com";
|
|
1337
|
+
var LEGACY_ACCOUNT = "default";
|
|
1338
|
+
var ENV_ACCOUNT = "FT_API_KEY";
|
|
1339
|
+
var ConfigError = class extends Error {
|
|
1340
|
+
};
|
|
1341
|
+
function readFile() {
|
|
1342
|
+
if (!existsSync2(CONFIG_PATH)) return {};
|
|
1343
|
+
let parsed;
|
|
1344
|
+
try {
|
|
1345
|
+
parsed = JSON.parse(readFileSync2(CONFIG_PATH, "utf8"));
|
|
1346
|
+
} catch (err) {
|
|
1347
|
+
throw new ConfigError(
|
|
1348
|
+
`${CONFIG_PATH} is not valid JSON (${err.message}). Fix it, or move it away and run \`ft login\` again.`
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
1352
|
+
throw new ConfigError(`${CONFIG_PATH} does not hold a JSON object.`);
|
|
1353
|
+
return migrate(parsed);
|
|
1354
|
+
}
|
|
1355
|
+
function migrate(file) {
|
|
1356
|
+
const { apiKey, ...rest } = file;
|
|
1357
|
+
if (!apiKey) return rest;
|
|
1358
|
+
const accounts = { ...rest.accounts };
|
|
1359
|
+
accounts[LEGACY_ACCOUNT] ??= {
|
|
1360
|
+
apiKey,
|
|
1361
|
+
...rest.workspaceId ? { workspaceId: rest.workspaceId } : {}
|
|
1362
|
+
};
|
|
1363
|
+
return { ...rest, accounts };
|
|
1364
|
+
}
|
|
1365
|
+
function loginTarget(file, email, accountFlag2) {
|
|
1366
|
+
if (accountFlag2) return { name: accountFlag2 };
|
|
1367
|
+
const accounts = file.accounts ?? {};
|
|
1368
|
+
const same = Object.entries(accounts).find(
|
|
1369
|
+
([, a]) => email !== void 0 && a.email === email
|
|
1370
|
+
);
|
|
1371
|
+
if (same) return { name: same[0] };
|
|
1372
|
+
const name = email ?? LEGACY_ACCOUNT;
|
|
1373
|
+
const names = Object.keys(accounts);
|
|
1374
|
+
if (names.length === 1 && names[0] === LEGACY_ACCOUNT && !accounts[LEGACY_ACCOUNT]?.email)
|
|
1375
|
+
return { name, replaces: LEGACY_ACCOUNT };
|
|
1376
|
+
return { name };
|
|
1377
|
+
}
|
|
1378
|
+
function resolveAccounts(file, env, accountFlag2) {
|
|
1379
|
+
const apiUrl = (a) => env.FT_API_URL ?? a?.apiUrl ?? file.apiUrl ?? DEFAULT_API_URL;
|
|
1380
|
+
if (env.FT_API_KEY) {
|
|
1381
|
+
if (accountFlag2)
|
|
1382
|
+
throw new Error(
|
|
1383
|
+
"FT_API_KEY overrides --account; unset it to use stored accounts."
|
|
1384
|
+
);
|
|
1385
|
+
return [
|
|
1320
1386
|
{
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1387
|
+
name: ENV_ACCOUNT,
|
|
1388
|
+
apiUrl: apiUrl(),
|
|
1389
|
+
apiKey: env.FT_API_KEY,
|
|
1390
|
+
// Same as before accounts existed: a workspace pinned in the file
|
|
1391
|
+
// still applies to the env key.
|
|
1392
|
+
workspaceId: env.FT_WORKSPACE_ID ?? file.workspaceId
|
|
1324
1393
|
}
|
|
1325
|
-
]
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1394
|
+
];
|
|
1395
|
+
}
|
|
1396
|
+
const accounts = file.accounts ?? {};
|
|
1397
|
+
const resolve = ([name, a]) => ({
|
|
1398
|
+
name,
|
|
1399
|
+
apiUrl: apiUrl(a),
|
|
1400
|
+
apiKey: a.apiKey,
|
|
1401
|
+
workspaceId: env.FT_WORKSPACE_ID ?? a.workspaceId
|
|
1402
|
+
});
|
|
1403
|
+
const pick2 = accountFlag2 ?? file.activeAccount;
|
|
1404
|
+
if (pick2 !== void 0) {
|
|
1405
|
+
const picked = accounts[pick2];
|
|
1406
|
+
if (!picked) {
|
|
1407
|
+
const names = Object.keys(accounts).join(", ");
|
|
1408
|
+
throw new Error(
|
|
1409
|
+
`No account named "${pick2}". Stored: ${names || "(none)"}`
|
|
1410
|
+
);
|
|
1331
1411
|
}
|
|
1332
|
-
|
|
1412
|
+
return [resolve([pick2, picked])];
|
|
1413
|
+
}
|
|
1414
|
+
return Object.entries(accounts).map(resolve);
|
|
1415
|
+
}
|
|
1416
|
+
function effectiveAccounts(accountFlag2) {
|
|
1417
|
+
return resolveAccounts(readFile(), process.env, accountFlag2);
|
|
1418
|
+
}
|
|
1419
|
+
function readConfig() {
|
|
1420
|
+
return readFile();
|
|
1421
|
+
}
|
|
1422
|
+
function loadConfig() {
|
|
1423
|
+
const file = readFile();
|
|
1424
|
+
let accounts = [];
|
|
1425
|
+
try {
|
|
1426
|
+
accounts = resolveAccounts(file, process.env);
|
|
1427
|
+
} catch {
|
|
1428
|
+
}
|
|
1429
|
+
const only = accounts.length === 1 ? accounts[0] : void 0;
|
|
1430
|
+
return {
|
|
1431
|
+
apiUrl: process.env.FT_API_URL ?? only?.apiUrl ?? file.apiUrl ?? DEFAULT_API_URL,
|
|
1432
|
+
apiKey: only?.apiKey,
|
|
1433
|
+
workspaceId: process.env.FT_WORKSPACE_ID ?? only?.workspaceId,
|
|
1434
|
+
adminSession: process.env.FT_ADMIN_SESSION ?? file.adminSession
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
function saveConfig(patch) {
|
|
1438
|
+
const next = { ...readFile(), ...patch };
|
|
1439
|
+
mkdirSync2(dirname2(CONFIG_PATH), { recursive: true });
|
|
1440
|
+
const tmp = `${CONFIG_PATH}.${process.pid}.tmp`;
|
|
1441
|
+
writeFileSync2(tmp, `${JSON.stringify(next, null, 2)}
|
|
1442
|
+
`, { mode: 384 });
|
|
1443
|
+
chmodSync(tmp, 384);
|
|
1444
|
+
renameSync(tmp, CONFIG_PATH);
|
|
1445
|
+
}
|
|
1446
|
+
function saveAccount(name, account, replaces) {
|
|
1447
|
+
const file = readFile();
|
|
1448
|
+
const accounts = { ...file.accounts };
|
|
1449
|
+
if (replaces && replaces !== name) delete accounts[replaces];
|
|
1450
|
+
if (account) accounts[name] = account;
|
|
1451
|
+
else delete accounts[name];
|
|
1452
|
+
let activeAccount = file.activeAccount;
|
|
1453
|
+
if (replaces && activeAccount === replaces) activeAccount = name;
|
|
1454
|
+
if (!account && activeAccount === name) activeAccount = void 0;
|
|
1455
|
+
saveConfig({ accounts, activeAccount });
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
// src/lib/api.ts
|
|
1459
|
+
var accountFlag;
|
|
1460
|
+
function setAccountFlag(name) {
|
|
1461
|
+
accountFlag = name;
|
|
1462
|
+
}
|
|
1463
|
+
function accountsInScope() {
|
|
1464
|
+
try {
|
|
1465
|
+
return effectiveAccounts(accountFlag);
|
|
1466
|
+
} catch (err) {
|
|
1467
|
+
const message = err.message;
|
|
1468
|
+
fail(
|
|
1469
|
+
message,
|
|
1470
|
+
message.startsWith("No account named") ? "See them with `ft account list`; `ft account clear` unpins." : void 0
|
|
1471
|
+
);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
function resolveAccount() {
|
|
1475
|
+
const accounts = accountsInScope();
|
|
1476
|
+
if (accounts.length === 0) {
|
|
1477
|
+
fail(
|
|
1478
|
+
"Not logged in. Run `ft login` to sign in through your browser.",
|
|
1479
|
+
"CI / headless only: set FT_API_KEY (or `ft login --key`) with a backend-issued token."
|
|
1480
|
+
);
|
|
1481
|
+
}
|
|
1482
|
+
if (accounts.length > 1) {
|
|
1483
|
+
fail(
|
|
1484
|
+
`Several accounts are stored (${accounts.map((a) => a.name).join(", ")}) and this command needs exactly one.`,
|
|
1485
|
+
"Pick one with --account <name>, or pin it with `ft account use <name>`."
|
|
1486
|
+
);
|
|
1487
|
+
}
|
|
1488
|
+
return accounts[0];
|
|
1489
|
+
}
|
|
1490
|
+
function configureClient(workspaceOverride, account = resolveAccount()) {
|
|
1491
|
+
const workspaceId = workspaceOverride ?? account.workspaceId;
|
|
1492
|
+
client.setConfig({
|
|
1493
|
+
baseUrl: `${account.apiUrl.replace(/\/$/, "")}/api/v1`,
|
|
1494
|
+
headers: {
|
|
1495
|
+
Authorization: `Bearer ${account.apiKey}`,
|
|
1496
|
+
...workspaceId ? { "X-Workspace-Id": workspaceId } : {}
|
|
1497
|
+
}
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
function configureAdminClient() {
|
|
1501
|
+
const cfg = loadConfig();
|
|
1502
|
+
if (!cfg.adminSession) {
|
|
1503
|
+
fail(
|
|
1504
|
+
"No superadmin session. Export FT_ADMIN_SESSION with a SUPER_ADMIN better-auth session token.",
|
|
1505
|
+
"Log in to the admin panel in your browser and copy the `better-auth.session_token` cookie."
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
client2.setConfig({
|
|
1509
|
+
baseUrl: `${cfg.apiUrl.replace(/\/$/, "")}/api/admin`,
|
|
1510
|
+
headers: {
|
|
1511
|
+
Cookie: `better-auth.session_token=${cfg.adminSession}`
|
|
1512
|
+
}
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1515
|
+
async function signedDownload(path) {
|
|
1516
|
+
const cfg = client.getConfig();
|
|
1517
|
+
const res = await fetch(`${cfg.baseUrl}${path}`, {
|
|
1518
|
+
redirect: "manual",
|
|
1519
|
+
headers: cfg.headers
|
|
1520
|
+
});
|
|
1521
|
+
const url = res.headers.get("location");
|
|
1522
|
+
if (!url)
|
|
1523
|
+
fail(
|
|
1524
|
+
`The API did not return a signed URL (HTTP ${res.status}).`,
|
|
1525
|
+
res.status === 404 ? "The settlement has no document yet, or it belongs to another workspace." : void 0
|
|
1526
|
+
);
|
|
1527
|
+
return { url, expiresInMinutes: 5 };
|
|
1528
|
+
}
|
|
1529
|
+
function unwrap(res) {
|
|
1530
|
+
if (res.data !== void 0) return res.data;
|
|
1531
|
+
const status = res.response?.status;
|
|
1532
|
+
const err = res.error?.error;
|
|
1533
|
+
if (err) {
|
|
1534
|
+
const lines = [
|
|
1535
|
+
`Error ${status ?? ""} [${err.code ?? "?"}]: ${err.message ?? "unknown failure"}`.trim()
|
|
1536
|
+
];
|
|
1537
|
+
for (const d of err.details ?? [])
|
|
1538
|
+
lines.push(` \xB7 ${d.path ?? ""}: ${d.message ?? ""}`.trimEnd());
|
|
1539
|
+
fail(lines.join("\n"), hintFor(status));
|
|
1540
|
+
}
|
|
1541
|
+
fail(
|
|
1542
|
+
`The API responded with ${status ?? "an error"} without a readable body.`
|
|
1543
|
+
);
|
|
1544
|
+
}
|
|
1545
|
+
function describeError(res) {
|
|
1546
|
+
const status = res.response?.status;
|
|
1547
|
+
const err = res.error?.error;
|
|
1548
|
+
if (!err) return `HTTP ${status ?? "error"}`;
|
|
1549
|
+
return `${status ?? ""} ${err.code ?? "?"}: ${err.message ?? "unknown failure"}`.trim();
|
|
1550
|
+
}
|
|
1551
|
+
function hintFor(status) {
|
|
1552
|
+
switch (status) {
|
|
1553
|
+
case 401:
|
|
1554
|
+
return "Your session is invalid, revoked, or expired. Run `ft login`.";
|
|
1555
|
+
case 403:
|
|
1556
|
+
return "Your role or workspace does not allow this action.";
|
|
1557
|
+
case 404:
|
|
1558
|
+
return "The resource does not exist or belongs to another workspace.";
|
|
1559
|
+
case 422:
|
|
1560
|
+
return "Validation failed. Fix the fields listed above and retry.";
|
|
1561
|
+
case 429:
|
|
1562
|
+
return "Rate limit reached. Wait and retry with backoff - this one is worth retrying.";
|
|
1563
|
+
case 500:
|
|
1564
|
+
return "Server error. Retry once; if it persists, report it.";
|
|
1565
|
+
case 501:
|
|
1566
|
+
return "The backend has not implemented this operation yet.";
|
|
1567
|
+
case 503:
|
|
1568
|
+
return "The service is temporarily unavailable. Retry with backoff.";
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
function fail(message, hint) {
|
|
1572
|
+
console.error(message);
|
|
1573
|
+
if (hint) console.error(`
|
|
1574
|
+
${hint}`);
|
|
1575
|
+
process.exit(1);
|
|
1576
|
+
}
|
|
1577
|
+
function expandTargets(perAccount, workspaceFlag) {
|
|
1578
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1579
|
+
const out = [];
|
|
1580
|
+
for (const { account, workspaces } of perAccount) {
|
|
1581
|
+
const only = workspaceFlag ?? account.workspaceId;
|
|
1582
|
+
for (const w of workspaces) {
|
|
1583
|
+
if (w.sections !== null && w.sections.length === 0) continue;
|
|
1584
|
+
if (only && w.id !== only) continue;
|
|
1585
|
+
if (seen.has(w.id)) continue;
|
|
1586
|
+
seen.add(w.id);
|
|
1587
|
+
out.push({ id: w.id, name: w.name, account });
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
return out;
|
|
1591
|
+
}
|
|
1592
|
+
async function listTargets(override) {
|
|
1593
|
+
const accounts = accountsInScope();
|
|
1594
|
+
if (accounts.length <= 1) {
|
|
1595
|
+
const account = resolveAccount();
|
|
1596
|
+
if (!override && !account.workspaceId) {
|
|
1597
|
+
configureClient(void 0, account);
|
|
1598
|
+
const res = await getMe({});
|
|
1599
|
+
const targets2 = expandTargets([
|
|
1600
|
+
{ account, workspaces: res.data?.data.workspaces ?? [] }
|
|
1601
|
+
]);
|
|
1602
|
+
if (targets2.length > 1) return targets2;
|
|
1603
|
+
}
|
|
1604
|
+
configureClient(override, account);
|
|
1605
|
+
return null;
|
|
1606
|
+
}
|
|
1607
|
+
const perAccount = [];
|
|
1608
|
+
const errors = [];
|
|
1609
|
+
for (const account of accounts) {
|
|
1610
|
+
configureClient(void 0, account);
|
|
1611
|
+
const res = await getMe({});
|
|
1612
|
+
if (!res.data) {
|
|
1613
|
+
errors.push(`account ${account.name}: ${describeError(res)}`);
|
|
1614
|
+
continue;
|
|
1615
|
+
}
|
|
1616
|
+
perAccount.push({ account, workspaces: res.data.data.workspaces });
|
|
1617
|
+
}
|
|
1618
|
+
failIfNoneAnswered(perAccount.length, errors);
|
|
1619
|
+
const targets = expandTargets(perAccount, override);
|
|
1620
|
+
if (override && targets.length === 0) {
|
|
1621
|
+
fail(
|
|
1622
|
+
`No stored account reaches workspace ${override}.`,
|
|
1623
|
+
"See what each one reaches with `ft workspace list`."
|
|
1624
|
+
);
|
|
1625
|
+
}
|
|
1626
|
+
const [only] = targets;
|
|
1627
|
+
if (only && targets.length === 1) {
|
|
1628
|
+
configureClient(only.id, only.account);
|
|
1629
|
+
return null;
|
|
1630
|
+
}
|
|
1631
|
+
return targets;
|
|
1632
|
+
}
|
|
1633
|
+
function failIfNoneAnswered(answered, errors) {
|
|
1634
|
+
if (answered === 0 && errors.length > 0)
|
|
1635
|
+
fail(
|
|
1636
|
+
`No account could be read:
|
|
1637
|
+
${errors.map((e) => ` \xB7 ${e}`).join("\n")}`,
|
|
1638
|
+
"Run `ft login` again for the expired ones, or remove them with `ft account remove <name>`."
|
|
1639
|
+
);
|
|
1640
|
+
for (const e of errors) console.error(`(${e})`);
|
|
1641
|
+
}
|
|
1642
|
+
function spansAccounts(targets) {
|
|
1643
|
+
return new Set(targets.map((t) => t.account.name)).size > 1;
|
|
1644
|
+
}
|
|
1645
|
+
function tagRow(ws, row, withAccount) {
|
|
1646
|
+
return {
|
|
1647
|
+
...withAccount ? { account: ws.account.name } : {},
|
|
1648
|
+
workspaceId: ws.id,
|
|
1649
|
+
workspaceName: ws.name,
|
|
1650
|
+
...row
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1653
|
+
function splitLimit(limit, targets) {
|
|
1654
|
+
if (!Number.isFinite(limit) || limit <= 0 || targets <= 1) return limit;
|
|
1655
|
+
return Math.max(1, Math.ceil(limit / targets));
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
// src/lib/output.ts
|
|
1659
|
+
import chalk2 from "chalk";
|
|
1660
|
+
import Table from "cli-table3";
|
|
1661
|
+
function print(data, opts = {}) {
|
|
1662
|
+
if (opts.json) {
|
|
1663
|
+
const out = opts.columnsExplicit && opts.columns && Array.isArray(data) ? data.map(
|
|
1664
|
+
(row) => pick(row, opts.columns)
|
|
1665
|
+
) : data;
|
|
1666
|
+
process.stdout.write(`${JSON.stringify(out, null, 2)}
|
|
1667
|
+
`);
|
|
1668
|
+
return;
|
|
1669
|
+
}
|
|
1670
|
+
if (Array.isArray(data) && data.every(isPlainRow)) {
|
|
1671
|
+
if (data.length === 0) {
|
|
1672
|
+
console.log(chalk2.dim("(no results)"));
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
const cols = opts.columns ?? Object.keys(data[0]);
|
|
1676
|
+
const table = new Table({ head: cols.map((c2) => chalk2.hex("#07C2BA")(c2)) });
|
|
1677
|
+
for (const row of data) {
|
|
1678
|
+
table.push(cols.map((c2) => fmt(row[c2])));
|
|
1679
|
+
}
|
|
1680
|
+
console.log(table.toString());
|
|
1681
|
+
return;
|
|
1682
|
+
}
|
|
1683
|
+
process.stdout.write(`${JSON.stringify(data, null, 2)}
|
|
1684
|
+
`);
|
|
1685
|
+
}
|
|
1686
|
+
function csvEsc(v) {
|
|
1687
|
+
if (v === null || v === void 0) return "";
|
|
1688
|
+
const s = typeof v === "object" ? JSON.stringify(v) : String(v);
|
|
1689
|
+
return /[",\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
|
1690
|
+
}
|
|
1691
|
+
function toCsv(rows, columns) {
|
|
1692
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
1693
|
+
return columns?.length ? columns.map(csvEsc).join(",") : "";
|
|
1694
|
+
}
|
|
1695
|
+
const cols = columns ?? Object.keys(rows[0]);
|
|
1696
|
+
const head = cols.map(csvEsc).join(",");
|
|
1697
|
+
const body = rows.map(
|
|
1698
|
+
(r) => cols.map((c2) => csvEsc(r[c2])).join(",")
|
|
1699
|
+
);
|
|
1700
|
+
return [head, ...body].join("\n");
|
|
1701
|
+
}
|
|
1702
|
+
function resolveColumns(opts, fallback) {
|
|
1703
|
+
if (opts.full) return void 0;
|
|
1704
|
+
if (opts.columns) {
|
|
1705
|
+
return opts.columns.split(",").map((c2) => c2.trim()).filter(Boolean);
|
|
1706
|
+
}
|
|
1707
|
+
return fallback;
|
|
1708
|
+
}
|
|
1709
|
+
function printNextCursor(page) {
|
|
1710
|
+
if (page?.nextCursor) {
|
|
1711
|
+
console.error(chalk2.dim(`
|
|
1712
|
+
More results: --cursor ${page.nextCursor}`));
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
function isPlainRow(v) {
|
|
1716
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
1717
|
+
}
|
|
1718
|
+
function fmt(v) {
|
|
1719
|
+
if (v === null || v === void 0) return chalk2.dim("\u2014");
|
|
1720
|
+
if (typeof v === "object") return JSON.stringify(v);
|
|
1721
|
+
return String(v);
|
|
1722
|
+
}
|
|
1723
|
+
function pick(row, columns) {
|
|
1724
|
+
const out = {};
|
|
1725
|
+
for (const c2 of columns) if (c2 in row) out[c2] = row[c2];
|
|
1726
|
+
return out;
|
|
1727
|
+
}
|
|
1728
|
+
function printEmptyScope(workspaceFlag) {
|
|
1729
|
+
const workspaceId = workspaceFlag ?? loadConfig().workspaceId;
|
|
1730
|
+
console.error(
|
|
1731
|
+
chalk2.dim(
|
|
1732
|
+
workspaceId ? `(0 results in workspace ${workspaceId})` : "(0 results in the account's default workspace - see them all with `ft workspace list`, or scope with --workspace <id>)"
|
|
1733
|
+
)
|
|
1734
|
+
);
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
// src/commands/auth.ts
|
|
1738
|
+
import { spawn } from "child_process";
|
|
1739
|
+
import chalk3 from "chalk";
|
|
1740
|
+
function registerAuth(program2) {
|
|
1741
|
+
program2.command("login").description(
|
|
1742
|
+
"Log in via the browser (device flow) and add the account to ~/.freeticket/config.json"
|
|
1743
|
+
).option(
|
|
1744
|
+
"--account <name>",
|
|
1745
|
+
"store this login under <name> (default: the account's email); an existing one with that name is replaced"
|
|
1746
|
+
).option(
|
|
1747
|
+
"--key <ft_live_\u2026>",
|
|
1748
|
+
"Skip the browser and use an API key issued in the backend (CI / automation)"
|
|
1749
|
+
).option(
|
|
1750
|
+
"--url <url>",
|
|
1751
|
+
"API base URL (default: https://admin.appfreeticket.com)"
|
|
1752
|
+
).option(
|
|
1753
|
+
"--workspace <id>",
|
|
1754
|
+
"pin this login to one workspace (default: every workspace the account reaches)"
|
|
1755
|
+
).option("--json", "raw JSON output (confirmation goes to stderr)").action(async (opts) => {
|
|
1756
|
+
const apiUrl = opts.url ?? process.env.FT_API_URL ?? readConfig().apiUrl ?? DEFAULT_API_URL;
|
|
1757
|
+
const apiKey = opts.key ?? await deviceLogin(apiUrl);
|
|
1758
|
+
const workspaceId = opts.workspace;
|
|
1759
|
+
configureClient(workspaceId, {
|
|
1760
|
+
name: opts.account ?? LEGACY_ACCOUNT,
|
|
1761
|
+
apiUrl,
|
|
1762
|
+
apiKey,
|
|
1763
|
+
workspaceId
|
|
1764
|
+
});
|
|
1765
|
+
const me = unwrap(await getMe({})).data;
|
|
1766
|
+
const { name, replaces } = loginTarget(
|
|
1767
|
+
readConfig(),
|
|
1768
|
+
me.email,
|
|
1769
|
+
opts.account
|
|
1770
|
+
);
|
|
1771
|
+
saveAccount(
|
|
1772
|
+
name,
|
|
1773
|
+
{
|
|
1774
|
+
apiKey,
|
|
1775
|
+
...me.email ? { email: me.email } : {},
|
|
1776
|
+
...opts.url ? { apiUrl: opts.url } : {},
|
|
1777
|
+
...workspaceId ? { workspaceId } : {}
|
|
1778
|
+
},
|
|
1779
|
+
replaces
|
|
1780
|
+
);
|
|
1781
|
+
console.error(
|
|
1782
|
+
`${chalk3.green("\u2713")} Account ${chalk3.bold(name)} saved in ${chalk3.dim(CONFIG_PATH)}`
|
|
1783
|
+
);
|
|
1784
|
+
const { activeAccount, accounts } = readConfig();
|
|
1785
|
+
if (activeAccount && activeAccount !== name) {
|
|
1786
|
+
console.error(
|
|
1787
|
+
chalk3.dim(
|
|
1788
|
+
`(account ${activeAccount} is pinned: \`ft account clear\` reads all of them, \`ft account use ${name}\` switches)`
|
|
1789
|
+
)
|
|
1790
|
+
);
|
|
1791
|
+
} else if (Object.keys(accounts ?? {}).length > 1) {
|
|
1792
|
+
console.error(
|
|
1793
|
+
chalk3.dim(
|
|
1794
|
+
"(lists now read every stored account; narrow with --account or `ft account use`)"
|
|
1795
|
+
)
|
|
1796
|
+
);
|
|
1797
|
+
}
|
|
1798
|
+
print(me, { json: opts.json });
|
|
1799
|
+
});
|
|
1800
|
+
program2.command("whoami").description(
|
|
1801
|
+
"Show the authenticated user and workspaces (GET /me), for every account in scope"
|
|
1802
|
+
).option("--account <name>", "only this account (default: all stored)").option("--json", "raw JSON output").action(async (opts) => {
|
|
1803
|
+
const accounts = accountsInScope();
|
|
1804
|
+
if (accounts.length <= 1) {
|
|
1805
|
+
configureClient();
|
|
1806
|
+
const me = unwrap(await getMe({})).data;
|
|
1807
|
+
print(me, { json: opts.json });
|
|
1808
|
+
return;
|
|
1809
|
+
}
|
|
1810
|
+
const all = [];
|
|
1811
|
+
const errors = [];
|
|
1812
|
+
for (const account of accounts) {
|
|
1813
|
+
configureClient(void 0, account);
|
|
1814
|
+
const res = await getMe({});
|
|
1815
|
+
if (!res.data) {
|
|
1816
|
+
errors.push(`account ${account.name}: ${describeError(res)}`);
|
|
1817
|
+
continue;
|
|
1818
|
+
}
|
|
1819
|
+
all.push({ account: account.name, ...res.data.data });
|
|
1820
|
+
}
|
|
1821
|
+
failIfNoneAnswered(all.length, errors);
|
|
1822
|
+
if (opts.json) {
|
|
1823
|
+
print(all, { json: true });
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
print(
|
|
1827
|
+
all.map((m) => ({
|
|
1828
|
+
account: m.account,
|
|
1829
|
+
email: m.email,
|
|
1830
|
+
name: m.name,
|
|
1831
|
+
workspaces: m.workspaces.map((w) => w.slug).join(", ")
|
|
1832
|
+
}))
|
|
1833
|
+
);
|
|
1834
|
+
});
|
|
1835
|
+
program2.command("logout").description(
|
|
1836
|
+
"Remove a stored account (the pinned one, or the only one; --account / --all otherwise)"
|
|
1837
|
+
).option("--account <name>", "remove this account").option("--all", "remove every stored account").action((opts) => {
|
|
1838
|
+
const { accounts = {}, activeAccount } = readConfig();
|
|
1839
|
+
const names = Object.keys(accounts);
|
|
1840
|
+
if (opts.all) {
|
|
1841
|
+
saveConfig({ accounts: {}, activeAccount: void 0 });
|
|
1842
|
+
console.log(
|
|
1843
|
+
`${chalk3.green("\u2713")} Logged out of every account \u2014 sessions removed from ${chalk3.dim(CONFIG_PATH)}`
|
|
1844
|
+
);
|
|
1845
|
+
return;
|
|
1846
|
+
}
|
|
1847
|
+
const name = opts.account ?? activeAccount ?? (names.length === 1 ? names[0] : void 0);
|
|
1848
|
+
if (names.length === 0) {
|
|
1849
|
+
console.log(
|
|
1850
|
+
`${chalk3.green("\u2713")} No stored session \u2014 nothing to remove.`
|
|
1851
|
+
);
|
|
1852
|
+
return;
|
|
1853
|
+
}
|
|
1854
|
+
if (!name) {
|
|
1855
|
+
fail(
|
|
1856
|
+
`Several accounts are stored (${names.join(", ")}): say which one to log out of.`,
|
|
1857
|
+
"Use `ft logout --account <name>`, or `ft logout --all`."
|
|
1858
|
+
);
|
|
1859
|
+
}
|
|
1860
|
+
if (!accounts[name]) {
|
|
1861
|
+
fail(`No account named "${name}".`, `Stored: ${names.join(", ")}`);
|
|
1862
|
+
}
|
|
1863
|
+
saveAccount(name, void 0);
|
|
1864
|
+
console.log(
|
|
1865
|
+
`${chalk3.green("\u2713")} Logged out of ${chalk3.bold(name)} \u2014 session removed from ${chalk3.dim(CONFIG_PATH)}`
|
|
1866
|
+
);
|
|
1867
|
+
});
|
|
1868
|
+
program2.command("config").description("Show active configuration (credentials are masked)").option("--json", "raw JSON output").action((opts) => {
|
|
1869
|
+
const file = readConfig();
|
|
1870
|
+
const cfg = loadConfig();
|
|
1871
|
+
print(
|
|
1872
|
+
{
|
|
1873
|
+
apiUrl: cfg.apiUrl,
|
|
1874
|
+
// FT_API_KEY overrides every stored account (CI).
|
|
1875
|
+
env: process.env.FT_API_KEY ? mask(process.env.FT_API_KEY) : null,
|
|
1876
|
+
activeAccount: file.activeAccount ?? null,
|
|
1877
|
+
accounts: Object.entries(file.accounts ?? {}).map(([name, a]) => ({
|
|
1878
|
+
name,
|
|
1879
|
+
apiUrl: a.apiUrl ?? null,
|
|
1880
|
+
// Same field whether it's a device-flow session or a CI API key.
|
|
1881
|
+
session: mask(a.apiKey),
|
|
1882
|
+
workspaceId: a.workspaceId ?? null
|
|
1883
|
+
}))
|
|
1884
|
+
},
|
|
1885
|
+
{ json: opts.json }
|
|
1886
|
+
);
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
function mask(secret) {
|
|
1890
|
+
return `${secret.slice(0, 12)}\u2026`;
|
|
1891
|
+
}
|
|
1892
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1893
|
+
function openBrowser(url) {
|
|
1894
|
+
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
1895
|
+
try {
|
|
1896
|
+
spawn(cmd, [url], { stdio: "ignore", detached: true }).unref();
|
|
1897
|
+
} catch {
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
async function deviceLogin(baseUrl) {
|
|
1901
|
+
const apiUrl = baseUrl.replace(/\/$/, "");
|
|
1902
|
+
client.setConfig({ baseUrl: `${apiUrl}/api/v1` });
|
|
1903
|
+
const start = unwrap(await postAuthDeviceCode({}));
|
|
1904
|
+
console.log(
|
|
1905
|
+
`
|
|
1906
|
+
Open ${chalk3.cyan(start.verification_uri)} and enter the code:
|
|
1907
|
+
|
|
1908
|
+
${chalk3.bold(start.user_code)}
|
|
1909
|
+
|
|
1910
|
+
${chalk3.dim("Opening your browser\u2026 (Ctrl+C to cancel)")}
|
|
1911
|
+
`
|
|
1912
|
+
);
|
|
1913
|
+
openBrowser(start.verification_uri_complete);
|
|
1914
|
+
const deadline = Date.now() + start.expires_in * 1e3;
|
|
1915
|
+
let interval = start.interval;
|
|
1916
|
+
while (Date.now() < deadline) {
|
|
1917
|
+
await sleep(interval * 1e3);
|
|
1918
|
+
const res = await postAuthDeviceToken({
|
|
1919
|
+
body: {
|
|
1920
|
+
device_code: start.device_code,
|
|
1921
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
|
|
1922
|
+
}
|
|
1923
|
+
});
|
|
1924
|
+
if (res.data) {
|
|
1925
|
+
const { access_token, workspaces } = res.data;
|
|
1926
|
+
if (workspaces.length > 1) {
|
|
1927
|
+
console.log(
|
|
1928
|
+
`
|
|
1929
|
+
${chalk3.dim("Workspaces:")} ${workspaces.map((w) => w.slug).join(", ")} ${chalk3.dim("(all of them; narrow with --workspace or `ft workspace use`)")}`
|
|
1930
|
+
);
|
|
1931
|
+
}
|
|
1932
|
+
return access_token;
|
|
1933
|
+
}
|
|
1934
|
+
switch (res.error?.error) {
|
|
1935
|
+
case "authorization_pending":
|
|
1936
|
+
break;
|
|
1937
|
+
// keep polling at the same cadence
|
|
1938
|
+
case "slow_down":
|
|
1939
|
+
interval += 5;
|
|
1940
|
+
break;
|
|
1941
|
+
case "access_denied":
|
|
1942
|
+
fail("Login was denied in the browser.");
|
|
1943
|
+
break;
|
|
1944
|
+
case "expired_token":
|
|
1945
|
+
fail("The code expired before approval. Run `ft login` again.");
|
|
1946
|
+
break;
|
|
1947
|
+
default:
|
|
1948
|
+
fail(`Device login failed: ${res.error?.error ?? "unknown error"}.`);
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
return fail("The code expired before approval. Run `ft login` again.");
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
// src/commands/account.ts
|
|
1955
|
+
function registerAccount(program2) {
|
|
1956
|
+
const root = program2.command("account").alias("accounts").description("List, pin, unpin and remove stored accounts");
|
|
1957
|
+
root.command("list").alias("ls").description("List stored accounts (marks the pinned one)").option("--json", "raw JSON output").action((opts) => {
|
|
1958
|
+
const { accounts = {}, activeAccount, apiUrl } = readConfig();
|
|
1959
|
+
const rows = Object.entries(accounts).map(([name, a]) => ({
|
|
1960
|
+
active: name === activeAccount ? "*" : "",
|
|
1961
|
+
name,
|
|
1962
|
+
apiUrl: a.apiUrl ?? apiUrl ?? DEFAULT_API_URL,
|
|
1963
|
+
workspaceId: a.workspaceId ?? "",
|
|
1964
|
+
session: mask(a.apiKey)
|
|
1965
|
+
}));
|
|
1966
|
+
print(rows, {
|
|
1967
|
+
json: opts.json,
|
|
1968
|
+
columns: ["active", "name", "apiUrl", "workspaceId", "session"]
|
|
1969
|
+
});
|
|
1970
|
+
if (rows.length === 0)
|
|
1971
|
+
console.error(chalk4.dim("(no accounts: add one with `ft login`)"));
|
|
1972
|
+
else if (process.env.FT_API_KEY)
|
|
1973
|
+
console.error(
|
|
1974
|
+
chalk4.dim("(FT_API_KEY is set: it overrides every stored account)")
|
|
1975
|
+
);
|
|
1976
|
+
});
|
|
1977
|
+
root.command("use <name>").description(
|
|
1978
|
+
"Pin every command to one account (persisted in ~/.freeticket/config.json)"
|
|
1979
|
+
).action((name) => {
|
|
1980
|
+
const names = Object.keys(readConfig().accounts ?? {});
|
|
1981
|
+
if (!names.includes(name)) {
|
|
1982
|
+
fail(
|
|
1983
|
+
`No account named "${name}".`,
|
|
1984
|
+
`Stored: ${names.join(", ") || "(none) - add one with `ft login`"}`
|
|
1985
|
+
);
|
|
1986
|
+
}
|
|
1987
|
+
saveConfig({ activeAccount: name });
|
|
1988
|
+
console.log(`${chalk4.green("\u2713")} Active account: ${chalk4.bold(name)}`);
|
|
1989
|
+
});
|
|
1990
|
+
root.command("clear").description("Unpin: go back to reading every stored account").action(() => {
|
|
1991
|
+
saveConfig({ activeAccount: void 0 });
|
|
1992
|
+
console.log(
|
|
1993
|
+
`${chalk4.green("\u2713")} No account pinned: lists now read all of them.`
|
|
1994
|
+
);
|
|
1995
|
+
});
|
|
1996
|
+
root.command("remove <name>").alias("rm").description("Remove a stored account (same as `ft logout --account`)").action((name) => {
|
|
1997
|
+
const names = Object.keys(readConfig().accounts ?? {});
|
|
1998
|
+
if (!names.includes(name)) {
|
|
1999
|
+
fail(`No account named "${name}".`, `Stored: ${names.join(", ")}`);
|
|
2000
|
+
}
|
|
2001
|
+
saveAccount(name, void 0);
|
|
2002
|
+
console.log(
|
|
2003
|
+
`${chalk4.green("\u2713")} Removed ${chalk4.bold(name)} from ${chalk4.dim(CONFIG_PATH)}`
|
|
2004
|
+
);
|
|
2005
|
+
});
|
|
2006
|
+
}
|
|
2007
|
+
|
|
2008
|
+
// src/commands/admin.ts
|
|
2009
|
+
import chalk5 from "chalk";
|
|
2010
|
+
|
|
2011
|
+
// src/admin-client/sdk.gen.ts
|
|
2012
|
+
var getMe2 = (options) => {
|
|
2013
|
+
return (options?.client ?? client2).get({
|
|
2014
|
+
security: [
|
|
2015
|
+
{
|
|
2016
|
+
in: "cookie",
|
|
2017
|
+
name: "better-auth.session_token",
|
|
2018
|
+
type: "apiKey"
|
|
2019
|
+
}
|
|
2020
|
+
],
|
|
2021
|
+
url: "/me",
|
|
2022
|
+
...options
|
|
2023
|
+
});
|
|
2024
|
+
};
|
|
2025
|
+
var getWorkspaces = (options) => {
|
|
2026
|
+
return (options?.client ?? client2).get({
|
|
2027
|
+
security: [
|
|
2028
|
+
{
|
|
2029
|
+
in: "cookie",
|
|
2030
|
+
name: "better-auth.session_token",
|
|
2031
|
+
type: "apiKey"
|
|
2032
|
+
}
|
|
2033
|
+
],
|
|
2034
|
+
url: "/workspaces",
|
|
2035
|
+
...options
|
|
2036
|
+
});
|
|
2037
|
+
};
|
|
2038
|
+
var postWorkspaces = (options) => {
|
|
2039
|
+
return (options.client ?? client2).post({
|
|
2040
|
+
security: [
|
|
2041
|
+
{
|
|
2042
|
+
in: "cookie",
|
|
2043
|
+
name: "better-auth.session_token",
|
|
2044
|
+
type: "apiKey"
|
|
2045
|
+
}
|
|
2046
|
+
],
|
|
2047
|
+
url: "/workspaces",
|
|
2048
|
+
...options,
|
|
2049
|
+
headers: {
|
|
2050
|
+
"Content-Type": "application/json",
|
|
2051
|
+
...options?.headers
|
|
2052
|
+
}
|
|
2053
|
+
});
|
|
2054
|
+
};
|
|
2055
|
+
var getWorkspacesId = (options) => {
|
|
2056
|
+
return (options.client ?? client2).get({
|
|
2057
|
+
security: [
|
|
2058
|
+
{
|
|
2059
|
+
in: "cookie",
|
|
2060
|
+
name: "better-auth.session_token",
|
|
2061
|
+
type: "apiKey"
|
|
2062
|
+
}
|
|
2063
|
+
],
|
|
2064
|
+
url: "/workspaces/{id}",
|
|
2065
|
+
...options
|
|
2066
|
+
});
|
|
2067
|
+
};
|
|
2068
|
+
var patchWorkspacesId = (options) => {
|
|
2069
|
+
return (options.client ?? client2).patch({
|
|
2070
|
+
security: [
|
|
2071
|
+
{
|
|
2072
|
+
in: "cookie",
|
|
2073
|
+
name: "better-auth.session_token",
|
|
2074
|
+
type: "apiKey"
|
|
2075
|
+
}
|
|
2076
|
+
],
|
|
2077
|
+
url: "/workspaces/{id}",
|
|
2078
|
+
...options,
|
|
2079
|
+
headers: {
|
|
2080
|
+
"Content-Type": "application/json",
|
|
2081
|
+
...options?.headers
|
|
2082
|
+
}
|
|
2083
|
+
});
|
|
2084
|
+
};
|
|
2085
|
+
var postWorkspacesIdPlan = (options) => {
|
|
2086
|
+
return (options.client ?? client2).post({
|
|
2087
|
+
security: [
|
|
2088
|
+
{
|
|
2089
|
+
in: "cookie",
|
|
2090
|
+
name: "better-auth.session_token",
|
|
2091
|
+
type: "apiKey"
|
|
2092
|
+
}
|
|
2093
|
+
],
|
|
2094
|
+
url: "/workspaces/{id}/plan",
|
|
2095
|
+
...options,
|
|
2096
|
+
headers: {
|
|
2097
|
+
"Content-Type": "application/json",
|
|
2098
|
+
...options?.headers
|
|
2099
|
+
}
|
|
2100
|
+
});
|
|
2101
|
+
};
|
|
2102
|
+
var postWorkspacesIdSuspend = (options) => {
|
|
2103
|
+
return (options.client ?? client2).post({
|
|
2104
|
+
security: [
|
|
2105
|
+
{
|
|
2106
|
+
in: "cookie",
|
|
2107
|
+
name: "better-auth.session_token",
|
|
2108
|
+
type: "apiKey"
|
|
2109
|
+
}
|
|
2110
|
+
],
|
|
2111
|
+
url: "/workspaces/{id}/suspend",
|
|
2112
|
+
...options
|
|
2113
|
+
});
|
|
2114
|
+
};
|
|
2115
|
+
var postWorkspacesIdRestore = (options) => {
|
|
2116
|
+
return (options.client ?? client2).post({
|
|
2117
|
+
security: [
|
|
2118
|
+
{
|
|
2119
|
+
in: "cookie",
|
|
2120
|
+
name: "better-auth.session_token",
|
|
2121
|
+
type: "apiKey"
|
|
2122
|
+
}
|
|
2123
|
+
],
|
|
2124
|
+
url: "/workspaces/{id}/restore",
|
|
2125
|
+
...options
|
|
2126
|
+
});
|
|
2127
|
+
};
|
|
2128
|
+
var getUsers = (options) => {
|
|
2129
|
+
return (options?.client ?? client2).get({
|
|
2130
|
+
security: [
|
|
2131
|
+
{
|
|
2132
|
+
in: "cookie",
|
|
2133
|
+
name: "better-auth.session_token",
|
|
2134
|
+
type: "apiKey"
|
|
2135
|
+
}
|
|
2136
|
+
],
|
|
2137
|
+
url: "/users",
|
|
2138
|
+
...options
|
|
2139
|
+
});
|
|
2140
|
+
};
|
|
2141
|
+
var getUsersId = (options) => {
|
|
2142
|
+
return (options.client ?? client2).get({
|
|
2143
|
+
security: [
|
|
2144
|
+
{
|
|
2145
|
+
in: "cookie",
|
|
2146
|
+
name: "better-auth.session_token",
|
|
2147
|
+
type: "apiKey"
|
|
2148
|
+
}
|
|
2149
|
+
],
|
|
2150
|
+
url: "/users/{id}",
|
|
2151
|
+
...options
|
|
2152
|
+
});
|
|
2153
|
+
};
|
|
2154
|
+
var patchUsersId = (options) => {
|
|
2155
|
+
return (options.client ?? client2).patch({
|
|
2156
|
+
security: [
|
|
2157
|
+
{
|
|
2158
|
+
in: "cookie",
|
|
2159
|
+
name: "better-auth.session_token",
|
|
2160
|
+
type: "apiKey"
|
|
2161
|
+
}
|
|
2162
|
+
],
|
|
2163
|
+
url: "/users/{id}",
|
|
2164
|
+
...options,
|
|
2165
|
+
headers: {
|
|
2166
|
+
"Content-Type": "application/json",
|
|
2167
|
+
...options?.headers
|
|
2168
|
+
}
|
|
2169
|
+
});
|
|
2170
|
+
};
|
|
2171
|
+
var postImpersonate = (options) => {
|
|
2172
|
+
return (options.client ?? client2).post({
|
|
2173
|
+
security: [
|
|
2174
|
+
{
|
|
2175
|
+
in: "cookie",
|
|
2176
|
+
name: "better-auth.session_token",
|
|
2177
|
+
type: "apiKey"
|
|
2178
|
+
}
|
|
2179
|
+
],
|
|
2180
|
+
url: "/impersonate",
|
|
2181
|
+
...options,
|
|
2182
|
+
headers: {
|
|
2183
|
+
"Content-Type": "application/json",
|
|
2184
|
+
...options?.headers
|
|
2185
|
+
}
|
|
2186
|
+
});
|
|
1333
2187
|
};
|
|
1334
2188
|
var postImpersonateStop = (options) => {
|
|
1335
2189
|
return (options?.client ?? client2).post({
|
|
@@ -1491,123 +2345,8 @@ var deleteTokensId = (options) => {
|
|
|
1491
2345
|
});
|
|
1492
2346
|
};
|
|
1493
2347
|
|
|
1494
|
-
// src/lib/config.ts
|
|
1495
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
1496
|
-
import { homedir } from "os";
|
|
1497
|
-
import { dirname, join } from "path";
|
|
1498
|
-
var CONFIG_DIR = join(homedir(), ".freeticket");
|
|
1499
|
-
var CONFIG_PATH = join(CONFIG_DIR, "config.json");
|
|
1500
|
-
var DEFAULT_API_URL = "https://admin.appfreeticket.com";
|
|
1501
|
-
function readFile() {
|
|
1502
|
-
if (!existsSync(CONFIG_PATH)) return {};
|
|
1503
|
-
try {
|
|
1504
|
-
return JSON.parse(readFileSync(CONFIG_PATH, "utf8"));
|
|
1505
|
-
} catch {
|
|
1506
|
-
return {};
|
|
1507
|
-
}
|
|
1508
|
-
}
|
|
1509
|
-
function loadConfig() {
|
|
1510
|
-
const file = readFile();
|
|
1511
|
-
return {
|
|
1512
|
-
apiUrl: process.env.FT_API_URL ?? file.apiUrl ?? DEFAULT_API_URL,
|
|
1513
|
-
apiKey: process.env.FT_API_KEY ?? file.apiKey,
|
|
1514
|
-
workspaceId: process.env.FT_WORKSPACE_ID ?? file.workspaceId,
|
|
1515
|
-
adminSession: process.env.FT_ADMIN_SESSION ?? file.adminSession
|
|
1516
|
-
};
|
|
1517
|
-
}
|
|
1518
|
-
function saveConfig(patch) {
|
|
1519
|
-
const next = { ...readFile(), ...patch };
|
|
1520
|
-
mkdirSync(dirname(CONFIG_PATH), { recursive: true });
|
|
1521
|
-
writeFileSync(CONFIG_PATH, `${JSON.stringify(next, null, 2)}
|
|
1522
|
-
`, {
|
|
1523
|
-
mode: 384
|
|
1524
|
-
});
|
|
1525
|
-
}
|
|
1526
|
-
|
|
1527
|
-
// src/lib/api.ts
|
|
1528
|
-
function configureClient(workspaceOverride) {
|
|
1529
|
-
const cfg = loadConfig();
|
|
1530
|
-
if (!cfg.apiKey) {
|
|
1531
|
-
fail(
|
|
1532
|
-
"Not logged in. Run `ft login` to sign in through your browser.",
|
|
1533
|
-
"CI / headless only: set FT_API_KEY (or `ft login --key`) with a backend-issued token."
|
|
1534
|
-
);
|
|
1535
|
-
}
|
|
1536
|
-
const workspaceId = workspaceOverride ?? cfg.workspaceId;
|
|
1537
|
-
client.setConfig({
|
|
1538
|
-
baseUrl: `${cfg.apiUrl.replace(/\/$/, "")}/api/v1`,
|
|
1539
|
-
headers: {
|
|
1540
|
-
Authorization: `Bearer ${cfg.apiKey}`,
|
|
1541
|
-
...workspaceId ? { "X-Workspace-Id": workspaceId } : {}
|
|
1542
|
-
}
|
|
1543
|
-
});
|
|
1544
|
-
}
|
|
1545
|
-
function configureAdminClient() {
|
|
1546
|
-
const cfg = loadConfig();
|
|
1547
|
-
if (!cfg.adminSession) {
|
|
1548
|
-
fail(
|
|
1549
|
-
"No superadmin session. Export FT_ADMIN_SESSION with a SUPER_ADMIN better-auth session token.",
|
|
1550
|
-
"Log in to the admin panel in your browser and copy the `better-auth.session_token` cookie."
|
|
1551
|
-
);
|
|
1552
|
-
}
|
|
1553
|
-
client2.setConfig({
|
|
1554
|
-
baseUrl: `${cfg.apiUrl.replace(/\/$/, "")}/api/admin`,
|
|
1555
|
-
headers: {
|
|
1556
|
-
Cookie: `better-auth.session_token=${cfg.adminSession}`
|
|
1557
|
-
}
|
|
1558
|
-
});
|
|
1559
|
-
}
|
|
1560
|
-
async function signedDownload(path) {
|
|
1561
|
-
const cfg = client.getConfig();
|
|
1562
|
-
const res = await fetch(`${cfg.baseUrl}${path}`, {
|
|
1563
|
-
redirect: "manual",
|
|
1564
|
-
headers: cfg.headers
|
|
1565
|
-
});
|
|
1566
|
-
const url = res.headers.get("location");
|
|
1567
|
-
if (!url)
|
|
1568
|
-
fail(
|
|
1569
|
-
`The API did not return a signed URL (HTTP ${res.status}).`,
|
|
1570
|
-
res.status === 404 ? "The settlement has no document yet, or it belongs to another workspace." : void 0
|
|
1571
|
-
);
|
|
1572
|
-
return { url, expiresInMinutes: 5 };
|
|
1573
|
-
}
|
|
1574
|
-
function unwrap(res) {
|
|
1575
|
-
if (res.data !== void 0) return res.data;
|
|
1576
|
-
const status = res.response?.status;
|
|
1577
|
-
const err = res.error?.error;
|
|
1578
|
-
if (err) {
|
|
1579
|
-
const lines = [
|
|
1580
|
-
`Error ${status ?? ""} [${err.code ?? "?"}]: ${err.message ?? "unknown failure"}`.trim()
|
|
1581
|
-
];
|
|
1582
|
-
for (const d of err.details ?? [])
|
|
1583
|
-
lines.push(` \xB7 ${d.path ?? ""}: ${d.message ?? ""}`.trimEnd());
|
|
1584
|
-
fail(lines.join("\n"), hintFor(status));
|
|
1585
|
-
}
|
|
1586
|
-
fail(
|
|
1587
|
-
`The API responded with ${status ?? "an error"} without a readable body.`
|
|
1588
|
-
);
|
|
1589
|
-
}
|
|
1590
|
-
function hintFor(status) {
|
|
1591
|
-
switch (status) {
|
|
1592
|
-
case 401:
|
|
1593
|
-
return "Your session is invalid, revoked, or expired. Run `ft login`.";
|
|
1594
|
-
case 403:
|
|
1595
|
-
return "Your role or workspace does not allow this action.";
|
|
1596
|
-
case 404:
|
|
1597
|
-
return "The resource does not exist or belongs to another workspace.";
|
|
1598
|
-
case 501:
|
|
1599
|
-
return "The backend has not implemented this operation yet.";
|
|
1600
|
-
}
|
|
1601
|
-
}
|
|
1602
|
-
function fail(message, hint) {
|
|
1603
|
-
console.error(message);
|
|
1604
|
-
if (hint) console.error(`
|
|
1605
|
-
${hint}`);
|
|
1606
|
-
process.exit(1);
|
|
1607
|
-
}
|
|
1608
|
-
|
|
1609
2348
|
// src/lib/input.ts
|
|
1610
|
-
import { readFileSync as
|
|
2349
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
1611
2350
|
import * as readline from "readline";
|
|
1612
2351
|
function parseData(input) {
|
|
1613
2352
|
if (!input) {
|
|
@@ -1619,7 +2358,7 @@ function parseData(input) {
|
|
|
1619
2358
|
let raw = input;
|
|
1620
2359
|
if (raw.startsWith("@")) {
|
|
1621
2360
|
try {
|
|
1622
|
-
raw =
|
|
2361
|
+
raw = readFileSync3(raw.slice(1), "utf8");
|
|
1623
2362
|
} catch {
|
|
1624
2363
|
fail(`Cannot read file: ${raw.slice(1)}`);
|
|
1625
2364
|
}
|
|
@@ -1654,84 +2393,6 @@ function confirm(question) {
|
|
|
1654
2393
|
});
|
|
1655
2394
|
}
|
|
1656
2395
|
|
|
1657
|
-
// src/lib/output.ts
|
|
1658
|
-
import chalk from "chalk";
|
|
1659
|
-
import Table from "cli-table3";
|
|
1660
|
-
function print(data, opts = {}) {
|
|
1661
|
-
if (opts.json) {
|
|
1662
|
-
const out = opts.columnsExplicit && opts.columns && Array.isArray(data) ? data.map(
|
|
1663
|
-
(row) => pick(row, opts.columns)
|
|
1664
|
-
) : data;
|
|
1665
|
-
process.stdout.write(`${JSON.stringify(out, null, 2)}
|
|
1666
|
-
`);
|
|
1667
|
-
return;
|
|
1668
|
-
}
|
|
1669
|
-
if (Array.isArray(data) && data.every(isPlainRow)) {
|
|
1670
|
-
if (data.length === 0) {
|
|
1671
|
-
console.log(chalk.dim("(no results)"));
|
|
1672
|
-
return;
|
|
1673
|
-
}
|
|
1674
|
-
const cols = opts.columns ?? Object.keys(data[0]);
|
|
1675
|
-
const table = new Table({ head: cols.map((c2) => chalk.hex("#07C2BA")(c2)) });
|
|
1676
|
-
for (const row of data) {
|
|
1677
|
-
table.push(cols.map((c2) => fmt(row[c2])));
|
|
1678
|
-
}
|
|
1679
|
-
console.log(table.toString());
|
|
1680
|
-
return;
|
|
1681
|
-
}
|
|
1682
|
-
process.stdout.write(`${JSON.stringify(data, null, 2)}
|
|
1683
|
-
`);
|
|
1684
|
-
}
|
|
1685
|
-
function csvEsc(v) {
|
|
1686
|
-
if (v === null || v === void 0) return "";
|
|
1687
|
-
const s = typeof v === "object" ? JSON.stringify(v) : String(v);
|
|
1688
|
-
return /[",\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
|
1689
|
-
}
|
|
1690
|
-
function toCsv(rows, columns) {
|
|
1691
|
-
if (!Array.isArray(rows) || rows.length === 0) {
|
|
1692
|
-
return columns?.length ? columns.map(csvEsc).join(",") : "";
|
|
1693
|
-
}
|
|
1694
|
-
const cols = columns ?? Object.keys(rows[0]);
|
|
1695
|
-
const head = cols.map(csvEsc).join(",");
|
|
1696
|
-
const body = rows.map(
|
|
1697
|
-
(r) => cols.map((c2) => csvEsc(r[c2])).join(",")
|
|
1698
|
-
);
|
|
1699
|
-
return [head, ...body].join("\n");
|
|
1700
|
-
}
|
|
1701
|
-
function resolveColumns(opts, fallback) {
|
|
1702
|
-
if (opts.full) return void 0;
|
|
1703
|
-
if (opts.columns) {
|
|
1704
|
-
return opts.columns.split(",").map((c2) => c2.trim()).filter(Boolean);
|
|
1705
|
-
}
|
|
1706
|
-
return fallback;
|
|
1707
|
-
}
|
|
1708
|
-
function printNextCursor(page) {
|
|
1709
|
-
if (page?.nextCursor) {
|
|
1710
|
-
console.error(chalk.dim(`
|
|
1711
|
-
More results: --cursor ${page.nextCursor}`));
|
|
1712
|
-
}
|
|
1713
|
-
}
|
|
1714
|
-
function isPlainRow(v) {
|
|
1715
|
-
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
1716
|
-
}
|
|
1717
|
-
function fmt(v) {
|
|
1718
|
-
if (v === null || v === void 0) return chalk.dim("\u2014");
|
|
1719
|
-
if (typeof v === "object") return JSON.stringify(v);
|
|
1720
|
-
return String(v);
|
|
1721
|
-
}
|
|
1722
|
-
function pick(row, columns) {
|
|
1723
|
-
const out = {};
|
|
1724
|
-
for (const c2 of columns) if (c2 in row) out[c2] = row[c2];
|
|
1725
|
-
return out;
|
|
1726
|
-
}
|
|
1727
|
-
function printEmptyScope(workspaceId) {
|
|
1728
|
-
console.error(
|
|
1729
|
-
chalk.dim(
|
|
1730
|
-
workspaceId ? `(0 results in workspace ${workspaceId})` : "(0 results in the session's active workspace - check it with `ft whoami`, or scope with --workspace <id>)"
|
|
1731
|
-
)
|
|
1732
|
-
);
|
|
1733
|
-
}
|
|
1734
|
-
|
|
1735
2396
|
// src/commands/admin.ts
|
|
1736
2397
|
function registerAdmin(program2) {
|
|
1737
2398
|
const admin = program2.command("admin").description("Superadmin (cross-tenant) \u2014 SUPER_ADMIN session required");
|
|
@@ -1749,14 +2410,14 @@ function registerAdmin(program2) {
|
|
|
1749
2410
|
configureAdminClient();
|
|
1750
2411
|
const me = unwrap(await getMe2({})).data;
|
|
1751
2412
|
console.log(
|
|
1752
|
-
`${
|
|
2413
|
+
`${chalk5.green("\u2713")} Admin session saved in ${chalk5.dim(CONFIG_PATH)}`
|
|
1753
2414
|
);
|
|
1754
2415
|
print(me, {});
|
|
1755
2416
|
});
|
|
1756
2417
|
admin.command("logout").description("Remove the stored superadmin session").action(() => {
|
|
1757
2418
|
saveConfig({ adminSession: void 0 });
|
|
1758
2419
|
console.log(
|
|
1759
|
-
`${
|
|
2420
|
+
`${chalk5.green("\u2713")} Admin session removed from ${chalk5.dim(CONFIG_PATH)}`
|
|
1760
2421
|
);
|
|
1761
2422
|
});
|
|
1762
2423
|
admin.command("config").description("Show admin configuration (the session is masked)").action(() => {
|
|
@@ -1774,11 +2435,14 @@ function registerAdmin(program2) {
|
|
|
1774
2435
|
const body = unwrap(await getMe2({}));
|
|
1775
2436
|
print(body.data, { json: opts.json });
|
|
1776
2437
|
});
|
|
1777
|
-
admin.command("impersonate").description(`Start impersonation (--data '{"targetUserId":"..."}')`).requiredOption("--data <json>", "JSON body (inline or @file.json)").option("--json", "raw JSON output").action(async (opts) => {
|
|
2438
|
+
admin.command("impersonate").description(`Start impersonation (--data '{"targetUserId":"..."}')`).requiredOption("--data <json>", "JSON body (inline or @file.json)").option("--yes", "skip confirmation").option("--json", "raw JSON output").action(async (opts) => {
|
|
1778
2439
|
configureAdminClient();
|
|
1779
|
-
const
|
|
1780
|
-
|
|
2440
|
+
const payload = parseData(opts.data);
|
|
2441
|
+
await confirmOrExit(
|
|
2442
|
+
`Impersonate user ${payload.targetUserId ?? "(no targetUserId in --data)"}?`,
|
|
2443
|
+
opts.yes
|
|
1781
2444
|
);
|
|
2445
|
+
const body = unwrap(await postImpersonate({ body: payload }));
|
|
1782
2446
|
print(body?.data ?? body, { json: opts.json });
|
|
1783
2447
|
});
|
|
1784
2448
|
admin.command("impersonate-stop").description("Stop the current impersonation").option("--json", "raw JSON output").action(async (opts) => {
|
|
@@ -1909,8 +2573,12 @@ function registerAdminResource(parent, spec) {
|
|
|
1909
2573
|
cmd.option("--limit <n>", "results per page (1-100)", "20").option("--cursor <id>", "pagination cursor");
|
|
1910
2574
|
}
|
|
1911
2575
|
for (const f of spec.listFlags ?? []) cmd.option(f.flag, f.describe);
|
|
2576
|
+
cmd.option("--columns <list>", "comma-separated columns to display");
|
|
2577
|
+
cmd.option("--full", "show every field instead of the curated columns");
|
|
2578
|
+
cmd.option("--all", "auto-paginate: fetch every page (ignores --cursor)");
|
|
1912
2579
|
cmd.option("--csv", "CSV output");
|
|
1913
2580
|
cmd.option("--json", "raw JSON output");
|
|
2581
|
+
cmd.option("--raw", "raw JSON output including pagination metadata (page)");
|
|
1914
2582
|
cmd.action(async (opts) => {
|
|
1915
2583
|
configureAdminClient();
|
|
1916
2584
|
const query = {};
|
|
@@ -1922,13 +2590,44 @@ function registerAdminResource(parent, spec) {
|
|
|
1922
2590
|
const v = opts[camel(f.query)];
|
|
1923
2591
|
if (v !== void 0) query[f.query] = v;
|
|
1924
2592
|
}
|
|
2593
|
+
const columns = resolveColumns(opts, spec.columns);
|
|
2594
|
+
if (opts.all && !spec.noPaging) {
|
|
2595
|
+
const rows = [];
|
|
2596
|
+
let cursor;
|
|
2597
|
+
let page;
|
|
2598
|
+
do {
|
|
2599
|
+
const body2 = unwrap(await list({ query: { ...query, cursor } }));
|
|
2600
|
+
rows.push(...body2.data ?? []);
|
|
2601
|
+
page = body2.page;
|
|
2602
|
+
cursor = page?.nextCursor ?? void 0;
|
|
2603
|
+
} while (page?.hasMore && cursor);
|
|
2604
|
+
if (opts.csv) {
|
|
2605
|
+
process.stdout.write(`${toCsv(rows, columns)}
|
|
2606
|
+
`);
|
|
2607
|
+
return;
|
|
2608
|
+
}
|
|
2609
|
+
print(rows, {
|
|
2610
|
+
json: opts.json,
|
|
2611
|
+
columns,
|
|
2612
|
+
columnsExplicit: Boolean(opts.columns)
|
|
2613
|
+
});
|
|
2614
|
+
return;
|
|
2615
|
+
}
|
|
1925
2616
|
const body = unwrap(await list({ query }));
|
|
2617
|
+
if (opts.raw) {
|
|
2618
|
+
print(body, { json: true });
|
|
2619
|
+
return;
|
|
2620
|
+
}
|
|
1926
2621
|
if (opts.csv) {
|
|
1927
|
-
process.stdout.write(`${toCsv(body.data,
|
|
2622
|
+
process.stdout.write(`${toCsv(body.data, columns)}
|
|
1928
2623
|
`);
|
|
1929
2624
|
return;
|
|
1930
2625
|
}
|
|
1931
|
-
print(body.data, {
|
|
2626
|
+
print(body.data, {
|
|
2627
|
+
json: opts.json,
|
|
2628
|
+
columns,
|
|
2629
|
+
columnsExplicit: Boolean(opts.columns)
|
|
2630
|
+
});
|
|
1932
2631
|
if (!opts.json && !spec.noPaging) printNextCursor(body.page);
|
|
1933
2632
|
});
|
|
1934
2633
|
}
|
|
@@ -1981,7 +2680,7 @@ function camel(s) {
|
|
|
1981
2680
|
}
|
|
1982
2681
|
|
|
1983
2682
|
// src/commands/api-keys.ts
|
|
1984
|
-
import
|
|
2683
|
+
import chalk6 from "chalk";
|
|
1985
2684
|
var COLUMNS = [
|
|
1986
2685
|
"name",
|
|
1987
2686
|
"keyPrefix",
|
|
@@ -2014,13 +2713,13 @@ function registerApiKeys(program2) {
|
|
|
2014
2713
|
}
|
|
2015
2714
|
console.log(
|
|
2016
2715
|
`
|
|
2017
|
-
${
|
|
2716
|
+
${chalk6.green("\u2713")} API key "${chalk6.bold(key.name)}" created (scope: ${key.scope}).
|
|
2018
2717
|
`
|
|
2019
2718
|
);
|
|
2020
2719
|
console.log(
|
|
2021
|
-
|
|
2720
|
+
chalk6.yellow(" Copy your secret now \u2014 it will NOT be shown again:\n")
|
|
2022
2721
|
);
|
|
2023
|
-
console.log(` ${
|
|
2722
|
+
console.log(` ${chalk6.bold.cyan(key.key)}
|
|
2024
2723
|
`);
|
|
2025
2724
|
print(
|
|
2026
2725
|
{
|
|
@@ -2058,7 +2757,12 @@ ${chalk3.green("\u2713")} API key "${chalk3.bold(key.name)}" created (scope: ${k
|
|
|
2058
2757
|
`);
|
|
2059
2758
|
return;
|
|
2060
2759
|
}
|
|
2061
|
-
print(rows, {
|
|
2760
|
+
print(rows, {
|
|
2761
|
+
json: opts.json,
|
|
2762
|
+
columns,
|
|
2763
|
+
columnsExplicit: Boolean(opts.columns)
|
|
2764
|
+
});
|
|
2765
|
+
if (rows.length === 0) printEmptyScope(opts.workspace);
|
|
2062
2766
|
return;
|
|
2063
2767
|
}
|
|
2064
2768
|
const body = unwrap(await getApiKeys({ query }));
|
|
@@ -2067,7 +2771,12 @@ ${chalk3.green("\u2713")} API key "${chalk3.bold(key.name)}" created (scope: ${k
|
|
|
2067
2771
|
`);
|
|
2068
2772
|
return;
|
|
2069
2773
|
}
|
|
2070
|
-
print(body.data, {
|
|
2774
|
+
print(body.data, {
|
|
2775
|
+
json: opts.json,
|
|
2776
|
+
columns,
|
|
2777
|
+
columnsExplicit: Boolean(opts.columns)
|
|
2778
|
+
});
|
|
2779
|
+
if ((body.data ?? []).length === 0) printEmptyScope(opts.workspace);
|
|
2071
2780
|
if (!opts.json) printNextCursor(body.page);
|
|
2072
2781
|
});
|
|
2073
2782
|
root.command("revoke <id>").description("Revoke one of your API keys").option("--yes", "skip confirmation").option("--workspace <id>", "workspace override").option("--json", "raw JSON output").action(async (id, opts) => {
|
|
@@ -2078,120 +2787,6 @@ ${chalk3.green("\u2713")} API key "${chalk3.bold(key.name)}" created (scope: ${k
|
|
|
2078
2787
|
});
|
|
2079
2788
|
}
|
|
2080
2789
|
|
|
2081
|
-
// src/commands/auth.ts
|
|
2082
|
-
import { spawn } from "child_process";
|
|
2083
|
-
import chalk4 from "chalk";
|
|
2084
|
-
function registerAuth(program2) {
|
|
2085
|
-
program2.command("login").description(
|
|
2086
|
-
"Log in via the browser (device flow) and store the session in ~/.freeticket/config.json"
|
|
2087
|
-
).option(
|
|
2088
|
-
"--key <ft_live_\u2026>",
|
|
2089
|
-
"Skip the browser and use an API key issued in the backend (CI / automation)"
|
|
2090
|
-
).option(
|
|
2091
|
-
"--url <url>",
|
|
2092
|
-
"API base URL (default: https://admin.appfreeticket.com)"
|
|
2093
|
-
).option("--workspace <id>", "default active workspace").option("--json", "raw JSON output (confirmation goes to stderr)").action(async (opts) => {
|
|
2094
|
-
if (opts.url) saveConfig({ apiUrl: opts.url });
|
|
2095
|
-
const cred = opts.key ? { apiKey: opts.key, workspaceId: void 0 } : await deviceLogin();
|
|
2096
|
-
const workspaceId = opts.workspace ?? cred.workspaceId;
|
|
2097
|
-
saveConfig({
|
|
2098
|
-
apiKey: cred.apiKey,
|
|
2099
|
-
...workspaceId ? { workspaceId } : {}
|
|
2100
|
-
});
|
|
2101
|
-
configureClient(workspaceId);
|
|
2102
|
-
const me = unwrap(await getMe({})).data;
|
|
2103
|
-
console.error(
|
|
2104
|
-
`${chalk4.green("\u2713")} Session saved in ${chalk4.dim(CONFIG_PATH)}`
|
|
2105
|
-
);
|
|
2106
|
-
print(me, { json: opts.json });
|
|
2107
|
-
});
|
|
2108
|
-
program2.command("whoami").description("Show the authenticated user and workspaces (GET /me)").option("--json", "raw JSON output").action(async (opts) => {
|
|
2109
|
-
configureClient();
|
|
2110
|
-
const me = unwrap(await getMe({})).data;
|
|
2111
|
-
print(me, { json: opts.json });
|
|
2112
|
-
});
|
|
2113
|
-
program2.command("logout").description("Log out and remove the stored session").action(() => {
|
|
2114
|
-
saveConfig({ apiKey: void 0 });
|
|
2115
|
-
console.log(
|
|
2116
|
-
`${chalk4.green("\u2713")} Logged out \u2014 session removed from ${chalk4.dim(CONFIG_PATH)}`
|
|
2117
|
-
);
|
|
2118
|
-
});
|
|
2119
|
-
program2.command("config").description("Show active configuration (the credential is masked)").option("--json", "raw JSON output").action((opts) => {
|
|
2120
|
-
const cfg = loadConfig();
|
|
2121
|
-
print(
|
|
2122
|
-
{
|
|
2123
|
-
apiUrl: cfg.apiUrl,
|
|
2124
|
-
// Same field whether it's a device-flow session or a CI API key.
|
|
2125
|
-
session: cfg.apiKey ? `${cfg.apiKey.slice(0, 12)}\u2026` : null,
|
|
2126
|
-
workspaceId: cfg.workspaceId ?? null
|
|
2127
|
-
},
|
|
2128
|
-
{ json: opts.json }
|
|
2129
|
-
);
|
|
2130
|
-
});
|
|
2131
|
-
}
|
|
2132
|
-
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
2133
|
-
function openBrowser(url) {
|
|
2134
|
-
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
2135
|
-
try {
|
|
2136
|
-
spawn(cmd, [url], { stdio: "ignore", detached: true }).unref();
|
|
2137
|
-
} catch {
|
|
2138
|
-
}
|
|
2139
|
-
}
|
|
2140
|
-
async function deviceLogin() {
|
|
2141
|
-
const apiUrl = loadConfig().apiUrl.replace(/\/$/, "");
|
|
2142
|
-
client.setConfig({ baseUrl: `${apiUrl}/api/v1` });
|
|
2143
|
-
const start = unwrap(await postAuthDeviceCode({}));
|
|
2144
|
-
console.log(
|
|
2145
|
-
`
|
|
2146
|
-
Open ${chalk4.cyan(start.verification_uri)} and enter the code:
|
|
2147
|
-
|
|
2148
|
-
${chalk4.bold(start.user_code)}
|
|
2149
|
-
|
|
2150
|
-
${chalk4.dim("Opening your browser\u2026 (Ctrl+C to cancel)")}
|
|
2151
|
-
`
|
|
2152
|
-
);
|
|
2153
|
-
openBrowser(start.verification_uri_complete);
|
|
2154
|
-
const deadline = Date.now() + start.expires_in * 1e3;
|
|
2155
|
-
let interval = start.interval;
|
|
2156
|
-
while (Date.now() < deadline) {
|
|
2157
|
-
await sleep(interval * 1e3);
|
|
2158
|
-
const res = await postAuthDeviceToken({
|
|
2159
|
-
body: {
|
|
2160
|
-
device_code: start.device_code,
|
|
2161
|
-
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
|
|
2162
|
-
}
|
|
2163
|
-
});
|
|
2164
|
-
if (res.data) {
|
|
2165
|
-
const { access_token, workspaces } = res.data;
|
|
2166
|
-
const active = workspaces[0];
|
|
2167
|
-
if (workspaces.length > 1 && active) {
|
|
2168
|
-
console.log(
|
|
2169
|
-
`
|
|
2170
|
-
${chalk4.dim("Workspaces:")} ${workspaces.map((w) => w.slug).join(", ")} ${chalk4.dim(`(using ${active.slug}; switch with --workspace)`)}`
|
|
2171
|
-
);
|
|
2172
|
-
}
|
|
2173
|
-
return { apiKey: access_token, workspaceId: active?.id };
|
|
2174
|
-
}
|
|
2175
|
-
switch (res.error?.error) {
|
|
2176
|
-
case "authorization_pending":
|
|
2177
|
-
break;
|
|
2178
|
-
// keep polling at the same cadence
|
|
2179
|
-
case "slow_down":
|
|
2180
|
-
interval += 5;
|
|
2181
|
-
break;
|
|
2182
|
-
case "access_denied":
|
|
2183
|
-
fail("Login was denied in the browser.");
|
|
2184
|
-
break;
|
|
2185
|
-
case "expired_token":
|
|
2186
|
-
fail("The code expired before approval. Run `ft login` again.");
|
|
2187
|
-
break;
|
|
2188
|
-
default:
|
|
2189
|
-
fail(`Device login failed: ${res.error?.error ?? "unknown error"}.`);
|
|
2190
|
-
}
|
|
2191
|
-
}
|
|
2192
|
-
return fail("The code expired before approval. Run `ft login` again.");
|
|
2193
|
-
}
|
|
2194
|
-
|
|
2195
2790
|
// src/commands/event-dates.ts
|
|
2196
2791
|
function registerEventDates(program2, spec) {
|
|
2197
2792
|
const root = program2.command("event-dates").description("Event dates (sessions of an event)");
|
|
@@ -2232,22 +2827,49 @@ function registerEventDates(program2, spec) {
|
|
|
2232
2827
|
}
|
|
2233
2828
|
|
|
2234
2829
|
// src/commands/reports.ts
|
|
2830
|
+
async function reportRows(workspaceFlag, call) {
|
|
2831
|
+
const targets = await listTargets(workspaceFlag);
|
|
2832
|
+
if (!targets) {
|
|
2833
|
+
const res = await call();
|
|
2834
|
+
return unwrap(res).data;
|
|
2835
|
+
}
|
|
2836
|
+
const withAccount = spansAccounts(targets);
|
|
2837
|
+
const rows = [];
|
|
2838
|
+
for (const ws of targets) {
|
|
2839
|
+
configureClient(ws.id, ws.account);
|
|
2840
|
+
const res = await call();
|
|
2841
|
+
if (res.error !== void 0) {
|
|
2842
|
+
const where = withAccount ? `${ws.account.name} / ${ws.name}` : ws.name;
|
|
2843
|
+
console.error(`(${where}: ${describeError(res)})`);
|
|
2844
|
+
continue;
|
|
2845
|
+
}
|
|
2846
|
+
for (const row of res.data?.data ?? [])
|
|
2847
|
+
rows.push(tagRow(ws, row, withAccount));
|
|
2848
|
+
}
|
|
2849
|
+
return rows;
|
|
2850
|
+
}
|
|
2235
2851
|
function registerReports(program2) {
|
|
2236
2852
|
const root = program2.command("reports").description("KPIs and exports");
|
|
2237
|
-
root.command("summary").description("Workspace KPI summary").option("--period <p>", "7d | 30d | 90d | 1y", "30d").option(
|
|
2853
|
+
root.command("summary").description("Workspace KPI summary").option("--period <p>", "7d | 30d | 90d | 1y", "30d").option(
|
|
2854
|
+
"--workspace <id>",
|
|
2855
|
+
"read only this workspace (default: all you can access)"
|
|
2856
|
+
).option("--json", "raw JSON output").action(async (opts) => {
|
|
2238
2857
|
configureClient(opts.workspace);
|
|
2239
2858
|
const body = unwrap(
|
|
2240
2859
|
await getReportsSummary({ query: { period: opts.period } })
|
|
2241
2860
|
);
|
|
2242
|
-
|
|
2861
|
+
printExport(body.data, opts);
|
|
2243
2862
|
});
|
|
2244
2863
|
root.command("reconciliation").description("CFO reconciliation: Mercado Pago vs sale vs Siigo invoice").requiredOption("--from <date>", "date_from (ISO 8601)").requiredOption("--to <date>", "date_to (ISO 8601)").option(
|
|
2245
2864
|
"--match <status>",
|
|
2246
2865
|
"OK | MISSING_INVOICE | MISSING_CUFE | AMOUNT_MISMATCH | MISSING_PAYMENT"
|
|
2247
|
-
).option("--provider <p>", "filter by payment provider").option("--page <n>", "page number").option("--page-size <n>", "rows per page").option(
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2866
|
+
).option("--provider <p>", "filter by payment provider").option("--page <n>", "page number").option("--page-size <n>", "rows per page").option(
|
|
2867
|
+
"--workspace <id>",
|
|
2868
|
+
"read only this workspace (default: all you can access)"
|
|
2869
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2870
|
+
const rows = await reportRows(
|
|
2871
|
+
opts.workspace,
|
|
2872
|
+
() => getReportsReconciliation({
|
|
2251
2873
|
query: {
|
|
2252
2874
|
date_from: opts.from,
|
|
2253
2875
|
date_to: opts.to,
|
|
@@ -2258,35 +2880,44 @@ function registerReports(program2) {
|
|
|
2258
2880
|
}
|
|
2259
2881
|
})
|
|
2260
2882
|
);
|
|
2261
|
-
|
|
2883
|
+
printExport(rows, opts);
|
|
2262
2884
|
});
|
|
2263
2885
|
root.command("financials").description(
|
|
2264
2886
|
"Per-function P&L: gross, platform fee, facial, payment fee, 4x1000, net to settle"
|
|
2265
|
-
).option("--event <id>", "filter by event").option("--past", "only functions that already happened (settleable)").option(
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2887
|
+
).option("--event <id>", "filter by event").option("--past", "only functions that already happened (settleable)").option(
|
|
2888
|
+
"--workspace <id>",
|
|
2889
|
+
"read only this workspace (default: all you can access)"
|
|
2890
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2891
|
+
const rows = await reportRows(
|
|
2892
|
+
opts.workspace,
|
|
2893
|
+
() => getReportsFinancials({
|
|
2269
2894
|
query: {
|
|
2270
2895
|
event: opts.event,
|
|
2271
2896
|
past: opts.past === void 0 ? void 0 : opts.past ? "true" : "false"
|
|
2272
2897
|
}
|
|
2273
2898
|
})
|
|
2274
2899
|
);
|
|
2275
|
-
printExport(
|
|
2276
|
-
});
|
|
2277
|
-
root.command("by-event").description("Revenue / tickets sold / availability grouped by event").option("--from <date>", "created from (ISO 8601)").option("--to <date>", "created to (ISO 8601)").option("--status <s>", "filter by sale status").option(
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2900
|
+
printExport(rows, opts);
|
|
2901
|
+
});
|
|
2902
|
+
root.command("by-event").description("Revenue / tickets sold / availability grouped by event").option("--from <date>", "created from (ISO 8601)").option("--to <date>", "created to (ISO 8601)").option("--status <s>", "filter by sale status").option(
|
|
2903
|
+
"--workspace <id>",
|
|
2904
|
+
"read only this workspace (default: all you can access)"
|
|
2905
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2906
|
+
const rows = await reportRows(
|
|
2907
|
+
opts.workspace,
|
|
2908
|
+
() => getReportsByEvent({
|
|
2281
2909
|
query: { from: opts.from, to: opts.to, status: opts.status }
|
|
2282
2910
|
})
|
|
2283
2911
|
);
|
|
2284
|
-
|
|
2285
|
-
});
|
|
2286
|
-
root.command("timeseries").description("Revenue / tickets sold bucketed over time").requiredOption("--interval <i>", "day | week | month").option("--from <date>", "created from (ISO 8601)").option("--to <date>", "created to (ISO 8601)").option("--event <id>", "filter by event").option(
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2912
|
+
printExport(rows, opts);
|
|
2913
|
+
});
|
|
2914
|
+
root.command("timeseries").description("Revenue / tickets sold bucketed over time").requiredOption("--interval <i>", "day | week | month").option("--from <date>", "created from (ISO 8601)").option("--to <date>", "created to (ISO 8601)").option("--event <id>", "filter by event").option(
|
|
2915
|
+
"--workspace <id>",
|
|
2916
|
+
"read only this workspace (default: all you can access)"
|
|
2917
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2918
|
+
const rows = await reportRows(
|
|
2919
|
+
opts.workspace,
|
|
2920
|
+
() => getReportsTimeseries({
|
|
2290
2921
|
query: {
|
|
2291
2922
|
interval: opts.interval,
|
|
2292
2923
|
from: opts.from,
|
|
@@ -2295,15 +2926,18 @@ function registerReports(program2) {
|
|
|
2295
2926
|
}
|
|
2296
2927
|
})
|
|
2297
2928
|
);
|
|
2298
|
-
|
|
2299
|
-
});
|
|
2300
|
-
root.command("inventory").description("Capacity / sold / reserved / available per event\xB7date\xB7type").option("--event
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2929
|
+
printExport(rows, opts);
|
|
2930
|
+
});
|
|
2931
|
+
root.command("inventory").description("Capacity / sold / reserved / available per event\xB7date\xB7type").option("--event <id>", "filter by event").option("--event-date <id>", "filter by event date").option("--event-id <id>", "alias of --event").option("--event-date-id <id>", "alias of --event-date").option("--from <date>", "date from (ISO 8601)").option("--to <date>", "date to (ISO 8601)").option("--include-drafts", "include draft events").option("--group-by <g>", "ticketType | date | event").option(
|
|
2932
|
+
"--workspace <id>",
|
|
2933
|
+
"read only this workspace (default: all you can access)"
|
|
2934
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2935
|
+
const rows = await reportRows(
|
|
2936
|
+
opts.workspace,
|
|
2937
|
+
() => getReportsInventory({
|
|
2304
2938
|
query: {
|
|
2305
|
-
eventId: opts.eventId,
|
|
2306
|
-
eventDateId: opts.eventDateId,
|
|
2939
|
+
eventId: opts.event ?? opts.eventId,
|
|
2940
|
+
eventDateId: opts.eventDate ?? opts.eventDateId,
|
|
2307
2941
|
from: opts.from,
|
|
2308
2942
|
to: opts.to,
|
|
2309
2943
|
includeDrafts: opts.includeDrafts,
|
|
@@ -2311,12 +2945,15 @@ function registerReports(program2) {
|
|
|
2311
2945
|
}
|
|
2312
2946
|
})
|
|
2313
2947
|
);
|
|
2314
|
-
|
|
2948
|
+
printExport(rows, opts);
|
|
2315
2949
|
});
|
|
2316
2950
|
const exp = root.command("export").description(
|
|
2317
2951
|
"Export buyers, attendees, subscribers or reconciliation (CSV)"
|
|
2318
2952
|
);
|
|
2319
|
-
exp.command("reconciliation").description("Export reconciliation (CSV) for accounting").requiredOption("--from <date>", "date_from (ISO 8601)").requiredOption("--to <date>", "date_to (ISO 8601)").option("--match <status>", "filter by match_status").option("--provider <p>", "filter by payment provider").option(
|
|
2953
|
+
exp.command("reconciliation").description("Export reconciliation (CSV) for accounting").requiredOption("--from <date>", "date_from (ISO 8601)").requiredOption("--to <date>", "date_to (ISO 8601)").option("--match <status>", "filter by match_status").option("--provider <p>", "filter by payment provider").option(
|
|
2954
|
+
"--workspace <id>",
|
|
2955
|
+
"read only this workspace (default: all you can access)"
|
|
2956
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2320
2957
|
configureClient(opts.workspace);
|
|
2321
2958
|
const body = unwrap(
|
|
2322
2959
|
await getReportsExportsReconciliation({
|
|
@@ -2334,7 +2971,10 @@ function registerReports(program2) {
|
|
|
2334
2971
|
["buyers", getReportsExportsBuyers, "buyers (one row per sale)"],
|
|
2335
2972
|
["attendees", getReportsExportsAttendees, "attendees (one row per ticket)"]
|
|
2336
2973
|
]) {
|
|
2337
|
-
exp.command(name).description(`Export ${label}`).option("--event <id>", "filter by event").option("--event-date <id>", "filter by event date").option("--from <date>", "created from (ISO 8601)").option("--to <date>", "created to (ISO 8601)").option("--status <s>", "filter by sale status").option(
|
|
2974
|
+
exp.command(name).description(`Export ${label}`).option("--event <id>", "filter by event").option("--event-date <id>", "filter by event date").option("--from <date>", "created from (ISO 8601)").option("--to <date>", "created to (ISO 8601)").option("--status <s>", "filter by sale status").option(
|
|
2975
|
+
"--workspace <id>",
|
|
2976
|
+
"read only this workspace (default: all you can access)"
|
|
2977
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2338
2978
|
configureClient(opts.workspace);
|
|
2339
2979
|
const body = unwrap(
|
|
2340
2980
|
await fn({
|
|
@@ -2350,7 +2990,10 @@ function registerReports(program2) {
|
|
|
2350
2990
|
printExport(body.data ?? body, opts);
|
|
2351
2991
|
});
|
|
2352
2992
|
}
|
|
2353
|
-
exp.command("subscribers").description("Export subscribers").option(
|
|
2993
|
+
exp.command("subscribers").description("Export subscribers").option(
|
|
2994
|
+
"--workspace <id>",
|
|
2995
|
+
"read only this workspace (default: all you can access)"
|
|
2996
|
+
).option("--json", "raw JSON output").option("--csv", "CSV output (for spreadsheets/accounting)").action(async (opts) => {
|
|
2354
2997
|
configureClient(opts.workspace);
|
|
2355
2998
|
const body = unwrap(await getReportsExportsSubscribers({}));
|
|
2356
2999
|
printExport(body.data ?? body, opts);
|
|
@@ -2371,19 +3014,21 @@ function printExport(rows, opts) {
|
|
|
2371
3014
|
}
|
|
2372
3015
|
|
|
2373
3016
|
// src/commands/resource.ts
|
|
2374
|
-
|
|
2375
|
-
return flag ?? loadConfig().workspaceId;
|
|
2376
|
-
}
|
|
3017
|
+
import chalk7 from "chalk";
|
|
2377
3018
|
function registerResource(program2, spec) {
|
|
2378
3019
|
const root = program2.command(spec.name).description(spec.describe);
|
|
2379
3020
|
const singular = spec.name.replace(/s$/, "");
|
|
2380
3021
|
if (spec.list) {
|
|
2381
3022
|
const list = spec.list;
|
|
2382
|
-
const cmd = root.command("list").description(
|
|
3023
|
+
const cmd = root.command("list").description(
|
|
3024
|
+
`List ${spec.name} from every workspace you can reach (--workspace narrows it to one)`
|
|
3025
|
+
).option("--limit <n>", "results per page (1-100)", "20").option("--cursor <id>", "pagination cursor").option("--all", "auto-paginate: fetch every page (ignores --cursor)").option("--columns <list>", "comma-separated columns to display").option("--full", "show every field instead of the curated columns").option(
|
|
3026
|
+
"--workspace <id>",
|
|
3027
|
+
"read only this workspace (default: all you can access)"
|
|
3028
|
+
).option("--csv", "CSV output (for spreadsheets/accounting)").option("--json", "raw JSON output (data only)").option("--raw", "raw JSON output including pagination metadata (page)");
|
|
2383
3029
|
for (const f of spec.listFlags ?? []) cmd.option(f.flag, f.describe);
|
|
2384
3030
|
cmd.action(async (opts) => {
|
|
2385
|
-
|
|
2386
|
-
const columns = resolveColumns(opts, spec.columns);
|
|
3031
|
+
let columns = resolveColumns(opts, spec.columns);
|
|
2387
3032
|
const query = {
|
|
2388
3033
|
limit: Number(opts.limit),
|
|
2389
3034
|
cursor: opts.cursor
|
|
@@ -2392,6 +3037,68 @@ function registerResource(program2, spec) {
|
|
|
2392
3037
|
const v = opts[camel2(f.query)];
|
|
2393
3038
|
if (v !== void 0) query[f.query] = v;
|
|
2394
3039
|
}
|
|
3040
|
+
const targets = await listTargets(opts.workspace);
|
|
3041
|
+
if (targets) {
|
|
3042
|
+
if (opts.cursor)
|
|
3043
|
+
fail(
|
|
3044
|
+
"--cursor needs a single workspace: a cursor from one workspace means nothing in another.",
|
|
3045
|
+
"Pin one with --workspace <id> (or `ft workspace use <slug>`), then paginate."
|
|
3046
|
+
);
|
|
3047
|
+
const limit = opts.all ? Number(opts.limit) : splitLimit(Number(opts.limit), targets.length);
|
|
3048
|
+
const withAccount = spansAccounts(targets);
|
|
3049
|
+
const rows = [];
|
|
3050
|
+
for (const ws of targets) {
|
|
3051
|
+
configureClient(ws.id, ws.account);
|
|
3052
|
+
let cursor;
|
|
3053
|
+
let page;
|
|
3054
|
+
do {
|
|
3055
|
+
const res = await list({ query: { ...query, limit, cursor } });
|
|
3056
|
+
if (res.error !== void 0) {
|
|
3057
|
+
console.error(
|
|
3058
|
+
chalk7.dim(
|
|
3059
|
+
`(${withAccount ? `${ws.account.name} / ` : ""}${ws.name}: ${describeError(res)})`
|
|
3060
|
+
)
|
|
3061
|
+
);
|
|
3062
|
+
break;
|
|
3063
|
+
}
|
|
3064
|
+
const body2 = res.data;
|
|
3065
|
+
for (const row of body2?.data ?? [])
|
|
3066
|
+
rows.push(tagRow(ws, row, withAccount));
|
|
3067
|
+
page = body2?.page;
|
|
3068
|
+
cursor = page?.nextCursor ?? void 0;
|
|
3069
|
+
} while (opts.all && page?.hasMore && cursor);
|
|
3070
|
+
}
|
|
3071
|
+
const max = Number(opts.limit);
|
|
3072
|
+
const out = !opts.all && Number.isFinite(max) && max > 0 ? rows.slice(0, max) : rows;
|
|
3073
|
+
if (columns && !columns.includes("workspaceName"))
|
|
3074
|
+
columns = ["workspaceName", ...columns];
|
|
3075
|
+
if (columns && withAccount && !columns.includes("account"))
|
|
3076
|
+
columns = ["account", ...columns];
|
|
3077
|
+
if (opts.raw) {
|
|
3078
|
+
print(
|
|
3079
|
+
{ data: out, page: { nextCursor: null, hasMore: false } },
|
|
3080
|
+
{ json: true }
|
|
3081
|
+
);
|
|
3082
|
+
return;
|
|
3083
|
+
}
|
|
3084
|
+
if (opts.csv) {
|
|
3085
|
+
process.stdout.write(`${toCsv(out, columns)}
|
|
3086
|
+
`);
|
|
3087
|
+
return;
|
|
3088
|
+
}
|
|
3089
|
+
print(out, {
|
|
3090
|
+
json: opts.json,
|
|
3091
|
+
columns,
|
|
3092
|
+
columnsExplicit: Boolean(opts.columns)
|
|
3093
|
+
});
|
|
3094
|
+
if (out.length === 0)
|
|
3095
|
+
console.error(
|
|
3096
|
+
chalk7.dim(
|
|
3097
|
+
`(0 results across ${targets.length} workspaces - check them with \`ft workspace list\`)`
|
|
3098
|
+
)
|
|
3099
|
+
);
|
|
3100
|
+
return;
|
|
3101
|
+
}
|
|
2395
3102
|
if (opts.all) {
|
|
2396
3103
|
const rows = [];
|
|
2397
3104
|
let cursor;
|
|
@@ -2419,7 +3126,7 @@ function registerResource(program2, spec) {
|
|
|
2419
3126
|
columns,
|
|
2420
3127
|
columnsExplicit: Boolean(opts.columns)
|
|
2421
3128
|
});
|
|
2422
|
-
if (rows.length === 0) printEmptyScope(
|
|
3129
|
+
if (rows.length === 0) printEmptyScope(opts.workspace);
|
|
2423
3130
|
return;
|
|
2424
3131
|
}
|
|
2425
3132
|
const body = unwrap(await list({ query }));
|
|
@@ -2437,14 +3144,16 @@ function registerResource(program2, spec) {
|
|
|
2437
3144
|
columns,
|
|
2438
3145
|
columnsExplicit: Boolean(opts.columns)
|
|
2439
3146
|
});
|
|
2440
|
-
if ((body.data ?? []).length === 0)
|
|
2441
|
-
printEmptyScope(activeWorkspace(opts.workspace));
|
|
3147
|
+
if ((body.data ?? []).length === 0) printEmptyScope(opts.workspace);
|
|
2442
3148
|
if (!opts.json) printNextCursor(body.page);
|
|
2443
3149
|
});
|
|
2444
3150
|
}
|
|
2445
3151
|
if (spec.get) {
|
|
2446
3152
|
const get = spec.get;
|
|
2447
|
-
root.command("get <id>").description(`Get one ${singular} by id`).option(
|
|
3153
|
+
root.command("get <id>").description(`Get one ${singular} by id`).option(
|
|
3154
|
+
"--workspace <id>",
|
|
3155
|
+
"read only this workspace (default: all you can access)"
|
|
3156
|
+
).option("--json", "raw JSON output").action(async (id, opts) => {
|
|
2448
3157
|
configureClient(opts.workspace);
|
|
2449
3158
|
const body = unwrap(await get({ path: { id } }));
|
|
2450
3159
|
print(body.data, { json: opts.json });
|
|
@@ -2452,7 +3161,10 @@ function registerResource(program2, spec) {
|
|
|
2452
3161
|
}
|
|
2453
3162
|
if (spec.create) {
|
|
2454
3163
|
const create = spec.create;
|
|
2455
|
-
root.command("create").description(`Create a ${singular}`).requiredOption("--data <json>", "JSON body (inline or @file.json)").option(
|
|
3164
|
+
root.command("create").description(`Create a ${singular}`).requiredOption("--data <json>", "JSON body (inline or @file.json)").option(
|
|
3165
|
+
"--workspace <id>",
|
|
3166
|
+
"read only this workspace (default: all you can access)"
|
|
3167
|
+
).option("--json", "raw JSON output").action(async (opts) => {
|
|
2456
3168
|
configureClient(opts.workspace);
|
|
2457
3169
|
const body = unwrap(await create({ body: parseData(opts.data) }));
|
|
2458
3170
|
print(body.data ?? body, { json: opts.json });
|
|
@@ -2460,7 +3172,10 @@ function registerResource(program2, spec) {
|
|
|
2460
3172
|
}
|
|
2461
3173
|
if (spec.update) {
|
|
2462
3174
|
const update = spec.update;
|
|
2463
|
-
root.command("update <id>").description(`Update a ${singular} by id`).requiredOption("--data <json>", "JSON body (inline or @file.json)").option(
|
|
3175
|
+
root.command("update <id>").description(`Update a ${singular} by id`).requiredOption("--data <json>", "JSON body (inline or @file.json)").option(
|
|
3176
|
+
"--workspace <id>",
|
|
3177
|
+
"read only this workspace (default: all you can access)"
|
|
3178
|
+
).option("--json", "raw JSON output").action(async (id, opts) => {
|
|
2464
3179
|
configureClient(opts.workspace);
|
|
2465
3180
|
const body = unwrap(
|
|
2466
3181
|
await update({ path: { id }, body: parseData(opts.data) })
|
|
@@ -2470,7 +3185,10 @@ function registerResource(program2, spec) {
|
|
|
2470
3185
|
}
|
|
2471
3186
|
if (spec.del) {
|
|
2472
3187
|
const del = spec.del;
|
|
2473
|
-
root.command("delete <id>").description(`Delete a ${singular} by id`).option("--yes", "skip confirmation").option(
|
|
3188
|
+
root.command("delete <id>").description(`Delete a ${singular} by id`).option("--yes", "skip confirmation").option(
|
|
3189
|
+
"--workspace <id>",
|
|
3190
|
+
"read only this workspace (default: all you can access)"
|
|
3191
|
+
).option("--json", "raw JSON output").action(async (id, opts) => {
|
|
2474
3192
|
await confirmOrExit(`Delete ${singular} ${id}?`, opts.yes);
|
|
2475
3193
|
configureClient(opts.workspace);
|
|
2476
3194
|
const body = unwrap(await del({ path: { id } }));
|
|
@@ -2478,7 +3196,10 @@ function registerResource(program2, spec) {
|
|
|
2478
3196
|
});
|
|
2479
3197
|
}
|
|
2480
3198
|
for (const action of spec.actions ?? []) {
|
|
2481
|
-
const cmd = root.command(`${action.name} <id>`).description(action.describe).option(
|
|
3199
|
+
const cmd = root.command(`${action.name} <id>`).description(action.describe).option(
|
|
3200
|
+
"--workspace <id>",
|
|
3201
|
+
"read only this workspace (default: all you can access)"
|
|
3202
|
+
).option("--json", "raw JSON output");
|
|
2482
3203
|
if (action.body) {
|
|
2483
3204
|
cmd.option("--data <json>", "JSON body (inline or @file.json)");
|
|
2484
3205
|
}
|
|
@@ -2525,147 +3246,124 @@ function registerTickets(program2) {
|
|
|
2525
3246
|
}
|
|
2526
3247
|
|
|
2527
3248
|
// src/commands/workspace.ts
|
|
2528
|
-
import
|
|
3249
|
+
import chalk8 from "chalk";
|
|
2529
3250
|
function registerWorkspace(program2) {
|
|
2530
|
-
const root = program2.command("workspace").description("List,
|
|
2531
|
-
root.command("list").alias("ls").description(
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
const
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
3251
|
+
const root = program2.command("workspace").description("List, pin, unpin, and show workspaces");
|
|
3252
|
+
root.command("list").alias("ls").description(
|
|
3253
|
+
"List the workspaces you can access, across every account in scope (marks the pinned one)"
|
|
3254
|
+
).option("--account <name>", "only this account (default: all stored)").option("--json", "raw JSON output").action(async (opts) => {
|
|
3255
|
+
const accounts = accountsInScope();
|
|
3256
|
+
if (accounts.length === 0) resolveAccount();
|
|
3257
|
+
const withAccount = accounts.length > 1;
|
|
3258
|
+
const rows = [];
|
|
3259
|
+
const errors = [];
|
|
3260
|
+
let answered = 0;
|
|
3261
|
+
for (const account of accounts) {
|
|
3262
|
+
configureClient(void 0, account);
|
|
3263
|
+
const res = await getMe({});
|
|
3264
|
+
if (!res.data && withAccount) {
|
|
3265
|
+
errors.push(`account ${account.name}: ${describeError(res)}`);
|
|
3266
|
+
continue;
|
|
3267
|
+
}
|
|
3268
|
+
answered++;
|
|
3269
|
+
const me = unwrap(res).data;
|
|
3270
|
+
const activeId = account.workspaceId ?? me.activeWorkspaceId;
|
|
3271
|
+
for (const w of me.workspaces)
|
|
3272
|
+
rows.push({
|
|
3273
|
+
...withAccount ? { account: account.name } : {},
|
|
3274
|
+
active: w.id === activeId ? "*" : "",
|
|
3275
|
+
id: w.id,
|
|
3276
|
+
name: w.name,
|
|
3277
|
+
slug: w.slug,
|
|
3278
|
+
role: w.role,
|
|
3279
|
+
access: w.sections === null ? "full" : w.sections.length === 0 ? "revoked" : w.sections.join("|")
|
|
3280
|
+
});
|
|
3281
|
+
}
|
|
3282
|
+
failIfNoneAnswered(answered, errors);
|
|
2543
3283
|
print(rows, {
|
|
2544
3284
|
json: opts.json,
|
|
2545
|
-
columns: [
|
|
3285
|
+
columns: [
|
|
3286
|
+
...withAccount ? ["account"] : [],
|
|
3287
|
+
"active",
|
|
3288
|
+
"id",
|
|
3289
|
+
"name",
|
|
3290
|
+
"slug",
|
|
3291
|
+
"role",
|
|
3292
|
+
"access"
|
|
3293
|
+
]
|
|
2546
3294
|
});
|
|
2547
3295
|
});
|
|
2548
3296
|
root.command("use <idOrSlug>").description(
|
|
2549
|
-
"
|
|
2550
|
-
).action(async (idOrSlug) => {
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
const
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
3297
|
+
"Pin every command to one workspace, and to the account that reaches it (persisted in ~/.freeticket/config.json)"
|
|
3298
|
+
).option("--account <name>", "only look in this account").action(async (idOrSlug) => {
|
|
3299
|
+
const accounts = accountsInScope();
|
|
3300
|
+
if (accounts.length === 0) resolveAccount();
|
|
3301
|
+
const slugs = [];
|
|
3302
|
+
for (const account of accounts) {
|
|
3303
|
+
configureClient(void 0, account);
|
|
3304
|
+
const res = await getMe({});
|
|
3305
|
+
if (!res.data && accounts.length > 1) continue;
|
|
3306
|
+
const me = unwrap(res).data;
|
|
3307
|
+
const match = me.workspaces.find(
|
|
3308
|
+
(w) => w.id === idOrSlug || w.slug === idOrSlug
|
|
3309
|
+
);
|
|
3310
|
+
slugs.push(...me.workspaces.map((w) => w.slug));
|
|
3311
|
+
if (!match) continue;
|
|
3312
|
+
const stored = readConfig().accounts?.[account.name];
|
|
3313
|
+
if (stored)
|
|
3314
|
+
saveAccount(account.name, { ...stored, workspaceId: match.id });
|
|
3315
|
+
if (account.name !== ENV_ACCOUNT && Object.keys(readConfig().accounts ?? {}).length > 1)
|
|
3316
|
+
saveConfig({ activeAccount: account.name });
|
|
3317
|
+
console.log(
|
|
3318
|
+
`${chalk8.green("\u2713")} Active workspace: ${chalk8.bold(match.name)} ${chalk8.dim(`(${match.slug})`)}` + (accounts.length > 1 ? ` ${chalk8.dim(`via ${account.name}`)}` : "")
|
|
2560
3319
|
);
|
|
3320
|
+
return;
|
|
3321
|
+
}
|
|
3322
|
+
fail(
|
|
3323
|
+
`No workspace matches "${idOrSlug}".`,
|
|
3324
|
+
`Available: ${slugs.join(", ") || "(none)"}`
|
|
3325
|
+
);
|
|
3326
|
+
});
|
|
3327
|
+
root.command("clear").description(
|
|
3328
|
+
"Unpin: go back to reading every workspace (and every account) you can access"
|
|
3329
|
+
).option("--account <name>", "only unpin this account's workspace").action((opts) => {
|
|
3330
|
+
const file = readConfig();
|
|
3331
|
+
const names = opts.account ? [opts.account] : Object.keys(file.accounts ?? {});
|
|
3332
|
+
for (const name of names) {
|
|
3333
|
+
const stored = file.accounts?.[name];
|
|
3334
|
+
if (!stored) fail(`No account named "${name}".`);
|
|
3335
|
+
saveAccount(name, { ...stored, workspaceId: void 0 });
|
|
2561
3336
|
}
|
|
2562
|
-
|
|
3337
|
+
if (!opts.account)
|
|
3338
|
+
saveConfig({ activeAccount: void 0, workspaceId: void 0 });
|
|
2563
3339
|
console.log(
|
|
2564
|
-
`${
|
|
3340
|
+
`${chalk8.green("\u2713")} No workspace pinned: lists now read all of them.`
|
|
2565
3341
|
);
|
|
2566
3342
|
});
|
|
2567
|
-
root.command("show").description("Show the active workspace").option(
|
|
2568
|
-
|
|
2569
|
-
|
|
3343
|
+
root.command("show").description("Show the active workspace").option(
|
|
3344
|
+
"--account <name>",
|
|
3345
|
+
"the account to ask (needed when several are stored)"
|
|
3346
|
+
).option("--json", "raw JSON output").action(async (opts) => {
|
|
3347
|
+
const account = resolveAccount();
|
|
3348
|
+
configureClient(void 0, account);
|
|
2570
3349
|
const me = unwrap(await getMe({})).data;
|
|
2571
|
-
const activeId =
|
|
3350
|
+
const activeId = account.workspaceId ?? me.activeWorkspaceId;
|
|
2572
3351
|
const active = me.workspaces.find((w) => w.id === activeId);
|
|
2573
3352
|
if (!active) {
|
|
2574
3353
|
fail(
|
|
2575
|
-
"No
|
|
2576
|
-
"Pick one with `ft workspace use <slug
|
|
3354
|
+
"No workspace pinned and the account has no default one.",
|
|
3355
|
+
"Pick one with `ft workspace use <slug>`, or list them with `ft workspace list`."
|
|
2577
3356
|
);
|
|
2578
3357
|
}
|
|
2579
3358
|
print(active, { json: opts.json });
|
|
2580
3359
|
});
|
|
2581
3360
|
}
|
|
2582
3361
|
|
|
2583
|
-
// src/
|
|
2584
|
-
import { Chalk } from "chalk";
|
|
2585
|
-
var YELLOW = "#FFD500";
|
|
2586
|
-
var c = new Chalk({ level: 3 });
|
|
2587
|
-
var ART = String.raw`
|
|
2588
|
-
___ ___ ___ ___ _____ ___ ___ _ _____ _____
|
|
2589
|
-
| __| _ \ __| __|_ _|_ _/ __| |/ / __|_ _|
|
|
2590
|
-
| _|| / _|| _| | | | | (__| ' <| _| | |
|
|
2591
|
-
|_| |_|_\___|___| |_| |___\___|_|\_\___| |_|
|
|
2592
|
-
`;
|
|
2593
|
-
function banner() {
|
|
2594
|
-
return [
|
|
2595
|
-
c.hex(YELLOW)(ART),
|
|
2596
|
-
c.hex(YELLOW)(" \u25CF ") + c.hex(YELLOW).bold("FreeTicket") + c.hex(YELLOW)(" \u2014 events, tickets, and sales from your terminal"),
|
|
2597
|
-
""
|
|
2598
|
-
].join("\n");
|
|
2599
|
-
}
|
|
2600
|
-
|
|
2601
|
-
// src/lib/update-check.ts
|
|
2602
|
-
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
2603
|
-
import { homedir as homedir2 } from "os";
|
|
2604
|
-
import { dirname as dirname2, join as join2 } from "path";
|
|
2605
|
-
import chalk6 from "chalk";
|
|
2606
|
-
var CACHE_PATH = join2(homedir2(), ".freeticket", "update-check.json");
|
|
2607
|
-
var REGISTRY = "https://registry.npmjs.org/@freeticket/cli/latest";
|
|
2608
|
-
var ONE_DAY = 24 * 60 * 60 * 1e3;
|
|
2609
|
-
var FETCH_TIMEOUT = 1500;
|
|
2610
|
-
async function notifyUpdate(current) {
|
|
2611
|
-
if (!process.stderr.isTTY || process.env.FT_NO_UPDATE_CHECK || process.env.CI) {
|
|
2612
|
-
return;
|
|
2613
|
-
}
|
|
2614
|
-
try {
|
|
2615
|
-
const latest = await getLatest();
|
|
2616
|
-
if (latest && isNewer(latest, current)) {
|
|
2617
|
-
process.stderr.write(
|
|
2618
|
-
`
|
|
2619
|
-
${chalk6.yellow(`\u26A0 Update available: @freeticket/cli ${current} \u2192 ${latest}`)}
|
|
2620
|
-
${chalk6.dim(" npm i -g @freeticket/cli@latest \xB7 or use npx @freeticket/cli@latest")}
|
|
2621
|
-
|
|
2622
|
-
`
|
|
2623
|
-
);
|
|
2624
|
-
}
|
|
2625
|
-
} catch {
|
|
2626
|
-
}
|
|
2627
|
-
}
|
|
2628
|
-
async function getLatest() {
|
|
2629
|
-
const cached = readCache();
|
|
2630
|
-
if (cached && Date.now() - cached.at < ONE_DAY) return cached.latest;
|
|
2631
|
-
const res = await fetch(REGISTRY, {
|
|
2632
|
-
signal: AbortSignal.timeout(FETCH_TIMEOUT),
|
|
2633
|
-
headers: { Accept: "application/vnd.npm.install-v1+json" }
|
|
2634
|
-
});
|
|
2635
|
-
if (!res.ok) throw new Error(`registry ${res.status}`);
|
|
2636
|
-
const latest = (await res.json()).version ?? null;
|
|
2637
|
-
if (latest) writeCache({ at: Date.now(), latest });
|
|
2638
|
-
return latest;
|
|
2639
|
-
}
|
|
2640
|
-
function readCache() {
|
|
2641
|
-
if (!existsSync2(CACHE_PATH)) return null;
|
|
2642
|
-
try {
|
|
2643
|
-
return JSON.parse(readFileSync3(CACHE_PATH, "utf8"));
|
|
2644
|
-
} catch {
|
|
2645
|
-
return null;
|
|
2646
|
-
}
|
|
2647
|
-
}
|
|
2648
|
-
function writeCache(v) {
|
|
2649
|
-
try {
|
|
2650
|
-
mkdirSync2(dirname2(CACHE_PATH), { recursive: true });
|
|
2651
|
-
writeFileSync2(CACHE_PATH, JSON.stringify(v));
|
|
2652
|
-
} catch {
|
|
2653
|
-
}
|
|
2654
|
-
}
|
|
2655
|
-
function isNewer(a, b) {
|
|
2656
|
-
const parse = (v) => {
|
|
2657
|
-
const [maj, min, pat] = (v.replace(/^v/, "").split("-")[0] ?? "").split(".").map((n) => Number.parseInt(n, 10) || 0);
|
|
2658
|
-
return [maj ?? 0, min ?? 0, pat ?? 0];
|
|
2659
|
-
};
|
|
2660
|
-
const [aM, aN, aP] = parse(a);
|
|
2661
|
-
const [bM, bN, bP] = parse(b);
|
|
2662
|
-
return aM > bM || aM === bM && (aN > bN || aN === bN && aP > bP);
|
|
2663
|
-
}
|
|
2664
|
-
|
|
2665
|
-
// src/index.ts
|
|
3362
|
+
// src/program.ts
|
|
2666
3363
|
var program = new Command();
|
|
2667
3364
|
program.name("ft").description("Official FreeTicket CLI").version(package_default.version, "-v, --version").addHelpText("beforeAll", banner());
|
|
2668
3365
|
registerAuth(program);
|
|
3366
|
+
registerAccount(program);
|
|
2669
3367
|
registerWorkspace(program);
|
|
2670
3368
|
registerResource(program, {
|
|
2671
3369
|
name: "events",
|
|
@@ -2736,7 +3434,10 @@ registerResource(program, {
|
|
|
2736
3434
|
},
|
|
2737
3435
|
{
|
|
2738
3436
|
name: "refund",
|
|
2739
|
-
|
|
3437
|
+
// The contract's SaleRefundRequest only takes `acknowledge_manual` and is
|
|
3438
|
+
// additionalProperties:false, so there is no partial-amount field to pass.
|
|
3439
|
+
// The old text promised one and earned a guaranteed 422 (issue #42).
|
|
3440
|
+
describe: `Refund a sale (--data '{"acknowledge_manual":true}' for a manual/offline refund)`,
|
|
2740
3441
|
fn: postSalesIdRefund,
|
|
2741
3442
|
body: true
|
|
2742
3443
|
},
|
|
@@ -2880,10 +3581,58 @@ registerResource(program, {
|
|
|
2880
3581
|
}
|
|
2881
3582
|
]
|
|
2882
3583
|
});
|
|
3584
|
+
registerResource(program, {
|
|
3585
|
+
name: "content-videos",
|
|
3586
|
+
describe: "Organization videos",
|
|
3587
|
+
list: getContentVideos,
|
|
3588
|
+
columns: ["id", "title", "publishedAt", "memberOnly"]
|
|
3589
|
+
});
|
|
3590
|
+
registerResource(program, {
|
|
3591
|
+
name: "content-posts",
|
|
3592
|
+
describe: "Organization posts",
|
|
3593
|
+
list: getContentPosts,
|
|
3594
|
+
columns: ["id", "title", "publishedAt", "memberOnly"]
|
|
3595
|
+
});
|
|
3596
|
+
registerResource(program, {
|
|
3597
|
+
name: "content-lives",
|
|
3598
|
+
describe: "Organization live streams",
|
|
3599
|
+
list: getContentLives,
|
|
3600
|
+
get: getContentLivesId,
|
|
3601
|
+
columns: ["id", "title", "status", "startsAt", "memberOnly"],
|
|
3602
|
+
extend: (root) => {
|
|
3603
|
+
root.command("playback-token").description(
|
|
3604
|
+
"Mint a signed playback token (30 min live / 1 h video). `memberOnly` content needs a buyer session."
|
|
3605
|
+
).requiredOption("--data <json>", "JSON body (inline or @file.json)").option("--workspace <id>", "workspace override").option("--json", "raw JSON output").action(async (opts) => {
|
|
3606
|
+
configureClient(opts.workspace);
|
|
3607
|
+
const body = unwrap(
|
|
3608
|
+
await postContentPlaybackToken({
|
|
3609
|
+
body: parseData(opts.data)
|
|
3610
|
+
})
|
|
3611
|
+
);
|
|
3612
|
+
print(body?.data ?? body, { json: opts.json });
|
|
3613
|
+
});
|
|
3614
|
+
}
|
|
3615
|
+
});
|
|
2883
3616
|
registerTickets(program);
|
|
2884
3617
|
registerReports(program);
|
|
2885
3618
|
registerApiKeys(program);
|
|
2886
3619
|
registerAdmin(program);
|
|
3620
|
+
function addAccountOption(cmd) {
|
|
3621
|
+
if (cmd.name() === "admin") return;
|
|
3622
|
+
const longs = cmd.options.map((o) => o.long);
|
|
3623
|
+
if (longs.includes("--workspace") && !longs.includes("--account"))
|
|
3624
|
+
cmd.option(
|
|
3625
|
+
"--account <name>",
|
|
3626
|
+
"use only this stored account (default: all of them; see `ft account list`)"
|
|
3627
|
+
);
|
|
3628
|
+
for (const sub of cmd.commands) addAccountOption(sub);
|
|
3629
|
+
}
|
|
3630
|
+
addAccountOption(program);
|
|
3631
|
+
program.hook("preAction", (_program, action) => {
|
|
3632
|
+
setAccountFlag(action.opts().account);
|
|
3633
|
+
});
|
|
3634
|
+
|
|
3635
|
+
// src/index.ts
|
|
2887
3636
|
if (process.argv.length <= 2) {
|
|
2888
3637
|
console.log(banner());
|
|
2889
3638
|
program.outputHelp();
|