-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
172 lines (172 loc) · 7.64 KB
/
cli.js
File metadata and controls
172 lines (172 loc) · 7.64 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
import { Command } from 'commander';
import chalk from 'chalk';
import Table from 'cli-table3';
import { PUBLIC_VERSION } from './constants.generated.js';
import { startRepl } from './repl.js';
import { loginCommand } from './commands/auth/login.js';
import { logoutCommand } from './commands/auth/logout.js';
import { meCommand } from './commands/auth/me.js';
import { aiCommand } from './commands/widget/ai.js';
import { createCommand } from './commands/widget/create.js';
import { deleteCommand } from './commands/widget/delete.js';
import { pushCommand } from './commands/widget/push.js';
import { copyableCommand } from './commands/widget/copyable.js';
import { validateCommand } from './commands/widget/validate.js';
import { connectCommand } from './commands/widget/connect.js';
import { connectionsCommand } from './commands/widget/connections.js';
import { disconnectCommand } from './commands/widget/disconnect.js';
import { flushCommand } from './commands/widget/flush.js';
import { previewCommand } from './commands/widget/preview.js';
import { proxyCommand } from './commands/widget/proxy.js';
import { listCommand } from './commands/widget/list.js';
import { showCommand } from './commands/widget/show.js';
import { doctorCommand } from './commands/system/doctor.js';
import { initCommand } from './commands/system/init.js';
import { sprintf } from './utils/sprintf.js';
let inRepl = false;
const wrap = (fn) => {
return async (...args) => {
try {
await fn(...args);
}
catch (e) {
const message = e instanceof Error ? e.message : String(e);
console.error(chalk.red(sprintf('Error: %s', message)));
if (!inRepl) {
process.exit(1);
}
}
};
};
const collectSubcommands = (cmd, indent = 0) => {
const rows = [];
for (const sub of cmd.commands) {
if (sub.name() === 'help')
continue;
const pad = ' '.repeat(indent);
const usage = sub.usage();
const term = usage && usage !== '[options]'
? sprintf('%s %s', sub.name(), usage)
: sub.name();
rows.push({ command: pad + term, description: sub.description() });
if (sub.commands.length > 0) {
rows.push(...collectSubcommands(sub, indent + 1));
}
}
return rows;
};
const buildProgram = () => {
const program = new Command();
program
.name('pobo')
.description('Pobo Page Builder CLI')
.version(PUBLIC_VERSION);
const auth = program.command('auth').description('Authentication');
auth.command('login').description('Sign in with email and password').action(wrap(loginCommand));
auth.command('logout').description('Sign out').action(wrap(logoutCommand));
auth.command('me').description('Show current user info and available eshops').action(wrap(meCommand));
const widget = program.command('widget').description('Widget management');
widget.command('list').description('List your widgets').action(wrap(listCommand));
widget.command('create').description('Create a new widget').action(wrap(createCommand));
widget
.command('ai [id]')
.description('Generate widget HTML/SCSS from an image via Claude (server-side)')
.requiredOption('-i, --image <path>', 'image file (PNG/JPG/WebP, ≤ 5 MB)')
.option('-f, --force', 'overwrite local files without confirmation prompt')
.action(wrap((id, options) => aiCommand({ id, image: options.image, force: options.force })));
widget
.command('show [id]')
.description('Show widget details from server')
.action(wrap((id) => showCommand({ id })));
widget
.command('push [id]')
.description('Push widget to server (HTML + compiled CSS)')
.option('-y, --yes', 'skip post-push follow-up prompt')
.action(wrap((id, options) => pushCommand({ id, yes: options.yes })));
widget
.command('copyable [id]')
.description('Manage which element classes are duplicatable in the editor')
.option('-l, --list', 'list current copyable classes and exit')
.option('-a, --add <classes>', 'comma-separated class names to add')
.option('-r, --remove <classes>', 'comma-separated class names to remove')
.option('-s, --set <classes>', 'replace the list with these comma-separated class names')
.option('-c, --clear', 'remove all copyable classes')
.action(wrap((id, options) => copyableCommand({ id, ...options })));
widget
.command('validate [id]')
.description('Validate widget HTML')
.action(wrap((id) => validateCommand({ id })));
widget
.command('connect [id]')
.description('Connect widget to one or more e-shops')
.action(wrap((id) => connectCommand({ id })));
widget
.command('disconnect [id]')
.description('Disconnect widget from one or more e-shops')
.option('-y, --yes', 'skip confirmation prompt')
.action(wrap((id, options) => disconnectCommand({ id, yes: options.yes })));
widget
.command('connections')
.description('Show widget × eshop connections matrix (all widgets in one call)')
.action(wrap(connectionsCommand));
widget
.command('flush [id]')
.description('Delete widget elements')
.option('-y, --yes', 'skip confirmation prompt')
.action(wrap((id, options) => flushCommand({ id, yes: options.yes })));
widget
.command('delete [id]')
.description('Delete widget from server (cannot be undone)')
.option('-y, --yes', 'skip confirmation prompt')
.action(wrap((id, options) => deleteCommand({ id, yes: options.yes })));
widget
.command('preview [id]')
.description('Open widget preview in browser')
.option('--no-open', 'do not auto-open the preview URL in the browser')
.action(wrap((id, options) => previewCommand({ id, open: options.open })));
widget
.command('proxy [url]')
.description('Live preview widget on an e-shop page (wizard if no URL)')
.option('-p, --port <port>', 'Preview server port', '3001')
.option('-s, --selector <selector>', 'CSS selector for widget injection target', '.basic-description')
.option('--no-open', 'do not auto-open the preview URL in the browser')
.action(wrap((url, options) => proxyCommand({
url,
port: parseInt(options.port, 10),
selector: options.selector,
open: options.open,
})));
program
.command('doctor')
.description('Health check: environment, config, connectivity, local widgets')
.action(wrap(doctorCommand));
program
.command('init')
.description('Write CLAUDE.md to the current directory for Claude Code widget creation context')
.action(wrap(initCommand));
program.addHelpText('after', () => {
const rows = collectSubcommands(program);
if (rows.length === 0)
return '';
const table = new Table({
head: [chalk.bold('Command'), chalk.bold('Description')],
style: { head: [], border: ['gray'] },
chars: { mid: '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' },
});
for (const r of rows) {
table.push([r.command, r.description]);
}
return sprintf('\nAll commands (recursive):\n%s\n', table.toString());
});
return program;
};
export const run = async (argv) => {
const userArgs = argv.slice(2);
if (userArgs.length === 0 && process.stdin.isTTY) {
inRepl = true;
await startRepl(buildProgram);
return;
}
const program = buildProgram();
await program.parseAsync(argv);
};