@zilfu/cli 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1519 -13
- package/dist/cli.js.map +1 -1
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
import { defineCommand, runMain } from 'citty';
|
|
3
3
|
import { createZilfuClient, ZilfuApiError } from '@zilfu/sdk';
|
|
4
4
|
import { hostname, homedir } from 'os';
|
|
5
|
-
import { join } from 'path';
|
|
5
|
+
import { basename, join } from 'path';
|
|
6
6
|
import { existsSync, rmSync, mkdirSync, writeFileSync, chmodSync, readFileSync } from 'fs';
|
|
7
7
|
import 'buffer';
|
|
8
8
|
import { createServer } from 'http';
|
|
9
9
|
import { randomBytes, createHash } from 'crypto';
|
|
10
10
|
import { spawn } from 'child_process';
|
|
11
|
+
import { readFile } from 'fs/promises';
|
|
11
12
|
|
|
12
13
|
function configDir() {
|
|
13
14
|
if (process.platform === "win32") {
|
|
@@ -323,13 +324,84 @@ function clientFor(args, opts) {
|
|
|
323
324
|
function wantsJson(args) {
|
|
324
325
|
return args["json"] === true;
|
|
325
326
|
}
|
|
326
|
-
function
|
|
327
|
+
function toId(value, label) {
|
|
327
328
|
const id = Number(value);
|
|
328
329
|
if (!Number.isInteger(id) || id <= 0) {
|
|
329
|
-
throw new Error(`Invalid
|
|
330
|
+
throw new Error(`Invalid ${label} id: ${String(value)}`);
|
|
330
331
|
}
|
|
331
332
|
return id;
|
|
332
333
|
}
|
|
334
|
+
function toSpaceId(value) {
|
|
335
|
+
return toId(value, "space");
|
|
336
|
+
}
|
|
337
|
+
function toList(value) {
|
|
338
|
+
if (value === void 0 || value === null) {
|
|
339
|
+
return [];
|
|
340
|
+
}
|
|
341
|
+
const values = Array.isArray(value) ? value : [value];
|
|
342
|
+
return values.map((entry) => String(entry)).filter((entry) => entry.length > 0);
|
|
343
|
+
}
|
|
344
|
+
function toEpochMs(value) {
|
|
345
|
+
const raw = String(value);
|
|
346
|
+
if (/^\d+$/.test(raw)) {
|
|
347
|
+
return Number(raw);
|
|
348
|
+
}
|
|
349
|
+
const parsed = Date.parse(raw);
|
|
350
|
+
if (Number.isNaN(parsed)) {
|
|
351
|
+
throw new Error(
|
|
352
|
+
`Invalid --at value: ${raw}
|
|
353
|
+
Pass epoch milliseconds or an ISO 8601 timestamp, e.g. 2026-08-14T18:00:00Z.`
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
return parsed;
|
|
357
|
+
}
|
|
358
|
+
function toDate(value, flag) {
|
|
359
|
+
const raw = String(value);
|
|
360
|
+
if (Number.isNaN(Date.parse(raw))) {
|
|
361
|
+
throw new Error(
|
|
362
|
+
`Invalid --${flag} value: ${raw}
|
|
363
|
+
Pass a date, for example 2026-07-01.`
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
return raw;
|
|
367
|
+
}
|
|
368
|
+
var DAY_NUMBERS = {
|
|
369
|
+
mon: 1,
|
|
370
|
+
tue: 2,
|
|
371
|
+
wed: 3,
|
|
372
|
+
thu: 4,
|
|
373
|
+
fri: 5,
|
|
374
|
+
sat: 6,
|
|
375
|
+
sun: 7
|
|
376
|
+
};
|
|
377
|
+
function toDaysOfWeek(value) {
|
|
378
|
+
const tokens = toList(value).flatMap((entry) => entry.split(","));
|
|
379
|
+
const days = /* @__PURE__ */ new Set();
|
|
380
|
+
for (const token of tokens) {
|
|
381
|
+
const raw = token.trim().toLowerCase();
|
|
382
|
+
if (raw.length === 0) {
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const day = /^\d+$/.test(raw) ? Number(raw) : DAY_NUMBERS[raw.slice(0, 3)];
|
|
386
|
+
if (day === void 0 || !Number.isInteger(day) || day < 1 || day > 7) {
|
|
387
|
+
throw new Error(
|
|
388
|
+
`Invalid day: ${token.trim()}
|
|
389
|
+
Use 1-7 (1 = Monday, 7 = Sunday) or names like mon,wed,fri.`
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
days.add(day);
|
|
393
|
+
}
|
|
394
|
+
if (days.size === 0) {
|
|
395
|
+
throw new Error("Pass at least one day via --days.");
|
|
396
|
+
}
|
|
397
|
+
return [...days].sort((a, b) => a - b);
|
|
398
|
+
}
|
|
399
|
+
function toOptionalBool(args, name, rawArgs) {
|
|
400
|
+
const mentioned = rawArgs.some(
|
|
401
|
+
(arg) => arg === `--${name}` || arg === `--no-${name}` || arg.startsWith(`--${name}=`)
|
|
402
|
+
);
|
|
403
|
+
return mentioned ? args[name] === true : void 0;
|
|
404
|
+
}
|
|
333
405
|
async function guard(fn) {
|
|
334
406
|
try {
|
|
335
407
|
await fn();
|
|
@@ -449,43 +521,1474 @@ var accounts = defineCommand({
|
|
|
449
521
|
meta: { name: "accounts", description: "Manage connected accounts" },
|
|
450
522
|
subCommands: { list: list2 }
|
|
451
523
|
});
|
|
524
|
+
|
|
525
|
+
// src/mime.ts
|
|
526
|
+
var MIME_BY_EXTENSION = {
|
|
527
|
+
jpg: "image/jpeg",
|
|
528
|
+
jpeg: "image/jpeg",
|
|
529
|
+
png: "image/png",
|
|
530
|
+
webp: "image/webp",
|
|
531
|
+
mp4: "video/mp4",
|
|
532
|
+
mov: "video/quicktime"
|
|
533
|
+
};
|
|
534
|
+
function extensionOf(value) {
|
|
535
|
+
const withoutQuery = value.split(/[?#]/)[0] ?? "";
|
|
536
|
+
const lastDot = withoutQuery.lastIndexOf(".");
|
|
537
|
+
return lastDot === -1 ? "" : withoutQuery.slice(lastDot + 1).toLowerCase();
|
|
538
|
+
}
|
|
539
|
+
function mimeForPath(path) {
|
|
540
|
+
const mime = MIME_BY_EXTENSION[extensionOf(path)];
|
|
541
|
+
if (!mime) {
|
|
542
|
+
throw new Error(
|
|
543
|
+
`Unsupported file type: ${path}
|
|
544
|
+
Zilfu accepts ${Object.keys(MIME_BY_EXTENSION).join(", ")}.`
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
return mime;
|
|
548
|
+
}
|
|
549
|
+
function mediaTypeForMime(mime) {
|
|
550
|
+
return mime.startsWith("video/") ? "video" : "image";
|
|
551
|
+
}
|
|
552
|
+
function mediaTypeForUrl(url) {
|
|
553
|
+
const mime = MIME_BY_EXTENSION[extensionOf(url)];
|
|
554
|
+
if (!mime) {
|
|
555
|
+
throw new Error(
|
|
556
|
+
`Cannot tell whether this is an image or a video: ${url}
|
|
557
|
+
Use a URL ending in ${Object.keys(MIME_BY_EXTENSION).join(", ")}, or upload the file with \`zilfu media upload\` first.`
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
return mediaTypeForMime(mime);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// src/commands/posts.ts
|
|
452
564
|
var STATUS_LABELS = {
|
|
453
565
|
0: "scheduled",
|
|
454
566
|
1: "published",
|
|
455
567
|
2: "error",
|
|
456
568
|
3: "draft",
|
|
457
|
-
4: "processing"
|
|
569
|
+
4: "processing",
|
|
570
|
+
5: "pending_approval"
|
|
571
|
+
};
|
|
572
|
+
var CONTENT_LIMITS = {
|
|
573
|
+
threads: 500,
|
|
574
|
+
x: 280,
|
|
575
|
+
instagram: 2200,
|
|
576
|
+
linkedin: 3e3,
|
|
577
|
+
tiktok: 2200,
|
|
578
|
+
facebook: 63206,
|
|
579
|
+
pinterest: 500
|
|
458
580
|
};
|
|
581
|
+
var COMMENT_LIMITS = {
|
|
582
|
+
threads: 500,
|
|
583
|
+
instagram: 2200,
|
|
584
|
+
facebook: 8e3,
|
|
585
|
+
linkedin: 1250
|
|
586
|
+
};
|
|
587
|
+
var COMMENT_FALLBACK_LIMIT = 500;
|
|
588
|
+
var REPLY_ENABLED = /* @__PURE__ */ new Set(["threads", "instagram", "facebook"]);
|
|
589
|
+
var REPLY_CAPABLE = /* @__PURE__ */ new Set([...REPLY_ENABLED, "linkedin"]);
|
|
590
|
+
var MEDIA_REQUIRED = /* @__PURE__ */ new Set(["instagram", "tiktok", "pinterest"]);
|
|
591
|
+
var PRIVACY_LEVELS = [
|
|
592
|
+
"PUBLIC_TO_EVERYONE",
|
|
593
|
+
"MUTUAL_FOLLOW_FRIENDS",
|
|
594
|
+
"FOLLOWER_OF_CREATOR",
|
|
595
|
+
"SELF_ONLY"
|
|
596
|
+
];
|
|
597
|
+
var MAX_COMMENT_DELAY = 10080;
|
|
598
|
+
var PIN_TITLE_MAX = 100;
|
|
599
|
+
var MAX_PER_PAGE = 200;
|
|
459
600
|
function statusLabel(status) {
|
|
460
601
|
return STATUS_LABELS[status] ?? String(status);
|
|
461
602
|
}
|
|
603
|
+
function charCount(value) {
|
|
604
|
+
return [...value].length;
|
|
605
|
+
}
|
|
606
|
+
function readString2(value) {
|
|
607
|
+
if (value === void 0 || value === null) {
|
|
608
|
+
return void 0;
|
|
609
|
+
}
|
|
610
|
+
const raw = String(value);
|
|
611
|
+
return raw.length > 0 ? raw : void 0;
|
|
612
|
+
}
|
|
613
|
+
function readUrl(value, flag) {
|
|
614
|
+
const raw = readString2(value);
|
|
615
|
+
if (raw !== void 0 && !/^https?:\/\//i.test(raw)) {
|
|
616
|
+
throw new Error(`--${flag} must be an http(s) URL: ${raw}`);
|
|
617
|
+
}
|
|
618
|
+
return raw;
|
|
619
|
+
}
|
|
620
|
+
function camelize(name) {
|
|
621
|
+
return name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
622
|
+
}
|
|
623
|
+
function repeatedValues(args, rawArgs, name) {
|
|
624
|
+
const names = /* @__PURE__ */ new Set([name, camelize(name)]);
|
|
625
|
+
const values = [];
|
|
626
|
+
for (let index = 0; index < rawArgs.length; index += 1) {
|
|
627
|
+
const token = rawArgs[index];
|
|
628
|
+
if (token === "--") {
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
if (!token.startsWith("--")) {
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
const separator = token.indexOf("=");
|
|
635
|
+
const flag = separator === -1 ? token.slice(2) : token.slice(2, separator);
|
|
636
|
+
if (!names.has(flag)) {
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (separator !== -1) {
|
|
640
|
+
values.push(token.slice(separator + 1));
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
const next = rawArgs[index + 1];
|
|
644
|
+
if (next !== void 0 && !next.startsWith("--")) {
|
|
645
|
+
values.push(next);
|
|
646
|
+
index += 1;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return values.length > 1 ? values.filter((value) => value.length > 0) : toList(args[name]);
|
|
650
|
+
}
|
|
651
|
+
function parsePairs(entries, flag, example) {
|
|
652
|
+
return entries.map((entry) => {
|
|
653
|
+
const separator = entry.indexOf("=");
|
|
654
|
+
if (separator <= 0) {
|
|
655
|
+
throw new Error(
|
|
656
|
+
`Invalid --${flag} value: ${entry}
|
|
657
|
+
Use --${flag} <key>=<value>, for example ${example}`
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
return [entry.slice(0, separator), entry.slice(separator + 1)];
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
function toStatus(value) {
|
|
664
|
+
const raw = value.trim().toLowerCase().replace(/-/g, "_");
|
|
665
|
+
const match = Object.entries(STATUS_LABELS).find(
|
|
666
|
+
([number, label]) => label === raw || number === raw
|
|
667
|
+
);
|
|
668
|
+
if (!match) {
|
|
669
|
+
throw new Error(
|
|
670
|
+
`Invalid --status value: ${value}
|
|
671
|
+
Use one of: ${Object.entries(STATUS_LABELS).map(([number, label]) => `${label} (${number})`).join(", ")}.`
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
return Number(match[0]);
|
|
675
|
+
}
|
|
676
|
+
function toCount(value, flag, max) {
|
|
677
|
+
const count = Number(value);
|
|
678
|
+
if (!Number.isInteger(count) || count < 1 || max !== void 0 && count > max) {
|
|
679
|
+
throw new Error(
|
|
680
|
+
`Invalid --${flag} value: ${String(value)}
|
|
681
|
+
Pass a whole number ${max === void 0 ? "of 1 or more" : `between 1 and ${max}`}.`
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
return count;
|
|
685
|
+
}
|
|
462
686
|
var list3 = defineCommand({
|
|
463
687
|
meta: { name: "list", description: "List posts in a space" },
|
|
464
688
|
args: {
|
|
465
689
|
...globalArgs,
|
|
466
|
-
space: { type: "string", required: true, description: "The space id" }
|
|
690
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
691
|
+
status: {
|
|
692
|
+
type: "string",
|
|
693
|
+
description: "Filter by status \u2014 name or number (scheduled, published, error, draft, processing, pending_approval). Repeat to match any of several"
|
|
694
|
+
},
|
|
695
|
+
account: {
|
|
696
|
+
type: "string",
|
|
697
|
+
description: "Filter by account id (repeat to match any of several)"
|
|
698
|
+
},
|
|
699
|
+
from: {
|
|
700
|
+
type: "string",
|
|
701
|
+
description: "Only posts scheduled or published on/after this date"
|
|
702
|
+
},
|
|
703
|
+
to: {
|
|
704
|
+
type: "string",
|
|
705
|
+
description: "Only posts scheduled or published on/before this date"
|
|
706
|
+
},
|
|
707
|
+
"per-page": {
|
|
708
|
+
type: "string",
|
|
709
|
+
description: `Results per page, 1-${MAX_PER_PAGE} (default 15)`
|
|
710
|
+
},
|
|
711
|
+
page: {
|
|
712
|
+
type: "string",
|
|
713
|
+
description: "Page of results to fetch (default 1)"
|
|
714
|
+
}
|
|
467
715
|
},
|
|
468
|
-
run: ({ args }) => guard(async () => {
|
|
716
|
+
run: ({ args, rawArgs }) => guard(async () => {
|
|
469
717
|
const space = toSpaceId(args["space"]);
|
|
718
|
+
const statuses = repeatedValues(args, rawArgs, "status").map(toStatus);
|
|
719
|
+
const accountIds = repeatedValues(args, rawArgs, "account").map(
|
|
720
|
+
(id) => toId(id, "account")
|
|
721
|
+
);
|
|
722
|
+
const query = {};
|
|
723
|
+
if (statuses.length === 1) {
|
|
724
|
+
query.status = statuses[0];
|
|
725
|
+
} else if (statuses.length > 1) {
|
|
726
|
+
query["statuses[]"] = statuses;
|
|
727
|
+
}
|
|
728
|
+
if (accountIds.length === 1) {
|
|
729
|
+
query.account_id = accountIds[0];
|
|
730
|
+
} else if (accountIds.length > 1) {
|
|
731
|
+
query["account_ids[]"] = accountIds;
|
|
732
|
+
}
|
|
733
|
+
if (args["from"] !== void 0) {
|
|
734
|
+
query.from = toDate(args["from"], "from");
|
|
735
|
+
}
|
|
736
|
+
if (args["to"] !== void 0) {
|
|
737
|
+
query.to = toDate(args["to"], "to");
|
|
738
|
+
}
|
|
739
|
+
if (args["per-page"] !== void 0) {
|
|
740
|
+
query.per_page = toCount(args["per-page"], "per-page", MAX_PER_PAGE);
|
|
741
|
+
}
|
|
742
|
+
if (args["page"] !== void 0) {
|
|
743
|
+
query.page = toCount(args["page"], "page");
|
|
744
|
+
}
|
|
470
745
|
const api = clientFor(args);
|
|
471
|
-
const res = await api.posts.list({ path: { space } });
|
|
472
|
-
const
|
|
746
|
+
const res = await api.posts.list({ path: { space }, query });
|
|
747
|
+
const posts3 = res.data?.data ?? [];
|
|
473
748
|
if (wantsJson(args)) {
|
|
474
|
-
print(
|
|
749
|
+
print(res.data ?? { data: [] }, { json: true });
|
|
475
750
|
return;
|
|
476
751
|
}
|
|
477
|
-
const rows =
|
|
752
|
+
const rows = posts3.map((post) => ({
|
|
478
753
|
id: post.id,
|
|
754
|
+
account_id: post.account_id,
|
|
479
755
|
status: statusLabel(post.status),
|
|
480
756
|
scheduled_at: post.scheduled_at,
|
|
481
757
|
published_at: post.published_at
|
|
482
758
|
}));
|
|
483
|
-
print(rows, {
|
|
759
|
+
print(rows, {
|
|
760
|
+
columns: [
|
|
761
|
+
"id",
|
|
762
|
+
"account_id",
|
|
763
|
+
"status",
|
|
764
|
+
"scheduled_at",
|
|
765
|
+
"published_at"
|
|
766
|
+
]
|
|
767
|
+
});
|
|
768
|
+
const meta = res.data?.meta;
|
|
769
|
+
if (meta && meta.total > 0) {
|
|
770
|
+
const shown = meta.from !== null && meta.to !== null ? `${meta.from}-${meta.to}` : String(posts3.length);
|
|
771
|
+
const more = meta.current_page < meta.last_page ? `. Pass --page ${meta.current_page + 1} for the next page` : "";
|
|
772
|
+
process.stdout.write(`Showing ${shown} of ${meta.total}${more}.
|
|
773
|
+
`);
|
|
774
|
+
}
|
|
775
|
+
})
|
|
776
|
+
});
|
|
777
|
+
async function resolveAccounts(api, space, accountIds) {
|
|
778
|
+
const res = await api.accounts.list({ path: { space } });
|
|
779
|
+
const accounts2 = res.data?.data ?? [];
|
|
780
|
+
const accountById = new Map(accounts2.map((a) => [a.id, a]));
|
|
781
|
+
for (const accountId of accountIds) {
|
|
782
|
+
if (!accountById.has(accountId)) {
|
|
783
|
+
const known = accounts2.map((a) => a.id).join(", ");
|
|
784
|
+
throw new Error(
|
|
785
|
+
`Account ${accountId} is not in space ${space}.
|
|
786
|
+
` + (known ? `Accounts in this space: ${known}` : "This space has no connected accounts.")
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
const stale = accountIds.map((id) => accountById.get(id)).filter((a) => !a.is_active || Boolean(a.disconnected_at));
|
|
791
|
+
if (stale.length > 0) {
|
|
792
|
+
const many = stale.length > 1;
|
|
793
|
+
process.stderr.write(
|
|
794
|
+
`Note: account${many ? "s" : ""} ${stale.map((a) => `${a.id} (${a.social})`).join(", ")} ${many ? "are" : "is"} disconnected; publishing will fail until reconnected.
|
|
795
|
+
`
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
return accountById;
|
|
799
|
+
}
|
|
800
|
+
function parseAccountTexts(entries, accountIds) {
|
|
801
|
+
const texts = /* @__PURE__ */ new Map();
|
|
802
|
+
for (const [rawId, text] of parsePairs(
|
|
803
|
+
entries,
|
|
804
|
+
"account-text",
|
|
805
|
+
'--account-text 7="Shorter copy for X"'
|
|
806
|
+
)) {
|
|
807
|
+
const accountId = toId(rawId, "account");
|
|
808
|
+
if (!accountIds.includes(accountId)) {
|
|
809
|
+
throw new Error(
|
|
810
|
+
`--account-text targets account ${accountId}, which was not passed with --account.
|
|
811
|
+
Accounts in this call: ${accountIds.join(", ")}`
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
if (texts.has(accountId)) {
|
|
815
|
+
throw new Error(`--account-text was given twice for account ${accountId}.`);
|
|
816
|
+
}
|
|
817
|
+
if (text.length === 0) {
|
|
818
|
+
throw new Error(`--account-text for account ${accountId} is empty.`);
|
|
819
|
+
}
|
|
820
|
+
texts.set(accountId, text);
|
|
821
|
+
}
|
|
822
|
+
return texts;
|
|
823
|
+
}
|
|
824
|
+
function parseReplyMedia(entries, replyCount) {
|
|
825
|
+
const byReply = /* @__PURE__ */ new Map();
|
|
826
|
+
for (const [rawIndex, url] of parsePairs(
|
|
827
|
+
entries,
|
|
828
|
+
"reply-media",
|
|
829
|
+
"--reply-media 1=https://\u2026/clip.mp4"
|
|
830
|
+
)) {
|
|
831
|
+
const index = Number(rawIndex);
|
|
832
|
+
if (!Number.isInteger(index) || index < 1 || index > replyCount) {
|
|
833
|
+
throw new Error(
|
|
834
|
+
`--reply-media ${rawIndex}=\u2026 does not match a reply.
|
|
835
|
+
` + (replyCount === 0 ? "Pass --reply first \u2014 --reply-media attaches media to a reply by its 1-based position." : `Use 1-${replyCount}: the position of the --reply this media belongs to.`)
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
const media2 = byReply.get(index) ?? [];
|
|
839
|
+
media2.push({ url, type: mediaTypeForUrl(url) });
|
|
840
|
+
byReply.set(index, media2);
|
|
841
|
+
}
|
|
842
|
+
return byReply;
|
|
843
|
+
}
|
|
844
|
+
var BOOLEAN_METADATA = [
|
|
845
|
+
["allow-comments", "allow_comments"],
|
|
846
|
+
["allow-duet", "allow_duet"],
|
|
847
|
+
["allow-stitch", "allow_stitch"],
|
|
848
|
+
["branded-content", "branded_content"],
|
|
849
|
+
["brand-organic", "brand_organic"]
|
|
850
|
+
];
|
|
851
|
+
function buildMetadata(args, rawArgs) {
|
|
852
|
+
const metadata = {};
|
|
853
|
+
const topicTag = readString2(args["topic-tag"]);
|
|
854
|
+
if (topicTag !== void 0) {
|
|
855
|
+
metadata.topic_tag = topicTag;
|
|
856
|
+
}
|
|
857
|
+
const postType = readString2(args["post-type"]);
|
|
858
|
+
if (postType !== void 0) {
|
|
859
|
+
metadata.post_type = postType;
|
|
860
|
+
}
|
|
861
|
+
const link = readUrl(args["link"], "link");
|
|
862
|
+
if (link !== void 0) {
|
|
863
|
+
metadata.link = link;
|
|
864
|
+
}
|
|
865
|
+
const boardId = readString2(args["board-id"]);
|
|
866
|
+
if (boardId !== void 0) {
|
|
867
|
+
metadata.board_id = boardId;
|
|
868
|
+
}
|
|
869
|
+
const title = readString2(args["title"]);
|
|
870
|
+
if (title !== void 0) {
|
|
871
|
+
if (charCount(title) > PIN_TITLE_MAX) {
|
|
872
|
+
throw new Error(
|
|
873
|
+
`--title is ${charCount(title)} characters; the limit is ${PIN_TITLE_MAX}.`
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
metadata.title = title;
|
|
877
|
+
}
|
|
878
|
+
const coverImageUrl = readUrl(args["cover-image-url"], "cover-image-url");
|
|
879
|
+
if (coverImageUrl !== void 0) {
|
|
880
|
+
metadata.cover_image_url = coverImageUrl;
|
|
881
|
+
}
|
|
882
|
+
const privacyLevel = readString2(args["privacy-level"]);
|
|
883
|
+
if (privacyLevel !== void 0) {
|
|
884
|
+
const normalised = privacyLevel.trim().toUpperCase().replace(/-/g, "_");
|
|
885
|
+
const match = PRIVACY_LEVELS.find((level) => level === normalised);
|
|
886
|
+
if (!match) {
|
|
887
|
+
throw new Error(
|
|
888
|
+
`Invalid --privacy-level value: ${privacyLevel}
|
|
889
|
+
Use one of: ${PRIVACY_LEVELS.join(", ")}.`
|
|
890
|
+
);
|
|
891
|
+
}
|
|
892
|
+
metadata.privacy_level = match;
|
|
893
|
+
}
|
|
894
|
+
for (const [flag, key] of BOOLEAN_METADATA) {
|
|
895
|
+
const value = toOptionalBool(args, flag, rawArgs);
|
|
896
|
+
if (value !== void 0) {
|
|
897
|
+
metadata[key] = value;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return Object.keys(metadata).length > 0 ? metadata : void 0;
|
|
901
|
+
}
|
|
902
|
+
function buildComment(args) {
|
|
903
|
+
const content = readString2(args["comment"]);
|
|
904
|
+
const rawDelay = args["comment-delay"];
|
|
905
|
+
if (content === void 0) {
|
|
906
|
+
if (rawDelay !== void 0) {
|
|
907
|
+
throw new Error(
|
|
908
|
+
"--comment-delay was passed without --comment. Add --comment <text>, or drop the delay."
|
|
909
|
+
);
|
|
910
|
+
}
|
|
911
|
+
return void 0;
|
|
912
|
+
}
|
|
913
|
+
if (rawDelay === void 0) {
|
|
914
|
+
throw new Error(
|
|
915
|
+
`--comment needs --comment-delay <minutes>: how long after publishing to post it.
|
|
916
|
+
Pass a whole number between 1 and ${MAX_COMMENT_DELAY} (7 days).`
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
const delayMinutes = Number(rawDelay);
|
|
920
|
+
if (!Number.isInteger(delayMinutes) || delayMinutes < 1 || delayMinutes > MAX_COMMENT_DELAY) {
|
|
921
|
+
throw new Error(
|
|
922
|
+
`Invalid --comment-delay value: ${String(rawDelay)}
|
|
923
|
+
Pass a whole number of minutes between 1 and ${MAX_COMMENT_DELAY} (7 days).`
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
return { content, delay_minutes: delayMinutes };
|
|
927
|
+
}
|
|
928
|
+
function commentLimitAcross(socials) {
|
|
929
|
+
const limits = socials.map((social) => COMMENT_LIMITS[social]).filter((limit) => limit !== void 0);
|
|
930
|
+
return limits.length > 0 ? Math.min(...limits) : COMMENT_FALLBACK_LIMIT;
|
|
931
|
+
}
|
|
932
|
+
function preflight(accountId, social, content, children, media2, metadata) {
|
|
933
|
+
const limit = CONTENT_LIMITS[social];
|
|
934
|
+
if (limit !== void 0) {
|
|
935
|
+
const length = charCount(content);
|
|
936
|
+
if (length > limit) {
|
|
937
|
+
throw new Error(
|
|
938
|
+
`Content for account ${accountId} (${social}) is ${length} characters; ${social} allows ${limit}.
|
|
939
|
+
Use --account-text ${accountId}=<text> to tailor the copy for this account.`
|
|
940
|
+
);
|
|
941
|
+
}
|
|
942
|
+
for (const [index, child] of children.entries()) {
|
|
943
|
+
const replyLength = charCount(child.content);
|
|
944
|
+
if (replyLength > limit) {
|
|
945
|
+
throw new Error(
|
|
946
|
+
`Reply ${index + 1} for account ${accountId} (${social}) is ${replyLength} characters; ${social} allows ${limit}.`
|
|
947
|
+
);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
if (MEDIA_REQUIRED.has(social) && media2.length === 0) {
|
|
952
|
+
throw new Error(
|
|
953
|
+
`Account ${accountId} (${social}) cannot publish a text-only post. Attach a file with --media.`
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
if (social === "linkedin" && media2.length > 1 && !media2.some((item) => item.type === "image")) {
|
|
957
|
+
throw new Error(
|
|
958
|
+
`Account ${accountId} (linkedin) has ${media2.length} media files and no image; LinkedIn only takes several files as a multi-image post.
|
|
959
|
+
Attach a single video with --media, or include at least one image.`
|
|
960
|
+
);
|
|
961
|
+
}
|
|
962
|
+
if (social === "pinterest") {
|
|
963
|
+
if (!metadata?.board_id) {
|
|
964
|
+
throw new Error(
|
|
965
|
+
`Account ${accountId} (pinterest) needs --board-id: the board the pin is created on.`
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
const hasVideo = media2.some((item) => item.type === "video");
|
|
969
|
+
const hasImage = media2.some((item) => item.type === "image");
|
|
970
|
+
if (hasVideo && !hasImage && !metadata.cover_image_url) {
|
|
971
|
+
throw new Error(
|
|
972
|
+
`Account ${accountId} (pinterest) is publishing a video pin, which needs a cover image.
|
|
973
|
+
Add an image with --media, or point --cover-image-url at one.`
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
if (social === "tiktok") {
|
|
978
|
+
const privacyLevel = metadata?.privacy_level;
|
|
979
|
+
if (!privacyLevel) {
|
|
980
|
+
throw new Error(
|
|
981
|
+
`Account ${accountId} (tiktok) needs --privacy-level; TikTok defaults to SELF_ONLY (visible only to you).
|
|
982
|
+
Use one of: ${PRIVACY_LEVELS.join(", ")}.`
|
|
983
|
+
);
|
|
984
|
+
}
|
|
985
|
+
if (metadata?.branded_content === true && privacyLevel === "SELF_ONLY") {
|
|
986
|
+
throw new Error(
|
|
987
|
+
`Account ${accountId} (tiktok) cannot combine --branded-content with --privacy-level SELF_ONLY; TikTok rejects branded content on a private post.`
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
var create = defineCommand({
|
|
993
|
+
meta: {
|
|
994
|
+
name: "create",
|
|
995
|
+
description: "Create a post \u2014 scheduled, draft, or published now"
|
|
996
|
+
},
|
|
997
|
+
args: {
|
|
998
|
+
...globalArgs,
|
|
999
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
1000
|
+
account: {
|
|
1001
|
+
type: "string",
|
|
1002
|
+
required: true,
|
|
1003
|
+
description: "Account id to post to (repeat for multiple accounts)"
|
|
1004
|
+
},
|
|
1005
|
+
text: {
|
|
1006
|
+
type: "string",
|
|
1007
|
+
description: "The post content, shared by every account. Optional when every account has --account-text"
|
|
1008
|
+
},
|
|
1009
|
+
"account-text": {
|
|
1010
|
+
type: "string",
|
|
1011
|
+
description: "Per-account copy as <accountId>=<text>, overriding --text for that account (repeat per account)"
|
|
1012
|
+
},
|
|
1013
|
+
at: {
|
|
1014
|
+
type: "string",
|
|
1015
|
+
description: "When to publish \u2014 ISO 8601 or epoch ms. Omit to publish immediately"
|
|
1016
|
+
},
|
|
1017
|
+
draft: { type: "boolean", description: "Save as a draft instead" },
|
|
1018
|
+
media: {
|
|
1019
|
+
type: "string",
|
|
1020
|
+
description: "Media URL to attach (repeat for multiple)"
|
|
1021
|
+
},
|
|
1022
|
+
reply: {
|
|
1023
|
+
type: "string",
|
|
1024
|
+
description: "Threaded reply content (repeat for a longer thread)"
|
|
1025
|
+
},
|
|
1026
|
+
"reply-media": {
|
|
1027
|
+
type: "string",
|
|
1028
|
+
description: "Media for a threaded reply as <n>=<url>, where <n> is the 1-based position of the --reply it belongs to (1 = first --reply). Repeat with the same <n> for several files"
|
|
1029
|
+
},
|
|
1030
|
+
comment: {
|
|
1031
|
+
type: "string",
|
|
1032
|
+
description: "First comment to post after publishing (threads, instagram and facebook only). Requires --comment-delay"
|
|
1033
|
+
},
|
|
1034
|
+
"comment-delay": {
|
|
1035
|
+
type: "string",
|
|
1036
|
+
description: `Minutes to wait after publishing before posting --comment, 1-${MAX_COMMENT_DELAY} (7 days)`
|
|
1037
|
+
},
|
|
1038
|
+
"topic-tag": {
|
|
1039
|
+
type: "string",
|
|
1040
|
+
description: "Threads: topic tag for the post"
|
|
1041
|
+
},
|
|
1042
|
+
"board-id": {
|
|
1043
|
+
type: "string",
|
|
1044
|
+
description: "Pinterest: board the pin is created on (required)"
|
|
1045
|
+
},
|
|
1046
|
+
title: {
|
|
1047
|
+
type: "string",
|
|
1048
|
+
description: `Pinterest: pin title, max ${PIN_TITLE_MAX} characters`
|
|
1049
|
+
},
|
|
1050
|
+
link: {
|
|
1051
|
+
type: "string",
|
|
1052
|
+
description: "Pinterest: destination URL the pin links to"
|
|
1053
|
+
},
|
|
1054
|
+
"cover-image-url": {
|
|
1055
|
+
type: "string",
|
|
1056
|
+
description: "Pinterest: cover image for a video pin, used when --media has no image"
|
|
1057
|
+
},
|
|
1058
|
+
"privacy-level": {
|
|
1059
|
+
type: "string",
|
|
1060
|
+
description: `TikTok: who can see the post \u2014 ${PRIVACY_LEVELS.join(" | ")} (required for TikTok; TikTok itself defaults to SELF_ONLY)`
|
|
1061
|
+
},
|
|
1062
|
+
"allow-comments": {
|
|
1063
|
+
type: "boolean",
|
|
1064
|
+
description: "TikTok: allow comments (--no-allow-comments to disable; unset leaves TikTok's default)"
|
|
1065
|
+
},
|
|
1066
|
+
"allow-duet": {
|
|
1067
|
+
type: "boolean",
|
|
1068
|
+
description: "TikTok video: allow duets (--no-allow-duet to disable; ignored on photo posts)"
|
|
1069
|
+
},
|
|
1070
|
+
"allow-stitch": {
|
|
1071
|
+
type: "boolean",
|
|
1072
|
+
description: "TikTok video: allow stitches (--no-allow-stitch to disable; ignored on photo posts)"
|
|
1073
|
+
},
|
|
1074
|
+
"branded-content": {
|
|
1075
|
+
type: "boolean",
|
|
1076
|
+
description: "TikTok: mark as branded content (paid partnership); not allowed with --privacy-level SELF_ONLY"
|
|
1077
|
+
},
|
|
1078
|
+
"brand-organic": {
|
|
1079
|
+
type: "boolean",
|
|
1080
|
+
description: "TikTok: mark as promoting your own brand"
|
|
1081
|
+
},
|
|
1082
|
+
"post-type": {
|
|
1083
|
+
type: "string",
|
|
1084
|
+
description: "Stored on the post as metadata but not read by any platform \u2014 set it only if your own tooling reads it back"
|
|
1085
|
+
}
|
|
1086
|
+
},
|
|
1087
|
+
run: ({ args, rawArgs }) => guard(async () => {
|
|
1088
|
+
const space = toSpaceId(args["space"]);
|
|
1089
|
+
const accountIds = [
|
|
1090
|
+
...new Set(
|
|
1091
|
+
repeatedValues(args, rawArgs, "account").map(
|
|
1092
|
+
(id) => toId(id, "account")
|
|
1093
|
+
)
|
|
1094
|
+
)
|
|
1095
|
+
];
|
|
1096
|
+
if (accountIds.length === 0) {
|
|
1097
|
+
throw new Error("Pass at least one --account.");
|
|
1098
|
+
}
|
|
1099
|
+
const isDraft = args["draft"] === true;
|
|
1100
|
+
const rawAt = args["at"];
|
|
1101
|
+
if (isDraft && rawAt !== void 0) {
|
|
1102
|
+
throw new Error("--draft and --at are mutually exclusive.");
|
|
1103
|
+
}
|
|
1104
|
+
const at = rawAt === void 0 ? null : toEpochMs(rawAt);
|
|
1105
|
+
if (at !== null) {
|
|
1106
|
+
const raw = String(rawAt);
|
|
1107
|
+
if (/^\d+$/.test(raw) && at < 1e12) {
|
|
1108
|
+
throw new Error(
|
|
1109
|
+
`--at ${raw} looks like epoch seconds; the API reads epoch milliseconds, so this would schedule for ${new Date(at).toISOString()}.
|
|
1110
|
+
Multiply by 1000, or pass an ISO 8601 timestamp like 2026-08-14T18:00:00Z.`
|
|
1111
|
+
);
|
|
1112
|
+
}
|
|
1113
|
+
if (at <= Date.now()) {
|
|
1114
|
+
throw new Error(
|
|
1115
|
+
`--at ${new Date(at).toISOString()} is in the past; the scheduler never picks up a post whose time has already passed.
|
|
1116
|
+
Omit --at to publish immediately, or pass a future timestamp.`
|
|
1117
|
+
);
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
const sharedText = readString2(args["text"]);
|
|
1121
|
+
const accountTexts = parseAccountTexts(
|
|
1122
|
+
repeatedValues(args, rawArgs, "account-text"),
|
|
1123
|
+
accountIds
|
|
1124
|
+
);
|
|
1125
|
+
if (sharedText === void 0) {
|
|
1126
|
+
const missing = accountIds.filter((id) => !accountTexts.has(id));
|
|
1127
|
+
if (missing.length > 0) {
|
|
1128
|
+
throw new Error(
|
|
1129
|
+
`No content for account${missing.length > 1 ? "s" : ""} ${missing.join(", ")}.
|
|
1130
|
+
Pass --text for copy shared by every account, or --account-text <accountId>=<text> for each one.`
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
const media2 = repeatedValues(args, rawArgs, "media").map((url) => ({
|
|
1135
|
+
url,
|
|
1136
|
+
type: mediaTypeForUrl(url)
|
|
1137
|
+
}));
|
|
1138
|
+
const replies = repeatedValues(args, rawArgs, "reply");
|
|
1139
|
+
const replyMedia = parseReplyMedia(
|
|
1140
|
+
repeatedValues(args, rawArgs, "reply-media"),
|
|
1141
|
+
replies.length
|
|
1142
|
+
);
|
|
1143
|
+
const children = replies.map((content, index) => {
|
|
1144
|
+
const attached = replyMedia.get(index + 1);
|
|
1145
|
+
return attached ? { content, media: attached } : { content };
|
|
1146
|
+
});
|
|
1147
|
+
const metadata = buildMetadata(args, rawArgs);
|
|
1148
|
+
const comment = buildComment(args);
|
|
1149
|
+
const api = clientFor(args);
|
|
1150
|
+
const accountById = await resolveAccounts(api, space, accountIds);
|
|
1151
|
+
const items = accountIds.map((accountId) => {
|
|
1152
|
+
const social = accountById.get(accountId).social;
|
|
1153
|
+
const content = accountTexts.get(accountId) ?? sharedText;
|
|
1154
|
+
preflight(accountId, social, content, children, media2, metadata);
|
|
1155
|
+
return {
|
|
1156
|
+
account_id: accountId,
|
|
1157
|
+
social,
|
|
1158
|
+
main: { content, media: media2.length > 0 ? media2 : null },
|
|
1159
|
+
...children.length > 0 ? { children } : {},
|
|
1160
|
+
...metadata ? { metadata } : {}
|
|
1161
|
+
};
|
|
1162
|
+
});
|
|
1163
|
+
if (comment) {
|
|
1164
|
+
const socials = accountIds.map((id) => accountById.get(id).social);
|
|
1165
|
+
const limit = commentLimitAcross(socials);
|
|
1166
|
+
const length = charCount(comment.content);
|
|
1167
|
+
if (length > limit) {
|
|
1168
|
+
throw new Error(
|
|
1169
|
+
`--comment is ${length} characters; ${limit} is the cap for this set of accounts (the strictest comment-capable platform in the call wins).`
|
|
1170
|
+
);
|
|
1171
|
+
}
|
|
1172
|
+
const unsupported = [
|
|
1173
|
+
...new Set(socials.filter((social) => !REPLY_CAPABLE.has(social)))
|
|
1174
|
+
];
|
|
1175
|
+
if (unsupported.length > 0) {
|
|
1176
|
+
process.stderr.write(
|
|
1177
|
+
`Note: ${unsupported.join(", ")} cannot post a scheduled comment; --comment applies to the other accounts only.
|
|
1178
|
+
`
|
|
1179
|
+
);
|
|
1180
|
+
}
|
|
1181
|
+
const gated = [
|
|
1182
|
+
...new Set(
|
|
1183
|
+
socials.filter(
|
|
1184
|
+
(social) => REPLY_CAPABLE.has(social) && !REPLY_ENABLED.has(social)
|
|
1185
|
+
)
|
|
1186
|
+
)
|
|
1187
|
+
];
|
|
1188
|
+
if (gated.length > 0) {
|
|
1189
|
+
process.stderr.write(
|
|
1190
|
+
`Note: ${gated.join(", ")} is not on the follow-up comment rollout, so --comment is usually dropped for those accounts.
|
|
1191
|
+
`
|
|
1192
|
+
);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
const res = await api.posts.create({
|
|
1196
|
+
path: { space: String(space) },
|
|
1197
|
+
body: {
|
|
1198
|
+
space_id: space,
|
|
1199
|
+
at,
|
|
1200
|
+
is_draft: isDraft,
|
|
1201
|
+
items,
|
|
1202
|
+
...comment ? { comment } : {}
|
|
1203
|
+
}
|
|
1204
|
+
});
|
|
1205
|
+
const created = res.data?.data ?? [];
|
|
1206
|
+
if (wantsJson(args)) {
|
|
1207
|
+
print(created, { json: true });
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
const rows = created.map((post) => ({
|
|
1211
|
+
id: post.id,
|
|
1212
|
+
account_id: post.account_id,
|
|
1213
|
+
status: statusLabel(post.status),
|
|
1214
|
+
scheduled_at: post.scheduled_at
|
|
1215
|
+
}));
|
|
1216
|
+
print(rows, {
|
|
1217
|
+
columns: ["id", "account_id", "status", "scheduled_at"]
|
|
1218
|
+
});
|
|
1219
|
+
})
|
|
1220
|
+
});
|
|
1221
|
+
var del = defineCommand({
|
|
1222
|
+
meta: {
|
|
1223
|
+
name: "delete",
|
|
1224
|
+
description: "Delete a post. The API answers 204 No Content, so --json prints a CLI-made acknowledgement rather than a server payload"
|
|
1225
|
+
},
|
|
1226
|
+
args: {
|
|
1227
|
+
...globalArgs,
|
|
1228
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
1229
|
+
post: { type: "string", required: true, description: "The post id" }
|
|
1230
|
+
},
|
|
1231
|
+
run: ({ args }) => guard(async () => {
|
|
1232
|
+
const space = toSpaceId(args["space"]);
|
|
1233
|
+
const post = toId(args["post"], "post");
|
|
1234
|
+
const api = clientFor(args);
|
|
1235
|
+
await api.posts.delete({ path: { space, post } });
|
|
1236
|
+
if (wantsJson(args)) {
|
|
1237
|
+
print({ deleted: true, id: post }, { json: true });
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
process.stdout.write(`Deleted post ${post}.
|
|
1241
|
+
`);
|
|
484
1242
|
})
|
|
485
1243
|
});
|
|
486
1244
|
var posts = defineCommand({
|
|
487
1245
|
meta: { name: "posts", description: "Manage posts" },
|
|
488
|
-
subCommands: { list: list3 }
|
|
1246
|
+
subCommands: { list: list3, create, delete: del }
|
|
1247
|
+
});
|
|
1248
|
+
var DAY_LABELS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
|
|
1249
|
+
var DAY_NAMES = /* @__PURE__ */ new Set([
|
|
1250
|
+
"mon",
|
|
1251
|
+
"monday",
|
|
1252
|
+
"tue",
|
|
1253
|
+
"tuesday",
|
|
1254
|
+
"wed",
|
|
1255
|
+
"wednesday",
|
|
1256
|
+
"thu",
|
|
1257
|
+
"thursday",
|
|
1258
|
+
"fri",
|
|
1259
|
+
"friday",
|
|
1260
|
+
"sat",
|
|
1261
|
+
"saturday",
|
|
1262
|
+
"sun",
|
|
1263
|
+
"sunday"
|
|
1264
|
+
]);
|
|
1265
|
+
function dayLabel(day) {
|
|
1266
|
+
return DAY_LABELS[day - 1] ?? String(day);
|
|
1267
|
+
}
|
|
1268
|
+
function collectRepeated(name, rawArgs, fallback) {
|
|
1269
|
+
const flag = `--${name}`;
|
|
1270
|
+
const inline = `${flag}=`;
|
|
1271
|
+
const values = [];
|
|
1272
|
+
for (let i = 0; i < rawArgs.length; i += 1) {
|
|
1273
|
+
const arg = rawArgs[i];
|
|
1274
|
+
if (arg === "--") {
|
|
1275
|
+
break;
|
|
1276
|
+
}
|
|
1277
|
+
if (arg === flag) {
|
|
1278
|
+
const next = rawArgs[i + 1];
|
|
1279
|
+
if (next !== void 0 && !next.startsWith("-")) {
|
|
1280
|
+
values.push(next);
|
|
1281
|
+
i += 1;
|
|
1282
|
+
}
|
|
1283
|
+
continue;
|
|
1284
|
+
}
|
|
1285
|
+
if (arg.startsWith(inline)) {
|
|
1286
|
+
values.push(arg.slice(inline.length));
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
return values.length > 0 ? values : fallback;
|
|
1290
|
+
}
|
|
1291
|
+
function toDayNumbers(value) {
|
|
1292
|
+
const tokens = toList(value).flatMap((entry) => entry.split(",")).map((token) => token.trim()).filter((token) => token.length > 0);
|
|
1293
|
+
for (const token of tokens) {
|
|
1294
|
+
const raw = token.toLowerCase();
|
|
1295
|
+
if (!/^\d+$/.test(raw) && !DAY_NAMES.has(raw)) {
|
|
1296
|
+
throw new Error(
|
|
1297
|
+
`Invalid day: ${token}
|
|
1298
|
+
Use 1-7 (1 = Monday, 7 = Sunday) or names like mon,wed,fri.`
|
|
1299
|
+
);
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
return toDaysOfWeek(tokens);
|
|
1303
|
+
}
|
|
1304
|
+
function toTime(value) {
|
|
1305
|
+
const raw = String(value ?? "").trim();
|
|
1306
|
+
const match = /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/.exec(raw);
|
|
1307
|
+
const hour = Number(match?.[1]);
|
|
1308
|
+
const minute = Number(match?.[2]);
|
|
1309
|
+
if (!match || match[3] !== void 0 && match[3] !== "00" || hour > 23 || minute > 59) {
|
|
1310
|
+
throw new Error(
|
|
1311
|
+
`Invalid --time value: ${raw}
|
|
1312
|
+
Pass a 24-hour time as HH:MM, for example 09:00 or 17:30.`
|
|
1313
|
+
);
|
|
1314
|
+
}
|
|
1315
|
+
return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
|
|
1316
|
+
}
|
|
1317
|
+
function rethrowWithSlotHint(error) {
|
|
1318
|
+
if (error instanceof ZilfuApiError && error.status === 403) {
|
|
1319
|
+
const hint = /^you have read-only access/i.test(error.message) ? "Hint: this token has read-only access to the workspace, and slot changes additionally require space ownership." : "Hint: adding or removing slots requires space ownership \u2014 members cannot change the schedule, whatever their permissions.";
|
|
1320
|
+
process.stderr.write(`${hint}
|
|
1321
|
+
`);
|
|
1322
|
+
}
|
|
1323
|
+
throw error;
|
|
1324
|
+
}
|
|
1325
|
+
function slotRows(slots2) {
|
|
1326
|
+
return slots2.map((slot) => ({
|
|
1327
|
+
id: slot.id,
|
|
1328
|
+
day: dayLabel(slot.day_of_week),
|
|
1329
|
+
time: slot.time
|
|
1330
|
+
}));
|
|
1331
|
+
}
|
|
1332
|
+
var SLOT_COLUMNS = ["id", "day", "time"];
|
|
1333
|
+
var list4 = defineCommand({
|
|
1334
|
+
meta: { name: "list", description: "List the posting slots in a space" },
|
|
1335
|
+
args: {
|
|
1336
|
+
...globalArgs,
|
|
1337
|
+
space: { type: "string", required: true, description: "The space id" }
|
|
1338
|
+
},
|
|
1339
|
+
run: ({ args }) => guard(async () => {
|
|
1340
|
+
const space = toSpaceId(args["space"]);
|
|
1341
|
+
const api = clientFor(args);
|
|
1342
|
+
const res = await api.slots.list({ path: { space } });
|
|
1343
|
+
const slots2 = res.data?.data ?? [];
|
|
1344
|
+
if (wantsJson(args)) {
|
|
1345
|
+
print(slots2, { json: true });
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
print(slotRows(slots2), { columns: SLOT_COLUMNS });
|
|
1349
|
+
})
|
|
1350
|
+
});
|
|
1351
|
+
var create2 = defineCommand({
|
|
1352
|
+
meta: {
|
|
1353
|
+
name: "create",
|
|
1354
|
+
description: "Add a posting slot at one time on one or more days (an existing day is a no-op)"
|
|
1355
|
+
},
|
|
1356
|
+
args: {
|
|
1357
|
+
...globalArgs,
|
|
1358
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
1359
|
+
days: {
|
|
1360
|
+
type: "string",
|
|
1361
|
+
required: true,
|
|
1362
|
+
description: "Days for this slot \u2014 mon,wed,fri or 1,3,5 (1 = Monday, 7 = Sunday); comma-separated or repeatable"
|
|
1363
|
+
},
|
|
1364
|
+
time: {
|
|
1365
|
+
type: "string",
|
|
1366
|
+
required: true,
|
|
1367
|
+
description: "Time of day in the space's timezone, as HH:MM (e.g. 09:00)"
|
|
1368
|
+
}
|
|
1369
|
+
},
|
|
1370
|
+
run: ({ args, rawArgs }) => guard(async () => {
|
|
1371
|
+
const space = toSpaceId(args["space"]);
|
|
1372
|
+
const days_of_week = toDayNumbers(
|
|
1373
|
+
collectRepeated("days", rawArgs, args["days"])
|
|
1374
|
+
);
|
|
1375
|
+
const time = toTime(args["time"]);
|
|
1376
|
+
const api = clientFor(args);
|
|
1377
|
+
const res = await api.slots.create({ path: { space }, body: { days_of_week, time } }).catch(rethrowWithSlotHint);
|
|
1378
|
+
const slots2 = res.data?.data ?? [];
|
|
1379
|
+
if (wantsJson(args)) {
|
|
1380
|
+
print(slots2, { json: true });
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
print(slotRows(slots2), { columns: SLOT_COLUMNS });
|
|
1384
|
+
})
|
|
1385
|
+
});
|
|
1386
|
+
var del2 = defineCommand({
|
|
1387
|
+
meta: {
|
|
1388
|
+
name: "delete",
|
|
1389
|
+
description: "Remove a posting slot (ids come from `slots list`)"
|
|
1390
|
+
},
|
|
1391
|
+
args: {
|
|
1392
|
+
...globalArgs,
|
|
1393
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
1394
|
+
slot: { type: "string", required: true, description: "The slot id" }
|
|
1395
|
+
},
|
|
1396
|
+
run: ({ args }) => guard(async () => {
|
|
1397
|
+
const space = toSpaceId(args["space"]);
|
|
1398
|
+
const slot = toId(args["slot"], "slot");
|
|
1399
|
+
const api = clientFor(args);
|
|
1400
|
+
await api.slots.delete({ path: { space, slot } }).catch(rethrowWithSlotHint);
|
|
1401
|
+
if (wantsJson(args)) {
|
|
1402
|
+
print({ deleted: true, id: slot }, { json: true });
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1405
|
+
process.stdout.write(`Deleted slot ${slot}.
|
|
1406
|
+
`);
|
|
1407
|
+
})
|
|
1408
|
+
});
|
|
1409
|
+
var slots = defineCommand({
|
|
1410
|
+
meta: {
|
|
1411
|
+
name: "slots",
|
|
1412
|
+
description: "Manage a space's recurring posting schedule"
|
|
1413
|
+
},
|
|
1414
|
+
subCommands: { list: list4, create: create2, delete: del2 }
|
|
1415
|
+
});
|
|
1416
|
+
var MIN_PER_PAGE = 10;
|
|
1417
|
+
var MAX_PER_PAGE2 = 100;
|
|
1418
|
+
var NUMBER = new Intl.NumberFormat("en-US");
|
|
1419
|
+
var WEEKDAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
|
|
1420
|
+
function formatValue(value) {
|
|
1421
|
+
return value === null || value === void 0 ? "-" : NUMBER.format(value);
|
|
1422
|
+
}
|
|
1423
|
+
function formatDelta(value) {
|
|
1424
|
+
if (value === null || value === void 0) {
|
|
1425
|
+
return "-";
|
|
1426
|
+
}
|
|
1427
|
+
return `${value > 0 ? "+" : ""}${NUMBER.format(value)}`;
|
|
1428
|
+
}
|
|
1429
|
+
function formatChange(value, unit) {
|
|
1430
|
+
if (value === null) {
|
|
1431
|
+
return "-";
|
|
1432
|
+
}
|
|
1433
|
+
const sign = value > 0 ? "+" : "";
|
|
1434
|
+
if (unit === "percent") {
|
|
1435
|
+
return `${sign}${value}%`;
|
|
1436
|
+
}
|
|
1437
|
+
if (unit === "percentage_points") {
|
|
1438
|
+
return `${sign}${value} pp`;
|
|
1439
|
+
}
|
|
1440
|
+
return `${sign}${value} ${unit}`;
|
|
1441
|
+
}
|
|
1442
|
+
function formatRate(value) {
|
|
1443
|
+
return value === null ? "-" : `${value}%`;
|
|
1444
|
+
}
|
|
1445
|
+
function formatStatus(status, reason) {
|
|
1446
|
+
return reason ? `${status} (${reason})` : status;
|
|
1447
|
+
}
|
|
1448
|
+
function formatCollection(collection) {
|
|
1449
|
+
return formatStatus(collection.state, collection.reason);
|
|
1450
|
+
}
|
|
1451
|
+
function describeCollection(collection) {
|
|
1452
|
+
const parts = [
|
|
1453
|
+
formatCollection(collection),
|
|
1454
|
+
`last collected ${collection.last_collected_at ?? "never"}`,
|
|
1455
|
+
`every ${collection.cadence_hours}h over ${collection.polling_window_days} days`
|
|
1456
|
+
];
|
|
1457
|
+
if (collection.remedy) {
|
|
1458
|
+
parts.push(`remedy: ${collection.remedy}`);
|
|
1459
|
+
}
|
|
1460
|
+
return parts.join(", ");
|
|
1461
|
+
}
|
|
1462
|
+
function formatAccount(account2) {
|
|
1463
|
+
const handle = account2.handler ? ` @${account2.handler}` : "";
|
|
1464
|
+
return `${account2.display_name}${handle} (${account2.social}, id ${account2.id})`;
|
|
1465
|
+
}
|
|
1466
|
+
function formatRange(range) {
|
|
1467
|
+
return `${range.from} to ${range.to} (${range.days} days, ${range.timezone})`;
|
|
1468
|
+
}
|
|
1469
|
+
function formatSlot(cell) {
|
|
1470
|
+
const name = WEEKDAYS[cell.weekday - 1] ?? `day ${cell.weekday}`;
|
|
1471
|
+
return `${name} ${String(cell.hour).padStart(2, "0")}:00`;
|
|
1472
|
+
}
|
|
1473
|
+
function oneLine(text, max) {
|
|
1474
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
1475
|
+
return flat.length > max ? `${flat.slice(0, max - 3)}...` : flat;
|
|
1476
|
+
}
|
|
1477
|
+
function heading(title) {
|
|
1478
|
+
process.stdout.write(`
|
|
1479
|
+
${title}
|
|
1480
|
+
|
|
1481
|
+
`);
|
|
1482
|
+
}
|
|
1483
|
+
function windowQuery(args) {
|
|
1484
|
+
const query = {};
|
|
1485
|
+
if (args["from"] !== void 0) {
|
|
1486
|
+
query.from = toDate(args["from"], "from");
|
|
1487
|
+
}
|
|
1488
|
+
if (args["to"] !== void 0) {
|
|
1489
|
+
query.to = toDate(args["to"], "to");
|
|
1490
|
+
}
|
|
1491
|
+
return query;
|
|
1492
|
+
}
|
|
1493
|
+
function describeWindow(query) {
|
|
1494
|
+
if (query.from && query.to) {
|
|
1495
|
+
return `${query.from} to ${query.to} (requested)`;
|
|
1496
|
+
}
|
|
1497
|
+
if (query.from) {
|
|
1498
|
+
return `from ${query.from} (requested)`;
|
|
1499
|
+
}
|
|
1500
|
+
if (query.to) {
|
|
1501
|
+
return `up to ${query.to} (requested)`;
|
|
1502
|
+
}
|
|
1503
|
+
return "last 7 days in the space timezone (API default)";
|
|
1504
|
+
}
|
|
1505
|
+
function toDir(value) {
|
|
1506
|
+
const dir = String(value).toLowerCase();
|
|
1507
|
+
if (dir !== "asc" && dir !== "desc") {
|
|
1508
|
+
throw new Error(
|
|
1509
|
+
`Invalid --dir value: ${String(value)}
|
|
1510
|
+
Use asc or desc.`
|
|
1511
|
+
);
|
|
1512
|
+
}
|
|
1513
|
+
return dir;
|
|
1514
|
+
}
|
|
1515
|
+
function toPerPage(value) {
|
|
1516
|
+
const perPage = Number(value);
|
|
1517
|
+
if (!Number.isInteger(perPage) || perPage < MIN_PER_PAGE || perPage > MAX_PER_PAGE2) {
|
|
1518
|
+
throw new Error(
|
|
1519
|
+
`Invalid --per-page value: ${String(value)}
|
|
1520
|
+
Use a whole number between ${MIN_PER_PAGE} and ${MAX_PER_PAGE2}.`
|
|
1521
|
+
);
|
|
1522
|
+
}
|
|
1523
|
+
return perPage;
|
|
1524
|
+
}
|
|
1525
|
+
function toPage(value) {
|
|
1526
|
+
const page = Number(value);
|
|
1527
|
+
if (!Number.isInteger(page) || page < 1) {
|
|
1528
|
+
throw new Error(
|
|
1529
|
+
`Invalid --page value: ${String(value)}
|
|
1530
|
+
Pages start at 1.`
|
|
1531
|
+
);
|
|
1532
|
+
}
|
|
1533
|
+
return page;
|
|
1534
|
+
}
|
|
1535
|
+
var windowArgs = {
|
|
1536
|
+
from: {
|
|
1537
|
+
type: "string",
|
|
1538
|
+
description: "Window start, e.g. 2026-07-01 (default: 6 days ago)"
|
|
1539
|
+
},
|
|
1540
|
+
to: {
|
|
1541
|
+
type: "string",
|
|
1542
|
+
description: "Window end, e.g. 2026-07-31 (default: today, space timezone)"
|
|
1543
|
+
}
|
|
1544
|
+
};
|
|
1545
|
+
var overview = defineCommand({
|
|
1546
|
+
meta: {
|
|
1547
|
+
name: "overview",
|
|
1548
|
+
description: "One analytics summary row per account in a space"
|
|
1549
|
+
},
|
|
1550
|
+
args: {
|
|
1551
|
+
...globalArgs,
|
|
1552
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
1553
|
+
...windowArgs
|
|
1554
|
+
},
|
|
1555
|
+
run: ({ args }) => guard(async () => {
|
|
1556
|
+
const space = toSpaceId(args["space"]);
|
|
1557
|
+
const query = windowQuery(args);
|
|
1558
|
+
const api = clientFor(args);
|
|
1559
|
+
const res = await api.analytics.list({ path: { space }, query });
|
|
1560
|
+
const summaries = res.data?.data ?? [];
|
|
1561
|
+
if (wantsJson(args)) {
|
|
1562
|
+
print(summaries, { json: true });
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
process.stdout.write(`Window ${describeWindow(query)}
|
|
1566
|
+
|
|
1567
|
+
`);
|
|
1568
|
+
if (summaries.length === 0) {
|
|
1569
|
+
process.stdout.write("No accounts in this space.\n");
|
|
1570
|
+
return;
|
|
1571
|
+
}
|
|
1572
|
+
print(
|
|
1573
|
+
summaries.map((row) => ({
|
|
1574
|
+
id: row.account.id,
|
|
1575
|
+
account: row.account.display_name,
|
|
1576
|
+
social: row.account.social,
|
|
1577
|
+
collection: formatCollection(row.collection),
|
|
1578
|
+
posts: formatValue(row.coverage?.posts_in_range),
|
|
1579
|
+
interactions: formatValue(row.interactions?.value),
|
|
1580
|
+
remedy: row.collection.remedy ?? ""
|
|
1581
|
+
})),
|
|
1582
|
+
{
|
|
1583
|
+
columns: [
|
|
1584
|
+
"id",
|
|
1585
|
+
"account",
|
|
1586
|
+
"social",
|
|
1587
|
+
"collection",
|
|
1588
|
+
"posts",
|
|
1589
|
+
"interactions",
|
|
1590
|
+
"remedy"
|
|
1591
|
+
]
|
|
1592
|
+
}
|
|
1593
|
+
);
|
|
1594
|
+
const totals = summaries.flatMap(
|
|
1595
|
+
(row) => row.totals.map((total) => ({
|
|
1596
|
+
account: row.account.display_name,
|
|
1597
|
+
metric: total.label,
|
|
1598
|
+
unit: total.unit,
|
|
1599
|
+
value: formatValue(total.value),
|
|
1600
|
+
previous: formatValue(total.previous),
|
|
1601
|
+
change: formatChange(total.change_pct, total.change_unit),
|
|
1602
|
+
note: total.reason ?? ""
|
|
1603
|
+
}))
|
|
1604
|
+
);
|
|
1605
|
+
if (totals.length > 0) {
|
|
1606
|
+
heading("Totals");
|
|
1607
|
+
print(totals, {
|
|
1608
|
+
columns: [
|
|
1609
|
+
"account",
|
|
1610
|
+
"metric",
|
|
1611
|
+
"unit",
|
|
1612
|
+
"value",
|
|
1613
|
+
"previous",
|
|
1614
|
+
"change",
|
|
1615
|
+
"note"
|
|
1616
|
+
]
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
const rates = summaries.flatMap(
|
|
1620
|
+
(row) => row.rates.map((rate) => ({
|
|
1621
|
+
account: row.account.display_name,
|
|
1622
|
+
rate: rate.label,
|
|
1623
|
+
value: formatRate(rate.value),
|
|
1624
|
+
per_post: formatRate(rate.per_post_mean),
|
|
1625
|
+
previous: formatRate(rate.previous),
|
|
1626
|
+
change: formatChange(rate.change_pp, rate.change_unit),
|
|
1627
|
+
note: rate.reason ?? ""
|
|
1628
|
+
}))
|
|
1629
|
+
);
|
|
1630
|
+
if (rates.length > 0) {
|
|
1631
|
+
heading("Rates");
|
|
1632
|
+
print(rates, {
|
|
1633
|
+
columns: [
|
|
1634
|
+
"account",
|
|
1635
|
+
"rate",
|
|
1636
|
+
"value",
|
|
1637
|
+
"per_post",
|
|
1638
|
+
"previous",
|
|
1639
|
+
"change",
|
|
1640
|
+
"note"
|
|
1641
|
+
]
|
|
1642
|
+
});
|
|
1643
|
+
}
|
|
1644
|
+
})
|
|
1645
|
+
});
|
|
1646
|
+
function printReport(report) {
|
|
1647
|
+
const header = {
|
|
1648
|
+
account: formatAccount(report.account),
|
|
1649
|
+
range: formatRange(report.range),
|
|
1650
|
+
previous: `${report.range.previous_from} to ${report.range.previous_to}`,
|
|
1651
|
+
collection: describeCollection(report.collection),
|
|
1652
|
+
coverage: report.coverage ? `${report.coverage.posts_in_range} posts (${report.coverage.posts_imported} imported) on ${report.coverage.days_with_posts} of ${report.coverage.days_in_range} days` : "-",
|
|
1653
|
+
contract: report.contract
|
|
1654
|
+
};
|
|
1655
|
+
if (report.basis) {
|
|
1656
|
+
header["basis"] = report.basis.comparison_immature ? `${report.basis.values} (comparison window is still maturing)` : report.basis.values;
|
|
1657
|
+
header["note"] = report.basis.note;
|
|
1658
|
+
}
|
|
1659
|
+
print(header);
|
|
1660
|
+
if (report.metrics.length === 0) {
|
|
1661
|
+
process.stdout.write(
|
|
1662
|
+
"\nThis platform has no analytics driver, so there is nothing to report.\n"
|
|
1663
|
+
);
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
if (report.totals.length > 0) {
|
|
1667
|
+
heading("Totals");
|
|
1668
|
+
print(
|
|
1669
|
+
report.totals.map((total) => ({
|
|
1670
|
+
// The platform's own wording, printed verbatim: Facebook's "Like
|
|
1671
|
+
// reactions" and LinkedIn's "Reactions" count different things under
|
|
1672
|
+
// the same `likes` key, and only the label says so.
|
|
1673
|
+
metric: total.label,
|
|
1674
|
+
unit: total.unit,
|
|
1675
|
+
value: formatValue(total.value),
|
|
1676
|
+
previous: formatValue(total.previous),
|
|
1677
|
+
change: formatChange(total.change_pct, total.change_unit),
|
|
1678
|
+
note: total.reason ?? ""
|
|
1679
|
+
})),
|
|
1680
|
+
{
|
|
1681
|
+
columns: ["metric", "unit", "value", "previous", "change", "note"]
|
|
1682
|
+
}
|
|
1683
|
+
);
|
|
1684
|
+
}
|
|
1685
|
+
const interactions = report.interactions;
|
|
1686
|
+
if (interactions) {
|
|
1687
|
+
heading(interactions.label);
|
|
1688
|
+
print({
|
|
1689
|
+
value: formatValue(interactions.value),
|
|
1690
|
+
previous: formatValue(interactions.previous),
|
|
1691
|
+
change: formatChange(interactions.change_pct, interactions.change_unit),
|
|
1692
|
+
// At report scope `components` is a list of keys, not key/value pairs,
|
|
1693
|
+
// and on a `mixed` source it is a union — adding it up double-counts.
|
|
1694
|
+
source: `${interactions.source}${interactions.native ? ", reported natively" : ""}`,
|
|
1695
|
+
components: interactions.components.join(", "),
|
|
1696
|
+
status: formatStatus(interactions.status, interactions.reason),
|
|
1697
|
+
definition: interactions.definition
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
if (report.rates.length > 0) {
|
|
1701
|
+
heading("Rates");
|
|
1702
|
+
print(
|
|
1703
|
+
report.rates.map((rate) => ({
|
|
1704
|
+
rate: rate.label,
|
|
1705
|
+
value: formatRate(rate.value),
|
|
1706
|
+
per_post: formatRate(rate.per_post_mean),
|
|
1707
|
+
previous: formatRate(rate.previous),
|
|
1708
|
+
change: formatChange(rate.change_pp, rate.change_unit),
|
|
1709
|
+
over: rate.denominator_label ?? rate.denominator_key,
|
|
1710
|
+
note: rate.reason ?? ""
|
|
1711
|
+
})),
|
|
1712
|
+
{
|
|
1713
|
+
columns: [
|
|
1714
|
+
"rate",
|
|
1715
|
+
"value",
|
|
1716
|
+
"per_post",
|
|
1717
|
+
"previous",
|
|
1718
|
+
"change",
|
|
1719
|
+
"over",
|
|
1720
|
+
"note"
|
|
1721
|
+
]
|
|
1722
|
+
}
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
1725
|
+
heading("Metrics");
|
|
1726
|
+
print(
|
|
1727
|
+
report.metrics.map((metric) => ({
|
|
1728
|
+
key: metric.key,
|
|
1729
|
+
scope: metric.scope,
|
|
1730
|
+
label: metric.label,
|
|
1731
|
+
unit: metric.unit,
|
|
1732
|
+
role: metric.role,
|
|
1733
|
+
window: metric.window_days ?? "",
|
|
1734
|
+
status: metric.status,
|
|
1735
|
+
note: metric.reason ?? ""
|
|
1736
|
+
})),
|
|
1737
|
+
{
|
|
1738
|
+
columns: [
|
|
1739
|
+
"key",
|
|
1740
|
+
"scope",
|
|
1741
|
+
"label",
|
|
1742
|
+
"unit",
|
|
1743
|
+
"role",
|
|
1744
|
+
"window",
|
|
1745
|
+
"status",
|
|
1746
|
+
"note"
|
|
1747
|
+
]
|
|
1748
|
+
}
|
|
1749
|
+
);
|
|
1750
|
+
const audience = report.audience;
|
|
1751
|
+
const latest = audience.latest;
|
|
1752
|
+
if (audience.supported && latest) {
|
|
1753
|
+
heading(`Audience (as of ${latest.date})`);
|
|
1754
|
+
print(
|
|
1755
|
+
audience.metrics.map((key) => ({
|
|
1756
|
+
metric: audience.labels?.[key] ?? key,
|
|
1757
|
+
value: formatValue(latest.values[key]),
|
|
1758
|
+
// latest minus the first point in the window, so an absolute delta.
|
|
1759
|
+
change: formatDelta(audience.change?.[key])
|
|
1760
|
+
})),
|
|
1761
|
+
{ columns: ["metric", "value", "change"] }
|
|
1762
|
+
);
|
|
1763
|
+
}
|
|
1764
|
+
const notes = {};
|
|
1765
|
+
const series = report.series;
|
|
1766
|
+
if (series) {
|
|
1767
|
+
const first = series.points.at(0);
|
|
1768
|
+
const last = series.points.at(-1);
|
|
1769
|
+
const span = first && last ? `, ${first.date} to ${last.date}` : "";
|
|
1770
|
+
notes["series"] = `${series.points.length} daily points${span}, values ${series.values} (${series.metrics.join(", ")}). Full series in --json.`;
|
|
1771
|
+
}
|
|
1772
|
+
const bestTimes = report.best_times;
|
|
1773
|
+
if (bestTimes) {
|
|
1774
|
+
const window = `${bestTimes.lookback_days}-day lookback, min ${bestTimes.min_posts} posts, ${bestTimes.timezone}`;
|
|
1775
|
+
notes["best times"] = bestTimes.best ? `${formatSlot(bestTimes.best)}, average ${bestTimes.best.average} interactions from ${bestTimes.best.posts} posts (${window})` : `no slot qualifies yet (${window})`;
|
|
1776
|
+
}
|
|
1777
|
+
if (Object.keys(notes).length > 0) {
|
|
1778
|
+
heading("Insights");
|
|
1779
|
+
print(notes);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
var account = defineCommand({
|
|
1783
|
+
meta: {
|
|
1784
|
+
name: "account",
|
|
1785
|
+
description: "The full analytics report for one connected account"
|
|
1786
|
+
},
|
|
1787
|
+
args: {
|
|
1788
|
+
...globalArgs,
|
|
1789
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
1790
|
+
account: { type: "string", required: true, description: "The account id" },
|
|
1791
|
+
...windowArgs
|
|
1792
|
+
},
|
|
1793
|
+
run: ({ args }) => guard(async () => {
|
|
1794
|
+
const space = toSpaceId(args["space"]);
|
|
1795
|
+
const accountId = toId(args["account"], "account");
|
|
1796
|
+
const query = windowQuery(args);
|
|
1797
|
+
const api = clientFor(args);
|
|
1798
|
+
const res = await api.analytics.get({
|
|
1799
|
+
path: { space, account: accountId },
|
|
1800
|
+
query
|
|
1801
|
+
});
|
|
1802
|
+
const report = res.data?.data;
|
|
1803
|
+
if (!report) {
|
|
1804
|
+
throw new Error("The API returned no report.");
|
|
1805
|
+
}
|
|
1806
|
+
if (wantsJson(args)) {
|
|
1807
|
+
print(report, { json: true });
|
|
1808
|
+
return;
|
|
1809
|
+
}
|
|
1810
|
+
printReport(report);
|
|
1811
|
+
})
|
|
1812
|
+
});
|
|
1813
|
+
function postColumnHeaders(columns, taken) {
|
|
1814
|
+
const used = new Set(taken);
|
|
1815
|
+
return columns.map((column) => {
|
|
1816
|
+
let header = column.label;
|
|
1817
|
+
if (used.has(header)) {
|
|
1818
|
+
header = `${column.label} (${column.key})`;
|
|
1819
|
+
}
|
|
1820
|
+
if (used.has(header)) {
|
|
1821
|
+
header = column.key;
|
|
1822
|
+
}
|
|
1823
|
+
used.add(header);
|
|
1824
|
+
return header;
|
|
1825
|
+
});
|
|
1826
|
+
}
|
|
1827
|
+
function printPosts(payload) {
|
|
1828
|
+
print({
|
|
1829
|
+
account: formatAccount(payload.account),
|
|
1830
|
+
range: formatRange(payload.range),
|
|
1831
|
+
collection: describeCollection(payload.collection),
|
|
1832
|
+
sort: `${payload.sort.key} ${payload.sort.direction}`,
|
|
1833
|
+
page: `${payload.meta.current_page} of ${payload.meta.last_page} (${payload.meta.per_page} per page, ${payload.meta.total} posts in range)`,
|
|
1834
|
+
contract: payload.contract
|
|
1835
|
+
});
|
|
1836
|
+
const fixed = ["post_id", "published_at", "excerpt", "interactions"];
|
|
1837
|
+
const headers = postColumnHeaders(payload.columns, fixed);
|
|
1838
|
+
heading("Posts");
|
|
1839
|
+
print(
|
|
1840
|
+
payload.data.map((post) => {
|
|
1841
|
+
const row = {
|
|
1842
|
+
post_id: post.post_id,
|
|
1843
|
+
// Published timestamps are ISO instants while `range` dates are
|
|
1844
|
+
// Y-m-d — two formats on purpose, so neither is reformatted here.
|
|
1845
|
+
published_at: post.published_at ?? "-",
|
|
1846
|
+
excerpt: oneLine(post.excerpt, 48),
|
|
1847
|
+
interactions: formatValue(post.interactions.total)
|
|
1848
|
+
};
|
|
1849
|
+
payload.columns.forEach((column, index) => {
|
|
1850
|
+
row[headers[index] ?? column.key] = formatValue(
|
|
1851
|
+
post.metrics[column.key]?.value
|
|
1852
|
+
);
|
|
1853
|
+
});
|
|
1854
|
+
return row;
|
|
1855
|
+
}),
|
|
1856
|
+
{ columns: [...fixed, ...headers] }
|
|
1857
|
+
);
|
|
1858
|
+
if (payload.columns.length === 0) {
|
|
1859
|
+
process.stdout.write(
|
|
1860
|
+
"\nThis platform has no analytics driver, so it declares no metric columns.\n"
|
|
1861
|
+
);
|
|
1862
|
+
}
|
|
1863
|
+
const unavailable = payload.columns.map((column, index) => ({ column, header: headers[index] ?? column.key })).filter(({ column }) => column.status !== "ok");
|
|
1864
|
+
if (unavailable.length > 0) {
|
|
1865
|
+
process.stdout.write("\n");
|
|
1866
|
+
for (const { column, header } of unavailable) {
|
|
1867
|
+
process.stdout.write(
|
|
1868
|
+
`${header}: ${formatStatus(column.status, column.reason)}
|
|
1869
|
+
`
|
|
1870
|
+
);
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
const sortable = payload.sort.options.filter((option) => option.sortable);
|
|
1874
|
+
const unhealthy = payload.sort.options.filter((option) => !option.sortable);
|
|
1875
|
+
process.stdout.write(
|
|
1876
|
+
`
|
|
1877
|
+
Sort keys: ${sortable.map((option) => option.key).join(", ")}
|
|
1878
|
+
`
|
|
1879
|
+
);
|
|
1880
|
+
if (unhealthy.length > 0) {
|
|
1881
|
+
process.stdout.write(
|
|
1882
|
+
`Accepted but currently unmeasured: ${unhealthy.map((option) => option.key).join(", ")}
|
|
1883
|
+
`
|
|
1884
|
+
);
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
var posts2 = defineCommand({
|
|
1888
|
+
meta: {
|
|
1889
|
+
name: "posts",
|
|
1890
|
+
description: "One row per published post, with this platform's metrics"
|
|
1891
|
+
},
|
|
1892
|
+
args: {
|
|
1893
|
+
...globalArgs,
|
|
1894
|
+
space: { type: "string", required: true, description: "The space id" },
|
|
1895
|
+
account: { type: "string", required: true, description: "The account id" },
|
|
1896
|
+
...windowArgs,
|
|
1897
|
+
sort: {
|
|
1898
|
+
type: "string",
|
|
1899
|
+
description: "Order by published_at, interactions or any metric key this account declares"
|
|
1900
|
+
},
|
|
1901
|
+
dir: { type: "string", description: "asc or desc (default: desc)" },
|
|
1902
|
+
"per-page": {
|
|
1903
|
+
type: "string",
|
|
1904
|
+
description: `Rows per page, ${MIN_PER_PAGE}-${MAX_PER_PAGE2} (default: 20)`
|
|
1905
|
+
},
|
|
1906
|
+
page: { type: "string", description: "Page number (default: 1)" }
|
|
1907
|
+
},
|
|
1908
|
+
run: ({ args }) => guard(async () => {
|
|
1909
|
+
const space = toSpaceId(args["space"]);
|
|
1910
|
+
const accountId = toId(args["account"], "account");
|
|
1911
|
+
const query = windowQuery(args);
|
|
1912
|
+
if (args["sort"] !== void 0) {
|
|
1913
|
+
query.sort = String(args["sort"]);
|
|
1914
|
+
}
|
|
1915
|
+
if (args["dir"] !== void 0) {
|
|
1916
|
+
query.dir = toDir(args["dir"]);
|
|
1917
|
+
}
|
|
1918
|
+
if (args["per-page"] !== void 0) {
|
|
1919
|
+
query.per_page = toPerPage(args["per-page"]);
|
|
1920
|
+
}
|
|
1921
|
+
if (args["page"] !== void 0) {
|
|
1922
|
+
query.page = toPage(args["page"]);
|
|
1923
|
+
}
|
|
1924
|
+
const api = clientFor(args);
|
|
1925
|
+
const res = await api.analytics.posts({
|
|
1926
|
+
path: { space, account: accountId },
|
|
1927
|
+
query
|
|
1928
|
+
});
|
|
1929
|
+
const payload = res.data;
|
|
1930
|
+
if (!payload) {
|
|
1931
|
+
throw new Error("The API returned no posts payload.");
|
|
1932
|
+
}
|
|
1933
|
+
if (wantsJson(args)) {
|
|
1934
|
+
print(payload, { json: true });
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
printPosts(payload);
|
|
1938
|
+
})
|
|
1939
|
+
});
|
|
1940
|
+
var analytics = defineCommand({
|
|
1941
|
+
meta: { name: "analytics", description: "Read analytics reports" },
|
|
1942
|
+
subCommands: { overview, account, posts: posts2 }
|
|
1943
|
+
});
|
|
1944
|
+
var upload = defineCommand({
|
|
1945
|
+
meta: {
|
|
1946
|
+
name: "upload",
|
|
1947
|
+
description: "Upload a local file and print a URL you can attach to a post"
|
|
1948
|
+
},
|
|
1949
|
+
args: {
|
|
1950
|
+
...globalArgs,
|
|
1951
|
+
file: { type: "string", required: true, description: "Path to the file" },
|
|
1952
|
+
space: {
|
|
1953
|
+
type: "string",
|
|
1954
|
+
description: "Also add the upload to this space's media library"
|
|
1955
|
+
}
|
|
1956
|
+
},
|
|
1957
|
+
run: ({ args }) => guard(async () => {
|
|
1958
|
+
const path = String(args["file"]);
|
|
1959
|
+
const mime = mimeForPath(path);
|
|
1960
|
+
const body = await readFile(path);
|
|
1961
|
+
const api = clientFor(args);
|
|
1962
|
+
const signed = await api.media.sign({ body: { mime } });
|
|
1963
|
+
if (!signed.data) {
|
|
1964
|
+
throw new Error("The API returned no upload URL.");
|
|
1965
|
+
}
|
|
1966
|
+
const { upload_url: uploadUrl, url } = signed.data;
|
|
1967
|
+
const res = await fetch(uploadUrl, {
|
|
1968
|
+
method: "PUT",
|
|
1969
|
+
headers: { "Content-Type": mime },
|
|
1970
|
+
body: new Uint8Array(body)
|
|
1971
|
+
});
|
|
1972
|
+
if (!res.ok) {
|
|
1973
|
+
throw new Error(
|
|
1974
|
+
`Upload failed: ${res.status} ${res.statusText}
|
|
1975
|
+
The signed URL expires 15 minutes after it is issued.`
|
|
1976
|
+
);
|
|
1977
|
+
}
|
|
1978
|
+
const type = mediaTypeForMime(mime);
|
|
1979
|
+
if (args["space"] !== void 0) {
|
|
1980
|
+
const space = toSpaceId(args["space"]);
|
|
1981
|
+
await api.media.create({
|
|
1982
|
+
path: { space },
|
|
1983
|
+
body: { url, type, mime, name: basename(path) }
|
|
1984
|
+
});
|
|
1985
|
+
}
|
|
1986
|
+
print({ url, type, mime }, { json: wantsJson(args) });
|
|
1987
|
+
})
|
|
1988
|
+
});
|
|
1989
|
+
var media = defineCommand({
|
|
1990
|
+
meta: { name: "media", description: "Upload and manage media" },
|
|
1991
|
+
subCommands: { upload }
|
|
489
1992
|
});
|
|
490
1993
|
|
|
491
1994
|
// src/cli.ts
|
|
@@ -502,7 +2005,10 @@ var main = defineCommand({
|
|
|
502
2005
|
health,
|
|
503
2006
|
spaces,
|
|
504
2007
|
accounts,
|
|
505
|
-
posts
|
|
2008
|
+
posts,
|
|
2009
|
+
slots,
|
|
2010
|
+
analytics,
|
|
2011
|
+
media
|
|
506
2012
|
}
|
|
507
2013
|
});
|
|
508
2014
|
runMain(main);
|