2018-03-26 23:54:21 +00:00
|
|
|
#!/usr/bin/env ts-node
|
|
|
|
|
|
|
|
import * as Path from 'path'
|
|
|
|
import * as Fs from 'fs'
|
|
|
|
|
2020-07-08 08:41:20 +00:00
|
|
|
import Ajv, { ErrorObject } from 'ajv'
|
2018-03-26 23:54:21 +00:00
|
|
|
|
2018-03-27 21:01:23 +00:00
|
|
|
function handleError(error: string) {
|
2018-03-27 00:02:00 +00:00
|
|
|
console.error(error)
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
|
|
|
|
2020-07-08 08:41:20 +00:00
|
|
|
function formatErrors(errors: ErrorObject[]): string {
|
2018-03-27 00:43:05 +00:00
|
|
|
return errors
|
|
|
|
.map(error => {
|
|
|
|
const { dataPath, message } = error
|
|
|
|
const additionalProperties = error.params as any
|
2022-03-18 11:58:24 +00:00
|
|
|
const additionalProperty =
|
|
|
|
additionalProperties.additionalProperty as string
|
2018-03-27 00:43:05 +00:00
|
|
|
|
|
|
|
let additionalPropertyText = ''
|
|
|
|
|
|
|
|
if (additionalProperty != null) {
|
2020-06-24 18:28:06 +00:00
|
|
|
additionalPropertyText = `, found: '${additionalProperties.additionalProperty}'`
|
2018-03-27 00:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dataPath starts with a leading "."," which is a bit confusing
|
2022-02-28 12:55:50 +00:00
|
|
|
const element = dataPath.substring(1)
|
2018-03-27 00:43:05 +00:00
|
|
|
|
2018-03-27 00:53:14 +00:00
|
|
|
return ` - ${element} - ${message}${additionalPropertyText}`
|
2018-03-27 00:43:05 +00:00
|
|
|
})
|
|
|
|
.join('\n')
|
|
|
|
}
|
|
|
|
|
2018-03-26 23:54:21 +00:00
|
|
|
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
|
2018-03-27 00:41:59 +00:00
|
|
|
const changelog = Fs.readFileSync(changelogPath, 'utf8')
|
|
|
|
|
|
|
|
let changelogObj = null
|
2018-03-26 23:54:21 +00:00
|
|
|
|
2018-03-27 00:02:00 +00:00
|
|
|
try {
|
2018-03-27 00:41:59 +00:00
|
|
|
changelogObj = JSON.parse(changelog)
|
2018-03-27 00:02:00 +00:00
|
|
|
} 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.'
|
2018-03-27 00:02:00 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-26 23:54:21 +00:00
|
|
|
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,
|
2018-03-26 23:54:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
additionalProperties: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-27 00:43:05 +00:00
|
|
|
const ajv = new Ajv({ allErrors: true, uniqueItems: true })
|
2018-03-26 23:54:21 +00:00
|
|
|
const validate = ajv.compile(schema)
|
|
|
|
|
2018-03-27 00:43:05 +00:00
|
|
|
const valid = validate(changelogObj)
|
2018-03-26 23:54:21 +00:00
|
|
|
|
2018-03-27 00:43:05 +00:00
|
|
|
if (!valid && validate.errors != null) {
|
2018-03-27 00:53:14 +00:00
|
|
|
handleError(`Errors: \n${formatErrors(validate.errors)}`)
|
2018-03-26 23:54:21 +00:00
|
|
|
}
|
2018-03-27 00:02:00 +00:00
|
|
|
|
|
|
|
console.log('The changelog is totally fine')
|