/**
 * Copyright © 2020 Johnson & Johnson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import pathUtil from 'path' ; import fs from 'fs' ; import { ensureDirSync } from 'fs-extra' ; import locateFiles from './locateFiles' ; import type { Copier } from './write' ; import { Tree } from './type' ; const isEnabled = ( ) => process . env . BODILESS_DOCS_API_ENABLED === '1' ; const API_DOC_PACKAGE_PATH = 'doc/api' ; const API_DOC_DOCSERVER_PATH = '/Development/API' ; /**
 * adds API link to the navigation tree.
 */ const updateNavigation = ( docPath , paths ) => { const paths$1 = paths ; if ( paths$1 . Development !== undefined ) { ensureDirSync ( pathUtil . join ( docPath , API_DOC_DOCSERVER_PATH ) ) ; ( paths$1 . Development as Tree ) . API = { 'README.md' : 'README.md' , } ; } return paths$1 ; } ; param packageJsonPath const getPackageNameFromPackageJson = ( packageJsonPath ) : string | undefined => { let packageName ; try { const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ; packageName = packageJson . name ; } catch { console . log ( `Error reading package.json from ${ packageJsonPath }` ) ; } return packageName ; } ; /**
 * finds all packages containing api docs.
 */ const findApiDocPaths = async ( ) => locateFiles ( { filePattern : new RegExp ( `${ API_DOC_PACKAGE_PATH }/globals.md$` ) , startingRoot : './' , action : filePath => { const apiDocPath = pathUtil . resolve ( filePath , '..' ) ; const resolveTo = new Array ( API_DOC_PACKAGE_PATH . split ( '/' ) . length ) . fill ( '..' ) . join ( '/' ) ; const packagePath = pathUtil . resolve ( apiDocPath , resolveTo ) ; const packageNameFromPackageJson = getPackageNameFromPackageJson ( pathUtil . resolve ( packagePath , 'package.json' ) ) ; const packageName = packageNameFromPackageJson || pathUtil . basename ( packagePath ) ; return Promise . resolve ( { [ packageName ] : apiDocPath , } ) ; } , } ) ; type Props = { copier : Copier ; targetDocPath : string ; } ; type Paths = { [ key ] : string ; } ; const copyApiDocs = async ( { paths , targetDocPath , copier } ) => { const promises = [ ] as Promise < any > [ ] ; Object . keys ( paths ) . forEach ( ( packageName ) => { const apiDocPath = paths [ packageName ] ; const targetPath = pathUtil . join ( targetDocPath , API_DOC_DOCSERVER_PATH , packageName ) ; ensureDirSync ( pathUtil . dirname ( targetPath ) ) ; promises . push ( copier ( apiDocPath , targetPath ) ) ; } ) ; return Promise . all ( promises ) ; } ; /**
 * Copies API docs to doc server.
 * Generates index page and navigation bar for API docs.
 */ const buildApiDoc = async ( props ) => { if ( ! isEnabled ( ) ) { console . warn ( 'API doc build is disabled. Set BODILESS_DOCS_API_ENABLED environment variable to "1" to enable it' ) ; } const { copier , targetDocPath } = props ; const paths = await findApiDocPaths ( ) ; const paths$1 = paths . reduce ( ( prevVal , curVal ) => ( { ... prevVal , ... curVal } ) , { } ) ; await copyApiDocs ( { copier , targetDocPath , paths : paths$1 } ) ; const APINavBarContent = Object . keys ( paths$1 ) . map ( pkg => `* [${ pkg }](${ API_DOC_DOCSERVER_PATH }/${ pkg }/globals)` ) . join ( '\n' ) ; const sideBarPath = pathUtil . join ( targetDocPath , API_DOC_DOCSERVER_PATH , '_sidebar.md' ) ; fs . writeFileSync ( sideBarPath , APINavBarContent ) ; const indexFilePath = pathUtil . join ( targetDocPath , API_DOC_DOCSERVER_PATH , 'README.md' ) ; fs . writeFileSync ( indexFilePath , APINavBarContent ) ; } ; export default buildApiDoc ; export { updateNavigation } ;