import { join } from 'path' ; import * as fs from 'fs-extra' ; import simpleGit from 'simple-git' ; import { TMP , GITBOOK_SOURCE , GITBOOK_REVISION , DESTINATION , ASSETS_DIR } from './constants' ; import { Parser } from './parser' ; import { transform } from './transformer' ; /** Ensures the given directory exists and prunes its contents. */ const pruneDirectory = ( path ) => { fs . removeSync ( path ) ; fs . mkdirpSync ( path ) ; } ; /**
 * Clears the @param destination directory and then populates it with
 * content of the @param repository checked out at @param revision commit.
 */ const fetchSource = async ( repository , revision , destination ) => { pruneDirectory ( TMP ) ; const git = simpleGit ( { baseDir : destination } ) ; await git . clone ( repository , '.' ) ; await git . checkout ( revision ) ; } ; const main = async ( ) => { await fetchSource ( GITBOOK_SOURCE , GITBOOK_REVISION , TMP ) ; const parser = new Parser ( TMP ) ; const index = parser . parse ( ) ; pruneDirectory ( DESTINATION ) ; transform ( index , TMP , DESTINATION ) ; fs . writeJSONSync ( join ( DESTINATION , 'index.json' ) , index ) ; fs . copySync ( join ( TMP , ASSETS_DIR ) , join ( DESTINATION , ASSETS_DIR ) ) ; } ; main ( ) ;