rokojori-godot/rokojori-cpp-generator/source/library/Files.ts

115 lines
2.1 KiB
TypeScript

import { promises as fs } from "fs";
import { RJLog } from "./RJLog";
export class Files
{
static async getFiles( filePath:string )
{
return await fs.readdir( filePath );
}
static async copy( from:string, to:string )
{
return await fs.copyFile( from, to );
}
static async getFilesRecursive( filePath:string )
{
let files = await fs.readdir( filePath, { recursive: true } );
return files;
}
static async delete( filePath:string )
{
await fs.unlink( filePath );
return Promise.resolve();
}
static async isDirectory( filePath:string )
{
let stats = await fs.stat( filePath );
return stats.isDirectory();
}
static async exists( filePath:string )
{
try
{
let stats = await fs.stat( filePath );
let exists = stats !== undefined && stats !== null;
return Promise.resolve( exists );
}
catch( e )
{
}
return Promise.resolve( false );
}
static async loadUTF8( filePath:string ):Promise<string>
{
try
{
let data = await fs.readFile( filePath );
let stringData = data.toString();
return Promise.resolve( stringData );
}
catch ( exception )
{
RJLog.warn( "loadUTF8: ", filePath );
RJLog.warn( exception );
}
return Promise.resolve( null );
}
static async saveUTF8( filePath:string, data:string )
{
try
{
await fs.writeFile( filePath, data );
return Promise.resolve( true );
}
catch ( exception )
{
RJLog.warn( "saveUTF8: ", filePath );
RJLog.warn( exception );
}
return Promise.resolve( false );
}
static async loadJSON<T>( filePath:string ):Promise<T>
{
let text = await this.loadUTF8( filePath );
if ( text === null )
{
return Promise.resolve( null ) as T;
}
try
{
let jsonObject = JSON.parse( text );
return Promise.resolve( jsonObject );
}
catch ( exception )
{
RJLog.warn( "LoadJSON: ", filePath );
RJLog.warn( exception );
}
return Promise.resolve( null );
}
}