Skip to content

Commit

Permalink
Merge branch 'main' into release-please/branches/main
Browse files Browse the repository at this point in the history
  • Loading branch information
drstrangelooker authored Oct 25, 2021
2 parents 3e36d5b + 6c0f0b2 commit c7f9b84
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"lint-staged": "yarn exec lint-staged",
"lint-changed": "eslint $(git diff --name-only --diff-filter=ACMRTUXB origin/main | grep -E \"(.js$|.ts$|.tsx$)\") --quiet",
"lint-changed-fix": "eslint $(git diff --name-only --diff-filter=ACMRTUXB origin/main | grep -E \"(.js$|.ts$|.tsx$)\") --quiet --fix",
"lint:spec": "SUPPRESS_NO_CONFIG_WARNING=1 ts-node -O '{ \"module\": \"commonjs\", \"target\": \"es2019\" }' packages/sdk-codegen-scripts/scripts/specLinter.ts",
"spec:diff": "SUPPRESS_NO_CONFIG_WARNING=1 ts-node -O '{ \"module\": \"commonjs\", \"target\": \"es2019\" }' packages/sdk-codegen-scripts/scripts/specLinter.ts",
"refresh": "SUPPRESS_NO_CONFIG_WARNING=1 ts-node -O '{ \"module\": \"commonjs\", \"target\": \"es2019\" }' packages/sdk-codegen-scripts/scripts/refresh_specs.ts",
"register": "SUPPRESS_NO_CONFIG_WARNING=1 ts-node -O '{ \"module\": \"commonjs\", \"target\": \"es2019\" }' packages/sdk-codegen-scripts/scripts/register.ts",
"test": "yarn jest",
Expand Down
120 changes: 98 additions & 22 deletions packages/sdk-codegen-scripts/scripts/specLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,115 @@
SOFTWARE.
*/

import * as path from 'path'
import { writeFileSync } from 'fs'
import type { DiffFilter, DiffRow, IMethod } from '@looker/sdk-codegen'
import { ApiModel, csvHeaderRow, csvDiffRow } from '@looker/sdk-codegen'
import {
ApiModel,
csvHeaderRow,
csvDiffRow,
includeDiffs,
mdDiffRow,
mdHeaderRow,
} from '@looker/sdk-codegen'
import { compareSpecs } from '@looker/sdk-codegen/src/specDiff'
import { readFileSync } from '../src'

/**
* Compares Looker API 3.1 beta endpoints with their 4.0 version and writes the
* result to csv.
*/
;(async () => {
const rootPath = path.join(__dirname, '../../../spec')
const spec31Path = path.join(rootPath, 'Looker.3.1.oas.json')
const spec40Path = path.join(rootPath, 'Looker.4.0.oas.json')
interface IDiffer {
/** Name of spec file A */
fileA: string
/** Name of spec file B */
fileB: string
/** Output format */
format: string
/** Beta status check? */
status: string
}

const spec31 = ApiModel.fromString(readFileSync(spec31Path))
const spec40 = ApiModel.fromString(readFileSync(spec40Path))
const rootPath = path.join(__dirname, '../../../spec')

const filter: DiffFilter = (
_delta: DiffRow,
lMethod?: IMethod,
_?: IMethod
) => lMethod?.status === 'beta'
const getOptions = () => {
const result: IDiffer = {
fileA: path.join(rootPath, 'Looker.3.1.oas.json'),
fileB: path.join(rootPath, 'Looker.4.0.oas.json'),
format: 'csv',
status: 'beta',
}
const args = process.argv.slice(1)
console.log(`${args[0]} [fileA] [fileB] [format] [status]\n
format=csv|md
status=beta|all
`)
console.log(`defaults:\n${JSON.stringify(result, null, 2)}`)
if (args.length > 1) {
result.fileA = args[1]
}
if (args.length > 2) {
result.fileB = args[2]
}
if (args.length > 3) {
const val = args[3].toLowerCase()
if (!['csv', 'md'].includes(val)) {
throw new Error(`"${val}" is not a recognized format`)
}
result.format = val
}
if (args.length > 4) {
const val = args[4].toLowerCase()
if (!['all', 'beta'].includes(val)) {
throw new Error(`"${val}" is not a recognized diff check status`)
}
result.status = args[4].toLowerCase()
}

const diff = compareSpecs(spec31, spec40, filter)
return result
}

let result = csvHeaderRow
diff.forEach((diffRow) => {
result += csvDiffRow(diffRow)
})
function checkSpecs() {
const opt = getOptions()
const specA = ApiModel.fromString(readFileSync(opt.fileA))
const specB = ApiModel.fromString(readFileSync(opt.fileB))

const filter: DiffFilter =
opt.status === 'beta'
? (_delta: DiffRow, lMethod?: IMethod, _?: IMethod) =>
lMethod?.status === 'beta'
: includeDiffs

writeFileSync(path.join(rootPath, '../results.csv'), result, {
const diff = compareSpecs(specA, specB, filter)

let result = ''
switch (opt.format) {
case 'csv':
result = csvHeaderRow
diff.forEach((diffRow) => {
result += csvDiffRow(diffRow)
})
break
case 'md':
result = mdHeaderRow
diff.forEach((diffRow) => {
result += mdDiffRow(diffRow)
})
break
}

const outFile = path.join(rootPath, `../results.${opt.format}`)
writeFileSync(outFile, result, {
encoding: 'utf-8',
})
console.log(`Wrote ${diff.length} method differences to ${outFile}`)
}

/**
* By default, compares Looker API 3.1 beta endpoints with their 4.0 version and writes the
* result to csv.
*/
;(async () => {
try {
checkSpecs()
} catch (err: unknown) {
console.error(err)
}
})()
2 changes: 1 addition & 1 deletion packages/sdk-codegen/src/specDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export const mdHeaderRow = `
| ------------ | -------- | --- | --- | ------------ | --------- | -------- | ------------ | --------- |`

export const mdDiffRow = (diff: DiffRow) => `
| ${diff.name} | ${diff.id} | ${diff.lStatus} | ${diff.rStatus} | ${
| ${diff.name} | ${diff.id} | ${diff.lStatus} | ${diff.rStatus} | ${
diff.typeDiff
} | ${diff.paramsDiff} | ${diff.bodyDiff} | ${
diff.responseDiff
Expand Down

0 comments on commit c7f9b84

Please sign in to comment.