import debugFactory from 'debug' ; import fs from 'fs-extra' ; import path from 'path' ; import { ApiDocsOptions , DEFAULT_APIDOCS_EXTRACTION_PATH , DEFAULT_APIDOCS_GENERATION_PATH , getPackagesWithTsDocs , getUnscopedPackageName , LernaPackage , } from './helper' ; const debug = debugFactory ( 'loopback:tsdocs' ) ; param options export async function updateApiDocs ( options = { } ) { options = Object . assign ( { rootDir : process . cwd ( ) , apiDocsGenerationPath : DEFAULT_APIDOCS_GENERATION_PATH , apiDocsExtractionPath : DEFAULT_APIDOCS_EXTRACTION_PATH , generateDefaultPackageDoc : true } , options ) ; const packages = await getPackagesWithTsDocs ( options . rootDir ) ; if ( ! packages . length ) return ; const packagesByName = { } ; for ( const pkg of packages ) { packagesByName [ getUnscopedPackageName ( pkg . name ) ] = pkg ; } options . lernaPackages = packagesByName ; const found = await addJekyllMetadata ( packages [ 0 ] . rootPath , options ) ; if ( found ) { } } param lernaRootDir param options async function addJekyllMetadata ( lernaRootDir , options ) { const apiDocsRoot = path . join ( lernaRootDir , options . apiDocsGenerationPath ! ) ; const exists = await fs . pathExists ( apiDocsRoot ) ; if ( ! exists ) { console . error ( 'No API docs found at %s.' , apiDocsRoot ) ; return false ; } const apiFiles = await fs . readdir ( apiDocsRoot ) ; for ( const f of apiFiles ) { if ( ! f . endsWith ( '.md' ) ) continue ; const name = f . replace ( /\.md$/ , '' ) ; const isPackage = f . match ( /^[^\.]+.md$/ ) && f !== 'index.md' ; if ( ! options . silent ) { if ( isPackage ) { console . log ( 'Updating *.md files for %s' , name ) ; } } const docFile = path . join ( apiDocsRoot , f ) ; let doc = await fs . readFile ( docFile , 'utf-8' ) ; const pkgName = name . split ( '.' ) [ 0 ] ; let relativeUri = `packages/${ pkgName }` ; const pkg = options . lernaPackages ?. [ pkgName ] ; if ( pkg != null ) { relativeUri = path . relative ( pkg . rootPath , pkg . location ) . split ( path . sep ) . join ( '/' ) ; } const pkgUrl = f === 'index.md' ? 'https://github.com/loopbackio/loopback-next' : `https://github.com/loopbackio/loopback-next/tree/master/${ relativeUri }` ; if ( isPackage && options . generateDefaultPackageDoc ) { const modelFile = path . join ( path . join ( lernaRootDir , options . apiDocsExtractionPath ! , `models/${ name }.api.json` ) ) ; /**
             * "kind": "Package",
             * "canonicalReference": "@loopback/authentication",
             * "docComment": "",
             * "name": "@loopback/authentication",
             */ const model = await fs . readJson ( modelFile , { encoding : 'utf-8' } ) ; debug ( 'Package %s' , name , model ) ; if ( model . kind === 'Package' && ! model . docComment ) { const pkgDoc = `[${ model . name }](${ pkgUrl })` ; doc = doc . replace ( `## ${ name } package` , `## ${ name } package\n\n${ pkgDoc }` ) ; } } doc = `---
lang: en
title: 'API docs: ${ name }'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI
sidebar: lb4_sidebar
editurl: ${ pkgUrl }
permalink: /doc/en/lb4/apidocs.${ name }.html
---

${ doc }
` ; if ( ! options . dryRun ) { await fs . writeFile ( docFile , doc , 'utf-8' ) ; } } return true ; }