mirror of
https://github.com/IT4Change/gradido.git
synced 2026-03-01 12:44:43 +00:00
31 lines
833 B
TypeScript
31 lines
833 B
TypeScript
import fs from 'node:fs'
|
|
import { getLogger } from 'log4js'
|
|
import { LOG4JS_BASE_CATEGORY } from '../config/const'
|
|
|
|
const logger = getLogger(`${LOG4JS_BASE_CATEGORY}.utils.filesystem`)
|
|
|
|
export function checkFileExist(filePath: string): boolean {
|
|
try {
|
|
fs.accessSync(filePath, fs.constants.R_OK | fs.constants.W_OK)
|
|
return true
|
|
} catch (_err) {
|
|
// logger.debug(`file ${filePath} does not exist: ${_err}`)
|
|
return false
|
|
}
|
|
}
|
|
|
|
export function checkPathExist(path: string, createIfMissing: boolean = false): boolean {
|
|
const exists = checkFileExist(path)
|
|
if (exists) {
|
|
return true
|
|
}
|
|
if (createIfMissing) {
|
|
logger.info(`create folder ${path}`)
|
|
fs.mkdirSync(path, { recursive: true })
|
|
if (!checkPathExist(path)) {
|
|
throw new Error(`Failed to create path ${path}`)
|
|
}
|
|
}
|
|
return false
|
|
}
|