import * as fs from "fs-plus" ; import * as path from "path" ; import * as sdk from "vscode-iot-device-cube-sdk" ; import * as crypto from "crypto" ; import { ScaffoldType } from "./constants" ; import extractzip = require ( "extract-zip" ) ; export class FileUtility { static async directoryExists ( type , dirPath ) { if ( type === ScaffoldType . Local ) { if ( ! ( await sdk . FileSystem . exists ( dirPath ) ) ) { return false ; } const isDirectory = await sdk . FileSystem . isDirectory ( dirPath ) ; return isDirectory ; } else { return new Promise ( ( resolve ) => { fs . stat ( dirPath , ( error , stats ) => { if ( error ) { resolve ( false ) ; return ; } resolve ( stats . isDirectory ( ) ) ; return ; } ) ; } ) ; } } static async fileExists ( type , filePath ) { if ( type === ScaffoldType . Local ) { const directoryExists = await sdk . FileSystem . exists ( filePath ) ; if ( ! directoryExists ) { return false ; } const isFile = await sdk . FileSystem . isFile ( filePath ) ; return isFile ; } else { return new Promise ( ( resolve ) => { fs . stat ( filePath , ( error , stats ) => { if ( error ) { resolve ( false ) ; return ; } resolve ( stats . isFile ( ) ) ; return ; } ) ; } ) ; } } static async mkdir ( type , dirPath ) { if ( type === ScaffoldType . Local ) { return await sdk . FileSystem . mkDir ( dirPath ) ; } else { return new Promise ( ( resolve , reject ) => { fs . mkdir ( dirPath , error => { if ( error ) { reject ( error ) ; return ; } resolve ( ) ; return ; } ) ; } ) ; } } static async mkdirRecursively ( type , dirPath ) { if ( await FileUtility . directoryExists ( type , dirPath ) ) { return ; } const dirname = path . dirname ( dirPath ) ; if ( path . normalize ( dirname ) === path . normalize ( dirPath ) ) { await FileUtility . mkdirRecursively ( type , dirname ) ; } else if ( await FileUtility . directoryExists ( type , dirname ) ) { await FileUtility . mkdir ( type , dirPath ) ; } else { await FileUtility . mkdirRecursively ( type , dirname ) ; await FileUtility . mkdir ( type , dirPath ) ; } } static async writeFile ( type , filePath , data ) { if ( type === ScaffoldType . Local ) { return await sdk . FileSystem . writeFile ( filePath , data ) ; } else { return new Promise ( ( resolve , reject ) => { fs . writeFile ( filePath , data , err => { if ( err ) { reject ( err ) ; return ; } resolve ( ) ; return ; } ) ; } ) ; } } param type param fileDestPath param data static async writeJsonFile ( type , fileDestPath , data ) { const indentationSpace = 4 ; const jsonString = JSON . stringify ( data , null , indentationSpace ) ; const fileDir = path . dirname ( fileDestPath ) ; if ( ! ( await FileUtility . directoryExists ( type , fileDir ) ) ) { await FileUtility . mkdirRecursively ( type , fileDir ) ; } await FileUtility . writeFile ( type , fileDestPath , jsonString ) ; } static async readFile ( type , filePath , encoding ? ) { if ( type === ScaffoldType . Local ) { return await sdk . FileSystem . readFile ( filePath , encoding ) ; } else { return new Promise ( ( resolve , reject ) => { fs . readFile ( filePath , encoding , ( err , data ) => { if ( err ) { reject ( err ) ; return ; } resolve ( data ) ; return ; } ) ; } ) ; } } static async extractZipFile ( sourceZip , targetFoder ) { return new Promise ( ( resolve , reject ) => { extractzip ( sourceZip , { dir : targetFoder } , err => { if ( err ) { return reject ( err ) ; } else { return resolve ( true ) ; } } ) ; } ) ; } static async getFileHash ( filename , algorithm = "md5" ) { const hash = crypto . createHash ( algorithm ) ; const input = fs . createReadStream ( filename ) ; let hashvalue = "" ; return new Promise ( ( resolve , reject ) => { input . on ( "readable" , ( ) => { const data = input . read ( ) ; if ( data ) { hash . update ( data ) ; } } ) ; input . on ( "error" , reject ) ; input . on ( "end" , ( ) => { hashvalue = hash . digest ( "hex" ) ; return resolve ( hashvalue ) ; } ) ; } ) ; } }