import { defaultConfig } from './config.default' ; import File from './file' ; import Log from './log' ; import { Stats } from 'fs' ; export const checkSource = async ( path = '' ) => { if ( ! await File . exists ( path ) ) { Log . sourceError ( path ) ; process . exit ( 1 ) ; } } ; export const checkConfig = async ( path = '' ) : Promise < any > => { const configPath = `${ path }/paper.config.json` ; if ( ! await File . exists ( configPath ) ) return { } ; const config = await File . readFile ( `${ path }/paper.config.json` ) ; let result ; try { result = JSON . parse ( config . toString ( ) || '{}' ) ; } catch ( e ) { Log . configInvalid ( ) ; } return result ; } ; export const checkTheme = async ( config ) : Promise < boolean > => { const theme = config . theme || 'default' ; const p = `${ __dirname }/../../node_modules/simpler-paper-themes/dist/${ theme }.css` ; if ( ! await File . exists ( p ) ) { Log . notFoundTheme ( theme ) ; process . exit ( 1 ) ; return false ; } return true ; } ; export const findSource = async ( userPath ) : Promise < string > => { const files = await File . readdir ( userPath ) ; const directories = [ ] ; for ( const f of files ) { const stat = await File . stat ( `${ userPath }/${ f }` ) ; if ( stat . isFile ( ) ) continue ; if ( await File . exists ( `${ userPath }/${ f }/paper.config.json` ) ) { directories . push ( f ) ; } } if ( directories . length > 1 ) { Log . configNonUnique ( directories ) ; process . exit ( 1 ) ; } if ( directories . length === 0 ) { Log . configNotFound ( ) ; process . exit ( 1 ) ; } return directories [ 0 ] ; } ; export const assignConfig = async ( source ) : Promise < Config > => { await checkSource ( source ) ; const userConfig = await checkConfig ( source ) ; const config = Object . assign ( { } , defaultConfig , userConfig , { __user_source_path : source } ) ; return config ; } ;