previewer-agency 0.3.1 → 0.3.2

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 CHANGED
@@ -79,7 +79,11 @@ npx previewer-agency push ./dist
79
79
  ```
80
80
 
81
81
  When it's done it prints the preview link and copies it to your clipboard. `--open` opens it.
82
- `--dry-run` shows what would go up without uploading.
82
+ `--dry-run` shows what would go up, and to which client and campaign, without uploading.
83
+
84
+ A `--client` you name has to own the campaign. In a folder linked to one client's campaign,
85
+ `push --client "Another client"` stops before anything is uploaded; add `--campaign` to say which
86
+ campaign of that client you mean.
83
87
 
84
88
  ## Check on it
85
89
 
package/bin/previewer.mjs CHANGED
@@ -9,6 +9,7 @@ import path from 'node:path';
9
9
  import { createInterface } from 'node:readline/promises';
10
10
  import { parseArgs } from 'node:util';
11
11
  import { PreviewerClient, PreviewerError, isApprovalPath, newCliToken } from '../lib/api.mjs';
12
+ import { clientMismatch } from '../lib/destination.mjs';
12
13
  import { collectUploads } from '../lib/files.mjs';
13
14
  import { createMcpServer, runStdio } from '../lib/mcp.mjs';
14
15
  import { selectMenu } from '../lib/menu.mjs';
@@ -265,8 +266,23 @@ const resolveClient = async (api, wanted) => {
265
266
  );
266
267
  };
267
268
 
268
- /** The campaign to work on, from flags, the folder's link, or (in a terminal) a picker. */
269
- const resolveCampaign = async (api, options, project, { allowCreate = false } = {}) => {
269
+ const MISMATCH_HINT = `Leave out --client, or name the campaign with --campaign (this folder's ${PROJECT_FILE} or the id points at another client's).`;
270
+
271
+ /**
272
+ * The campaign to work on, from flags, the folder's link, or (in a terminal) a picker. A client named
273
+ * with --client has to own it, however the campaign was found (the folder's link and a campaign id
274
+ * say nothing about the client).
275
+ */
276
+ const resolveCampaign = async (api, options, project, settings = {}) => {
277
+ const found = await findCampaign(api, options, project, settings);
278
+ if (options.client) {
279
+ const mismatch = clientMismatch(found.campaign, await resolveClient(api, options.client));
280
+ if (mismatch) fail(mismatch, MISMATCH_HINT);
281
+ }
282
+ return found;
283
+ };
284
+
285
+ const findCampaign = async (api, options, project, { allowCreate = false } = {}) => {
270
286
  if (options.campaign && /^\d+$/.test(options.campaign)) return { campaign: await api.campaign(Number(options.campaign)), fromFlags: true };
271
287
  if (options.campaign) {
272
288
  const clientId = options.client ? (await resolveClient(api, options.client)).id : project?.clientId;
@@ -361,14 +377,32 @@ async function push(options, folder = '.') {
361
377
  if (options['dry-run']) {
362
378
  say(`${bold('Would upload')} ${summary} (${kb(total)}):`);
363
379
  for (const item of items) say(` ${dim(item.type.padEnd(6))} ${item.source}${item.source !== item.name ? dim(` → ${item.name}`) : ''} ${dim(kb(item.size))}`);
364
- // Uploads nothing, but does say now what the real push would stop on: a client that isn't there.
380
+ // Uploads nothing, but does say where the real push would go and what it would stop on: a client
381
+ // that isn't there, or a campaign that isn't the named client's.
382
+ const linked = await findProject(process.cwd());
383
+ const auth = await resolveAuth({ site: options.site, project: linked });
384
+ if (!auth.token) return say('', dim(`Not signed in to ${auth.site}, so the client and campaign weren't checked.`));
385
+ const dry = new PreviewerClient({ baseUrl: auth.site, token: auth.token, userAgent: `previewer-cli/${VERSION}` });
386
+ let client = null;
365
387
  if (options.client) {
366
- const auth = await resolveAuth({ site: options.site, project: await findProject(process.cwd()) });
367
- if (!auth.token) return say('', dim(`Not signed in to ${auth.site}, so the client and campaign weren't checked.`));
368
- const clients = await new PreviewerClient({ baseUrl: auth.site, token: auth.token, userAgent: `previewer-cli/${VERSION}` }).clients();
369
- const known = /^\d+$/.test(String(options.client)) ? clients.some((client) => client.id === Number(options.client)) : matchName(clients, options.client).length > 0;
370
- if (!known) say('', `${yellow('!')} No client "${options.client}" yet. Create it in the Previewer admin first; the campaign is created for you.`);
388
+ const clients = await dry.clients();
389
+ const known = /^\d+$/.test(String(options.client)) ? clients.some((item) => item.id === Number(options.client)) : matchName(clients, options.client).length > 0;
390
+ if (!known) return say('', `${yellow('!')} No client "${options.client}" yet. Create it in the Previewer admin first; the campaign is created for you.`);
391
+ client = await resolveClient(dry, options.client);
371
392
  }
393
+ let campaign = null;
394
+ if (options.campaign && /^\d+$/.test(options.campaign)) campaign = await dry.campaign(Number(options.campaign));
395
+ else if (options.campaign) {
396
+ const clientId = client?.id ?? linked?.clientId;
397
+ const matches = matchName(await dry.campaigns(clientId ? { clientId } : {}), options.campaign);
398
+ if (matches.length > 1) return say('', `${yellow('!')} Several campaigns are called "${options.campaign}". Add --client, or use the campaign id.`);
399
+ if (matches.length === 0) return say('', `Campaign "${options.campaign}" doesn't exist yet${client ? ` for ${client.name}` : ''}: the push would ${options.create ? 'create it' : 'ask to create it (or add --create)'}.`);
400
+ campaign = matches[0];
401
+ } else if (linked?.campaignId) campaign = await dry.campaign(linked.campaignId);
402
+ else return say('', dim(`No campaign chosen yet: add --campaign <name|id>${client ? '' : ' (and --client <name>)'}.`));
403
+ const mismatch = clientMismatch(campaign, client);
404
+ if (mismatch) fail(mismatch, MISMATCH_HINT);
405
+ say('', `Would go to ${bold(`${campaign.client.name} → ${campaign.name}`)}.`);
372
406
  return;
373
407
  }
374
408
 
@@ -0,0 +1,14 @@
1
+ // Where an upload is about to go, checked against what the caller said.
2
+ //
3
+ // A folder can be linked to a campaign (.previewer.json), and a campaign can be named by its id. When
4
+ // the caller also names a client, the campaign has to be that client's: otherwise "push for client B"
5
+ // in a folder linked to client A's campaign would put B's banners on A's preview link. Shared by the
6
+ // command line and the MCP server, so the two can't drift apart.
7
+
8
+ /** The reason this campaign is not the named client's, or null when it is (or no client was named). */
9
+ export const clientMismatch = (campaign, client) => {
10
+ if (!client || !campaign) return null;
11
+ if (campaign.client?.id === client.id) return null;
12
+ const owner = campaign.client?.name ? `${campaign.client.name}'s` : "another client's";
13
+ return `Campaign "${campaign.name}" (${campaign.id}) is ${owner}, not ${client.name}'s.`;
14
+ };
package/lib/mcp.mjs CHANGED
@@ -7,6 +7,7 @@
7
7
  import os from 'node:os';
8
8
  import path from 'node:path';
9
9
  import { createInterface } from 'node:readline';
10
+ import { clientMismatch } from './destination.mjs';
10
11
  import { PreviewerClient, PreviewerError, isApprovalPath, newCliToken } from './api.mjs';
11
12
  import { collectUploads } from './files.mjs';
12
13
  import { PROJECT_FILE, findProject, resolveAuth, saveProject, saveSiteLogin } from './config.mjs';
@@ -111,7 +112,19 @@ export const createMcpServer = ({ version = '0.0.0', cwd = process.cwd(), deps =
111
112
  return client;
112
113
  };
113
114
 
114
- const resolveCampaign = async (api, project, args, { create = false } = {}) => {
115
+ // A client passed along has to own the campaign, however it was found: the folder's link and a
116
+ // campaign id say nothing about the client, and an upload to the wrong one lands on another
117
+ // client's preview link.
118
+ const resolveCampaign = async (api, project, args, settings = {}) => {
119
+ const found = await findCampaign(api, project, args, settings);
120
+ if (args.client !== undefined && args.client !== null && String(args.client).trim()) {
121
+ const mismatch = clientMismatch(found.campaign, await resolveClient(api, args.client));
122
+ if (mismatch) throw Object.assign(new ToolError(`${mismatch} Leave out client, or pass the campaign that is that client's.`), { mismatch: true });
123
+ }
124
+ return found;
125
+ };
126
+
127
+ const findCampaign = async (api, project, args, { create = false } = {}) => {
115
128
  const wanted = args.campaign === undefined || args.campaign === null ? '' : String(args.campaign).trim();
116
129
  if (!wanted) {
117
130
  if (project?.campaignId) return { campaign: await api.campaign(project.campaignId) };
@@ -287,7 +300,7 @@ export const createMcpServer = ({ version = '0.0.0', cwd = process.cwd(), deps =
287
300
  name: 'upload_folder',
288
301
  title: 'Upload banners',
289
302
  description:
290
- 'Upload a folder to a campaign: HTML5 banner ZIPs, banner folders with an index.html (zipped for you), images and videos, and backup images (300x250.jpg next to 300x250.zip, or anything in backups/). A file named like a banner already in the campaign replaces it as a new version. Returns the preview link to give the user. Use dry_run to see what would go up.',
303
+ 'Upload a folder to a campaign: HTML5 banner ZIPs, banner folders with an index.html (zipped for you), images and videos, and backup images (300x250.jpg next to 300x250.zip, or anything in backups/). A file named like a banner already in the campaign replaces it as a new version. Returns the preview link to give the user. Use dry_run to see what would go up and where. A client you pass has to own the campaign.',
291
304
  inputSchema: object(
292
305
  {
293
306
  folder: { ...FOLDER, description: `The folder to upload, e.g. "dist". ${FOLDER.description}` },
@@ -305,7 +318,21 @@ export const createMcpServer = ({ version = '0.0.0', cwd = process.cwd(), deps =
305
318
  throw new ToolError(error.code === 'ENOENT' ? `No folder ${dir}.` : `Could not read ${dir}: ${error.message}`);
306
319
  });
307
320
  const files = items.map((item) => ({ type: item.type, file: item.source ?? item.name, as: item.name, bytes: item.size }));
308
- if (args.dry_run) return { dryRun: true, folder: dir, files, skipped };
321
+ if (args.dry_run) {
322
+ // Nothing is sent, but the destination is the real one: where this upload would go, or why
323
+ // that isn't known yet. A campaign that isn't the named client's stops here too.
324
+ let destination = null;
325
+ let destinationNote = null;
326
+ try {
327
+ const { api, project } = await connect(args.folder);
328
+ const { campaign } = await resolveCampaign(api, project, args);
329
+ destination = campaignSummary(campaign);
330
+ } catch (error) {
331
+ if (!(error instanceof ToolError) || error.mismatch) throw error;
332
+ destinationNote = error.message;
333
+ }
334
+ return { dryRun: true, folder: dir, files, skipped, destination, destinationNote };
335
+ }
309
336
  if (items.length === 0) throw new ToolError(`Nothing to upload in ${dir}: expected ZIPs, banner folders with an index.html, images or videos.`);
310
337
 
311
338
  const { api, auth, project } = await connect(args.folder);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "previewer-agency",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Upload HTML5 banners, images and videos to Previewer from any folder, CI job or AI agent (MCP), and check approvals.",
5
5
  "type": "module",
6
6
  "bin": {