-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
287 additions
and
365 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
xhr, | ||
XHRResponse, | ||
configure as configureHttpRequests, | ||
getErrorStatusDescription, | ||
} from 'request-light'; | ||
import { URI } from 'vscode-uri'; | ||
import * as fs from 'fs'; | ||
|
||
export interface RequestService { | ||
getContent(uri: string): Promise<string>; | ||
} | ||
|
||
function getHTTPRequestService(): RequestService { | ||
return { | ||
getContent(uri: string, _encoding?: string) { | ||
const headers = { 'Accept-Encoding': 'gzip, deflate' }; | ||
return xhr({ url: uri, followRedirects: 5, headers }).then( | ||
(response) => { | ||
return response.responseText; | ||
}, | ||
(error: XHRResponse) => { | ||
return Promise.reject( | ||
error.responseText || | ||
getErrorStatusDescription(error.status) || | ||
error.toString() | ||
); | ||
} | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
function getFileRequestService(): RequestService { | ||
return { | ||
getContent(location: string, encoding?: BufferEncoding) { | ||
return new Promise((c, e) => { | ||
const uri = URI.parse(location); | ||
fs.readFile(uri.fsPath, encoding, (err, buf) => { | ||
if (err) { | ||
return e(err); | ||
} | ||
c(buf.toString()); | ||
}); | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
export function getSchemaRequestService( | ||
handledSchemas: string[] = ['https', 'http', 'file'] | ||
) { | ||
const builtInHandlers: { [protocol: string]: RequestService | undefined } = | ||
{}; | ||
for (const protocol of handledSchemas) { | ||
if (protocol === 'file') { | ||
builtInHandlers[protocol] = getFileRequestService(); | ||
} else if (protocol === 'http' || protocol === 'https') { | ||
builtInHandlers[protocol] = getHTTPRequestService(); | ||
} | ||
} | ||
return (uri: string): Thenable<string> => { | ||
const protocol = uri.substr(0, uri.indexOf(':')); | ||
|
||
const builtInHandler = builtInHandlers[protocol]; | ||
if (builtInHandler) { | ||
return builtInHandler.getContent(uri); | ||
} | ||
return Promise.reject('Unable to retrieve schema'); | ||
}; | ||
} |
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
60 changes: 60 additions & 0 deletions
60
libs/json-schema/src/lib/create-builders-and-executors-schema.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,60 @@ | ||
import { CollectionInfo } from '@nx-console/schema'; | ||
import { JSONSchema } from 'vscode-json-languageservice'; | ||
|
||
type BuildersSchema = JSONSchema; | ||
type ExecutorsSchema = JSONSchema; | ||
|
||
/** | ||
* Builds the schema for builders and executors. | ||
* | ||
* @param collections | ||
* @returns [BuildersSchema[], ExecutorsSchema[]] | ||
*/ | ||
export function createBuildersAndExecutorsSchema( | ||
collections: CollectionInfo[] | ||
): [BuildersSchema[], ExecutorsSchema[]] { | ||
return collections.reduce<[BuildersSchema[], ExecutorsSchema[]]>( | ||
(acc, collection) => { | ||
acc[0].push({ | ||
if: { | ||
properties: { builder: { const: collection.name } }, | ||
required: ['builder'], | ||
}, | ||
then: { | ||
properties: { | ||
options: { | ||
$ref: `file://${collection.path}`, | ||
}, | ||
configurations: { | ||
additionalProperties: { | ||
$ref: `file://${collection.path}`, | ||
required: [], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
acc[1].push({ | ||
if: { | ||
properties: { executor: { const: collection.name } }, | ||
required: ['executor'], | ||
}, | ||
then: { | ||
properties: { | ||
options: { | ||
$ref: `file://${collection.path}`, | ||
}, | ||
configurations: { | ||
additionalProperties: { | ||
$ref: `file://${collection.path}`, | ||
required: [], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return acc; | ||
}, | ||
[[], []] | ||
); | ||
} |
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 |
---|---|---|
@@ -1,100 +1,41 @@ | ||
import { CollectionInfo } from '@nx-console/schema'; | ||
import { JSONSchema } from 'vscode-json-languageservice'; | ||
import { createBuildersAndExecutorsSchema } from './create-builders-and-executors-schema'; | ||
|
||
export function getProjectJsonSchema(collections: CollectionInfo[]) { | ||
const [builders, executors] = createBuildersAndExecutorsSchema(collections); | ||
const contents = createJsonSchema(builders, executors); | ||
const [, executors] = createBuildersAndExecutorsSchema(collections); | ||
const contents = createJsonSchema(executors); | ||
return contents; | ||
} | ||
|
||
function createBuildersAndExecutorsSchema( | ||
collections: CollectionInfo[] | ||
): [string, string] { | ||
const builders = collections | ||
.map( | ||
(collection) => ` | ||
{ | ||
"if": { | ||
"properties": { "builder": { "const": "${collection.name}" } }, | ||
"required": ["builder"] | ||
}, | ||
"then": { | ||
"properties": { | ||
"options": { | ||
"$ref": "${collection.path}" | ||
}, | ||
"configurations": { | ||
"additionalProperties": { | ||
"$ref": "${collection.path}", | ||
"required": [] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
) | ||
.join(','); | ||
|
||
const executors = collections | ||
.map( | ||
(collection) => ` | ||
{ | ||
"if": { | ||
"properties": { "executor": { "const": "${collection.name}" } }, | ||
"required": ["executor"] | ||
}, | ||
"then": { | ||
"properties": { | ||
"options": { | ||
"$ref": "${collection.path}" | ||
}, | ||
"configurations": { | ||
"additionalProperties": { | ||
"$ref": "${collection.path}", | ||
"required": [] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
) | ||
.join(','); | ||
|
||
return [builders, executors]; | ||
} | ||
|
||
function createJsonSchema(builders: string, executors: string) { | ||
return ` | ||
{ | ||
"title": "JSON schema for Nx projects", | ||
"id": "https://nx.dev/project-schema", | ||
"type": "object", | ||
"properties": { | ||
"targets": { | ||
"description": "Configures all the targets which define what tasks you can run against the project", | ||
"additionalProperties": { | ||
"type": "object", | ||
"properties": { | ||
"executor": { | ||
"description": "The function that Nx will invoke when you run this target", | ||
"type": "string" | ||
function createJsonSchema(executors: JSONSchema[]): JSONSchema { | ||
return { | ||
title: 'JSON schema for Nx projects', | ||
id: 'https://nx.dev/project-schema', | ||
type: 'object', | ||
properties: { | ||
targets: { | ||
description: | ||
'Configures all the targets which define what tasks you can run against the project', | ||
additionalProperties: { | ||
type: 'object', | ||
properties: { | ||
executor: { | ||
description: | ||
'The function that Nx will invoke when you run this target', | ||
type: 'string', | ||
}, | ||
"options": { | ||
"type": "object" | ||
configurations: { | ||
description: | ||
'provides extra sets of values that will be merged into the options map', | ||
additionalProperties: { | ||
type: 'object', | ||
}, | ||
}, | ||
"configurations": { | ||
"description": "provides extra sets of values that will be merged into the options map", | ||
"additionalProperties": { | ||
"type": "object" | ||
} | ||
} | ||
}, | ||
"allOf": [ | ||
${executors} | ||
] | ||
} | ||
} | ||
} | ||
}`; | ||
allOf: executors, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} |
Oops, something went wrong.