import path from "path" ; import ASSET_FOLDER from "../../constants/pluginAssetsFolder" ; import Promise from "../../utils/promise" ; import fs from "../../utils/fs" ; param { Output } return { Promise } function copyPluginAssets ( output ) { const book = output . getBook ( ) ; if ( book . isLanguageBook ( ) ) { return Promise ( output ) ; } const plugins = output . getPlugins ( ) . reverse ( ) ; return Promise . forEach ( plugins , ( plugin ) => { return copyAssets ( output , plugin ) . then ( ( ) => { return copyResources ( output , plugin ) ; } ) ; } ) . thenResolve ( output ) ; } param { Plugin } return { Promise } function copyAssets ( output , plugin ) { const logger = output . getLogger ( ) ; const pluginRoot = plugin . getPath ( ) ; const options = output . getOptions ( ) ; const outputRoot = options . get ( "root" ) ; const assetOutputFolder = path . join ( outputRoot , "gitbook" ) ; const prefix = options . get ( "prefix" ) ; const assetFolder = path . join ( pluginRoot , ASSET_FOLDER , prefix ) ; if ( ! fs . existsSync ( assetFolder ) ) { return Promise ( ) ; } logger . debug . ln ( "copy assets from theme" , assetFolder ) ; return fs . copyDir ( assetFolder , assetOutputFolder , { deleteFirst : false , overwrite : true , confirm : false , } ) ; } param { Plugin } return { Promise } function copyResources ( output , plugin ) { const logger = output . getLogger ( ) ; const options = output . getOptions ( ) ; const outputRoot = options . get ( "root" ) ; const state = output . getState ( ) ; const resources = state . getResources ( ) ; const pluginRoot = plugin . getPath ( ) ; const pluginResources = resources . get ( plugin . getName ( ) ) ; let assetsFolder = pluginResources . get ( "assets" ) ; const assetOutputFolder = path . join ( outputRoot , "gitbook" , plugin . getNpmID ( ) ) ; if ( ! assetsFolder ) { return Promise ( ) ; } assetsFolder = path . resolve ( pluginRoot , assetsFolder ) ; if ( ! fs . existsSync ( assetsFolder ) ) { logger . warn . ln ( `assets folder for plugin "${ plugin . getName ( ) }" doesn't exist` ) ; return Promise ( ) ; } logger . debug . ln ( "copy resources from plugin" , assetsFolder ) ; return fs . copyDir ( assetsFolder , assetOutputFolder , { deleteFirst : false , overwrite : true , confirm : false , } ) ; } export default copyPluginAssets ;