stoplightio / spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.45k stars 234 forks source link

Expose `formatters` from either a separate package or the CLI package #2467

Closed sennyeya closed 1 year ago

sennyeya commented 1 year ago

User story. As a developer who wants to use Spectral in JS/TS, I can do create custom formatting, so that I can format my text output from an internal command without subshelling out to spectral-cli.

Is your feature request related to a problem? I'm looking to use the existing formatters in sepctral-cli in Javascript but cannot as they are not exported from the cli package.

Describe the solution you'd like Expose the formatters from their own package @stoplight/spectral-formatters. Alternatively, they could be exposed from the CLI, but would prefer their own package.

Additional context I'm happy to submit a PR, this should be pretty straightforward.

P0lip commented 1 year ago

Hey! That sounds good to me. I've been thinking about doing that too. However, there's one potential caveat to watch out for - that package should be isomorphic and work in any modern ES environment. Currently some of the formatters may rely on modules or globals specific to Node.js and that'd have to be addressed beforehand.

Happy to assist if needed. If you're up for that, you could indeed move the formatters into a separate package or maybe make it a part of packages/core. I could take care of the Node.js bit of the issue.

stoplight-bot commented 1 year ago

:tada: This issue has been resolved in version 6.7.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

sennyeya commented 1 year ago

@P0lip Sorry for the late follow up on this, I'm trying to use this now and unable to import it in my yarn project. Any advice? Getting a module not found error on a simple import like import { pretty } from '@stoplight/spectral-formatters'; . Tried tweaking with the exports of @stoplight/spectral-formatterss package.json locally, but no luck. My project is using the default classic TypeScript moduleResolution, which doesn't have support for exports.

P0lip commented 1 year ago

Hey @sennyeya! I'll take a look at it today. Could you share your tsconfig.json?

sennyeya commented 1 year ago

Thanks, https://github.com/backstage/backstage/blob/master/tsconfig.json Here's the main tsconfig.json, there's also the extends one which is https://github.com/backstage/backstage/blob/master/packages/cli/config/tsconfig.json.

P0lip commented 1 year ago

@sennyeya old TS moduleResolution will be supported once #2482 is merged

sennyeya commented 1 year ago

@P0lip Great, thanks for the quick turnaround!

stoplight-bot commented 1 year ago

:tada: This issue has been resolved in version 1.18.1 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket: