Files
release-changelog-builder-a…/src/utils.ts
T
Mike Penz 3fd4677f8a - introduce new capability to enforce a build failure
- introduce config to fail on error instead of just logging
- simplify main logic, move functionality in other classes
2020-10-18 11:06:02 +02:00

114 lines
3.0 KiB
TypeScript

import * as fs from 'fs'
import {Configuration, DefaultConfiguration} from './configuration'
import * as core from '@actions/core'
import * as path from 'path'
/**
* Resolves the repository path, relatively to the GITHUB_WORKSPACE
*/
export function retrieveRepositoryPath(providedPath: string): string {
let githubWorkspacePath = process.env['GITHUB_WORKSPACE']
if (!githubWorkspacePath) {
throw new Error('GITHUB_WORKSPACE not defined')
}
githubWorkspacePath = path.resolve(githubWorkspacePath)
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
let repositoryPath = providedPath || '.'
repositoryPath = path.resolve(githubWorkspacePath, repositoryPath)
core.debug(`repositoryPath = '${repositoryPath}'`)
return repositoryPath
}
/**
* Will automatically either report the message to the log, or mark the action as failed. Additionally defining the output failed, allowing it to be read in by other actions
*/
export function failOrError(
message: string | Error,
failOnError: boolean
): void {
// if we report any failure, consider the action to have failed, may not make the build fail
core.setOutput('failed', true)
if (failOnError) {
core.setFailed(message)
} else {
core.error(message)
}
}
/**
* Retrieves the configuration given the file path, if not found it will fallback to the `DefaultConfiguration`
*/
export function resolveConfiguration(
githubWorkspacePath: string,
configurationFile: string
): Configuration {
let configuration = DefaultConfiguration
if (configurationFile) {
const configurationPath = path.resolve(
githubWorkspacePath,
configurationFile
)
core.debug(`configurationPath = '${configurationPath}'`)
const providedConfiguration = readConfiguration(configurationPath)
if (!providedConfiguration) {
core.info(
`⚠️ Configuration provided, but it couldn't be found, or failed to parse. Fallback to Defaults`
)
} else {
configuration = providedConfiguration
}
}
return configuration
}
/**
* Reads in the configuration from the JSON file
*/
function readConfiguration(filename: string): Configuration | null {
try {
const rawdata = fs.readFileSync(filename, 'utf8')
const configurationJSON: Configuration = JSON.parse(rawdata)
return configurationJSON
} catch (error) {
return null
}
}
/**
* Checks if a given directory exists
*/
export function directoryExistsSync(
inputPath: string,
required?: boolean
): boolean {
if (!inputPath) {
throw new Error("Arg 'path' must not be empty")
}
let stats: fs.Stats
try {
stats = fs.statSync(inputPath)
} catch (error) {
if (error.code === 'ENOENT') {
if (!required) {
return false
}
throw new Error(`Directory '${inputPath}' does not exist`)
}
throw new Error(
`Encountered an error when checking whether path '${inputPath}' exists: ${error.message}`
)
}
if (stats.isDirectory()) {
return true
} else if (!required) {
return false
}
throw new Error(`Directory '${inputPath}' does not exist`)
}