import open from 'open' ; import marked from 'marked' ; import { join } from 'path' ; import { tmpdir } from 'os' ; import TerminalRenderer from 'marked-terminal' ; import { readFile , outputFile } from 'fs-extra' ; import { css } from './Styles' ; /**
 * Markdown renderer for opening the instructions md file inside the
 * terminal or the browser.
 */ export class MarkdownRenderer { constructor ( private mdFileAbsPath , private packageName ) { } /**
     * Generates HTML with the markdown processed code
     */ private generateHtml ( contents ) { return `<html>
      <head>
        <style type="text/css">${ css }</style>
      </head>
      <body>
        <article class="markdown-body">
          <h1> Setup instructions for
            <a href="https://npmjs.org/package/${ this . packageName }" target="_blank">${ this . packageName }</a>
          </h1>
          ${ contents }
        </article>
      </body>
    </html>` ; } /**
     * Opens the html contents by writing it to a temporary
     * file and opens up the file inside the browser.
     */ private async openContentsInBrowser ( html ) { const filePath = join ( tmpdir ( ) , `adonis-${ new Date ( ) . getTime ( ) }.html` ) ; await outputFile ( filePath , html ) ; await open ( filePath , { wait : false } ) ; } /**
     * Converts markdown to HTML and opens it up inside the browser
     */ public async renderInBrowser ( ) { try { const contents = await readFile ( this . mdFileAbsPath , 'utf-8' ) ; const html = this . generateHtml ( marked . setOptions ( { renderer : new marked . Renderer ( ) } ) ( contents . trim ( ) ) ) ; await this . openContentsInBrowser ( html ) ; } catch ( error ) { } } /**
     * Writes markdown in the terminal
     */ public async renderInTerminal ( ) { try { const contents = await readFile ( this . mdFileAbsPath , 'utf-8' ) ; console . log ( marked . setOptions ( { renderer : new TerminalRenderer ( ) } ) ( contents . trim ( ) ) . trim ( ) ) ; } catch ( error ) { } } }