Skip to content

Commit 7dfffad

Browse files
committed
fix: make sure API definition class is compatible with windows
1 parent ca74862 commit 7dfffad

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/definition.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {default as $RefParser, getJsonSchemaRefParserDefaultOptions} from '@apid
22
import asyncapi from '@asyncapi/specs'
33
import {CLIError} from '@oclif/core/errors'
44
import {safeStringify} from '@stoplight/yaml'
5+
import debug from 'debug'
56
import {
67
JSONSchema4,
78
JSONSchema4Array,
@@ -11,7 +12,7 @@ import {
1112
JSONSchema7,
1213
} from 'json-schema'
1314
import {createRequire} from 'node:module'
14-
import path from 'node:path'
15+
import {default as nodePath} from 'node:path'
1516

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

207+
isMainRefPath(path: string): boolean {
208+
// $refs from json-schema-ref-parser lib returns posix style
209+
// paths. We need to make sure we compare all paths in posix style
210+
// independently of the platform runtime.
211+
const resolvedAbsLocation = nodePath
212+
.resolve(this.location)
213+
.split(nodePath?.win32?.sep)
214+
.join(nodePath?.posix?.sep ?? '/')
215+
216+
return path === this.location || path === resolvedAbsLocation
217+
}
218+
206219
resolveContent(values: SpecSchema): [string, APIDefinition] {
207220
let mainReference: JSONSchemaWithRaw = {parsed: {}, raw: ''}
208221

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

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

@@ -246,11 +262,14 @@ class API {
246262
const refUrl = this.url(absPath)
247263

248264
if (
249-
/^\//.test(absPath) || // Unix style filesystem path
250-
/^[A-Za-z]+:\\/.test(absPath) || // Windows style filesystem path
265+
(refUrl.hostname === '' && // filesystem path
266+
(/^\//.test(absPath) || // Unix style
267+
/^[A-Za-z]+:[/\\]/.test(absPath))) || // Windows style
251268
(/^https?:\/\//.test(absPath) && definitionUrl.hostname === refUrl.hostname) // Same domain URLs
252269
) {
253-
return path.relative(path.dirname(this.location), absPath)
270+
const relativeLocation = nodePath.relative(nodePath.dirname(this.location), absPath)
271+
debug('bump-cli:definition')(`Resolved relative $ref location: ${relativeLocation}`)
272+
return relativeLocation
254273
}
255274

256275
return absPath

test/commands/deploy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('deploy subcommand', () => {
4545
)
4646
expect(stdout).to.equal('')
4747
expect(stderr).to.contain("Let's deploy on Bump.sh... done\n")
48-
expect(stderr).to.contain('Warning: Your coucou documentation has not changed\n')
48+
expect(stderr).to.contain('Warning: Your coucou documentation has not changed\n')
4949
})
5050

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

0 commit comments

Comments
 (0)