3ed59a7b20
Check dist / check-dist (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Licensed / Check licenses (push) Has been cancelled
Build and Test / build (push) Has been cancelled
Build and Test / test (macos-latest) (push) Has been cancelled
Build and Test / test (ubuntu-latest) (push) Has been cancelled
Build and Test / test (windows-latest) (push) Has been cancelled
Build and Test / test-proxy (push) Has been cancelled
Build and Test / test-bypass-proxy (push) Has been cancelled
Build and Test / test-git-container (push) Has been cancelled
Build and Test / test-output (push) Has been cancelled
84 lines
1.6 KiB
TypeScript
84 lines
1.6 KiB
TypeScript
import * as fs from 'fs'
|
|
|
|
export function directoryExistsSync(path: string, required?: boolean): boolean {
|
|
if (!path) {
|
|
throw new Error("Arg 'path' must not be empty")
|
|
}
|
|
|
|
let stats: fs.Stats
|
|
try {
|
|
stats = fs.statSync(path)
|
|
} catch (error) {
|
|
if ((error as any)?.code === 'ENOENT') {
|
|
if (!required) {
|
|
return false
|
|
}
|
|
|
|
throw new Error(`Directory '${path}' does not exist`)
|
|
}
|
|
|
|
throw new Error(
|
|
`Encountered an error when checking whether path '${path}' exists: ${
|
|
(error as any)?.message ?? error
|
|
}`
|
|
)
|
|
}
|
|
|
|
if (stats.isDirectory()) {
|
|
return true
|
|
} else if (!required) {
|
|
return false
|
|
}
|
|
|
|
throw new Error(`Directory '${path}' does not exist`)
|
|
}
|
|
|
|
export function existsSync(path: string): boolean {
|
|
if (!path) {
|
|
throw new Error("Arg 'path' must not be empty")
|
|
}
|
|
|
|
try {
|
|
fs.statSync(path)
|
|
} catch (error) {
|
|
if ((error as any)?.code === 'ENOENT') {
|
|
return false
|
|
}
|
|
|
|
throw new Error(
|
|
`Encountered an error when checking whether path '${path}' exists: ${
|
|
(error as any)?.message ?? error
|
|
}`
|
|
)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
export function fileExistsSync(path: string): boolean {
|
|
if (!path) {
|
|
throw new Error("Arg 'path' must not be empty")
|
|
}
|
|
|
|
let stats: fs.Stats
|
|
try {
|
|
stats = fs.statSync(path)
|
|
} catch (error) {
|
|
if ((error as any)?.code === 'ENOENT') {
|
|
return false
|
|
}
|
|
|
|
throw new Error(
|
|
`Encountered an error when checking whether path '${path}' exists: ${
|
|
(error as any)?.message ?? error
|
|
}`
|
|
)
|
|
}
|
|
|
|
if (!stats.isDirectory()) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|