// // Copyright 2021 DXOS.org // const chalk = require('chalk'); import fs from 'fs'; import path from 'path'; import YAML from 'yaml'; import { Argv, CommandModule } from 'yargs'; import { printKeyValue, sorter } from '../helpers'; type Topic = { topic: string docs?: string[] snippets?: string[] } type File = { topics: Topic[] } const print = ({ topic, docs, snippets }: Topic) => { console.log(chalk.green(topic)); if (docs && docs.length) { printKeyValue('Docs', docs[0], 'blue'); docs.slice(1).forEach(doc => printKeyValue('', doc, 'blue')); } if (snippets && snippets.length) { printKeyValue('Snippets', snippets[0], 'green'); snippets.slice(1).forEach(snippet => printKeyValue('', snippet, 'green')); } console.log(); } /** * Hints commands. */ export const kbaseModule: CommandModule = { command: 'kbase [search]', aliases: ['k'], describe: 'Knowledge base.', builder: (yargs: Argv) => yargs .option('list', { default: false, requiresArg: false }) .option('file', { // TODO(burdon): Get from Github. default: path.join(__dirname, '../../data/kbase.yml') }), handler: async ({ search, list, file: filename }: any) => { const file = fs.readFileSync(filename, 'utf8') const { topics } = YAML.parse(file) as File; topics.sort(sorter('topic')); if (list) { topics.forEach(({ topic }) => console.log(topic)); } else { topics .filter((topic) => !search || topic.topic.toLowerCase().indexOf(search) !== -1) .forEach(print); } } };