import path from "path" ; import Promise from "../utils/promise" ; import generatePage from "./generatePage" ; import { getCache } from "./page-cache" ; import Page from "../models/page" ; param { Generator } generator param { Output } output return { Promise < Output > } function generatePages ( generator , output ) { const cache = getCache ( ) ; const pages = output . getPages ( ) ; const logger = output . getLogger ( ) ; if ( ! generator . onPage ) { return Promise ( output ) ; } const root = output . book . getContentRoot ( ) ; const isIncrementBuilding = output . incrementalChangeFileSet . size !== 0 ; const pageList = pages . map ( ( page ) => { const file = page . getFile ( ) ; const absoluteFilePath = path . join ( root , file . getPath ( ) ) ; if ( isIncrementBuilding && ! output . incrementalChangeFileSet . has ( absoluteFilePath ) ) { logger . debug . ln ( `skip generate page "${ file . getPath ( ) }"` ) ; return ; } return generatePage ( output , page ) . then ( ( resultPage ) => { return generator . onPage ( output , resultPage ) ; } ) . fail ( ( err ) => { logger . error . ln ( `error while generating page "${ file . getPath ( ) }":` ) ; throw err ; } ) ; } ) ; return Promise . all ( pageList . toArray ( ) ) . then ( ( ) => { cache . save ( ) ; return output ; } ) ; } export default generatePages ;