previewer-agency 0.3.0 → 0.3.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/README.md CHANGED
@@ -1,13 +1,38 @@
1
1
  # previewer
2
2
 
3
3
  Upload HTML5 banners, images and videos to [Previewer](https://previewer.agency) from any folder,
4
- whatever tool built them, and check on approvals. Needs Node 20 or newer; nothing else to install.
4
+ whatever tool built them, and check on approvals.
5
5
 
6
6
  ```bash
7
7
  npx previewer-agency login
8
8
  npx previewer-agency push ./dist --client "Acme" --campaign "Spring sale"
9
9
  ```
10
10
 
11
+ ## Install
12
+
13
+ Needs [Node.js](https://nodejs.org) 20.11 or newer; check with `node --version`.
14
+
15
+ **Without installing**, put `npx previewer-agency` in front of each command, as in the examples on this
16
+ page. npx fetches the latest version the first time.
17
+
18
+ **On your computer**, for the shorter `previewer` command (run it again later to update):
19
+
20
+ ```bash
21
+ npm install -g previewer-agency
22
+ previewer --version
23
+ ```
24
+
25
+ Every example then works with `previewer` instead of `npx previewer-agency`, for example
26
+ `previewer push ./dist`.
27
+
28
+ **In a project**, so everyone on it uses the same version:
29
+
30
+ ```bash
31
+ npm install --save-dev previewer-agency
32
+ ```
33
+
34
+ Then add a script to `package.json`, `"preview": "previewer push dist"`, and run `npm run preview`.
35
+
11
36
  ## Sign in
12
37
 
13
38
  ```bash
package/bin/previewer.mjs CHANGED
@@ -167,7 +167,7 @@ const clientFor = async (options, project) => {
167
167
  const auth = await resolveAuth({ site: options.site, project });
168
168
  if (!auth.token && auth.envTokenWithheld) {
169
169
  fail(
170
- `PREVIEWER_TOKEN was not sent to ${auth.site}, which comes from ${PROJECT_FILE}.`,
170
+ `PREVIEWER_TOKEN was not sent to ${auth.site}, which comes from ${auth.siteFrom === 'project' ? PROJECT_FILE : 'your saved sign-in'}.`,
171
171
  `The token only goes to the site set with --site or PREVIEWER_URL (default ${DEFAULT_SITE}). If you trust ${auth.site}, set PREVIEWER_URL to it.`,
172
172
  );
173
173
  }
@@ -209,17 +209,24 @@ async function login(options) {
209
209
  const deadline = Date.parse(started.expiresAt);
210
210
  while (Date.now() < deadline) {
211
211
  await new Promise((resolve) => setTimeout(resolve, 2000));
212
- const status = await api.loginStatus(started.requestId, token);
212
+ // A dropped connection while waiting isn't the end of the sign-in: keep asking until it expires.
213
+ let status;
214
+ try {
215
+ status = await api.loginStatus(started.requestId, token);
216
+ } catch (error) {
217
+ if (error instanceof PreviewerError && error.code === 'network') continue;
218
+ throw error;
219
+ }
213
220
  if (status === 'approved') {
214
221
  const me = await new PreviewerClient({ baseUrl: site, token }).me();
215
222
  await saveSiteLogin(site, { token, account: me.account }, { makeDefault: Boolean(options.site) });
216
223
  say(`${green('✔')} Signed in to ${bold(me.account?.name ?? site)} (${me.token.permission}).`);
217
224
  return;
218
225
  }
219
- if (status === 'denied') fail('The login was denied.');
226
+ if (status === 'denied') fail('The sign-in was denied.');
220
227
  if (status === 'expired') break;
221
228
  }
222
- fail('The login expired before it was approved.', `Run ${CMD} login again.`);
229
+ fail('The sign-in expired before it was approved.', `Run ${CMD} login again.`);
223
230
  }
224
231
 
225
232
  async function logout(options) {
@@ -324,7 +331,9 @@ async function listCampaigns(options) {
324
331
  if (options.json) return say(JSON.stringify(campaigns, null, 2));
325
332
  if (campaigns.length === 0) return say('No campaigns.');
326
333
  for (const campaign of campaigns) {
327
- const approvals = campaign.approvedCount === null ? '' : ` · ${campaign.approvedCount}/${campaign.bannerCount} approved`;
334
+ // Out of what the client can approve (not links or banners hidden from the preview); an older
335
+ // server without that count falls back to all banners.
336
+ const approvals = campaign.approvedCount === null ? '' : ` · ${campaign.approvedCount}/${campaign.reviewableCount ?? campaign.bannerCount} approved`;
328
337
  say(`${dim(String(campaign.id).padStart(5))} ${bold(campaign.name)} ${dim(`— ${campaign.client.name}`)}`, ` ${campaign.bannerCount} banners${approvals} · ${campaign.previewUrl}`);
329
338
  }
330
339
  }
@@ -352,6 +361,14 @@ async function push(options, folder = '.') {
352
361
  if (options['dry-run']) {
353
362
  say(`${bold('Would upload')} ${summary} (${kb(total)}):`);
354
363
  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.
365
+ 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.`);
371
+ }
355
372
  return;
356
373
  }
357
374
 
@@ -384,7 +401,8 @@ async function status(options) {
384
401
  const { campaign } = await resolveCampaign(api, options, project);
385
402
  if (options.json) return say(JSON.stringify(campaign, null, 2));
386
403
 
387
- const reviewable = campaign.banners.filter((banner) => banner.kind !== 'external');
404
+ // What the client can approve: no links, nothing hidden from the preview.
405
+ const reviewable = campaign.banners.filter((banner) => banner.kind !== 'external' && !banner.hidden);
388
406
  const approved = reviewable.filter((banner) => banner.status === 'approved').length;
389
407
  say(
390
408
  `${bold(`${campaign.client.name} → ${campaign.name}`)}`,
@@ -399,7 +417,11 @@ async function status(options) {
399
417
  const width = Math.max(10, ...campaign.banners.map((banner) => banner.label.length));
400
418
  for (const banner of campaign.banners) {
401
419
  const approvedNow = banner.status === 'approved';
402
- const state = !campaign.approvalsRequired ? ''.padEnd(9) : (approvedNow ? green : yellow)((approvedNow ? 'approved' : 'in review').padEnd(9));
420
+ const state = banner.hidden
421
+ ? dim('hidden'.padEnd(9))
422
+ : !campaign.approvalsRequired
423
+ ? ''.padEnd(9)
424
+ : (approvedNow ? green : yellow)((approvedNow ? 'approved' : 'in review').padEnd(9));
403
425
  const backup = banner.kind !== 'html' ? ''.padEnd(9) : banner.hasBackup ? dim('backup ✓'.padEnd(9)) : yellow('no backup');
404
426
  say(` ${banner.label.padEnd(width)} ${dim(`${banner.width}×${banner.height}`.padEnd(9))} ${state} ${backup} ${dim(ago(banner.updatedAt) ?? '')}`);
405
427
  }
package/lib/config.mjs CHANGED
@@ -60,6 +60,8 @@ export const saveConfig = async (config) => {
60
60
  export const resolveAuth = async ({ site: siteFlag, project } = {}) => {
61
61
  const config = await loadConfig();
62
62
  const site = normalizeSite(siteFlag || process.env.PREVIEWER_URL || project?.site || config.defaultSite || DEFAULT_SITE);
63
+ // Where that site came from, for messages that have to say so.
64
+ const siteFrom = siteFlag ? 'flag' : process.env.PREVIEWER_URL ? 'env' : project?.site ? 'project' : config.defaultSite ? 'saved' : 'default';
63
65
  const problem = siteProblem(site);
64
66
  if (problem) throw new Error(problem);
65
67
  const envToken = String(process.env.PREVIEWER_TOKEN || '').trim();
@@ -68,6 +70,7 @@ export const resolveAuth = async ({ site: siteFlag, project } = {}) => {
68
70
  const saved = config.sites?.[site];
69
71
  return {
70
72
  site,
73
+ siteFrom,
71
74
  token: (useEnvToken ? envToken : '') || saved?.token || '',
72
75
  fromEnv: useEnvToken,
73
76
  envTokenWithheld: Boolean(envToken) && !useEnvToken,
package/lib/files.mjs CHANGED
@@ -244,6 +244,12 @@ export const collectUploads = async (dir) => {
244
244
  skipped.push({ name: image.name, reason: 'backup images must be JPG, PNG or WebP' });
245
245
  continue;
246
246
  }
247
+ // Its banner has no size in the file name (the size is only in the ad.size tag), so there is no
248
+ // id to hang the backup on, and the server would refuse the whole backup batch over it.
249
+ if (pair && !pair.id && !sizeFromName(image.name)) {
250
+ skipped.push({ name: image.name, reason: `backup for ${baseName(image.name)}.zip, which has no size in its name: rename both, e.g. ${baseName(image.name)}-300x250` });
251
+ continue;
252
+ }
247
253
  // Named after its banner's id when known, so it lands on exactly that banner.
248
254
  const name = pair?.id ? `${pair.id}${ext}` : image.name;
249
255
  items.push({ type: 'backup', name, size: info.size, path: image.full, mimeType: MIME[ext], source: path.relative(dir, image.full) });
package/lib/mcp.mjs CHANGED
@@ -56,6 +56,7 @@ const campaignSummary = (campaign) => ({
56
56
  client: campaign.client,
57
57
  previewUrl: campaign.previewUrl,
58
58
  bannerCount: campaign.bannerCount,
59
+ reviewableCount: campaign.reviewableCount ?? null,
59
60
  approvedCount: campaign.approvedCount,
60
61
  });
61
62
 
@@ -86,10 +87,17 @@ export const createMcpServer = ({ version = '0.0.0', cwd = process.cwd(), deps =
86
87
  const auth = await d.resolveAuth({ project });
87
88
  if (!auth.token && auth.envTokenWithheld) {
88
89
  throw new ToolError(
89
- `PREVIEWER_TOKEN is not sent to ${auth.site}, which comes from ${PROJECT_FILE}: it only goes to PREVIEWER_URL (default https://previewer.agency). Ask the user whether to trust that site.`,
90
+ `PREVIEWER_TOKEN is not sent to ${auth.site}, which comes from ${auth.siteFrom === 'project' ? PROJECT_FILE : 'the saved sign-in'}: it only goes to PREVIEWER_URL (default https://previewer.agency). Ask the user whether to trust that site.`,
90
91
  );
91
92
  }
92
93
  if (!auth.token) {
94
+ // sign_in only ever signs in to the site the person set up, so for a folder that points
95
+ // somewhere else it would never help: the person has to sign in there themselves.
96
+ if (auth.siteFrom === 'project') {
97
+ throw new ToolError(
98
+ `Not signed in to ${auth.site}, the site this folder's ${PROJECT_FILE} names. sign_in can't sign in there. Ask the user whether they trust that site, and if so to run: previewer login --site ${auth.site}`,
99
+ );
100
+ }
93
101
  const waiting = login?.status === 'pending' ? ` A sign-in is waiting for approval: code ${login.code} at ${login.url}.` : ' Call sign_in.';
94
102
  throw new ToolError(`Not signed in to ${auth.site}.${waiting}`);
95
103
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "previewer-agency",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
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": {