previewer-agency 0.1.0 → 0.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/README.md +4 -1
- package/bin/previewer.mjs +10 -1
- package/lib/menu.mjs +108 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -36,6 +36,9 @@ What it picks up:
|
|
|
36
36
|
| `300x250.jpg` next to `300x250.zip`, or anything in `backups/` | that banner's backup image |
|
|
37
37
|
| other images and videos | image and video creatives |
|
|
38
38
|
|
|
39
|
+
Leave out `--client` and `--campaign` and `push` lets you pick them from a menu: arrow keys to move,
|
|
40
|
+
type to filter, Enter to choose, or **+ New campaign** to make one.
|
|
41
|
+
|
|
39
42
|
A file with the same name as a banner already in the campaign replaces it as a new version. If the
|
|
40
43
|
campaign doesn't exist yet, `push` offers to create it (`--create` skips the question). Large files
|
|
41
44
|
and big batches are split up for you.
|
|
@@ -85,4 +88,4 @@ const { campaign } = await api.upload(42, items);
|
|
|
85
88
|
console.log(campaign.previewUrl);
|
|
86
89
|
```
|
|
87
90
|
|
|
88
|
-
The API itself is documented
|
|
91
|
+
The API itself is documented at [previewer.agency/docs/api](https://previewer.agency/docs/api#tokens).
|
package/bin/previewer.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import { createInterface } from 'node:readline/promises';
|
|
|
10
10
|
import { parseArgs } from 'node:util';
|
|
11
11
|
import { PreviewerClient, PreviewerError, newCliToken } from '../lib/api.mjs';
|
|
12
12
|
import { collectUploads } from '../lib/files.mjs';
|
|
13
|
+
import { selectMenu } from '../lib/menu.mjs';
|
|
13
14
|
import { DEFAULT_SITE, PROJECT_FILE, findProject, forgetSiteLogin, normalizeSite, resolveAuth, saveProject, saveSiteLogin } from '../lib/config.mjs';
|
|
14
15
|
|
|
15
16
|
const VERSION = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
|
|
@@ -20,6 +21,7 @@ const dim = color(2);
|
|
|
20
21
|
const green = color(32);
|
|
21
22
|
const red = color(31);
|
|
22
23
|
const yellow = color(33);
|
|
24
|
+
const cyan = color(36);
|
|
23
25
|
|
|
24
26
|
const say = (...lines) => console.log(lines.join('\n'));
|
|
25
27
|
const fail = (message, hint) => {
|
|
@@ -109,7 +111,14 @@ const ask = async (question) => {
|
|
|
109
111
|
}
|
|
110
112
|
};
|
|
111
113
|
|
|
114
|
+
// An arrow-key menu (type to filter) in a terminal that supports it, otherwise a numbered list.
|
|
115
|
+
const CANCELLED = Symbol('cancelled');
|
|
112
116
|
const choose = async (title, options) => {
|
|
117
|
+
if (typeof process.stdin.setRawMode === 'function') {
|
|
118
|
+
const picked = await selectMenu(title, options, { style: { bold, dim, accent: cyan }, cancelled: CANCELLED });
|
|
119
|
+
if (picked === CANCELLED) process.exit(130);
|
|
120
|
+
return picked;
|
|
121
|
+
}
|
|
113
122
|
say('', bold(title));
|
|
114
123
|
options.forEach((option, index) => say(` ${dim(`${index + 1}.`)} ${option.label}`));
|
|
115
124
|
for (;;) {
|
|
@@ -246,7 +255,7 @@ const pickCampaign = async (api, options, { allowCreate }) => {
|
|
|
246
255
|
const NEW = Symbol('new');
|
|
247
256
|
const picked = await choose(`Campaign for ${client.name}`, [
|
|
248
257
|
...campaigns.map((item) => ({ label: `${item.name} ${dim(`(${item.bannerCount} banners)`)}`, value: item })),
|
|
249
|
-
...(allowCreate ? [{ label: green('+ New campaign'), value: NEW }] : []),
|
|
258
|
+
...(allowCreate ? [{ label: green('+ New campaign'), value: NEW, sticky: true }] : []),
|
|
250
259
|
]);
|
|
251
260
|
if (picked === NEW) {
|
|
252
261
|
const name = await ask('Campaign name: ');
|
package/lib/menu.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// An arrow-key menu for the terminal, without dependencies: ↑/↓ to move, type to filter, Enter to
|
|
2
|
+
// choose, Esc to clear the filter (or cancel), Ctrl+C to cancel. `menuKey` and `menuLines` are pure,
|
|
3
|
+
// so the behaviour is tested without a terminal; `selectMenu` wires them to stdin/stdout.
|
|
4
|
+
|
|
5
|
+
import { emitKeypressEvents } from 'node:readline';
|
|
6
|
+
|
|
7
|
+
const ANSI = /\x1b\[[0-9;?]*[A-Za-z]/g;
|
|
8
|
+
export const plain = (text) => String(text).replace(ANSI, '');
|
|
9
|
+
|
|
10
|
+
// Options marked `sticky` (like "+ New campaign") stay visible whatever the filter.
|
|
11
|
+
export const filterOptions = (options, query) => {
|
|
12
|
+
const needle = query.trim().toLowerCase();
|
|
13
|
+
if (!needle) return options;
|
|
14
|
+
return options.filter((option) => option.sticky || plain(option.label).toLowerCase().includes(needle));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const initialMenuState = { query: '', index: 0, done: null, choice: undefined };
|
|
18
|
+
|
|
19
|
+
/** The next state after a keypress (`str` and `key` as readline's 'keypress' event gives them). */
|
|
20
|
+
export const menuKey = (state, options, str, key = {}) => {
|
|
21
|
+
const visible = filterOptions(options, state.query);
|
|
22
|
+
const count = visible.length;
|
|
23
|
+
if (key.ctrl && (key.name === 'c' || key.name === 'd')) return { ...state, done: 'cancel' };
|
|
24
|
+
switch (key.name) {
|
|
25
|
+
case 'escape':
|
|
26
|
+
return state.query ? { ...state, query: '', index: 0 } : { ...state, done: 'cancel' };
|
|
27
|
+
case 'up':
|
|
28
|
+
return { ...state, index: count ? (state.index - 1 + count) % count : 0 };
|
|
29
|
+
case 'down':
|
|
30
|
+
case 'tab':
|
|
31
|
+
return { ...state, index: count ? (state.index + 1) % count : 0 };
|
|
32
|
+
case 'home':
|
|
33
|
+
return { ...state, index: 0 };
|
|
34
|
+
case 'end':
|
|
35
|
+
return { ...state, index: Math.max(count - 1, 0) };
|
|
36
|
+
case 'return':
|
|
37
|
+
case 'enter':
|
|
38
|
+
return count ? { ...state, done: 'choose', choice: visible[Math.min(state.index, count - 1)] } : state;
|
|
39
|
+
case 'backspace':
|
|
40
|
+
return { ...state, query: state.query.slice(0, -1), index: 0 };
|
|
41
|
+
default:
|
|
42
|
+
if (str && !key.ctrl && !key.meta && /^[^\x00-\x1f\x7f]$/u.test(str)) return { ...state, query: state.query + str, index: 0 };
|
|
43
|
+
return state;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const fit = (line, columns) => {
|
|
48
|
+
const text = plain(line);
|
|
49
|
+
return text.length < columns ? line : `${text.slice(0, Math.max(columns - 2, 1))}…`;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** The lines to draw for a state: a hint or the filter, then a window of options around the cursor. */
|
|
53
|
+
export const menuLines = (options, state, { rows = 24, columns = 80, style = {} } = {}) => {
|
|
54
|
+
const { dim = String, accent = String } = style;
|
|
55
|
+
const visible = filterOptions(options, state.query);
|
|
56
|
+
const size = Math.max(3, Math.min(visible.length, rows - 5, 12));
|
|
57
|
+
const start = Math.min(Math.max(0, state.index - Math.floor(size / 2)), Math.max(0, visible.length - size));
|
|
58
|
+
const lines = [state.query ? `${dim('Filter:')} ${state.query}` : dim('↑↓ move · type to filter · enter to choose')];
|
|
59
|
+
if (!visible.length) lines.push(dim(' Nothing matches. Backspace or Esc to clear.'));
|
|
60
|
+
visible.slice(start, start + size).forEach((option, offset) => {
|
|
61
|
+
lines.push(start + offset === state.index ? `${accent('❯')} ${accent(plain(option.label))}` : ` ${option.label}`);
|
|
62
|
+
});
|
|
63
|
+
if (visible.length > size) lines.push(dim(` ${start + 1}–${Math.min(start + size, visible.length)} of ${visible.length}`));
|
|
64
|
+
return lines.map((line) => fit(line, columns));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Shows the menu and resolves with the chosen option's value, or `cancelled` when the person backs
|
|
69
|
+
* out. Needs a TTY on both ends; callers fall back to a numbered list otherwise.
|
|
70
|
+
*/
|
|
71
|
+
export const selectMenu = (title, options, { input = process.stdin, output = process.stdout, style = {}, cancelled = null } = {}) =>
|
|
72
|
+
new Promise((resolve) => {
|
|
73
|
+
const { bold = String, dim = String } = style;
|
|
74
|
+
let state = initialMenuState;
|
|
75
|
+
let drawn = 0;
|
|
76
|
+
const showCursor = () => output.write('\x1b[?25h');
|
|
77
|
+
const clear = () => (drawn ? `\x1b[${drawn}A\r\x1b[0J` : '');
|
|
78
|
+
const draw = () => {
|
|
79
|
+
const lines = menuLines(options, state, { rows: output.rows || 24, columns: output.columns || 80, style });
|
|
80
|
+
output.write(`${clear()}${lines.join('\n')}\n`);
|
|
81
|
+
drawn = lines.length;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const finish = (value, summary) => {
|
|
85
|
+
input.off('keypress', onKey);
|
|
86
|
+
input.setRawMode(wasRaw);
|
|
87
|
+
input.pause();
|
|
88
|
+
output.write(`${clear()}${summary ? `${summary}\n` : ''}`);
|
|
89
|
+
showCursor();
|
|
90
|
+
process.off('exit', showCursor);
|
|
91
|
+
resolve(value);
|
|
92
|
+
};
|
|
93
|
+
const onKey = (str, key) => {
|
|
94
|
+
state = menuKey(state, options, str, key ?? {});
|
|
95
|
+
if (state.done === 'cancel') return finish(cancelled, dim(' Cancelled.'));
|
|
96
|
+
if (state.done === 'choose') return finish(state.choice.value, ` ${dim('›')} ${plain(state.choice.label)}`);
|
|
97
|
+
draw();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
output.write(`\n${bold(title)}\n\x1b[?25l`);
|
|
101
|
+
process.once('exit', showCursor);
|
|
102
|
+
emitKeypressEvents(input);
|
|
103
|
+
const wasRaw = Boolean(input.isRaw);
|
|
104
|
+
input.setRawMode(true);
|
|
105
|
+
input.on('keypress', onKey);
|
|
106
|
+
input.resume();
|
|
107
|
+
draw();
|
|
108
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "previewer-agency",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Upload HTML5 banners, images and videos to Previewer from any folder or CI job, and check approvals.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"ads",
|
|
29
29
|
"previewer"
|
|
30
30
|
],
|
|
31
|
-
"homepage": "https://previewer.agency",
|
|
31
|
+
"homepage": "https://previewer.agency/docs/api",
|
|
32
32
|
"license": "UNLICENSED"
|
|
33
33
|
}
|