Skip to content

Commit

Permalink
feat: upgrade deps, improve messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 27, 2022
1 parent eb812c3 commit 071380a
Show file tree
Hide file tree
Showing 5 changed files with 725 additions and 271 deletions.
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
"dependencies": {
"@oclif/core": "^1.16.4",
"chalk": "^4.1.2",
"just-diff": "^3.1.1",
"just-diff": "^5.1.1",
"semver": "^7.3.5",
"sinon": "^11.1.2",
"ts-json-schema-generator": "^0.98.0",
"ts-json-schema-generator": "^1.1.0",
"tslib": "^2"
},
"devDependencies": {
Expand All @@ -23,16 +22,16 @@
"@types/semver": "^7.3.9",
"chai": "^4",
"commitlint": "^12.1.4",
"eslint": "^7",
"eslint-config-oclif": "^3.1",
"eslint-config-oclif-typescript": "^0.2",
"globby": "^11",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4",
"eslint-config-oclif-typescript": "^1.0.2",
"husky": "6",
"mocha": "^8",
"nyc": "^15",
"oclif": "^2.6.3",
"ts-node": "^9",
"typescript": "^4.6.3"
"sinon": "^11.1.2",
"ts-node": "^10",
"typescript": "^4.8.3"
},
"engines": {
"node": ">=12.0.0"
Expand Down Expand Up @@ -74,4 +73,4 @@
"test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\" --timeout 20000",
"version": "oclif readme && git add README.md"
}
}
}
44 changes: 35 additions & 9 deletions src/commands/schema/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import * as fs from 'fs'
import * as semver from 'semver'
import * as _ from 'lodash'
import {diff, Operation} from 'just-diff'
import {Flags} from '@oclif/core'
import {Flags, toConfiguredId} from '@oclif/core'
import {Schema} from 'ts-json-schema-generator'
import {SnapshotCommand} from '../../snapshot-command'
import {getAllFiles, SchemaGenerator, Schemas} from './generate'
import {bold, cyan, red, underline} from 'chalk'

export type SchemaComparison = Array<{ op: Operation; path: (string | number)[]; value: any }>

function isNumber(n: string | number): boolean {
return Number.isInteger(Number(n))
}

function isMeaningless(n: string | number): boolean {
const meaninglessKeys: Array<string | number> = ['$comment', '__computed']
return meaninglessKeys.includes(n)
}

export default class SchemaCompare extends SnapshotCommand {
public static flags = {
filepath: Flags.string({
Expand Down Expand Up @@ -39,25 +48,41 @@ export default class SchemaCompare extends SnapshotCommand {
return []
}

const humandReadableChanges: Record<string, string[]> = {}
const humanReadableChanges: Record<string, string[]> = {}
for (const change of changes) {
const lastPathElement = change.path[change.path.length - 1]
if (isMeaningless(lastPathElement)) continue

const objPath = change.path.join('.')
const existing = _.get(existingSchema, objPath)
const latest = _.get(latestSchema, objPath)
const [commandId] = objPath.split('.definitions')
const readablePath = objPath.replace(`${commandId}.`, '')
if (!humandReadableChanges[commandId]) {
humandReadableChanges[commandId] = []

if (!humanReadableChanges[commandId]) {
humanReadableChanges[commandId] = []
}

const lastElementIsNum = isNumber(lastPathElement)
const basePath = lastElementIsNum ? readablePath.replace(`.${lastPathElement}`, '') : readablePath

switch (change.op) {
case 'replace':
humandReadableChanges[commandId].push(`${underline(readablePath)} was changed from ${cyan(existing)} to ${cyan(latest)}`)
humanReadableChanges[commandId].push(`${underline(readablePath)} was changed from ${cyan(existing)} to ${cyan(latest)}`)
break
case 'add':
humandReadableChanges[commandId].push(`${underline(readablePath)} was ${cyan('added')} to latest schema`)
humanReadableChanges[commandId].push(
lastElementIsNum ?
`Array item at ${underline(basePath)} was ${cyan('added')} to latest schema` :
`${underline(readablePath)} was ${cyan('added')} to latest schema`,
)
break
case 'remove':
humandReadableChanges[commandId].push(`${underline(readablePath)} was ${cyan('not found')} in latest schema`)
humanReadableChanges[commandId].push(
lastElementIsNum ?
`Array item at ${underline(basePath)} was ${cyan('not found')} in latest schema` :
`${underline(readablePath)} was ${cyan('not found')} in latest schema`,
)
break
default:
break
Expand All @@ -66,15 +91,16 @@ export default class SchemaCompare extends SnapshotCommand {

this.log()
this.log(bold(red('Found the following schema changes:')))
for (const [commandId, changes] of Object.entries(humandReadableChanges)) {
for (const [commandId, changes] of Object.entries(humanReadableChanges)) {
this.log()
this.log(bold(commandId))
for (const change of changes) {
this.log(` - ${change}`)
}
}
this.log()
this.log('If intended, please update the schema file(s) and run again.')
const bin = process.platform === 'win32' ? 'bin\\dev.cmd' : 'bin/dev'
this.log('If intended, please update the schema file(s) and run again:', bold(`${bin} ${toConfiguredId('schema:generate', this.config)}`))
process.exitCode = 1
return changes
}
Expand Down
12 changes: 6 additions & 6 deletions src/commands/schema/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getAllFiles(dirPath: string, ext: string, allFiles: string[] = [
export class SchemaGenerator {
private classToId: Record<string, string> = {}

constructor(private base: SnapshotCommand, private ignorevoid = true) {}
constructor(private base: SnapshotCommand, private ignoreVoid = true) {}

public async generate(): Promise<Schemas> {
for (const cmd of this.base.commands) {
Expand All @@ -46,7 +46,7 @@ export class SchemaGenerator {

for (const file of this.getAllCmdFiles()) {
const {returnType, commandId} = this.parseCmdFile(file)
if (this.ignorevoid && returnType === 'void') continue
if (this.ignoreVoid && returnType === 'void') continue
cmdSchemas[commandId] = this.generateSchema(returnType, file)
}

Expand Down Expand Up @@ -142,12 +142,12 @@ export class SchemaGenerator {
}

private validateReturnType(returnType: string, commandId: string) {
const notAllowed = this.ignorevoid ? ['any', 'unknown'] : ['any', 'unknown', 'void']
const vaugeTypes = ['JsonMap', 'JsonCollection', 'AnyJson']
const notAllowed = this.ignoreVoid ? ['any', 'unknown'] : ['any', 'unknown', 'void']
const vagueTypes = ['JsonMap', 'JsonCollection', 'AnyJson']
if (notAllowed.includes(returnType)) {
throw new Error(`${returnType} (from ${commandId}) is not allowed. Please use a more specific type.`)
} else if (vaugeTypes.includes(returnType)) {
throw new Error(`${returnType} (from ${commandId}) is too vauge. Please use a more specific type.`)
} else if (vagueTypes.includes(returnType)) {
throw new Error(`${returnType} (from ${commandId}) is too vague. Please use a more specific type.`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/commands/schema.compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ describe('schema:compare', () => {
expect(ctx.stdout).to.contain('Found the following schema changes:')
expect(ctx.stdout).to.contain('- definitions.Snapshots.items.additionalProperties was added to latest schema')
expect(ctx.stdout).to.contain('- commands.snapshot:compare was not found in latest schema')
expect(ctx.stdout).to.contain('If intended, please update the schema file(s) and run again.')
expect(ctx.stdout).to.contain('If intended, please update the schema file(s) and run again:')
})
})
Loading

0 comments on commit 071380a

Please sign in to comment.