Skip to content

Commit

Permalink
refactor: try catch for the whole function
Browse files Browse the repository at this point in the history
  • Loading branch information
Renato66 committed Jun 25, 2024
1 parent 73d67e5 commit 5abd9ac
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/domain/getLabelConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@ import JSON5 from 'json5'
const jsonTypes = ['json', 'jsonc', 'json5']

const getFilePath = (configurationPath: string): string | undefined => {
const repoPath = `./${configurationPath}`
.replace('//', '/')
.replace('././', './')
if (configurationPath.includes('.json') && fs.existsSync(repoPath))
return repoPath
if (!configurationPath.includes('.json')) {
let allFiles
try {
allFiles = fs.readdirSync(repoPath)
} catch (error: any) {
core.warning(
`Could not read configuration file at ${repoPath}: ${error.message}. Skipping.`
try {
const repoPath = `./${configurationPath}`
.replace('//', '/')
.replace('././', './')
if (configurationPath.includes('.json') && fs.existsSync(repoPath))
return repoPath
if (!configurationPath.includes('.json')) {
const allFiles = fs.readdirSync(repoPath)

const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`)
const files = allFiles.filter((filename) =>
expectedFilenames.includes(filename)
)
return
if (!files.length) {
throw new Error('No default files located.')
}
return `${repoPath}/${files[0]}`.replace('//', '/')
}
const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`)
const files = allFiles.filter((filename) =>
expectedFilenames.includes(filename)
} catch (error: any) {
core.warning(
`Could not read configuration file, configurationPath: "${configurationPath}", error: "${error.message}". Skipping.`
)
if (!files.length) return
return `${repoPath}/${files[0]}`.replace('//', '/')
return
}
}

Expand Down

0 comments on commit 5abd9ac

Please sign in to comment.