All files file.ts

71.43% Statements 10/14
85.71% Branches 6/7
66.67% Functions 2/3
71.43% Lines 10/14
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  2x 2x     6x     5x 5x   1x   1x     2x   1x                         2x
import * as fs from 'fs'
 
export default class StackOutputFile {
  constructor (private path: string) { }
 
  public format (data: {}) {
    const ext = this.path.split('.').pop() || ''
 
    switch (ext.toUpperCase()) {
      case 'JSON':
        return JSON.stringify(data, null, 2)
      case 'TOML':
        return require('tomlify-j0.4')(data, null, 0)
      case 'YAML':
      case 'YML':
        return require('yamljs').stringify(data)
      default:
        throw new Error('No formatter found for `' + ext + '` extension')
    }
  }
 
  public save (data: {}) {
    const content = this.format(data)

    try {
      fs.writeFileSync(this.path, content)
    } catch (e) {
      throw new Error('Cannot write to file: ' + this.path)
    }
  }
}