Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-ravens-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/cli-utils": minor
---

Add `gql.tada/ts-plugin` to the init and doctor command
6 changes: 6 additions & 0 deletions .changeset/spotty-crabs-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gql.tada": minor
"@gql.tada/internal": patch
---

Add `gql.tada/ts-plugin` alias for `@0no-co/graphqlsp`
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ package-lock.json
/tsconfig.vitest-temp.json
/cli
/internal
/ts-plugin
4 changes: 2 additions & 2 deletions examples/example-pokemon-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"urql": "^4.0.7"
},
"devDependencies": {
"@0no-co/graphqlsp": "^1.12.0",
"@0no-co/graphqlsp": "^1.12.9",
"@types/react": "^18.2.79",
"@types/react-dom": "^18.2.25",
"@vitejs/plugin-react": "^4.2.1",
"typescript": "^5.4.2",
"typescript": "^5.5.2",
"vite": "^5.2.10"
}
}
4 changes: 2 additions & 2 deletions examples/example-pokemon-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"svelte": "^4.0.5"
},
"devDependencies": {
"@0no-co/graphqlsp": "^1.12.0",
"@0no-co/graphqlsp": "^1.12.9",
"@sveltejs/vite-plugin-svelte": "^3.1.0",
"typescript": "^5.4.2",
"typescript": "^5.5.2",
"vite": "^5.2.10"
}
}
4 changes: 2 additions & 2 deletions examples/example-pokemon-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"vue": "^3.4.25"
},
"devDependencies": {
"@0no-co/graphqlsp": "^1.12.0",
"@0no-co/graphqlsp": "^1.12.9",
"@vitejs/plugin-vue": "^5.0.4",
"typescript": "^5.4.2",
"typescript": "^5.5.2",
"vite": "^5.2.10",
"vue-tsc": "^2.0.14"
}
Expand Down
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"LICENSE.md",
"README.md",
"cli/",
"ts-plugin/",
"bin/",
"dist/"
],
Expand All @@ -38,13 +39,23 @@
"require": "./dist/gql-tada-internal.js",
"source": "./src/internal/index.ts"
},
"./ts-plugin": {
"types": "./dist/gql-tada-ts-plugin.d.ts",
"import": "./dist/gql-tada-ts-plugin.mjs",
"require": "./dist/gql-tada-ts-plugin.js",
"source": "./src/ts-plugin/index.ts"
},
"./package.json": "./package.json"
},
"dependencies": {
"@0no-co/graphql.web": "^1.0.5",
"@0no-co/graphqlsp": "^1.12.9",
"@gql.tada/cli-utils": "workspace:*",
"@gql.tada/internal": "workspace:*"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"public": true,
"keywords": [
"graphql",
Expand Down Expand Up @@ -93,7 +104,8 @@
"gql.tada": "workspace:*",
"@gql.tada/internal": "workspace:*",
"astro-expressive-code": "^0.31.0",
"typescript": "^5.3.3"
"typescript": "^5.5.2",
"@0no-co/graphqlsp": "^1.12.9"
}
},
"devDependencies": {
Expand Down Expand Up @@ -126,12 +138,9 @@
"rollup-plugin-cjs-check": "^1.0.3",
"rollup-plugin-dts": "^6.1.0",
"terser": "^5.26.0",
"typescript": "^5.3.3",
"typescript": "^5.5.2",
"vitest": "1.1.3"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"publishConfig": {
"access": "public",
"provenance": true
Expand Down
5 changes: 3 additions & 2 deletions packages/cli-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@
"semiver": "^1.1.0",
"typanion": "^3.14.0",
"type-fest": "^4.10.2",
"typescript": "^5.3.3",
"typescript": "^5.5.2",
"vscode-languageserver-textdocument": "^1.0.11",
"wonka": "^6.3.4"
},
"dependencies": {
"@0no-co/graphqlsp": "^1.12.8",
"@0no-co/graphqlsp": "^1.12.9",
"@gql.tada/internal": "workspace:*",
"@vue/compiler-dom": "^3.4.23",
"@vue/language-core": "^2.0.17",
"graphql": "^15.5.0 || ^16.0.0 || ^17.0.0",
"svelte2tsx": "^0.7.6"
},
"peerDependencies": {
"@0no-co/graphqlsp": "^1.12.9",
"graphql": "^15.5.0 || ^16.0.0 || ^17.0.0",
"typescript": "^5.0.0"
},
Expand Down
59 changes: 29 additions & 30 deletions packages/cli-utils/src/commands/doctor/runner.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import semiver from 'semiver';

import type { GraphQLSPConfig, LoadConfigResult } from '@gql.tada/internal';
import { loadRef, loadConfig, parseConfig } from '@gql.tada/internal';

import type { ComposeInput } from '../../term';
import { MINIMUM_VERSIONS, semverComply } from '../../utils/semver';
import { findGraphQLConfig } from './helpers/graphqlConfig';
import * as vscode from './helpers/vscode';
import * as logger from './logger';
Expand All @@ -22,11 +22,6 @@ const delay = (ms = 700) => {
}
};

const semiverComply = (version: string, compare: string) => {
const match = version.match(/\d+\.\d+\.\d+/);
return match ? semiver(match[0], compare) >= 0 : false;
};

const enum Messages {
TITLE = 'Doctor',
DESCRIPTION = 'Detects problems with your setup',
Expand All @@ -37,12 +32,6 @@ const enum Messages {
CHECK_SCHEMA = 'Checking schema',
}

const MINIMUM_VERSIONS = {
typescript: '4.1.0',
tada: '1.0.0',
lsp: '1.0.0',
};

export async function* run(): AsyncIterable<ComposeInput> {
yield logger.title(Messages.TITLE, Messages.DESCRIPTION);
yield logger.runningTask(Messages.CHECK_TS_VERSION);
Expand Down Expand Up @@ -79,7 +68,7 @@ export async function* run(): AsyncIterable<ComposeInput> {
`A version of ${logger.code('typescript')} was not found in your dependencies.\n` +
logger.hint(`Is ${logger.code('typescript')} installed in this package?`)
);
} else if (!semiverComply(typeScriptVersion[1], MINIMUM_VERSIONS.typescript)) {
} else if (!semverComply(typeScriptVersion[1], MINIMUM_VERSIONS.typescript)) {
// TypeScript version lower than v4.1 which is when they introduced template lits
yield logger.failedTask(Messages.CHECK_TS_VERSION);
throw logger.errorMessage(
Expand All @@ -94,21 +83,29 @@ export async function* run(): AsyncIterable<ComposeInput> {
yield logger.runningTask(Messages.CHECK_DEPENDENCIES);
await delay();

const gqlspVersion = deps.find((x) => x[0] === '@0no-co/graphqlsp');
if (!gqlspVersion) {
yield logger.failedTask(Messages.CHECK_DEPENDENCIES);
throw logger.errorMessage(
`A version of ${logger.code('@0no-co/graphqlsp')} was not found in your dependencies.\n` +
logger.hint(`Is ${logger.code('@0no-co/graphqlsp')} installed?`)
);
} else if (!semiverComply(gqlspVersion[1], MINIMUM_VERSIONS.lsp)) {
yield logger.failedTask(Messages.CHECK_DEPENDENCIES);
throw logger.errorMessage(
`The version of ${logger.code('@0no-co/graphqlsp')} in your dependencies is out of date.\n` +
logger.hint(
`${logger.code('gql.tada')} requires at least ${logger.bold(MINIMUM_VERSIONS.lsp)}`
)
);
const supportsEmbeddedLsp = semverComply(
typeScriptVersion[1],
MINIMUM_VERSIONS.typescript_embed_lsp
);
if (!supportsEmbeddedLsp) {
const gqlspVersion = deps.find((x) => x[0] === '@0no-co/graphqlsp');
if (!gqlspVersion) {
yield logger.failedTask(Messages.CHECK_DEPENDENCIES);
throw logger.errorMessage(
`A version of ${logger.code('@0no-co/graphqlsp')} was not found in your dependencies.\n` +
logger.hint(`Is ${logger.code('@0no-co/graphqlsp')} installed?`)
);
} else if (!semverComply(gqlspVersion[1], MINIMUM_VERSIONS.lsp)) {
yield logger.failedTask(Messages.CHECK_DEPENDENCIES);
throw logger.errorMessage(
`The version of ${logger.code(
'@0no-co/graphqlsp'
)} in your dependencies is out of date.\n` +
logger.hint(
`${logger.code('gql.tada')} requires at least ${logger.bold(MINIMUM_VERSIONS.lsp)}`
)
);
}
}

const gqlTadaVersion = deps.find((x) => x[0] === 'gql.tada');
Expand All @@ -118,7 +115,7 @@ export async function* run(): AsyncIterable<ComposeInput> {
`A version of ${logger.code('gql.tada')} was not found in your dependencies.\n` +
logger.hint(`Is ${logger.code('gql.tada')} installed?`)
);
} else if (!semiverComply(gqlTadaVersion[1], '1.0.0')) {
} else if (!semverComply(gqlTadaVersion[1], '1.0.0')) {
yield logger.failedTask(Messages.CHECK_DEPENDENCIES);
throw logger.errorMessage(
`The version of ${logger.code('gql.tada')} in your dependencies is out of date.\n` +
Expand Down Expand Up @@ -151,7 +148,9 @@ export async function* run(): AsyncIterable<ComposeInput> {
} catch (error) {
yield logger.failedTask(Messages.CHECK_TSCONFIG);
throw logger.externalError(
`The plugin configuration for ${logger.code('"@0no-co/graphqlsp"')} seems to be invalid.`,
`The plugin configuration for ${logger.code(
supportsEmbeddedLsp ? '"gql.tada/ts-plugin"' : '"@0no-co/graphqlsp"'
)} seems to be invalid.`,
error
);
}
Expand Down
75 changes: 55 additions & 20 deletions packages/cli-utils/src/commands/init/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs/promises';
import path from 'node:path';
import { execa } from 'execa';

import { MINIMUM_VERSIONS, semverComply } from '../../utils/semver';
import { readTSConfigFile } from '@gql.tada/internal';

const s = spinner();
Expand Down Expand Up @@ -90,24 +91,51 @@ export async function run(target: string) {
process.exit(0);
}

let supportsEmbeddedLsp = false;
let packageJson: {
dependencies: Record<string, string>;
devDependencies: Record<string, string>;
};

try {
const packageJsonPath = path.resolve(target, 'package.json');
const packageJsonContents = await fs.readFile(packageJsonPath, 'utf-8');
packageJson = JSON.parse(packageJsonContents);
const deps = Object.entries({
...packageJson.dependencies,
...packageJson.devDependencies,
});

const typeScriptVersion = deps.find((x) => x[0] === 'typescript');
if (typeScriptVersion && typeof typeScriptVersion[1] === 'string') {
supportsEmbeddedLsp = semverComply(
typeScriptVersion[1],
MINIMUM_VERSIONS.typescript_embed_lsp
);
}
} catch (e) {}

if (shouldInstallDependencies) {
s.start('Installing packages.');
await installPackages(getPkgManager(), target);
await installPackages(getPkgManager(), target, !supportsEmbeddedLsp);
s.stop('Installed packages.');
} else {
s.start('Writing to package.json.');
try {
const packageJsonPath = path.resolve(target, 'package.json');
const packageJsonContents = await fs.readFile(packageJsonPath, 'utf-8');
const packageJson = JSON.parse(packageJsonContents);
packageJson = JSON.parse(packageJsonContents);

if (!packageJson.dependencies) packageJson.dependencies = {};
if (!packageJson.dependencies['gql.tada']) {
packageJson.dependencies['gql.tada'] = TADA_VERSION;
}

if (!packageJson.devDependencies) packageJson.devDependencies = {};
if (!packageJson.devDependencies['@0no-co/graphqlsp']) {
packageJson.devDependencies['@0no-co/graphqlsp'] = LSP_VERSION;
if (!supportsEmbeddedLsp) {
if (!packageJson.devDependencies) packageJson.devDependencies = {};
if (!packageJson.devDependencies['@0no-co/graphqlsp']) {
packageJson.devDependencies['@0no-co/graphqlsp'] = LSP_VERSION;
}
}

await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
Expand All @@ -129,7 +157,7 @@ export async function run(target: string) {
...tsConfig.compilerOptions,
plugins: [
{
name: '@0no-co/graphqlsp',
name: supportsEmbeddedLsp ? 'gql.tada/ts-plugin' : '@0no-co/graphqlsp',
schema: isFile ? path.relative(target, schemaLocation) : schemaLocation,
tadaOutputLocation: path.relative(target, tadaLocation),
} as any,
Expand All @@ -143,20 +171,27 @@ export async function run(target: string) {
}

type PackageManager = 'yarn' | 'pnpm' | 'npm';
async function installPackages(packageManager: PackageManager, target: string) {
await execa(
packageManager,
[
// `yarn add` will fail if nothing is provided
packageManager === 'yarn' ? 'add' : 'install',
'-D',
'@0no-co/graphqlsp',
],
{
stdio: 'ignore',
cwd: target,
}
);
async function installPackages(
packageManager: PackageManager,
target: string,
shouldInstallGraphQLSP
) {
if (shouldInstallGraphQLSP) {
await execa(
packageManager,
[
// `yarn add` will fail if nothing is provided
packageManager === 'yarn' ? 'add' : 'install',
'-D',
'@0no-co/graphqlsp',
],
{
stdio: 'ignore',
cwd: target,
}
);
}

await execa(packageManager, [packageManager === 'yarn' ? 'add' : 'install', 'gql.tada'], {
stdio: 'ignore',
cwd: target,
Expand Down
13 changes: 13 additions & 0 deletions packages/cli-utils/src/utils/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import semiver from 'semiver';

export const MINIMUM_VERSIONS = {
typescript_embed_lsp: '5.5.0',
typescript: '4.1.0',
tada: '1.0.0',
lsp: '1.0.0',
};

export const semverComply = (version: string, compare: string) => {
const match = version.match(/\d+\.\d+\.\d+/);
return match ? semiver(match[0], compare) >= 0 : false;
};
2 changes: 1 addition & 1 deletion packages/internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"rollup": "^4.9.4",
"sade": "^1.8.1",
"type-fest": "^4.10.2",
"typescript": "^5.3.3"
"typescript": "^5.5.2"
},
"dependencies": {
"@0no-co/graphql.web": "^1.0.5"
Expand Down
5 changes: 4 additions & 1 deletion packages/internal/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ const getPluginConfig = (tsconfig: TsConfigJson | null): Record<string, unknown>
tsconfig.compilerOptions &&
tsconfig.compilerOptions.plugins &&
tsconfig.compilerOptions.plugins.find(
(x) => x.name === '@0no-co/graphqlsp' || x.name === 'gql.tada/lsp'
(x) =>
x.name === '@0no-co/graphqlsp' ||
x.name === 'gql.tada/lsp' ||
x.name === 'gql.tada/ts-plugin'
)) ||
null;

Expand Down
Loading