-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepl.js
More file actions
141 lines (141 loc) · 5.16 KB
/
Copy pathrepl.js
File metadata and controls
141 lines (141 loc) · 5.16 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
import readline from 'node:readline';
import chalk from 'chalk';
import Table from 'cli-table3';
import { PUBLIC_VERSION } from './constants.generated.js';
import { sprintf } from './utils/sprintf.js';
const BANNER_LINES = [
' ██████╗ ██████╗ ██████╗ ██████╗ ',
' ██╔══██╗██╔═══██╗██╔══██╗██╔═══██╗',
' ██████╔╝██║ ██║██████╔╝██║ ██║',
' ██╔═══╝ ██║ ██║██╔══██╗██║ ██║',
' ██║ ╚██████╔╝██████╔╝╚██████╔╝',
' ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ',
];
const collectCommands = (cmd, prefix = '') => {
const out = [];
for (const sub of cmd.commands) {
if (sub.name() === 'help')
continue;
const name = prefix ? sprintf('%s %s', prefix, sub.name()) : sub.name();
out.push({ name, description: sub.description() });
if (sub.commands.length > 0) {
out.push(...collectCommands(sub, name));
}
}
return out;
};
const completer = (program) => (line) => {
const all = [
...collectCommands(program).map((c) => c.name),
'help',
'exit',
'quit',
'clear',
];
const trimmed = line.trimStart();
const hits = all.filter((c) => c.startsWith(trimmed));
return [hits.length > 0 ? hits : all, trimmed];
};
const printBanner = () => {
console.log('');
for (const line of BANNER_LINES) {
console.log(chalk.green(line));
}
console.log('');
console.log(chalk.bold(sprintf(' Pobo Page Builder CLI · v%s', PUBLIC_VERSION)));
console.log(chalk.gray(' Type "help" for commands, "exit" to quit. Tab to autocomplete.\n'));
};
const BUILTINS = [
{ name: 'help', description: 'Show this help (list of commands)' },
{ name: 'clear', description: 'Clear the screen and redraw the banner' },
{ name: 'exit', description: 'Exit the shell (alias: quit, or Ctrl+D)' },
];
const buildHelpTable = (rows) => {
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 row of rows) {
table.push([chalk.green(row.name), chalk.gray(row.description)]);
}
return table;
};
const printHelp = (program) => {
const cmds = collectCommands(program);
console.log('');
console.log(chalk.bold(' Commands'));
console.log(buildHelpTable(cmds).toString());
console.log('');
console.log(chalk.bold(' Built-ins'));
console.log(buildHelpTable(BUILTINS).toString());
console.log(chalk.gray('\n Tab autocompletes. Append --help to any command for its options.\n'));
};
const readLine = (buildProgram, history) => new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
completer: completer(buildProgram()),
history,
historySize: 200,
removeHistoryDuplicates: true,
});
let resolved = false;
const finish = (value) => {
if (resolved)
return;
resolved = true;
// Snapshot history before closing — readline exposes it on the interface.
const current = rl.history;
if (Array.isArray(current)) {
history.length = 0;
history.push(...current);
}
rl.close();
resolve(value);
};
rl.on('close', () => finish({ input: '', closed: true }));
rl.question(chalk.green('pobo> '), (answer) => finish({ input: answer, closed: false }));
});
export const startRepl = async (buildProgram) => {
printBanner();
const history = [];
while (true) {
const { input, closed } = await readLine(buildProgram, history);
if (closed) {
console.log(chalk.gray('\nBye.'));
return;
}
const trimmed = input.trim();
if (trimmed === '')
continue;
if (trimmed === 'exit' || trimmed === 'quit') {
console.log(chalk.gray('Bye.'));
return;
}
if (trimmed === 'help') {
printHelp(buildProgram());
continue;
}
if (trimmed === 'clear') {
console.clear();
printBanner();
continue;
}
const args = trimmed.split(/\s+/);
const program = buildProgram();
program.exitOverride();
try {
await program.parseAsync(['node', 'pobo', ...args]);
}
catch (e) {
const message = e instanceof Error ? e.message : String(e);
const isCommanderHelp = message.includes('outputHelp') || message.includes('(outputHelp)');
const isCommanderVersion = message.includes('(version)');
if (!isCommanderHelp && !isCommanderVersion) {
console.error(chalk.red(sprintf('Error: %s', message)));
}
}
}
};