-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.js
More file actions
executable file
·316 lines (277 loc) · 9.67 KB
/
Copy pathbuffer.js
File metadata and controls
executable file
·316 lines (277 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env node
import { Command } from 'commander';
import chalk from 'chalk';
import ora from 'ora';
import { getConfig } from './lib/config.js';
import { validateApiKey } from './lib/auth.js';
import { BufferApi } from './lib/buffer-api.js';
import {
parseProfilesList,
parseScheduleTime,
validatePostText,
validateImagePath,
validatePostOptions,
parseLimit,
} from './lib/utils.js';
import {
CLI_NAME,
CLI_VERSION,
DEFAULT_QUEUE_LIMIT,
IDEA_SUCCESS_HINT_LIMIT,
POST_SUCCESS_QUEUE_HINT_LIMIT,
} from './lib/constants.js';
/**
* Render profiles command output.
* @param {Array<{id:string, service?:string, username?:string}>} profiles
* @returns {string}
*/
export function formatProfiles(profiles) {
if (!profiles.length) {
return 'No connected profiles found.';
}
const lines = profiles.map((profile) => {
const service = profile.service || 'unknown';
const username = profile.username ? `@${profile.username}` : 'n/a';
return `${chalk.green('✓')} ${service} (${username}) - ID: ${profile.id}`;
});
return ['Connected Profiles:', ...lines].join('\n');
}
/**
* Render a success message for created posts.
* @param {{id?: string, scheduledAt?: string, profiles?: Array<{service?: string}>}} post
* @returns {string}
*/
export function formatPostSuccess(post) {
const services =
(post.profiles || [])
.map((profile) => profile.service)
.filter(Boolean)
.join(', ') || 'unknown';
const lines = [
`${chalk.green('✅')} Post created successfully`,
`ID: ${post.id || 'n/a'}`,
`Profiles: ${services}`,
];
if (post.scheduledAt) {
lines.push(`Scheduled: ${new Date(post.scheduledAt).toISOString()}`);
} else {
lines.push('Scheduled: immediate/queue');
}
lines.push(
`Next step: Run "${CLI_NAME} queue --limit ${POST_SUCCESS_QUEUE_HINT_LIMIT}" to verify upcoming posts.`
);
return lines.join('\n');
}
/**
* Render queue entries in readable numbered form.
* @param {Array<{text?: string, scheduledAt?: string, profiles?: Array<{service?: string}>}>} posts
* @returns {string}
*/
export function formatQueuePosts(posts) {
if (!posts.length) {
return 'No upcoming posts in queue.';
}
const lines = [`Upcoming Posts (${posts.length}):`, ''];
posts.forEach((post, index) => {
const text = (post.text || '').trim();
const preview = text.length > 80 ? `${text.slice(0, 77)}...` : text;
const services =
(post.profiles || [])
.map((profile) => profile.service)
.filter(Boolean)
.join(', ') || 'unknown';
const scheduled = post.scheduledAt ? new Date(post.scheduledAt).toISOString() : 'n/a';
lines.push(`${index + 1}. "${preview}" → ${services}`);
lines.push(` Scheduled: ${scheduled}`);
lines.push('');
});
return lines.join('\n').trimEnd();
}
/**
* Render a success message for created ideas.
* @param {{id?: string, text?: string}} idea
* @returns {string}
*/
export function formatIdeaSuccess(idea) {
return [
`${chalk.green('✅')} Idea saved successfully`,
`ID: ${idea.id || 'n/a'}`,
`Text: ${(idea.text || '').trim() || 'n/a'}`,
`Next step: Use "${CLI_NAME} ideas --limit ${IDEA_SUCCESS_HINT_LIMIT}" to review your draft backlog.`,
].join('\n');
}
/**
* Render ideas list output.
* @param {Array<{text?: string, createdAt?: string}>} ideas
* @returns {string}
*/
export function formatIdeas(ideas) {
if (!ideas.length) {
return 'No saved ideas found.';
}
const lines = [`Saved Ideas (${ideas.length}):`, ''];
ideas.forEach((idea, index) => {
const text = (idea.text || '').trim();
const preview = text.length > 80 ? `${text.slice(0, 77)}...` : text;
const createdAt = idea.createdAt ? new Date(idea.createdAt).toISOString() : 'n/a';
lines.push(`${index + 1}. "${preview}"`);
lines.push(` Created: ${createdAt}`);
lines.push('');
});
return lines.join('\n').trimEnd();
}
/**
* Resolve final profile IDs from mutually exclusive target options.
* @param {{profile?: string, profiles?: string, all?: boolean}} options
* @param {Array<{id?: string}>} profiles
* @returns {string[]}
*/
function resolveProfileIds(options, profiles = []) {
if (options.profile) {
return [options.profile];
}
const list = parseProfilesList(options.profiles);
if (list.length) {
return list;
}
if (options.all) {
return profiles.map((profile) => profile.id).filter(Boolean);
}
throw new Error('No target profile provided. Use --profile, --profiles, or --all.');
}
/**
* Create commander CLI program.
* @param {{api?: any}} [options]
*/
export function createCli({ api } = {}) {
const program = new Command();
program
.name(CLI_NAME)
.description('Buffer CLI for posting and profile management')
.version(CLI_VERSION);
program
.command('profiles')
.description('List connected social media profiles')
.action(async () => {
const spinner = ora('Fetching connected profiles...').start();
try {
const activeApi =
api || new BufferApi({ ...getConfig(), apiKey: validateApiKey(getConfig().apiKey) });
const profiles = await activeApi.getProfiles();
spinner.stop();
console.log(formatProfiles(profiles));
} catch (error) {
spinner.fail('Failed to fetch profiles');
console.error(chalk.red(`\n❌ ${error.message}`));
process.exitCode = 1;
}
});
program
.command('post <text>')
.description('Create a post with text content')
.option('--profile <id>', 'Post to a single profile ID')
.option('--profiles <ids>', 'Comma-separated profile IDs')
.option('--all', 'Post to all connected profiles')
.option('--time <datetime>', 'Schedule post for an ISO datetime')
.option('--queue', 'Add to queue')
.option('--image <path>', 'Attach an image from local file path')
.option('--draft', 'Create as idea/draft instead of post')
.action(async (text, options) => {
const spinner = ora(options.draft ? 'Saving idea...' : 'Creating post...').start();
try {
const activeApi =
api || new BufferApi({ ...getConfig(), apiKey: validateApiKey(getConfig().apiKey) });
validatePostOptions(options);
const normalizedText = validatePostText(text);
const profiles = options.all ? await activeApi.getProfiles() : [];
const profileIds = resolveProfileIds(options, profiles);
// Buffer API currently only supports one channel per post
const channelId = profileIds[0];
if (!channelId) {
throw new Error('No channel ID specified. Use --profile <id> to specify a channel.');
}
if (options.draft) {
const createdIdea = await activeApi.createIdea({ text: normalizedText, profileIds });
spinner.stop();
console.log(formatIdeaSuccess(createdIdea));
return;
}
const scheduledAt = parseScheduleTime(options.time);
const imagePath = validateImagePath(options.image);
const input = {
text: normalizedText,
channelId,
now: !options.queue && !scheduledAt,
queue: Boolean(options.queue),
...(scheduledAt ? { scheduledAt } : {}),
...(imagePath
? {
// BLOCKED: Buffer GraphQL local upload flow is undocumented in public beta docs.
// TODO: revisit and switch to official upload mechanism once confirmed.
media: [{ filePath: imagePath }],
}
: {}),
};
const createdPost = await activeApi.createPost(input);
spinner.stop();
console.log(formatPostSuccess(createdPost));
} catch (error) {
spinner.fail(options.draft ? 'Failed to save idea' : 'Failed to create post');
console.error(chalk.red(`\n❌ ${error.message}`));
process.exitCode = 1;
}
});
program
.command('queue')
.description('View pending/scheduled posts')
.option('--profile <id>', 'Filter by profile ID')
.option('--limit <n>', 'Limit number of posts shown', String(DEFAULT_QUEUE_LIMIT))
.action(async (options) => {
const spinner = ora('Fetching scheduled posts...').start();
try {
const activeApi =
api || new BufferApi({ ...getConfig(), apiKey: validateApiKey(getConfig().apiKey) });
const posts = await activeApi.getScheduledPosts(options.profile);
const limit = parseLimit(options.limit);
const limited = posts.slice(0, limit);
spinner.stop();
console.log(formatQueuePosts(limited));
} catch (error) {
spinner.fail('Failed to fetch queue');
console.error(chalk.red(`\n❌ ${error.message}`));
process.exitCode = 1;
}
});
program
.command('ideas')
.description('List saved ideas/drafts')
.option('--limit <n>', 'Limit number of ideas shown', String(DEFAULT_QUEUE_LIMIT))
.action(async (options) => {
const spinner = ora('Fetching saved ideas...').start();
try {
const activeApi =
api || new BufferApi({ ...getConfig(), apiKey: validateApiKey(getConfig().apiKey) });
const ideas = await activeApi.getIdeas();
const limit = parseLimit(options.limit);
const limited = ideas.slice(0, limit);
spinner.stop();
console.log(formatIdeas(limited));
} catch (error) {
spinner.fail('Failed to fetch ideas');
console.error(chalk.red(`\n❌ ${error.message}`));
process.exitCode = 1;
}
});
return program;
}
/**
* Run CLI with argv.
* @param {string[]} argv
*/
export async function run(argv = process.argv) {
const program = createCli();
await program.parseAsync(argv);
}
if (import.meta.url === `file://${process.argv[1]}`) {
run();
}