65 lines
1014 B
JavaScript
65 lines
1014 B
JavaScript
|
const { stat } = require("node:fs");
|
||
|
const fs = require( "node:fs/promises" );
|
||
|
|
||
|
async function exists( filePath )
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
let stats = await fs.stat( filePath );
|
||
|
let exists = stats !== undefined && stats !== null;
|
||
|
return Promise.resolve( exists );
|
||
|
}
|
||
|
catch( e )
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
return Promise.resolve( false );
|
||
|
}
|
||
|
|
||
|
async function loadUTF8( filePath )
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
let data = await fs.readFile( filePath );
|
||
|
let stringData = data.toString();
|
||
|
return Promise.resolve( stringData );
|
||
|
}
|
||
|
catch ( exception )
|
||
|
{
|
||
|
console.warn( exception );
|
||
|
}
|
||
|
|
||
|
return Promise.resolve( null );
|
||
|
|
||
|
}
|
||
|
|
||
|
async function loadJSON( filePath )
|
||
|
{
|
||
|
let text = await loadUTF8( filePath );
|
||
|
|
||
|
if ( text === null )
|
||
|
{
|
||
|
return Promise.resolve( null );
|
||
|
}
|
||
|
|
||
|
try
|
||
|
{
|
||
|
let jsonObject = JSON.parse( text );
|
||
|
|
||
|
return Promise.resolve( jsonObject );
|
||
|
}
|
||
|
catch ( exception )
|
||
|
{
|
||
|
console.warn( exception );
|
||
|
}
|
||
|
|
||
|
return Promise.resolve( null );
|
||
|
}
|
||
|
|
||
|
module.exports =
|
||
|
{
|
||
|
exists,
|
||
|
loadUTF8,
|
||
|
loadJSON
|
||
|
}
|