CLI for Oblique Strategies.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

  1. import { generate, getDefaultCards } from '@theoryofnekomata/oblique-strategies-core';
  2. import * as file from './readers/file';
  3. import * as http from './readers/http';
  4. import * as https from './readers/https';
  5. import * as text from './readers/text';
  6. import * as presenters from './presenters';
  7. export const PRESENTATION = ['plain', 'card', 'centered'] as const;
  8. export type Presentation = typeof PRESENTATION[number];
  9. export type MainArgs = {
  10. cards: string[],
  11. formatted: boolean,
  12. presentation: Presentation,
  13. }
  14. const formatCard = async (card: string) => {
  15. const { default: chalk } = await import('chalk');
  16. return card
  17. .split('\n')
  18. .map((line) => (
  19. line.startsWith('-')
  20. ? chalk.italic(line)
  21. : line
  22. ))
  23. .join('\n');
  24. };
  25. const loadCards = async (cards: string[]) => {
  26. const readers = [
  27. text,
  28. file,
  29. https,
  30. http,
  31. ];
  32. const promises = cards.map(async (c) => {
  33. if (c === 'default') {
  34. return getDefaultCards();
  35. }
  36. const reader = readers.find((m) => c.startsWith(m.prefix));
  37. if (reader) {
  38. const { read, prefix } = reader;
  39. return read(c.slice(c.indexOf(prefix) + prefix.length));
  40. }
  41. throw new Error(`Invalid source: ${c}`);
  42. });
  43. const sources = await Promise.all(promises);
  44. return sources.flat(1);
  45. };
  46. export const main = async (args: MainArgs) => {
  47. const { cards, presentation, formatted } = args;
  48. const loadedCards = await loadCards(cards);
  49. const generatedCard = generate(loadedCards);
  50. const cardContent = await (
  51. formatted
  52. ? formatCard(generatedCard)
  53. : Promise.resolve(generatedCard)
  54. );
  55. await presenters[presentation]()(cardContent);
  56. };