All files / src/run startServerFromConfig.ts

100% Statements 20/20
100% Branches 4/4
100% Functions 3/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 393x 3x     3x   3x 1x   1x 1x       3x 3x   3x 1x 1x     2x   2x   2x   2x 1x 1x     1x          
import * as fs from 'fs'
import * as path from 'path'
import { ValidationError } from 'jsonschema'
 
import { consoleError, runServer, validateConfig } from '../utils'
 
const outputConfigErrors = (errors: ValidationError[]): void => {
  consoleError('Invalid configuration file:')
 
  errors.forEach(error => {
    console.log(`- ${error.message}`)
  })
}
 
export const startServerFromConfig = (configFilePath: string): void => {
  const configFileExists = fs.existsSync(configFilePath)
 
  if (!configFileExists) {
    consoleError(`The given configuration file ${configFilePath} doesn't exist!`)
    return
  }
 
  const configFileContent = fs.readFileSync(configFilePath, 'utf8')
 
  const config = JSON.parse(configFileContent)
 
  const validatedConfig = validateConfig(config)
 
  if (!validatedConfig.valid) {
    outputConfigErrors(validatedConfig.errors)
    return
  }
 
  runServer({
    ...config,
    rootDir: path.join(path.dirname(configFilePath), config.rootDir)
  })
}