/**
 * Copyright © 2019 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 flow from 'lodash/flow' ; import fs from 'fs-extra' ; import locateFiles from './locateFiles' ; import { withTreeFromFile , getSimplePaths , validatePaths } from './tree' ; import { writeTree , writeResources , copyFile , symlinkFile , } from './write' ; import { writeSideBars } from './createBar' ; import { Tree } from './type' ; import readSettings from './readSettings' ; import buildApiDoc , { updateNavigation as apiDocUpdateNavigation } from './blApiDocsBuild' ; const buildSubTree = async ( toc , namespace ) => { const updates = await locateFiles ( { filePattern : new RegExp ( `${ namespace }.docs.json$` ) , startingRoot : './' , action : withTreeFromFile , } ) ; const paths = flow ( updates ) ( toc ) as Tree ; return paths ; } ; const blDocsBuild = async ( ) => { const copier = process . env . BODILESS_DOCS_COPYFILES ? copyFile : symlinkFile ; const docPath = './doc' ; const { toc } = readSettings ( ) ; console . log ( 'Building documentation tree' ) ; const nameSpaces = Object . getOwnPropertyNames ( toc ) ; const buildPromises = nameSpaces . map ( ns => buildSubTree ( toc [ ns ] , ns ) ) ; const pathsList = await Promise . all ( [ ... buildPromises , fs . emptyDir ( docPath ) as any as Promise < Tree > , ] ) ; const paths = nameSpaces . reduce ( ( acc , nameSpace , i ) => ( i === 0 ? acc : { ... acc , [ nameSpace ] : pathsList [ i ] } ) , pathsList [ 0 ] ) ; try { validatePaths ( getSimplePaths ( paths ) ) ; } catch ( error ) { console . warn ( 'Error validating paths' , error ) ; } console . log ( 'Writing symlinks' ) ; try { await writeTree ( { paths , loc : docPath , } , copier ) ; } catch ( error ) { console . warn ( 'Error writing symlinks' , error ) ; } const navigationPaths = apiDocUpdateNavigation ( docPath , paths ) ; try { await writeSideBars ( docPath , navigationPaths ) ; } catch ( error ) { console . warn ( 'Error writing sidebars' , error ) ; } console . log ( 'Writing resources' ) ; try { await writeResources ( docPath , copier ) ; } catch ( error ) { console . warn ( 'Error writing resources' , error ) ; } console . log ( 'Building API docs' ) ; try { await buildApiDoc ( { targetDocPath : docPath , copier } ) ; } catch ( error ) { console . warn ( 'Error building API docs' , error ) ; } console . log ( 'Done' ) ; } ; export default blDocsBuild ;