-
-
Notifications
You must be signed in to change notification settings - Fork 240
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: static assets #671
Merged
Merged
feat: static assets #671
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ecae5b8
refactor: try a concept
P0lip ddea0ef
revert: "refactor: try a concept"
P0lip 429af28
feat: generate list of static assets
P0lip 48e7f01
feat: pre-resolve remote refs
P0lip 4e22f22
feat: use (at)spectral rather than spectral protocol
P0lip 4cd21ea
feat: registerStaticAssets
P0lip fe577f8
refactor: simplify
P0lip 1b7a078
refactor: simplify part 2
P0lip 09c3c25
test: fix mocks
P0lip aa898a3
chore: use @stoplight/path in generate-assets
P0lip 6e4431c
build(rollup): pick up all oas functions
P0lip 7f8087c
chore: explain the purpose of generate-assets.js
P0lip 1a47e5e
Merge branch 'develop' into feat/assets
P0lip 74650c3
Merge branch 'develop' into feat/assets
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 was deleted.
Oops, something went wrong.
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,61 @@ | ||
#!/usr/bin/env node | ||
/** | ||
* This script generates a list of assets that are needed to load spectral:oas ruleset. | ||
* It contains all OAS custom functions and *resolved* rulesets. | ||
* The assets are stores in a single filed call assets.json in the following format: | ||
* `<require-call-path>: <content>` | ||
* where the `require-call-path` is the path you'd normally pass to require(), i.e. `@stoplight/spectral/rulesets/oas/index.js` and `content` is the text data. | ||
* Assets can be loaded using Spectral#registerStaticAssets statc method, i.e. `Spectral.registerStaticAssets(require('@stoplight/spectral/rulesets/assets/assets.json'))`; | ||
* If you execute the code above, ruleset will be loaded fully offline, without a need to make any request. | ||
*/ | ||
|
||
const path = require('@stoplight/path'); | ||
const fs = require('fs'); | ||
const { promisify } = require('util'); | ||
const { parse } = require('@stoplight/yaml'); | ||
const { httpAndFileResolver } = require('../dist/resolvers/http-and-file'); | ||
|
||
const readFileAsync = promisify(fs.readFile); | ||
const writeFileAsync = promisify(fs.writeFile); | ||
const readdirAsync = promisify(fs.readdir); | ||
const statAsync = promisify(fs.stat); | ||
|
||
const baseDir = path.join(__dirname, '../rulesets/assets/'); | ||
|
||
if (!fs.existsSync(baseDir)) { | ||
fs.mkdirSync(baseDir); | ||
} | ||
|
||
const target = path.join(baseDir, `assets.json`); | ||
const assets = {}; | ||
|
||
(async () => { | ||
await Promise.all(['', '2', '3'].map(spec => processDirectory(assets, path.join(__dirname, `../rulesets/oas${spec}`)))); | ||
await writeFileAsync(target, JSON.stringify(assets, null, 2)); | ||
})(); | ||
|
||
async function processDirectory(assets, dir) { | ||
await Promise.all((await readdirAsync(dir)).map(async name => { | ||
if (name === 'schemas') return; | ||
const target = path.join(dir, name); | ||
const stats = await statAsync(target); | ||
if (stats.isDirectory()) { | ||
return processDirectory(assets, target); | ||
} else { | ||
let content = await readFileAsync(target, 'utf8'); | ||
if (path.extname(name) === '.json') { | ||
content = JSON.stringify((await httpAndFileResolver.resolve(JSON.parse(content), { | ||
dereferenceRemote: true, | ||
dereferenceInline: false, | ||
baseUri: target, | ||
parseResolveResult(opts) { | ||
opts.result = parse(opts.result); | ||
return opts; | ||
}, | ||
})).result); | ||
} | ||
|
||
assets[path.join('@stoplight/spectral', path.relative(path.join(__dirname, '..'), target))] = content; | ||
} | ||
})); | ||
} |
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,13 @@ | ||
import { Dictionary } from '@stoplight/types'; | ||
|
||
function resolveSpectralRuleset(ruleset: string) { | ||
return `@stoplight/spectral/rulesets/${ruleset}/index.json`; | ||
} | ||
|
||
export const RESOLVE_ALIASES: Dictionary<string, string> = { | ||
'spectral:oas': resolveSpectralRuleset('oas'), | ||
'spectral:oas2': resolveSpectralRuleset('oas2'), | ||
'spectral:oas3': resolveSpectralRuleset('oas3'), | ||
}; | ||
|
||
export const STATIC_ASSETS: Dictionary<string> = {}; |
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,47 @@ | ||
import { FetchMockSandbox } from 'fetch-mock'; | ||
import { Spectral } from '../../spectral'; | ||
import { readRuleset } from '../reader'; | ||
|
||
declare const fetch: FetchMockSandbox; | ||
|
||
describe('Rulesets reader', () => { | ||
afterEach(() => { | ||
Spectral.registerStaticAssets({}); | ||
}); | ||
|
||
it('is able to load the whole ruleset from static file', async () => { | ||
fetch.resetBehavior(); | ||
fetch.get('https://unpkg.com/@stoplight/spectral/rulesets/oas/index.json', { | ||
status: 404, | ||
body: {}, | ||
}); | ||
|
||
Spectral.registerStaticAssets(require('../../../rulesets/assets/assets.json')); | ||
|
||
const { rules, functions } = await readRuleset('spectral:oas'); | ||
|
||
expect(rules).toMatchObject({ | ||
'openapi-tags': expect.objectContaining({ | ||
description: 'OpenAPI object should have non-empty `tags` array.', | ||
formats: ['oas2', 'oas3'], | ||
}), | ||
'oas2-schema': expect.objectContaining({ | ||
description: 'Validate structure of OpenAPI v2 specification.', | ||
formats: ['oas2'], | ||
}), | ||
'oas3-schema': expect.objectContaining({ | ||
description: 'Validate structure of OpenAPI v3 specification.', | ||
formats: ['oas3'], | ||
}), | ||
}); | ||
|
||
expect(functions).toMatchObject({ | ||
oasOp2xxResponse: expect.any(Object), | ||
oasOpFormDataConsumeCheck: expect.any(Object), | ||
oasOpIdUnique: expect.any(Object), | ||
oasOpParams: expect.any(Object), | ||
oasOpSecurityDefined: expect.any(Object), | ||
oasPathParam: expect.any(Object), | ||
}); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
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.
Is this change needed if we are doing #561 ?
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.
Yeah, it's still going to be needed. It's mostly for Studio to use and we don't expose it anywhere in the docs, etc.
I'll leave a note somewhere in the code on what it actually does and how it works, as right now it's not obvious, and we will forget about why we need it.
I see Windows build is failing, so need to take a look what's broken.
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.
@collinbachi did we already switch to whatever new CircleCI payment structure that gets us Windows builds?
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.
We started a 2 week trial of the new structure Wednesday afternoon
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.
@P0lip ok, fair enough on the still needed. If you cannot get windows build working with Azure then maybe try out the CircleCI windows build, however that works.
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.
@philsturgeon I got Windows build working, but would love to try out Windows on CircleCI. Azure pipelines is tad slow.
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.
Another time! :)