import crypto = require ( 'crypto' ) ; import fs = require ( 'fs' ) ; import path = require ( 'path' ) ; import shell = require ( 'shelljs' ) ; export class FileWriter { public static createFolder ( folderName ) { const actualFolder = folderName ; if ( actualFolder . match ( /^~/ ) ) { actualFolder . replace ( '~' , FileWriter . homedir ) ; } if ( ! fs . existsSync ( actualFolder ) ) { shell . mkdir ( '-p' , actualFolder ) ; } return actualFolder ; } public static writeFile ( folderName , fileName , text ) { const actualFolder = FileWriter . createFolder ( folderName ) ; const completeFile = path . join ( actualFolder , fileName ) ; fs . writeFileSync ( completeFile , text ) ; } public static createTempFolder ( ) { let tempFolder = 'tmp' ; while ( fs . existsSync ( tempFolder ) ) { tempFolder = tempFolder + crypto . randomBytes ( 16 ) . toString ( 'hex' ) ; } shell . mkdir ( '-p' , tempFolder ) ; return tempFolder ; } private static homedir = require ( 'os' ) . homedir ( ) ; }