-
-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ruleset-migrator): implement ruleset migrator #1698
Merged
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8a50f4d
feat(ruleset-migrator): implement ruleset migrator
P0lip 3823996
feat(cli): hook up ruleset-migrator
P0lip 97e5d22
chore(cli): update tsconfig.build.json
P0lip 4e94c40
chore: build fixtures
P0lip 84ab4a0
fix(ruleset-migrator): require.resolve
P0lip 32fc932
revert: postbuild removal
P0lip afabce3
revert: unrelated changes
P0lip f90d2c8
docs(ruleset-migrator): add readme
P0lip 18710a7
Merge branch 'develop' into feat/migrator-project
P0lip 5ad1ace
revert(cli): ruleset requirement
P0lip 8742286
feat(ruleset-migrator): cover overrides
P0lip 296feff
Merge remote-tracking branch 'origin/develop' into feat/migrator-project
P0lip d86edb2
Merge branch 'develop' into feat/migrator-project
P0lip 256958a
chore(cli): sort package.json
P0lip abeef34
chore(ruleset-migrator): @types/node pkg
P0lip 80014e5
fix(ruleset-migrator): do not touch own rules
P0lip 29aa9e4
Merge remote-tracking branch 'origin/develop' into feat/migrator-project
P0lip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/cli/src/services/__tests__/__fixtures__/ruleset.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"rules": { | ||
"info-matches-stoplight": { | ||
"message": "Info must contain Stoplight", | ||
"given": "$.info", | ||
"recommended": true, | ||
"type": "style", | ||
"then": { | ||
"field": "title", | ||
"function": "pattern", | ||
"functionOptions": { | ||
"match": "Stoplight" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# @stoplight/spectral-ruleset-migrator | ||
|
||
This project serves as a converter between the legacy ruleset format and a new one. | ||
It's used internally, albeit it can be used externally too, also in browsers. | ||
|
||
For the time being there are two output formats supported: commonjs & esm. | ||
The migrator loads the ruleset, apply a number of conversions and return a valid JS code that can be executed later on. | ||
|
||
## Examples | ||
|
||
```yaml | ||
# .spectral.yaml | ||
extends: spectral:oas | ||
formats: [oas2, json-schema-loose] | ||
rules: | ||
oas3-schema: warning | ||
valid-type: | ||
message: Type must be valid | ||
given: $..type | ||
then: | ||
function: pattern | ||
functionOptions: | ||
mustMatch: ^(string|number)$ | ||
``` | ||
|
||
```js | ||
// .spectral.js (CommonJS) | ||
const { oas: oas } = require("@stoplight/spectral-rulesets"); | ||
const { oas2: oas2, jsonSchemaLoose: jsonSchemaLoose } = require("@stoplight/spectral-formats"); | ||
const { pattern: pattern } = require("@stoplight/spectral-functions"); | ||
module.exports = { | ||
extends: oas, | ||
formats: [oas2, jsonSchemaLoose], | ||
rules: { | ||
"oas3-schema": "warning", | ||
"valid-type": { | ||
message: "Type must be valid", | ||
given: "$..type", | ||
then: { | ||
function: pattern, | ||
functionOptions: { | ||
mustMatch: "^(string|number)$", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
```js | ||
// .spectral.js (ES Module) | ||
import { oas } from "@stoplight/spectral-rulesets"; | ||
import { oas2, jsonSchemaLoose } from "@stoplight/spectral-formats"; | ||
import { pattern } from "@stoplight/spectral-functions"; | ||
export default { | ||
extends: oas, | ||
formats: [oas2, jsonSchemaLoose], | ||
rules: { | ||
"oas3-schema": "warning", | ||
"valid-type": { | ||
message: "Type must be valid", | ||
given: "$..type", | ||
then: { | ||
function: pattern, | ||
functionOptions: { | ||
mustMatch: "^(string|number)$", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
## Usage | ||
|
||
### Via @stoplight/spectral-cli | ||
|
||
```ts | ||
npx @stoplight/spectral-cli ruleset migrate | ||
``` | ||
|
||
### Programmatically | ||
|
||
```ts | ||
const { migrateRuleset } = require("@stoplight/spectral-ruleset-migrator"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
migrateRuleset(path.join(__dirname, "spectral.json"), { | ||
fs, | ||
format: "commonjs", // esm available too, but not recommended for now | ||
}).then(fs.promises.writeFile.bind(fs.promises, path.join(__dirname, ".spectral.js"))); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@stoplight/spectral-ruleset-migrator", | ||
"version": "0.0.0", | ||
"homepage": "https://github.com/stoplightio/spectral", | ||
"bugs": "https://github.com/stoplightio/spectral/issues", | ||
"author": "Stoplight <support@stoplight.io>", | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"license": "Apache-2.0", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"/dist" | ||
], | ||
"browser": { | ||
"./dist/require-resolve.js": false | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/stoplightio/spectral.git" | ||
}, | ||
"dependencies": { | ||
"@stoplight/json": "3.15.0", | ||
"@stoplight/path": "1.3.2", | ||
"@stoplight/spectral-functions": "^0.0.0", | ||
"@stoplight/spectral-runtime": "^0.0.0", | ||
"@stoplight/types": "12.3.0", | ||
"@stoplight/yaml": "4.2.2", | ||
"ajv": "^8.6.0", | ||
"ast-types": "0.14.2", | ||
"astring": "^1.7.5" | ||
}, | ||
"devDependencies": { | ||
"json-schema-to-typescript": "^10.1.4", | ||
"memfs": "^3.2.2", | ||
"prettier": "^2.3.2" | ||
}, | ||
"scripts": { | ||
"pretest": "ts-node ./scripts/generate-test-fixtures.ts && yarn prebuild", | ||
"prebuild": "ts-node ./scripts/compile-schemas.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as fs from 'fs'; | ||
import * as path from '@stoplight/path'; | ||
import { compile } from 'json-schema-to-typescript'; | ||
|
||
import schema from '../src/validation/schema'; | ||
|
||
compile(schema, 'Ruleset', { | ||
bannerComment: '/*eslint-disable*/', | ||
style: { | ||
singleQuote: true, | ||
}, | ||
}).then(ts => { | ||
return fs.promises.writeFile(path.join(__dirname, '../src/validation/types.ts'), ts); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I even want to know?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, long story short - in rare circumstances a "top-level" await might be inserted.
This won't be a problem soon-ish once we hop on ESM, but for the time being it's a plain CommonJS.
The above is needed for us to be able to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Narrator: it turns out he really did not want to know