mirror of
https://code.forgejo.org/actions/checkout.git
synced 2025-04-29 06:32:57 +02:00
feat: improve typescript type assertion
This commit is contained in:
parent
47fbe2df0a
commit
828051d3b2
2 changed files with 70 additions and 28 deletions
|
@ -1,5 +1,11 @@
|
|||
import * as fs from 'fs'
|
||||
|
||||
function isErrorObject(
|
||||
error: unknown
|
||||
): error is {code?: string; message?: string} {
|
||||
return typeof error === 'object' && error !== null
|
||||
}
|
||||
|
||||
export function directoryExistsSync(path: string, required?: boolean): boolean {
|
||||
if (!path) {
|
||||
throw new Error("Arg 'path' must not be empty")
|
||||
|
@ -9,17 +15,24 @@ export function directoryExistsSync(path: string, required?: boolean): boolean {
|
|||
try {
|
||||
stats = fs.statSync(path)
|
||||
} catch (error) {
|
||||
if ((error as any)?.code === 'ENOENT') {
|
||||
if (!required) {
|
||||
return false
|
||||
if (isErrorObject(error)) {
|
||||
if (error.code === 'ENOENT') {
|
||||
if (!required) {
|
||||
return false
|
||||
}
|
||||
|
||||
throw new Error(`Directory '${path}' does not exist`)
|
||||
}
|
||||
|
||||
throw new Error(`Directory '${path}' does not exist`)
|
||||
if (error.message) {
|
||||
throw new Error(
|
||||
`Encountered an error when checking whether path '${path}' exists: ${error.message}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Encountered an error when checking whether path '${path}' exists: ${(error as any)
|
||||
?.message ?? error}`
|
||||
`Encountered an error when checking whether path '${path}' exists: ${error}`
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -40,13 +53,20 @@ export function existsSync(path: string): boolean {
|
|||
try {
|
||||
fs.statSync(path)
|
||||
} catch (error) {
|
||||
if ((error as any)?.code === 'ENOENT') {
|
||||
return false
|
||||
if (isErrorObject(error)) {
|
||||
if (error.code === 'ENOENT') {
|
||||
return false
|
||||
}
|
||||
|
||||
if (error.message) {
|
||||
throw new Error(
|
||||
`Encountered an error when checking whether path '${path}' exists: ${error.message}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Encountered an error when checking whether path '${path}' exists: ${(error as any)
|
||||
?.message ?? error}`
|
||||
`Encountered an error when checking whether path '${path}' exists: ${error}`
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -62,13 +82,20 @@ export function fileExistsSync(path: string): boolean {
|
|||
try {
|
||||
stats = fs.statSync(path)
|
||||
} catch (error) {
|
||||
if ((error as any)?.code === 'ENOENT') {
|
||||
return false
|
||||
if (isErrorObject(error)) {
|
||||
if (error.code === 'ENOENT') {
|
||||
return false
|
||||
}
|
||||
|
||||
if (error.message) {
|
||||
throw new Error(
|
||||
`Encountered an error when checking whether path '${path}' exists: ${error.message}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Encountered an error when checking whether path '${path}' exists: ${(error as any)
|
||||
?.message ?? error}`
|
||||
`Encountered an error when checking whether path '${path}' exists: ${error}`
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue