|
- import { generate, getDefaultCards } from '@theoryofnekomata/oblique-strategies-core';
- import * as file from './readers/file';
- import * as http from './readers/http';
- import * as https from './readers/https';
- import * as text from './readers/text';
- import * as presenters from './presenters';
-
- export const PRESENTATION = ['plain', 'card', 'centered'] as const;
-
- export type Presentation = typeof PRESENTATION[number];
-
- export type MainArgs = {
- cards: string[],
- formatted: boolean,
- presentation: Presentation,
- }
-
- const formatCard = async (card: string) => {
- const { default: chalk } = await import('chalk');
- return card
- .split('\n')
- .map((line) => (
- line.startsWith('-')
- ? chalk.italic(line)
- : line
- ))
- .join('\n');
- };
-
- const loadCards = async (cards: string[]) => {
- const readers = [
- text,
- file,
- https,
- http,
- ];
-
- const promises = cards.map(async (c) => {
- if (c === 'default') {
- return getDefaultCards();
- }
-
- const reader = readers.find((m) => c.startsWith(m.prefix));
-
- if (reader) {
- const { read, prefix } = reader;
- return read(c.slice(c.indexOf(prefix) + prefix.length));
- }
-
- throw new Error(`Invalid source: ${c}`);
- });
-
- const sources = await Promise.all(promises);
- return sources.flat(1);
- };
-
- export const main = async (args: MainArgs) => {
- const { cards, presentation, formatted } = args;
- const loadedCards = await loadCards(cards);
- const generatedCard = generate(loadedCards);
- const cardContent = await (
- formatted
- ? formatCard(generatedCard)
- : Promise.resolve(generatedCard)
- );
- await presenters[presentation]()(cardContent);
- };
|