import * as fs from 'fs' ; import mkdirp from 'mkdirp' ; import * as path from 'path' ; namespace FileUtil { param filePath export const unlink = ( filePath ) : Promise < void > => { return new Promise < void > ( ( reslove , reject ) => { fs . unlink ( filePath , err => { if ( err ) { reject ( err ) ; } else { reslove ( ) ; } } ) ; } ) ; } ; param filePath param mode export const access = ( filePath , mode ) : Promise < void > => { return new Promise < void > ( ( reslove , reject ) => { fs . access ( filePath , mode , err => { if ( err ) { reject ( err ) ; } else { reslove ( ) ; } } ) ; } ) ; } ; param dirPath export const mkdir = async ( dirPath ) : Promise < void > => { await mkdirp ( dirPath ) ; } ; param filePath return export const stat = ( filePath ) : Promise < fs . Stats > => { return new Promise < fs . Stats > ( ( reslove , reject ) => { fs . stat ( filePath , ( err , stats ) => { if ( err ) { reject ( err ) ; } else { reslove ( stats ) ; } } ) ; } ) ; } ; param filePath return throws export const getFileSize = async ( filePath ) : Promise < number > => { try { return ( await FileUtil . stat ( filePath ) ) . size ; } catch ( err ) { throw new Error ( 'FileIsNotFound' ) ; } } ; param dirPath return export const readDir = async ( dirPath ) : Promise < string [ ] > => { return new Promise ( ( resolve , reject ) => { fs . readdir ( dirPath , ( err , files ) => { if ( err ) { reject ( err ) ; } else { resolve ( files ) ; } } ) ; } ) ; } ; param filePath return export const readFile = async ( filePath ) : Promise < string > => { return new Promise ( ( resolve , reject ) => { fs . readFile ( filePath , 'utf-8' , ( err , data ) => { if ( err ) { reject ( err ) ; } else { resolve ( data ) ; } } ) ; } ) ; } ; param filePath param data return export const writeFile = async ( filePath , data ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { fs . writeFile ( filePath , data , err => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ; param src param dest return export const rename = ( src , dest ) : Promise < void > => { return new Promise < void > ( ( reslove , reject ) => { fs . rename ( src , dest , async ( err ) => { if ( err ) { await FileUtil . unlink ( dest ) . catch ( ( ) => { } ) ; reject ( err ) ; } else { reslove ( ) ; } } ) ; } ) ; } ; param src param dest return export const copyFile = ( src , dest ) : Promise < void > => { return new Promise < void > ( ( resolve , reject ) => { fs . copyFile ( src , dest , err => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ; param src param dest return export const move = async ( src , dest ) : Promise < void > => { try { await FileUtil . copyFile ( src , dest ) ; } catch ( err ) { await FileUtil . unlink ( dest ) . catch ( ( ) => { } ) ; throw err ; } await FileUtil . unlink ( src ) ; } ; param file return export const touchFile = ( file ) : Promise < void > => { return new Promise < void > ( ( resolve , reject ) => { fs . writeFile ( file , '' , err => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ; param file param str return export const appendFile = ( file , str ) : Promise < void > => { return new Promise < void > ( ( resolve , reject ) => { fs . appendFile ( file , str , err => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ; /**
     * FileList 定義
     */ export interface FileList { files : string [ ] ; directories : string [ ] ; } return export const getFileList = ( fileDir ) : Promise < FileUtil . FileList > => { return new Promise < FileUtil . FileList > ( ( resolve , reject ) => { fs . readdir ( fileDir , async ( err , files ) => { if ( err ) { reject ( err ) ; } else { const results = { files : [ ] , directories : [ ] , } ; for ( const file of files ) { if ( file . slice ( 0 , 1 ) === '.' ) { continue ; } const filePath = path . join ( fileDir , file ) ; if ( fs . statSync ( filePath ) . isDirectory ( ) ) { results . directories . push ( filePath ) ; try { const subFiles = await FileUtil . getFileList ( filePath ) ; Array . prototype . push . apply ( results . files , subFiles . files ) ; Array . prototype . push . apply ( results . directories , subFiles . directories ) ; } catch ( err ) { } } else { results . files . push ( filePath ) ; } } resolve ( results ) ; } } ) ; } ) ; } ; param dir return export const isEmptyDirectory = ( dir ) : Promise < boolean > => { return new Promise < boolean > ( ( resolve , reject ) => { fs . readdir ( dir , ( err , files ) => { if ( err ) { reject ( err ) ; } else { resolve ( files . length === 0 ) ; } } ) ; } ) ; } ; param dir return export const rmdir = ( dir ) : Promise < void > => { return new Promise < void > ( ( resolve , reject ) => { fs . rmdir ( dir , err => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ; } export default FileUtil ;