Skip to content

Commit

Permalink
fix: make sure API definition class is compatible with windows
Browse files Browse the repository at this point in the history
  • Loading branch information
paulRbr committed Nov 27, 2024
1 parent ca74862 commit 23801ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
34 changes: 28 additions & 6 deletions src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {default as $RefParser, getJsonSchemaRefParserDefaultOptions} from '@apid
import asyncapi from '@asyncapi/specs'
import {CLIError} from '@oclif/core/errors'
import {safeStringify} from '@stoplight/yaml'
import debug from 'debug'
import {
JSONSchema4,
JSONSchema4Array,
Expand All @@ -11,7 +12,7 @@ import {
JSONSchema7,
} from 'json-schema'
import {createRequire} from 'node:module'
import path from 'node:path'
import {default as nodePath} from 'node:path'

// Used to require JSON files
const require = createRequire(import.meta.url)
Expand Down Expand Up @@ -203,11 +204,23 @@ class API {
return (output || this.location).endsWith('.json') ? 'json' : 'yaml'
}

isMainRefPath(path: string): boolean {
// $refs from json-schema-ref-parser lib returns posix style
// paths. We need to make sure we compare all paths in posix style
// independently of the platform runtime.
const resolvedAbsLocation = nodePath
.resolve(this.location)
.split(nodePath?.win32?.sep)
.join(nodePath?.posix?.sep ?? '/')

return path === this.location || path === resolvedAbsLocation
}

resolveContent(values: SpecSchema): [string, APIDefinition] {
let mainReference: JSONSchemaWithRaw = {parsed: {}, raw: ''}

for (const [absPath, reference] of Object.entries(values)) {
if (absPath === this.location || absPath === path.resolve(this.location)) {
if (this.isMainRefPath(absPath)) {
// $refs.values is not properly typed so we need to force it
// with the resulting type of our custom defined parser
mainReference = reference as JSONSchemaWithRaw
Expand All @@ -230,6 +243,9 @@ class API {
const {parsed, raw} = mainReference

if (!parsed || !raw || !(parsed instanceof Object) || !('info' in parsed)) {
debug('bump-cli:definition')(
`Main location (${this.location}) not found or empty (within ${JSON.stringify(Object.keys(values))})`,
)
throw new UnsupportedFormat("Definition needs to be an object with at least an 'info' key")
}

Expand All @@ -246,11 +262,17 @@ class API {
const refUrl = this.url(absPath)

if (
/^\//.test(absPath) || // Unix style filesystem path
/^[A-Za-z]+:\\/.test(absPath) || // Windows style filesystem path
(/^https?:\/\//.test(absPath) && definitionUrl.hostname === refUrl.hostname) // Same domain URLs
(refUrl.hostname === '') && ( // filesystem path

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on ubuntu-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on ubuntu-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on ubuntu-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on macos-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on macos-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on macos-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on windows-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on windows-latest

Replace `)·&&·(` with `·&&`

Check failure on line 265 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on windows-latest

Replace `)·&&·(` with `·&&`
/^\//.test(absPath) || // Unix style

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on ubuntu-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on ubuntu-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on ubuntu-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on macos-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on macos-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on macos-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on windows-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on windows-latest

Insert `(`

Check failure on line 266 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on windows-latest

Insert `(`
/^[A-Za-z]+:[\\\/]/.test(absPath) // Windows style

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on ubuntu-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on ubuntu-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on ubuntu-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on ubuntu-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on ubuntu-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on ubuntu-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on ubuntu-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on ubuntu-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on ubuntu-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on macos-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on macos-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on macos-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on macos-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on macos-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on macos-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on macos-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on macos-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on macos-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on windows-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on windows-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on windows-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on windows-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on windows-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on windows-latest

Insert `))·||`

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on windows-latest

/^[A-Za-z]+:[\\\/]/ can be optimized to /^[A-Za-z]+:[/\\]/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on windows-latest

Unnecessary escape character: \/

Check failure on line 267 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on windows-latest

Insert `))·||`
) || (

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on ubuntu-latest

Replace `)·||·(⏎········/^https?:\/\//.test(absPath)·&&⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on ubuntu-latest

Replace `)·||·(⏎········/^https?:\/\//.test(absPath)·&&⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on ubuntu-latest

Replace `)·||·(⏎········/^https?:\/\//.test(absPath)·&&⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on macos-latest

Replace `)·||·(⏎········/^https?:\/\//.test(absPath)·&&⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on macos-latest

Replace `)·||·(⏎········/^https?:\/\//.test(absPath)·&&⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on macos-latest

Replace `)·||·(⏎········/^https?:\/\//.test(absPath)·&&⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 23 - x64 on windows-latest

Replace `)·||·(␍⏎········/^https?:\/\//.test(absPath)·&&␍⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs␍⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 20 - x64 on windows-latest

Replace `)·||·(␍⏎········/^https?:\/\//.test(absPath)·&&␍⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs␍⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`

Check failure on line 268 in src/definition.ts

View workflow job for this annotation

GitHub Actions / Node 18 - x64 on windows-latest

Replace `)·||·(␍⏎········/^https?:\/\//.test(absPath)·&&␍⏎··········definitionUrl.hostname·===·refUrl.hostname·//·Same·domain·URLs␍⏎······)` with `(/^https?:\/\//.test(absPath)·&&·definitionUrl.hostname·===·refUrl.hostname)·//·Same·domain·URLs`
/^https?:\/\//.test(absPath) &&
definitionUrl.hostname === refUrl.hostname // Same domain URLs
)
) {
return path.relative(path.dirname(this.location), absPath)
const relativeLocation = nodePath.relative(nodePath.dirname(this.location), absPath)
debug('bump-cli:definition')(`Resolved relative $ref location: ${relativeLocation}`)
return relativeLocation
}

return absPath
Expand Down
2 changes: 1 addition & 1 deletion test/commands/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('deploy subcommand', () => {
)
expect(stdout).to.equal('')
expect(stderr).to.contain("Let's deploy on Bump.sh... done\n")
expect(stderr).to.contain('Warning: Your coucou documentation has not changed\n')
expect(stderr).to.contain('Warning: Your coucou documentation has not changed\n')
})

it('sends version to Bump with doc read from env variable', async () => {
Expand Down

0 comments on commit 23801ae

Please sign in to comment.