github-desktop/script/validate-changelog.ts

81 lines
1.9 KiB
TypeScript
Raw Normal View History

#!/usr/bin/env ts-node
import * as Path from 'path'
import * as Fs from 'fs'
import Ajv, { ErrorObject } from 'ajv'
2018-03-27 21:01:23 +00:00
function handleError(error: string) {
console.error(error)
process.exit(-1)
}
function formatErrors(errors: ErrorObject[]): string {
return errors
.map(error => {
const { dataPath, message } = error
const additionalProperties = error.params as any
const additionalProperty =
additionalProperties.additionalProperty as string
let additionalPropertyText = ''
if (additionalProperty != null) {
2020-06-24 18:28:06 +00:00
additionalPropertyText = `, found: '${additionalProperties.additionalProperty}'`
}
// dataPath starts with a leading "."," which is a bit confusing
const element = dataPath.substring(1)
2018-03-27 00:53:14 +00:00
return ` - ${element} - ${message}${additionalPropertyText}`
})
.join('\n')
}
const repositoryRoot = Path.dirname(__dirname)
const changelogPath = Path.join(repositoryRoot, 'changelog.json')
2018-05-21 06:29:48 +00:00
// eslint-disable-next-line no-sync
const changelog = Fs.readFileSync(changelogPath, 'utf8')
let changelogObj = null
try {
changelogObj = JSON.parse(changelog)
} catch {
handleError(
2018-03-27 01:03:08 +00:00
'Unable to parse the contents of changelog.json into a JSON object. Please review the file contents.'
)
}
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
releases: {
type: 'object',
patternProperties: {
'^([0-9]+.[0-9]+.[0-9]+)(-beta[0-9]+|-test[0-9]+)?$': {
type: 'array',
items: {
type: 'string',
},
2018-03-27 00:03:41 +00:00
uniqueItems: true,
},
},
additionalProperties: false,
},
},
}
const ajv = new Ajv({ allErrors: true, uniqueItems: true })
const validate = ajv.compile(schema)
const valid = validate(changelogObj)
if (!valid && validate.errors != null) {
2018-03-27 00:53:14 +00:00
handleError(`Errors: \n${formatErrors(validate.errors)}`)
}
console.log('The changelog is totally fine')