@chaouidz/postpilot 1.0.0 → 1.1.1

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/bin/postpilot.mjs CHANGED
@@ -1,277 +1,292 @@
1
- #!/usr/bin/env node
2
- import { execFile } from "node:child_process";
3
- import { promisify } from "node:util";
4
- import { Client, CliError, EXIT_CODES } from "../src/client.mjs";
5
- import { keychain } from "../src/keychain.mjs";
6
- const args = process.argv.slice(2),
7
- options = {},
8
- positionals = [];
9
- const boolean = new Set(["json", "dry-run", "non-interactive", "token-stdin", "help"]);
10
- const valued = new Set([
11
- "url",
12
- "account",
13
- "at",
14
- "privacy",
15
- "post",
16
- "approval",
17
- "asset",
18
- "title",
19
- "content",
20
- "output",
21
- "idempotency-key",
22
- "timezone",
23
- "limit",
24
- "cursor",
25
- ]);
26
- function required(name) {
27
- if (!options[name]) throw new CliError(`--${name} is required.`);
28
- return options[name];
29
- }
30
- function output(value) {
31
- console.log(JSON.stringify(value, null, options.json ? 0 : 2));
32
- if (value?.status === "PENDING_APPROVAL") process.exitCode = 5;
33
- }
34
- async function tokenInput() {
35
- if (!process.stdin.isTTY) {
36
- if (!options["token-stdin"])
37
- throw new CliError("Use --token-stdin for piped login, or paste interactively.");
38
- let value = "";
39
- for await (const chunk of process.stdin) {
40
- value += chunk;
41
- if (value.length > 1024) throw new CliError("Invalid token length.");
42
- }
43
- return value.trim();
44
- }
45
- if (options["non-interactive"])
46
- throw new CliError("Non-interactive login requires --token-stdin.");
47
- process.stderr.write("Paste your PostPilot token (hidden): ");
48
- return new Promise((resolve, reject) => {
49
- let value = "";
50
- process.stdin.setRawMode(true);
51
- process.stdin.resume();
52
- const done = (error) => {
53
- process.stdin.setRawMode(false);
54
- process.stdin.pause();
55
- process.stdin.off("data", onData);
56
- process.stderr.write("\n");
57
- error ? reject(error) : resolve(value.trim());
58
- };
59
- const onData = (chunk) => {
60
- for (const char of chunk.toString()) {
61
- if (char === "\r" || char === "\n") return done();
62
- if (char === "\u0003") return done(new CliError("Login cancelled."));
63
- if (char === "\u007f" || char === "\b") value = value.slice(0, -1);
64
- else value += char;
65
- if (value.length > 512) return done(new CliError("Invalid token length."));
66
- }
67
- };
68
- process.stdin.on("data", onData);
69
- });
70
- }
71
- try {
72
- for (let index = 0; index < args.length; index++) {
73
- const value = args[index];
74
- if (!value.startsWith("--")) {
75
- positionals.push(value);
76
- continue;
77
- }
78
- const name = value.slice(2);
79
- if (boolean.has(name)) options[name] = true;
80
- else if (valued.has(name) && args[index + 1] && !args[index + 1].startsWith("--"))
81
- options[name] = args[++index];
82
- else throw new CliError(`Unknown or incomplete option --${name}.`);
83
- }
84
- const [command, subcommand] = positionals;
85
- const origin = new URL(options.url || process.env.POSTPILOT_URL || "https://postpilot.yktis.com")
86
- .origin;
87
- if (!command || command === "help" || options.help) {
88
- output({
89
- usage: "postpilot <command> [--json] [--non-interactive]",
90
- commands: [
91
- "login [--token-stdin]",
92
- "whoami",
93
- "logout",
94
- "accounts list",
95
- "assets list",
96
- "upload <file>",
97
- "prepare-video <file> --output <file>",
98
- "draft create --account <id-or-name> --asset <id>",
99
- "schedule --post <id> --account <id-or-name> --at <ISO-offset>",
100
- "publish --post <id> --account <id-or-name>",
101
- "status --post <id>",
102
- "cancel --post <id>",
103
- ],
104
- flags: [
105
- "--account",
106
- "--at",
107
- "--privacy",
108
- "--dry-run",
109
- "--json",
110
- "--non-interactive",
111
- "--idempotency-key",
112
- "--timezone",
113
- "--url",
114
- ],
115
- exitCodes: {
116
- success: 0,
117
- usage: 2,
118
- auth: 3,
119
- notFound: 4,
120
- approvalPending: 5,
121
- rateLimited: 6,
122
- providerOrNetwork: 7,
123
- },
124
- docs: "https://postpilot.yktis.com/cli",
125
- });
126
- } else if (command === "prepare-video") {
127
- const input = subcommand;
128
- if (!input) throw new CliError("Choose an input video.");
129
- const target = required("output");
130
- const ffargs = [
131
- "-hide_banner",
132
- "-loglevel",
133
- "error",
134
- "-n",
135
- "-threads",
136
- "2",
137
- "-filter_threads",
138
- "1",
139
- "-i",
140
- input,
141
- "-map",
142
- "0:v:0",
143
- "-map",
144
- "0:a?",
145
- "-vf",
146
- "scale=w=min(1920\\,iw):h=min(1920\\,ih):force_original_aspect_ratio=decrease:force_divisible_by=2,format=yuv420p,fps=30",
147
- "-c:v",
148
- "libx264",
149
- "-crf",
150
- "20",
151
- "-pix_fmt",
152
- "yuv420p",
153
- "-c:a",
154
- "aac",
155
- "-ar",
156
- "48000",
157
- "-ac",
158
- "2",
159
- "-movflags",
160
- "+faststart",
161
- target,
162
- ];
163
- if (options["dry-run"]) output({ dryRun: true, command: "ffmpeg", arguments: ffargs });
164
- else {
165
- try {
166
- await promisify(execFile)("ffmpeg", ffargs, {
167
- timeout: 180000,
168
- maxBuffer: 1024 * 1024,
169
- windowsHide: true,
170
- });
171
- } catch {
172
- throw new CliError(
173
- "Video preparation failed. Install ffmpeg, choose a fresh output path and check the input.",
174
- "INTERNAL_FAILURE"
175
- );
176
- }
177
- output({ output: target, uploaded: false });
178
- }
179
- } else if (command === "logout") {
180
- await keychain("delete", origin);
181
- output({
182
- loggedOut: true,
183
- message:
184
- "Local credential removed. Revoke the token in Settings to invalidate it everywhere.",
185
- });
186
- } else if (command === "login") {
187
- const token = await tokenInput();
188
- const client = new Client(origin, token);
189
- await client.tool("social_get_profile");
190
- await keychain("set", origin, token);
191
- output({ loggedIn: true, storage: "OS keychain", origin });
192
- } else {
193
- const client = new Client(
194
- origin,
195
- process.env.POSTPILOT_TOKEN || (await keychain("get", origin).catch(() => ""))
196
- );
197
- const key = options["idempotency-key"] ? { idempotencyKey: options["idempotency-key"] } : {};
198
- const common = {
199
- ...key,
200
- ...(options.timezone ? { timezone: options.timezone } : {}),
201
- ...(options["dry-run"] ? { dryRun: true } : {}),
202
- };
203
- if (command === "whoami") output(await client.tool("social_get_profile"));
204
- else if (command === "accounts" && subcommand === "list")
205
- output(await client.tool("accounts_list"));
206
- else if (command === "assets" && subcommand === "list")
207
- output(
208
- await client.tool("assets_list", {
209
- ...(options.limit ? { limit: Number(options.limit) } : {}),
210
- ...(options.cursor ? { cursor: options.cursor } : {}),
211
- })
212
- );
213
- else if (command === "upload") {
214
- if (!subcommand) throw new CliError("Choose a PNG or MP4 file.");
215
- if (options["dry-run"]) output({ dryRun: true, file: subcommand, uploaded: false });
216
- else output(await client.upload(subcommand, options["idempotency-key"]));
217
- } else if (command === "draft" && subcommand === "create") {
218
- const account = await client.account(required("account"));
219
- const post = {
220
- content: options.content || "",
221
- videoTitle: options.title || "",
222
- youtubePrivacyStatus: options.privacy || "private",
223
- platformTargets: [account.platform],
224
- selectedAccountIds: [account.id],
225
- ...(options.asset
226
- ? {
227
- uploadSessionId: options.asset,
228
- mediaType: account.platform === "YOUTUBE" ? "VIDEO_SHORT" : "VIDEO",
229
- }
230
- : {}),
231
- };
232
- if (options["dry-run"]) output({ dryRun: true, post, created: false });
233
- else output(await client.tool("post_create_draft", { post, ...key }));
234
- } else if (["schedule", "publish"].includes(command)) {
235
- const account = await client.account(required("account"));
236
- const postId = required("post");
237
- if (
238
- command === "schedule" &&
239
- (!options.at ||
240
- !/T.*(?:Z|[+-]\d{2}:\d{2})$/.test(options.at) ||
241
- !Number.isFinite(Date.parse(options.at)))
242
- )
243
- throw new CliError("--at requires an ISO timestamp with Z or a numeric offset.");
244
- if (options.privacy)
245
- throw new CliError("Set privacy when creating the draft; review it in the approval page.");
246
- output(
247
- await client.tool(command === "schedule" ? "post_schedule" : "post_publish", {
248
- postId,
249
- accountIds: [account.id],
250
- ...(command === "schedule" ? { scheduledAt: options.at } : {}),
251
- ...common,
252
- })
253
- );
254
- } else if (command === "status")
255
- output(
256
- await client.tool(
257
- options.approval ? "approval_status" : "post_status",
258
- options.approval ? { approvalId: options.approval } : { postId: required("post") }
259
- )
260
- );
261
- else if (command === "cancel")
262
- output(await client.tool("post_cancel", { postId: required("post"), ...key }));
263
- else throw new CliError("Unknown command. Run postpilot help.");
264
- }
265
- } catch (error) {
266
- const code = error instanceof CliError ? error.code : "INTERNAL_FAILURE";
267
- const message =
268
- error instanceof CliError
269
- ? error.message
270
- : "The operation failed. Check your input and OS keychain, then try again.";
271
- console.error(
272
- JSON.stringify({
273
- error: { code, message, ...(error instanceof CliError ? error.details : {}) },
274
- })
275
- );
276
- process.exitCode = EXIT_CODES[code] || 7;
277
- }
1
+ #!/usr/bin/env node
2
+ import { execFile } from "node:child_process";
3
+ import { promisify } from "node:util";
4
+ import { Client, CliError, EXIT_CODES } from "../src/client.mjs";
5
+ import { schedulePlan } from "../src/plan.mjs";
6
+ import { keychain } from "../src/keychain.mjs";
7
+ const args = process.argv.slice(2),
8
+ options = {},
9
+ positionals = [];
10
+ const boolean = new Set(["json", "dry-run", "non-interactive", "token-stdin", "help", "yes"]);
11
+ const valued = new Set([
12
+ "url",
13
+ "file",
14
+ "account",
15
+ "at",
16
+ "privacy",
17
+ "post",
18
+ "approval",
19
+ "asset",
20
+ "title",
21
+ "content",
22
+ "output",
23
+ "idempotency-key",
24
+ "timezone",
25
+ "limit",
26
+ "cursor",
27
+ ]);
28
+ function required(name) {
29
+ if (!options[name]) throw new CliError(`--${name} is required.`);
30
+ return options[name];
31
+ }
32
+ function output(value) {
33
+ console.log(JSON.stringify(value, null, options.json ? 0 : 2));
34
+ if (value?.status === "PENDING_APPROVAL") process.exitCode = 5;
35
+ }
36
+ async function tokenInput() {
37
+ if (!process.stdin.isTTY) {
38
+ if (!options["token-stdin"])
39
+ throw new CliError("Use --token-stdin for piped login, or paste interactively.");
40
+ let value = "";
41
+ for await (const chunk of process.stdin) {
42
+ value += chunk;
43
+ if (value.length > 1024) throw new CliError("Invalid token length.");
44
+ }
45
+ return value.trim();
46
+ }
47
+ if (options["non-interactive"])
48
+ throw new CliError("Non-interactive login requires --token-stdin.");
49
+ process.stderr.write("Paste your PostPilot token (hidden): ");
50
+ return new Promise((resolve, reject) => {
51
+ let value = "";
52
+ process.stdin.setRawMode(true);
53
+ process.stdin.resume();
54
+ const done = (error) => {
55
+ process.stdin.setRawMode(false);
56
+ process.stdin.pause();
57
+ process.stdin.off("data", onData);
58
+ process.stderr.write("\n");
59
+ error ? reject(error) : resolve(value.trim());
60
+ };
61
+ const onData = (chunk) => {
62
+ for (const char of chunk.toString()) {
63
+ if (char === "\r" || char === "\n") return done();
64
+ if (char === "\u0003") return done(new CliError("Login cancelled."));
65
+ if (char === "\u007f" || char === "\b") value = value.slice(0, -1);
66
+ else value += char;
67
+ if (value.length > 512) return done(new CliError("Invalid token length."));
68
+ }
69
+ };
70
+ process.stdin.on("data", onData);
71
+ });
72
+ }
73
+ try {
74
+ for (let index = 0; index < args.length; index++) {
75
+ const value = args[index];
76
+ if (!value.startsWith("--")) {
77
+ positionals.push(value);
78
+ continue;
79
+ }
80
+ const name = value.slice(2);
81
+ if (boolean.has(name)) options[name] = true;
82
+ else if (valued.has(name) && args[index + 1] && !args[index + 1].startsWith("--"))
83
+ options[name] = args[++index];
84
+ else throw new CliError(`Unknown or incomplete option --${name}.`);
85
+ }
86
+ const [command, subcommand] = positionals;
87
+ const origin = new URL(options.url || process.env.POSTPILOT_URL || "https://postpilot.yktis.com")
88
+ .origin;
89
+ if (!command || command === "help" || options.help) {
90
+ output({
91
+ usage: "postpilot <command> [--json] [--non-interactive]",
92
+ requires: {
93
+ node: ">=20",
94
+ operatingSystems: ["Windows", "macOS", "Linux"],
95
+ installRuntime:
96
+ "Install current Node.js LTS from https://nodejs.org/en/download, then reopen your terminal.",
97
+ },
98
+ commands: [
99
+ "login [--token-stdin]",
100
+ "whoami",
101
+ "logout",
102
+ "accounts list",
103
+ "assets list",
104
+ "upload <file>",
105
+ "prepare-video <file> --output <file>",
106
+ "draft create --account <id-or-name> --asset <id>",
107
+ "schedule --file <plan.json> [--account <id-or-name>] [--dry-run | --yes]",
108
+ "schedule --post <id> --account <id-or-name> --at <ISO-offset>",
109
+ "publish --post <id> --account <id-or-name>",
110
+ "status --post <id>",
111
+ "cancel --post <id>",
112
+ ],
113
+ flags: [
114
+ "--file",
115
+ "--yes",
116
+ "--account",
117
+ "--at",
118
+ "--privacy",
119
+ "--dry-run",
120
+ "--json",
121
+ "--non-interactive",
122
+ "--idempotency-key",
123
+ "--timezone",
124
+ "--url",
125
+ ],
126
+ exitCodes: {
127
+ success: 0,
128
+ usage: 2,
129
+ auth: 3,
130
+ notFound: 4,
131
+ approvalPending: 5,
132
+ rateLimited: 6,
133
+ providerOrNetwork: 7,
134
+ capacityExceeded: 8,
135
+ },
136
+ docs: "https://postpilot.yktis.com/cli",
137
+ });
138
+ } else if (command === "prepare-video") {
139
+ const input = subcommand;
140
+ if (!input) throw new CliError("Choose an input video.");
141
+ const target = required("output");
142
+ const ffargs = [
143
+ "-hide_banner",
144
+ "-loglevel",
145
+ "error",
146
+ "-n",
147
+ "-threads",
148
+ "2",
149
+ "-filter_threads",
150
+ "1",
151
+ "-i",
152
+ input,
153
+ "-map",
154
+ "0:v:0",
155
+ "-map",
156
+ "0:a?",
157
+ "-vf",
158
+ "scale=w=min(1920\\,iw):h=min(1920\\,ih):force_original_aspect_ratio=decrease:force_divisible_by=2,format=yuv420p,fps=30",
159
+ "-c:v",
160
+ "libx264",
161
+ "-crf",
162
+ "20",
163
+ "-pix_fmt",
164
+ "yuv420p",
165
+ "-c:a",
166
+ "aac",
167
+ "-ar",
168
+ "48000",
169
+ "-ac",
170
+ "2",
171
+ "-movflags",
172
+ "+faststart",
173
+ target,
174
+ ];
175
+ if (options["dry-run"]) output({ dryRun: true, command: "ffmpeg", arguments: ffargs });
176
+ else {
177
+ try {
178
+ await promisify(execFile)("ffmpeg", ffargs, {
179
+ timeout: 180000,
180
+ maxBuffer: 1024 * 1024,
181
+ windowsHide: true,
182
+ });
183
+ } catch {
184
+ throw new CliError(
185
+ "Video preparation failed. Install ffmpeg, choose a fresh output path and check the input.",
186
+ "INTERNAL_FAILURE"
187
+ );
188
+ }
189
+ output({ output: target, uploaded: false });
190
+ }
191
+ } else if (command === "logout") {
192
+ await keychain("delete", origin);
193
+ output({
194
+ loggedOut: true,
195
+ message:
196
+ "Local credential removed. Revoke the token in Settings to invalidate it everywhere.",
197
+ });
198
+ } else if (command === "login") {
199
+ const token = await tokenInput();
200
+ const client = new Client(origin, token);
201
+ await client.tool("social_get_profile");
202
+ await keychain("set", origin, token);
203
+ output({ loggedIn: true, storage: "OS keychain", origin });
204
+ } else {
205
+ const client = new Client(
206
+ origin,
207
+ process.env.POSTPILOT_TOKEN || (await keychain("get", origin).catch(() => ""))
208
+ );
209
+ const key = options["idempotency-key"] ? { idempotencyKey: options["idempotency-key"] } : {};
210
+ const common = {
211
+ ...key,
212
+ ...(options.timezone ? { timezone: options.timezone } : {}),
213
+ ...(options["dry-run"] ? { dryRun: true } : {}),
214
+ };
215
+ if (command === "whoami") output(await client.tool("social_get_profile"));
216
+ else if (command === "accounts" && subcommand === "list")
217
+ output(await client.tool("accounts_list"));
218
+ else if (command === "assets" && subcommand === "list")
219
+ output(
220
+ await client.tool("assets_list", {
221
+ ...(options.limit ? { limit: Number(options.limit) } : {}),
222
+ ...(options.cursor ? { cursor: options.cursor } : {}),
223
+ })
224
+ );
225
+ else if (command === "upload") {
226
+ if (!subcommand) throw new CliError("Choose a PNG or MP4 file.");
227
+ if (options["dry-run"]) output({ dryRun: true, file: subcommand, uploaded: false });
228
+ else output(await client.upload(subcommand, options["idempotency-key"]));
229
+ } else if (command === "draft" && subcommand === "create") {
230
+ const account = await client.account(required("account"));
231
+ const post = {
232
+ content: options.content || "",
233
+ videoTitle: options.title || "",
234
+ youtubePrivacyStatus: options.privacy || "private",
235
+ platformTargets: [account.platform],
236
+ selectedAccountIds: [account.id],
237
+ ...(options.asset
238
+ ? {
239
+ uploadSessionId: options.asset,
240
+ mediaType: account.platform === "YOUTUBE" ? "VIDEO_SHORT" : "VIDEO",
241
+ }
242
+ : {}),
243
+ };
244
+ if (options["dry-run"]) output({ dryRun: true, post, created: false });
245
+ else output(await client.tool("post_create_draft", { post, ...key }));
246
+ } else if (command === "schedule" && options.file) {
247
+ if (options.post || options.at) throw new CliError("Use --file or --post/--at, not both.");
248
+ output(await schedulePlan(client, options.file, options));
249
+ } else if (["schedule", "publish"].includes(command)) {
250
+ const account = await client.account(required("account"));
251
+ const postId = required("post");
252
+ if (
253
+ command === "schedule" &&
254
+ (!options.at ||
255
+ !/T.*(?:Z|[+-]\d{2}:\d{2})$/.test(options.at) ||
256
+ !Number.isFinite(Date.parse(options.at)))
257
+ )
258
+ throw new CliError("--at requires an ISO timestamp with Z or a numeric offset.");
259
+ if (options.privacy)
260
+ throw new CliError("Set privacy when creating the draft; review it in the approval page.");
261
+ output(
262
+ await client.tool(command === "schedule" ? "post_schedule" : "post_publish", {
263
+ postId,
264
+ accountIds: [account.id],
265
+ ...(command === "schedule" ? { scheduledAt: options.at } : {}),
266
+ ...common,
267
+ })
268
+ );
269
+ } else if (command === "status")
270
+ output(
271
+ await client.tool(
272
+ options.approval ? "approval_status" : "post_status",
273
+ options.approval ? { approvalId: options.approval } : { postId: required("post") }
274
+ )
275
+ );
276
+ else if (command === "cancel")
277
+ output(await client.tool("post_cancel", { postId: required("post"), ...key }));
278
+ else throw new CliError("Unknown command. Run postpilot help.");
279
+ }
280
+ } catch (error) {
281
+ const code = error instanceof CliError ? error.code : "INTERNAL_FAILURE";
282
+ const message =
283
+ error instanceof CliError
284
+ ? error.message
285
+ : "The operation failed. Check your input and OS keychain, then try again.";
286
+ console.error(
287
+ JSON.stringify({
288
+ error: { code, message, ...(error instanceof CliError ? error.details : {}) },
289
+ })
290
+ );
291
+ process.exitCode = EXIT_CODES[code] || 7;
292
+ }
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "name": "@chaouidz/postpilot",
3
- "version": "1.0.0",
4
- "description": "PostPilot CLI for scoped, approval-based social publishing and analytics",
5
- "type": "module",
6
- "bin": {
7
- "postpilot": "bin/postpilot.mjs"
8
- },
9
- "files": [
10
- "bin",
11
- "src",
12
- "README.md"
13
- ],
14
- "engines": {
15
- "node": ">=20"
16
- },
17
- "license": "UNLICENSED",
18
- "repository": {
19
- "type": "git",
20
- "url": "https://github.com/yktis/postpilot.git",
21
- "directory": "packages/cli"
22
- },
23
- "homepage": "https://postpilot.yktis.com/cli",
24
- "scripts": {
25
- "test": "node --test test/*.test.mjs"
26
- }
27
- }
1
+ {
2
+ "name": "@chaouidz/postpilot",
3
+ "version": "1.1.1",
4
+ "description": "PostPilot CLI for scoped, approval-based social publishing and batch scheduling",
5
+ "type": "module",
6
+ "bin": {
7
+ "postpilot": "bin/postpilot.mjs"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "src",
12
+ "README.md"
13
+ ],
14
+ "engines": {
15
+ "node": ">=20"
16
+ },
17
+ "license": "UNLICENSED",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/yktis/postpilot.git",
21
+ "directory": "packages/cli"
22
+ },
23
+ "homepage": "https://postpilot.yktis.com/cli",
24
+ "scripts": {
25
+ "test": "node --test test/*.test.mjs"
26
+ }
27
+ }