Files
ssh-portfolio/src/commands.js

37 lines
1.7 KiB
JavaScript

import * as home from './commands/home.js';
import * as help from './commands/help.js';
import * as pages from './commands/pages.js';
import * as open from './commands/open.js';
import * as contact from './commands/contact.js';
import * as chat from './commands/chat.js';
import * as clearCommand from './commands/clear.js';
import * as quit from './commands/quit.js';
import * as reload from './commands/reload.js';
import * as theme from './commands/theme.js';
import * as announce from './commands/announce.js';
import { pageMap } from './pages/index.js';
// Add a command module import here to make a new command available.
const commands = [home, help, pages, open, contact, chat, clearCommand, quit, reload, theme, announce];
export function createCommandRouter(api) {
const commandMap = new Map(commands.map(command => [command.name, command]));
const runPage = (ctx, name) => {
const page = pageMap.get(name);
return page ? page.render({ ...api, api, ctx }) : `No page named “${name}”. Try: pages`;
};
return {
home: ctx => runPage(ctx, 'home'),
execute(ctx, input) {
const [name, ...rest] = input.trim().split(/\s+/); const arg = rest.join(' ');
const command = commandMap.get(name || 'home') || pageMap.get(name);
if (!command) return `Unknown command: ${name}. Try “help”.`;
if (command.admin && !api.isAdmin(ctx)) return `Unknown command: ${name}. Try “help”.`;
if (pageMap.has(name) && !command.run) return runPage(ctx, name);
const result = command.run({ ...api, api, ctx, arg, config: api.config, pageMap, visibleCommands: commands });
if (typeof result === 'string' && result.startsWith('__PAGE__:')) return runPage(ctx, result.slice(9));
return result;
}
};
}