Skip to content

Commit

Permalink
Add feature detection
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Jan 9, 2023
1 parent 7af32db commit 26f0ba2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
2 changes: 1 addition & 1 deletion vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function activate(extensionContext: ExtensionContext) {

intializeStatusBarItem(relayExtensionContext);
registerCommands(relayExtensionContext);
registerProviders(relayExtensionContext);
await registerProviders(relayExtensionContext);
createAndStartLanguageClient(relayExtensionContext);

if (config.autoStartCompiler) {
Expand Down
44 changes: 42 additions & 2 deletions vscode-extension/src/providers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,55 @@
* LICENSE file in the root directory of this source tree.
*/

import path = require('path');
import * as fs from 'fs/promises';
import * as semver from 'semver';
import {workspace} from 'vscode';
import {RelayExtensionContext} from '../context';
import {RelayTextDocumentContentProvider} from './textDocumentContentProvider';

export function registerProviders(context: RelayExtensionContext) {
export async function registerProviders(context: RelayExtensionContext) {
const relayCompilerDirectory = path.join(
context.relayBinaryExecutionOptions.binaryPath,
'../..',
);

let packageManifest: any;

try {
packageManifest = JSON.parse(
await fs.readFile(
path.join(relayCompilerDirectory, 'package.json'),
'utf-8',
),
);
} catch {
// If we can't read this file, we don't have to bother continuing.
return;
}

const isSemverRangeSatisfied = semver.satisfies(
packageManifest.version,
'>=14.2',
);

const isPrerelease = semver.prerelease(packageManifest.version) != null;

if (!isSemverRangeSatisfied && !isPrerelease) {
// The config-schema.json does not yet part of the relay-compiler
// version used by the client.
return;
}

const configJsonSchemaPath = path.join(
relayCompilerDirectory,
'config-schema.json',
);

context.extensionContext.subscriptions.push(
workspace.registerTextDocumentContentProvider(
RelayTextDocumentContentProvider.scheme,
new RelayTextDocumentContentProvider(context),
new RelayTextDocumentContentProvider(configJsonSchemaPath),
),
);
}
13 changes: 2 additions & 11 deletions vscode-extension/src/providers/textDocumentContentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import * as fs from 'fs';
import {TextDocumentContentProvider, Event, Uri, ProviderResult} from 'vscode';
import {RelayExtensionContext} from '../context';

const EMPTY_JSON_SCHEMA = '{}';
const RELAY_CONFIG_SCHEMA_PATH = 'relay-config-schema';
Expand All @@ -18,14 +16,7 @@ const PACKAGE_JSON_RELAY_CONFIG_SCHEMA_PATH =
export class RelayTextDocumentContentProvider
implements TextDocumentContentProvider
{
private readonly jsonSchemaPath: string;

constructor(context: RelayExtensionContext) {
this.jsonSchemaPath = path.join(
context.relayBinaryExecutionOptions.binaryPath,
'../../config-schema.json',
);
}
constructor(private readonly configJsonSchemaPath: string) {}

static readonly scheme: string = 'relay';

Expand All @@ -38,7 +29,7 @@ export class RelayTextDocumentContentProvider

if (uri.authority === RELAY_CONFIG_SCHEMA_PATH) {
try {
return fs.readFileSync(this.jsonSchemaPath, {encoding: 'utf-8'});
return fs.readFileSync(this.configJsonSchemaPath, {encoding: 'utf-8'});
} catch {
return EMPTY_JSON_SCHEMA;
}
Expand Down

0 comments on commit 26f0ba2

Please sign in to comment.