Split pages and commands into editable modules
This commit is contained in:
@@ -27,4 +27,11 @@ Run `npm run notify` on the owner machine (or use `NOTIFICATION_PORT`) to receiv
|
||||
|
||||
## Extending
|
||||
|
||||
Pages are data-driven in `config.json`; commands are isolated in `src/commands.js`. Pong is intentionally left as the next module: it can use the same session registry and owner-online check.
|
||||
Pages and commands are source modules:
|
||||
|
||||
- `src/pages/home.js`, `about.js`, `projects.js`, and `links.js` are the premade editable pages.
|
||||
- `src/pages/index.js` is the page registry. Add an import and registry entry when creating a new page; its filename becomes the SSH command.
|
||||
- `src/commands/` contains the premade command modules (`chat`, `contact`, `announce`, and so on).
|
||||
- `src/commands.js` is the command registry. Add an import there to expose a new command.
|
||||
|
||||
Each page exports `name`, `title`, and `render({ config, ctx, api })`. The renderer returns terminal text, so you can build extensive custom pages while inheriting the shared header, theme, and prompt. Pong is intentionally left as the next module: it can use the same session registry and owner-online check.
|
||||
|
||||
+28
-36
@@ -1,44 +1,36 @@
|
||||
import { color, wrap } from './terminal.js';
|
||||
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 commands = new Map();
|
||||
const add = command => commands.set(command.name, command);
|
||||
const page = name => {
|
||||
const item = api.config.pages?.[name];
|
||||
if (!item) return `${color('red', `No page named “${name}”.`)}\nTry: pages`;
|
||||
return wrap((item.lines || []).join('\n')).join('\n');
|
||||
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`;
|
||||
};
|
||||
|
||||
add({ name: 'home', description: 'Return home', run: () => {
|
||||
const p = api.config.persona || {};
|
||||
const status = api.ownerOnline() ? color('green', 'Owner is online') : color(api.config.theme?.muted || 'brightBlack', 'Owner is offline');
|
||||
return `${color(api.config.theme?.accent || 'yellow', `Welcome to ${p.name || 'my portfolio'}`)}\n\n${wrap(p.bio || 'You are exploring a portfolio over SSH. Type “help” to get started.').join('\n')}\n\n${status}\n\nCommands: home · pages · open <page> · chat · contact · clear · quit`;
|
||||
}});
|
||||
add({ name: 'help', description: 'Show this help', run: ctx => {
|
||||
const actions = [...commands.values()].filter(c => !c.admin || api.isAdmin(ctx)).map(c => `${c.name.padEnd(18)} ${c.description}`);
|
||||
const pages = Object.entries(api.config.pages || {}).filter(([name]) => !commands.has(name)).map(([name, item]) => `${name.padEnd(18)} Open ${item.title || name}`);
|
||||
return [...actions, ...pages].join('\n');
|
||||
}});
|
||||
add({ name: 'pages', description: 'List available pages', run: () => Object.entries(api.config.pages || {}).map(([name, item]) => `${color(api.config.theme?.accent || 'yellow', name.padEnd(14))} ${item.title || ''}`).join('\n') || 'No pages configured.' });
|
||||
add({ name: 'open', description: 'Open a page', run: (ctx, arg) => arg ? page(arg) : 'Usage: open <page>' });
|
||||
add({ name: 'contact', description: 'Show contact details', run: () => `Email: ${api.config.persona?.email || 'not configured'}` });
|
||||
add({ name: 'chat', description: 'Chat with the owner/visitors', run: (ctx, arg) => {
|
||||
if (!arg) return 'Usage: chat <message>';
|
||||
const author = api.isAdmin(ctx) ? `${api.config.persona?.name || 'Owner'} [owner]` : (ctx.username || 'visitor');
|
||||
const line = `${author}: ${arg}`; api.broadcast(line); api.sendNotification(line); return api.ownerOnline() ? 'Message sent to the owner.' : 'Message posted; the owner is currently offline.';
|
||||
}});
|
||||
add({ name: 'clear', description: 'Clear the screen', run: () => '' });
|
||||
add({ name: 'quit', description: 'Disconnect', run: ctx => { ctx.channel?.end(); return ''; } });
|
||||
add({ name: 'reload', description: 'Reload configuration', admin: true, run: () => { api.reload(); return 'Configuration reloaded.'; } });
|
||||
add({ name: 'theme', description: 'Set runtime primary color', admin: true, run: (ctx, arg) => { if (!arg) return 'Usage: theme <color>'; api.config.theme.primary = arg; return `Primary color set to ${arg} (runtime only).`; } });
|
||||
add({ name: 'announce', description: 'Broadcast an announcement', admin: true, run: (ctx, arg) => { if (!arg) return 'Usage: announce <message>'; api.broadcast(`OWNER: ${arg}`); api.sendNotification(`OWNER: ${arg}`); return 'Announcement sent.'; } });
|
||||
|
||||
return {
|
||||
home: ctx => commands.get('home').run(ctx, ''),
|
||||
home: ctx => runPage(ctx, 'home'),
|
||||
execute(ctx, input) {
|
||||
const [name, ...rest] = input.trim().split(/\s+/); const command = commands.get(name || 'home') || (api.config.pages?.[name] ? { run: () => page(name) } : null);
|
||||
if (!command || (command.admin && !api.isAdmin(ctx))) return `Unknown command: ${name}. Try “help”.`;
|
||||
return command.run(ctx, rest.join(' '));
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'announce'; export const description = 'Broadcast an announcement'; export const admin = true;
|
||||
export function run({ arg, api }) { if (!arg) return 'Usage: announce <message>'; api.broadcast(`OWNER: ${arg}`); api.sendNotification(`OWNER: ${arg}`); return 'Announcement sent.'; }
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'chat'; export const description = 'Chat with the owner/visitors';
|
||||
export function run({ arg, ctx, api }) { if (!arg) return 'Usage: chat <message>'; const author = api.isAdmin(ctx) ? `${api.config.persona?.name || 'Owner'} [owner]` : (ctx.username || 'visitor'); const line = `${author}: ${arg}`; api.broadcast(line); api.sendNotification(line); return api.ownerOnline() ? 'Message sent to the owner.' : 'Message posted; the owner is currently offline.'; }
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'clear'; export const description = 'Clear the screen';
|
||||
export function run() { return ''; }
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'contact'; export const description = 'Show contact details';
|
||||
export function run({ config }) { return `Email: ${config.persona?.email || 'not configured'}`; }
|
||||
@@ -0,0 +1,6 @@
|
||||
export const name = 'help'; export const description = 'Show this help';
|
||||
export function run({ visibleCommands, pageMap, isAdmin, ctx }) {
|
||||
const actions = visibleCommands.filter(command => !command.admin || isAdmin(ctx)).map(command => `${command.name.padEnd(18)} ${command.description}`);
|
||||
const pages = [...pageMap.values()].filter(page => !visibleCommands.some(command => command.name === page.name)).map(page => `${page.name.padEnd(18)} Open ${page.title || page.name}`);
|
||||
return [...actions, ...pages].join('\n');
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'home'; export const description = 'Return home';
|
||||
export function run({ pageMap, config, ownerOnline, ctx, api }) { return pageMap.get('home').render({ config, ownerOnline, ctx, api }); }
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'open'; export const description = 'Open a page';
|
||||
export function run({ arg }) { return arg ? `__PAGE__:${arg}` : 'Usage: open <page>'; }
|
||||
@@ -0,0 +1,3 @@
|
||||
import { color } from '../terminal.js';
|
||||
export const name = 'pages'; export const description = 'List available pages';
|
||||
export function run({ pageMap, config }) { return [...pageMap.values()].map(page => `${color(config.theme?.accent || 'yellow', page.name.padEnd(14))} ${page.title || page.name}`).join('\n') || 'No pages configured.'; }
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'quit'; export const description = 'Disconnect';
|
||||
export function run({ ctx }) { ctx.channel?.end(); return ''; }
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'reload'; export const description = 'Reload configuration'; export const admin = true;
|
||||
export function run({ api }) { api.reload(); return 'Configuration reloaded.'; }
|
||||
@@ -0,0 +1,2 @@
|
||||
export const name = 'theme'; export const description = 'Set runtime primary color'; export const admin = true;
|
||||
export function run({ arg, config }) { if (!arg) return 'Usage: theme <color>'; config.theme.primary = arg; return `Primary color set to ${arg} (runtime only).`; }
|
||||
@@ -0,0 +1,10 @@
|
||||
import { wrap } from '../terminal.js';
|
||||
|
||||
export const name = 'about';
|
||||
export const title = 'About';
|
||||
|
||||
// Edit this function to customize your About page.
|
||||
export function render({ config }) {
|
||||
const persona = config.persona || {};
|
||||
return wrap((persona.about || 'Tell visitors who you are.') + '\n\n' + (persona.bio || 'This is your space on the internet.')).join('\n');
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { color, wrap } from '../terminal.js';
|
||||
|
||||
export const name = 'home';
|
||||
export const title = 'Home';
|
||||
|
||||
// Edit this function to customize the landing page.
|
||||
export function render({ config, ownerOnline }) {
|
||||
const p = config.persona || {};
|
||||
const status = ownerOnline() ? color('green', 'Owner is online') : color(config.theme?.muted || 'brightBlack', 'Owner is offline');
|
||||
return `${color(config.theme?.accent || 'yellow', `Welcome to ${p.name || 'my portfolio'}`)}\n\n${wrap(p.bio || 'You are exploring a portfolio over SSH. Type “help” to get started.').join('\n')}\n\n${status}\n\nCommands: home · pages · open <page> · chat · contact · clear · quit`;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import * as home from './home.js';
|
||||
import * as about from './about.js';
|
||||
import * as projects from './projects.js';
|
||||
import * as links from './links.js';
|
||||
|
||||
// Add new page modules to this registry to make them available as commands.
|
||||
export const pages = [home, about, projects, links];
|
||||
export const pageMap = new Map(pages.map(page => [page.name, page]));
|
||||
@@ -0,0 +1,10 @@
|
||||
export const name = 'links';
|
||||
export const title = 'Links';
|
||||
|
||||
// Edit these links directly, or move them into your config file.
|
||||
export function render({ config }) {
|
||||
return (config.pages?.links?.lines || [
|
||||
'GitHub: https://github.com/yourname',
|
||||
'Website: https://example.com'
|
||||
]).join('\n');
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export const name = 'projects';
|
||||
export const title = 'Projects';
|
||||
|
||||
// Replace these entries with your own work, or load them from config.
|
||||
export function render({ config }) {
|
||||
return (config.pages?.projects?.lines || [
|
||||
'Project One — what it does',
|
||||
'Project Two — what you learned'
|
||||
]).join('\n');
|
||||
}
|
||||
Reference in New Issue
Block a user