@posterly/cli 0.1.1 → 0.1.3
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 +45 -8
- package/bin/posterly.mjs +71 -181
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -2,11 +2,42 @@
|
|
|
2
2
|
|
|
3
3
|
Command line access to the posterly API.
|
|
4
4
|
|
|
5
|
+
## Quick start
|
|
6
|
+
|
|
7
|
+
Run the CLI without installing anything globally:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx @posterly/cli@latest auth:login
|
|
11
|
+
npx @posterly/cli@latest whoami --pretty
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`npx` downloads and runs the CLI for that command. It does not leave a permanent
|
|
15
|
+
`posterly` binary on your shell `PATH`, so keep using `npx @posterly/cli@latest`
|
|
16
|
+
for each command unless you install the CLI globally.
|
|
17
|
+
|
|
18
|
+
Optional global install:
|
|
19
|
+
|
|
5
20
|
```bash
|
|
21
|
+
npm i -g @posterly/cli@latest
|
|
6
22
|
posterly auth:login
|
|
7
|
-
posterly
|
|
8
|
-
|
|
9
|
-
|
|
23
|
+
posterly whoami --pretty
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
If global install fails with `EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@posterly'`,
|
|
27
|
+
your npm global folder is not writable by your user. Use the `npx` commands
|
|
28
|
+
above, or fix your npm setup with a user-owned Node install such as `nvm`.
|
|
29
|
+
|
|
30
|
+
Running `npm i @posterly/cli` without `-g` installs the package into the current
|
|
31
|
+
folder. In that case `posterly` will only be available as
|
|
32
|
+
`./node_modules/.bin/posterly` from that folder, not as a global terminal command.
|
|
33
|
+
|
|
34
|
+
## Examples
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
posterly auth:login
|
|
38
|
+
posterly auth:key
|
|
39
|
+
posterly whoami --pretty
|
|
40
|
+
posterly accounts:list --pretty
|
|
10
41
|
posterly accounts:disconnect 123 --confirm --pretty
|
|
11
42
|
posterly platforms:schema --platform instagram --pretty
|
|
12
43
|
posterly oauth:clients --pretty
|
|
@@ -27,12 +58,18 @@ posterly gbp:reply --account-id 123 --review-name accounts/123/locations/456/rev
|
|
|
27
58
|
```
|
|
28
59
|
|
|
29
60
|
The CLI outputs JSON by default so it can be piped into `jq` or other tools. Use
|
|
30
|
-
`--pretty` for formatted
|
|
61
|
+
`--pretty` for formatted output. `--human` is accepted as an alias for
|
|
62
|
+
`--pretty` for older examples.
|
|
63
|
+
|
|
64
|
+
Authentication:
|
|
31
65
|
|
|
32
|
-
|
|
66
|
+
- Prefer `posterly auth:login` for browser-based OAuth.
|
|
67
|
+
- Use `posterly auth:key` with no argument to paste an API key into a hidden prompt.
|
|
68
|
+
- For non-interactive setup, pipe the key on stdin: `printf '%s\n' "$POSTERLY_API_KEY" | posterly auth:key`.
|
|
69
|
+
- `POSTERLY_API_KEY` can be used directly without saving local config.
|
|
33
70
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
71
|
+
`--api-key` is still supported for automation, but avoid passing live keys as
|
|
72
|
+
command-line arguments in interactive shells because argv can be stored in shell
|
|
73
|
+
history or exposed through local process listings.
|
|
37
74
|
|
|
38
75
|
Set `--url` or `POSTERLY_URL` to point at a non-production posterly deployment.
|
package/bin/posterly.mjs
CHANGED
|
@@ -22,7 +22,7 @@ const OAUTH_SCOPES = ['accounts:read', 'accounts:write', 'posts:read', 'posts:wr
|
|
|
22
22
|
|
|
23
23
|
const COMMANDS = [
|
|
24
24
|
'auth:login [--url https://www.poster.ly] [--no-open]',
|
|
25
|
-
'auth:key [
|
|
25
|
+
'auth:key [--url https://www.poster.ly]',
|
|
26
26
|
'auth:status',
|
|
27
27
|
'auth:logout',
|
|
28
28
|
'whoami',
|
|
@@ -54,9 +54,8 @@ async function main() {
|
|
|
54
54
|
return print({
|
|
55
55
|
posterly: 'CLI for the posterly API',
|
|
56
56
|
usage: 'posterly <command> [--flag value]',
|
|
57
|
-
output: 'JSON by default. Add --human for readable Markdown-style tables, or --pretty for formatted JSON.',
|
|
58
57
|
commands: COMMANDS,
|
|
59
|
-
auth: 'Run `posterly auth:login
|
|
58
|
+
auth: 'Run `npx @posterly/cli@latest auth:login` for the no-install flow, `posterly auth:login` after global install, run `posterly auth:key` and paste a key at the prompt, or set POSTERLY_API_KEY.',
|
|
60
59
|
}, flags);
|
|
61
60
|
}
|
|
62
61
|
|
|
@@ -359,6 +358,10 @@ function parseArgs(args) {
|
|
|
359
358
|
}
|
|
360
359
|
}
|
|
361
360
|
|
|
361
|
+
if (flags.human && flags.pretty === undefined) {
|
|
362
|
+
flags.pretty = true;
|
|
363
|
+
}
|
|
364
|
+
|
|
362
365
|
return { command, flags, positional };
|
|
363
366
|
}
|
|
364
367
|
|
|
@@ -366,7 +369,9 @@ async function createClient(flags) {
|
|
|
366
369
|
const config = await readConfig();
|
|
367
370
|
const apiKey = flags.apiKey || process.env.POSTERLY_API_KEY || config.apiKey;
|
|
368
371
|
const baseUrl = normalizeBaseUrl(flags.url || process.env.POSTERLY_URL || config.baseUrl || DEFAULT_BASE_URL);
|
|
369
|
-
if (!apiKey)
|
|
372
|
+
if (!apiKey) {
|
|
373
|
+
throw new Error('Missing API key. Run `npx @posterly/cli@latest auth:login`, `posterly auth:login` after global install, run `posterly auth:key` and paste a key at the prompt, or set POSTERLY_API_KEY.');
|
|
374
|
+
}
|
|
370
375
|
|
|
371
376
|
async function request(method, path, body) {
|
|
372
377
|
const res = await fetch(`${baseUrl}/api/v1${path}`, {
|
|
@@ -400,8 +405,14 @@ async function createClient(flags) {
|
|
|
400
405
|
}
|
|
401
406
|
|
|
402
407
|
async function saveKey(key, flags) {
|
|
403
|
-
|
|
404
|
-
|
|
408
|
+
if (key || flags.apiKey) {
|
|
409
|
+
console.warn('Warning: passing API keys as command-line arguments can expose them through shell history or process listings. Prefer `posterly auth:key` with no argument or POSTERLY_API_KEY.');
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const apiKey = key || flags.apiKey || process.env.POSTERLY_API_KEY || await readApiKeyFromInput();
|
|
413
|
+
if (!apiKey) {
|
|
414
|
+
throw new Error('Missing API key. Run `posterly auth:key` and paste a key at the prompt, pipe a key on stdin, use `npx @posterly/cli@latest auth:key`, or set POSTERLY_API_KEY.');
|
|
415
|
+
}
|
|
405
416
|
await mkdir(CONFIG_DIR, { recursive: true });
|
|
406
417
|
const config = {
|
|
407
418
|
apiKey,
|
|
@@ -412,6 +423,60 @@ async function saveKey(key, flags) {
|
|
|
412
423
|
return print({ saved: true, config_file: CONFIG_FILE, base_url: config.baseUrl }, flags);
|
|
413
424
|
}
|
|
414
425
|
|
|
426
|
+
async function readApiKeyFromInput() {
|
|
427
|
+
if (!process.stdin.isTTY) {
|
|
428
|
+
const chunks = [];
|
|
429
|
+
for await (const chunk of process.stdin) {
|
|
430
|
+
chunks.push(Buffer.from(chunk));
|
|
431
|
+
}
|
|
432
|
+
return Buffer.concat(chunks).toString('utf8').trim();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (!process.stdout.isTTY || typeof process.stdin.setRawMode !== 'function') {
|
|
436
|
+
return '';
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
process.stdout.write('Posterly API key: ');
|
|
440
|
+
process.stdin.setRawMode(true);
|
|
441
|
+
process.stdin.resume();
|
|
442
|
+
process.stdin.setEncoding('utf8');
|
|
443
|
+
|
|
444
|
+
let value = '';
|
|
445
|
+
try {
|
|
446
|
+
return await new Promise((resolve, reject) => {
|
|
447
|
+
const onData = (chunk) => {
|
|
448
|
+
if (chunk === '\u0003') {
|
|
449
|
+
cleanup();
|
|
450
|
+
process.stdout.write('\n');
|
|
451
|
+
reject(new Error('Cancelled'));
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (chunk === '\r' || chunk === '\n') {
|
|
455
|
+
cleanup();
|
|
456
|
+
process.stdout.write('\n');
|
|
457
|
+
resolve(value.trim());
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (chunk === '\u0008' || chunk === '\u007f') {
|
|
461
|
+
value = value.slice(0, -1);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
value += chunk;
|
|
465
|
+
};
|
|
466
|
+
const cleanup = () => {
|
|
467
|
+
process.stdin.off('data', onData);
|
|
468
|
+
process.stdin.setRawMode(false);
|
|
469
|
+
process.stdin.pause();
|
|
470
|
+
};
|
|
471
|
+
process.stdin.on('data', onData);
|
|
472
|
+
});
|
|
473
|
+
} finally {
|
|
474
|
+
if (process.stdin.isRaw) {
|
|
475
|
+
process.stdin.setRawMode(false);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
415
480
|
async function authLogin(flags) {
|
|
416
481
|
const baseUrl = normalizeBaseUrl(flags.url || process.env.POSTERLY_URL || DEFAULT_BASE_URL);
|
|
417
482
|
const redirectUri = `http://127.0.0.1:${OAUTH_CALLBACK_PORT}${OAUTH_CALLBACK_PATH}`;
|
|
@@ -840,182 +905,7 @@ function escapeHtml(value) {
|
|
|
840
905
|
.replace(/'/g, ''');
|
|
841
906
|
}
|
|
842
907
|
|
|
843
|
-
function formatHuman(value) {
|
|
844
|
-
if (value === null || value === undefined) return 'No response body.';
|
|
845
|
-
if (typeof value !== 'object' || Array.isArray(value)) return JSON.stringify(value, null, 2);
|
|
846
|
-
|
|
847
|
-
if (Array.isArray(value.accounts)) {
|
|
848
|
-
return [
|
|
849
|
-
`## Connected accounts (${value.accounts.length})`,
|
|
850
|
-
markdownTable(['Platform', 'Account', 'Account ID', 'Workspace'], value.accounts.map((account) => [
|
|
851
|
-
account.platform,
|
|
852
|
-
account.username ? `@${account.username}` : 'n/a',
|
|
853
|
-
codeCell(account.id),
|
|
854
|
-
account.workspace_id ? codeCell(account.workspace_id) : 'default',
|
|
855
|
-
])),
|
|
856
|
-
].join('\n\n');
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
if (Array.isArray(value.brands)) {
|
|
860
|
-
return [
|
|
861
|
-
`## Brands (${value.brands.length})`,
|
|
862
|
-
markdownTable(['Brand', 'Brand ID', 'Accounts', 'Source', 'Workspace'], value.brands.map((brand) => [
|
|
863
|
-
brand.name,
|
|
864
|
-
codeCell(brand.id),
|
|
865
|
-
brand.account_count,
|
|
866
|
-
brand.source,
|
|
867
|
-
brand.workspace_id ? codeCell(brand.workspace_id) : 'default',
|
|
868
|
-
])),
|
|
869
|
-
].join('\n\n');
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
if (Array.isArray(value.posts)) {
|
|
873
|
-
return [
|
|
874
|
-
`## Posts (${value.posts.length}${value.total !== undefined ? ` of ${value.total}` : ''})`,
|
|
875
|
-
markdownTable(['Status', 'Post ID', 'Caption', 'Scheduled', 'Type'], value.posts.map((post) => [
|
|
876
|
-
post.status,
|
|
877
|
-
codeCell(post.id),
|
|
878
|
-
compactCell(post.content || post.caption || post.caption_snippet, 80) || '(empty)',
|
|
879
|
-
formatDateCell(post.scheduled_at || post.posted_at),
|
|
880
|
-
post.post_type || post.media_type || 'post',
|
|
881
|
-
])),
|
|
882
|
-
].join('\n\n');
|
|
883
|
-
}
|
|
884
|
-
|
|
885
|
-
if (value.account && value.summary && value.range) {
|
|
886
|
-
return [
|
|
887
|
-
`## Analytics for @${value.account.username}`,
|
|
888
|
-
`_${value.account.platform} · ${value.range.from} → ${value.range.to}_`,
|
|
889
|
-
'',
|
|
890
|
-
markdownTable(['Metric', 'Value'], analyticsRows(value.summary)),
|
|
891
|
-
].join('\n');
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
if (Array.isArray(value.reviews)) {
|
|
895
|
-
return [
|
|
896
|
-
`## Google Business reviews (${value.reviews.length}${value.total !== undefined ? ` of ${value.total}` : ''})`,
|
|
897
|
-
markdownTable(['Reviewer', 'Rating', 'Location', 'Reply', 'Comment'], value.reviews.map((review) => [
|
|
898
|
-
review.reviewer?.displayName || 'Anonymous',
|
|
899
|
-
review.starRating ? `${review.starRating} stars` : 'unrated',
|
|
900
|
-
review.businessName || review.locationName || review.locationId || 'location',
|
|
901
|
-
review.reviewReply ? 'replied' : 'unanswered',
|
|
902
|
-
compactCell(review.comment, 120) || '(rating only)',
|
|
903
|
-
])),
|
|
904
|
-
].join('\n\n');
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
if (Array.isArray(value.webhooks)) {
|
|
908
|
-
return [
|
|
909
|
-
`## Webhooks (${value.webhooks.length})`,
|
|
910
|
-
markdownTable(['Webhook ID', 'State', 'URL', 'Events'], value.webhooks.map((webhook) => [
|
|
911
|
-
codeCell(webhook.id),
|
|
912
|
-
webhook.is_active ? 'active' : 'inactive',
|
|
913
|
-
webhook.url,
|
|
914
|
-
Array.isArray(webhook.events) && webhook.events.length ? webhook.events.join(', ') : 'default events',
|
|
915
|
-
])),
|
|
916
|
-
].join('\n\n');
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
if (Array.isArray(value.platforms)) {
|
|
920
|
-
return [
|
|
921
|
-
`## Posterly platforms (${value.platforms.length})`,
|
|
922
|
-
markdownTable(['Platform', 'ID', 'Status', 'Post types', 'Analytics'], value.platforms.map((platform) => [
|
|
923
|
-
platform.label,
|
|
924
|
-
codeCell(platform.id),
|
|
925
|
-
platform.status,
|
|
926
|
-
Array.isArray(platform.post_types) ? platform.post_types.join(', ') : 'n/a',
|
|
927
|
-
Boolean(platform.analytics_supported) ? 'yes' : 'no',
|
|
928
|
-
])),
|
|
929
|
-
].join('\n\n');
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
if (value.post) {
|
|
933
|
-
return [
|
|
934
|
-
`## Post ${codeCell(value.post.id)}`,
|
|
935
|
-
markdownTable(['Field', 'Value'], [
|
|
936
|
-
['Status', value.post.status],
|
|
937
|
-
['Type', value.post.post_type || 'unknown'],
|
|
938
|
-
['Scheduled', formatDateCell(value.post.scheduled_at)],
|
|
939
|
-
['Published URL', value.post.platform_post_url],
|
|
940
|
-
]),
|
|
941
|
-
value.post.content ? `\n### Caption\n> ${String(value.post.content).replace(/\n/g, '\n> ')}` : '',
|
|
942
|
-
].filter(Boolean).join('\n\n');
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
return [
|
|
946
|
-
'## Posterly response',
|
|
947
|
-
markdownTable(['Field', 'Value'], Object.entries(value).map(([key, item]) => [
|
|
948
|
-
key,
|
|
949
|
-
Array.isArray(item)
|
|
950
|
-
? item.join('\n')
|
|
951
|
-
: typeof item === 'object' && item !== null
|
|
952
|
-
? JSON.stringify(item)
|
|
953
|
-
: item,
|
|
954
|
-
])),
|
|
955
|
-
].join('\n\n');
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
function analyticsRows(summary) {
|
|
959
|
-
return [
|
|
960
|
-
['Followers', summary.current_followers],
|
|
961
|
-
['Followers change', signedCell(summary.followers_change)],
|
|
962
|
-
['Reach', summary.total_reach],
|
|
963
|
-
['Views', summary.total_views],
|
|
964
|
-
['Profile views', summary.total_profile_views],
|
|
965
|
-
['Accounts engaged', summary.total_accounts_engaged],
|
|
966
|
-
['Engagement rate', summary.engagement_rate == null ? undefined : `${summary.engagement_rate}%`],
|
|
967
|
-
['Engagement rate by followers', summary.engagement_rate_by_followers == null ? undefined : `${summary.engagement_rate_by_followers}%`],
|
|
968
|
-
];
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
function markdownTable(headers, rows) {
|
|
972
|
-
const visibleRows = rows.filter((row) => row.some((cell) => cell !== undefined && cell !== null && cell !== ''));
|
|
973
|
-
if (visibleRows.length === 0) return '';
|
|
974
|
-
return [
|
|
975
|
-
`| ${headers.map(escapeTableCell).join(' | ')} |`,
|
|
976
|
-
`| ${headers.map(() => '---').join(' | ')} |`,
|
|
977
|
-
...visibleRows.map((row) => `| ${row.map(formatTableCell).map(escapeTableCell).join(' | ')} |`),
|
|
978
|
-
].join('\n');
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
function formatTableCell(value) {
|
|
982
|
-
if (value === undefined || value === null || value === '') return 'n/a';
|
|
983
|
-
if (typeof value === 'boolean') return value ? 'yes' : 'no';
|
|
984
|
-
if (typeof value === 'number') return value.toLocaleString();
|
|
985
|
-
return String(value);
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
function escapeTableCell(value) {
|
|
989
|
-
return String(value).replace(/\|/g, '\\|').replace(/\r?\n/g, '<br>');
|
|
990
|
-
}
|
|
991
|
-
|
|
992
|
-
function codeCell(value) {
|
|
993
|
-
return `\`${formatTableCell(value).replace(/`/g, '\\`')}\``;
|
|
994
|
-
}
|
|
995
|
-
|
|
996
|
-
function compactCell(value, maxLength = 80) {
|
|
997
|
-
const text = String(value || '').replace(/\s+/g, ' ').trim();
|
|
998
|
-
return text.length > maxLength ? `${text.slice(0, Math.max(0, maxLength - 1))}…` : text;
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
function signedCell(value) {
|
|
1002
|
-
if (value === undefined || value === null || value === '') return 'n/a';
|
|
1003
|
-
const number = Number(value);
|
|
1004
|
-
if (Number.isNaN(number)) return String(value);
|
|
1005
|
-
return number >= 0 ? `+${number.toLocaleString()}` : number.toLocaleString();
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
function formatDateCell(value) {
|
|
1009
|
-
if (!value) return 'n/a';
|
|
1010
|
-
const date = new Date(value);
|
|
1011
|
-
return Number.isNaN(date.getTime()) ? String(value) : date.toLocaleString();
|
|
1012
|
-
}
|
|
1013
|
-
|
|
1014
908
|
function print(value, flags = {}) {
|
|
1015
|
-
if (flagEnabled(flags.human)) {
|
|
1016
|
-
process.stdout.write(`${formatHuman(value)}\n`);
|
|
1017
|
-
return;
|
|
1018
|
-
}
|
|
1019
909
|
const json = flags.pretty ? JSON.stringify(value, null, 2) : JSON.stringify(value);
|
|
1020
910
|
process.stdout.write(`${json}\n`);
|
|
1021
911
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posterly/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Command line interface for the posterly API",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
|
-
"posterly": "
|
|
8
|
+
"posterly": "bin/posterly.mjs"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
9
12
|
},
|
|
10
13
|
"engines": {
|
|
11
14
|
"node": ">=20"
|