import { readFileSync , writeFileSync , existsSync , mkdirSync , readdirSync , statSync , lstatSync , unlinkSync , rmdirSync } from 'fs' ; import { dirname , join } from 'path' ; import { compile } from 'handlebars' ; import * as moment from 'moment' ; import * as chalk from 'chalk' ; import { kebabCase , find , sortBy , toPairs , fromPairs } from 'lodash' ; import { SwaggerSchema } from '../bootstrap/swagger' ; import { GeneratorOptions } from '../bootstrap/options' ; export const ENCODING = 'utf8' ; export function readAndCompileTemplateFile ( templatePath ) { let templateSource = readFileSync ( templatePath , ENCODING ) ; let template = compile ( templateSource ) ; return template ; } function readFile ( outputFileName ) { let file = readFileSync ( outputFileName , ENCODING ) ; return file ; } function writeFile ( outputFileName , contents ) { writeFileSync ( outputFileName , contents , { flag : 'w' , encoding : ENCODING } ) ; } export function writeFileIfContentsIsChanged ( outputFileName , contents ) { let isChanged = true ; if ( existsSync ( outputFileName ) ) { let oldContents = readFile ( outputFileName ) ; isChanged = oldContents !== contents ; } if ( isChanged ) { writeFile ( outputFileName , contents ) ; } return isChanged ; } export function ensureFile ( outputFileName , contents ) { ensureFolder ( dirname ( outputFileName ) ) ; if ( ! existsSync ( outputFileName ) ) { writeFileSync ( outputFileName , contents , ENCODING ) ; } } export function ensureFolder ( folder ) { if ( ! existsSync ( folder ) ) { mkdirSync ( folder ) ; } } export function getDirectories ( srcpath ) { return readdirSync ( srcpath ) . filter ( file => { return statSync ( join ( srcpath , file ) ) . isDirectory ( ) ; } ) ; } export function getFiles ( srcpath ) { return readdirSync ( srcpath ) . filter ( file => { return statSync ( join ( srcpath , file ) ) . isFile ( ) ; } ) ; } export function removeFolder ( folder ) { if ( existsSync ( folder ) ) { readdirSync ( folder ) . forEach ( ( file , index ) => { let curPath = folder + '/' + file ; if ( lstatSync ( curPath ) . isDirectory ( ) ) { removeFolder ( curPath ) ; } else { unlinkSync ( curPath ) ; } } ) ; rmdirSync ( folder ) ; } } export function getPathToRoot ( namespace ) { let path = './' ; if ( namespace ) { path = '' ; let namespaceLength = namespace . split ( '.' ) . length ; for ( let i = 0 ; i < namespaceLength ; ++ i ) { path += '../' ; } } return path ; } export function convertNamespaceToPath ( namespace ) { let parts = namespace . split ( '.' ) ; for ( let index = 0 ; index < parts . length ; index ++ ) { parts [ index ] = kebabCase ( parts [ index ] ) ; } let result = parts . join ( '/' ) ; return result ; } export function getSortedObjectProperties ( object ) { const pairs = sortBy ( toPairs ( object ) , 0 ) ; const result = fromPairs ( pairs as [ string , { } ] [ ] ) ; return result ; } export function isInTypesToFilter ( item , key , options ) { if ( options && options . typesToFilter ) { const result = ! ! find ( options . typesToFilter , element => { return element === key ; } ) ; return result ; } return false ; } export function removeExtension ( file ) { return file . replace ( '.ts' , '' ) ; } export function log ( message ) { let time = moment ( ) . format ( 'HH:mm:SS' ) ; console . log ( `[${ time }] ${ message }` ) ; } export function logError ( message ) { let time = moment ( ) . format ( 'HH:mm:SS' ) ; console . error ( `[${ time }]` , chalk . red ( `*** ERROR *** ${ message }` ) ) ; }