Skip to content

Commit

Permalink
feat(cli-utils): Add separate support packages for Vue & Svelte (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Jul 31, 2024
1 parent 02aff42 commit 478968b
Show file tree
Hide file tree
Showing 19 changed files with 604 additions and 93 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-fishes-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/cli-utils": minor
---

Split `.vue` and `.svelte` SFC file support out into support packages. If you need Vue support, you must now install `@gql.tada/vue-support` alongside `gql.tada`, and if you need Svelte support, you must now install `@gql.tada/svelte-support` alongside `gql.tada`.
24 changes: 0 additions & 24 deletions packages/cli-utils/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,6 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## @jridgewell/sourcemap-codec

The MIT License

Copyright (c) 2015 Rich Harris

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

## @volar/source-map

MIT License
Expand Down
17 changes: 11 additions & 6 deletions packages/cli-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
},
"devDependencies": {
"@clack/prompts": "^0.7.0",
"@jridgewell/sourcemap-codec": "^1.4.15",
"@types/node": "^20.11.0",
"@volar/source-map": "^2.1.6",
"clipanion": "4.0.0-rc.3",
Expand All @@ -52,19 +51,25 @@
"typanion": "^3.14.0",
"type-fest": "^4.10.2",
"typescript": "^5.5.2",
"vscode-languageserver-textdocument": "^1.0.11",
"wonka": "^6.3.4"
},
"dependencies": {
"@0no-co/graphqlsp": "^1.12.9",
"@gql.tada/internal": "workspace:*",
"@vue/compiler-dom": "^3.4.23",
"@vue/language-core": "~2.0.0",
"graphql": "^15.5.0 || ^16.0.0 || ^17.0.0",
"svelte2tsx": "^0.7.6"
"graphql": "^15.5.0 || ^16.0.0 || ^17.0.0"
},
"peerDependenciesMeta": {
"@gql.tada/svelte-support": {
"optional": true
},
"@gql.tada/vue-support": {
"optional": true
}
},
"peerDependencies": {
"@0no-co/graphqlsp": "^1.12.9",
"@gql.tada/svelte-support": "workspace:*",
"@gql.tada/vue-support": "workspace:*",
"graphql": "^15.5.0 || ^16.0.0 || ^17.0.0",
"typescript": "^5.0.0"
},
Expand Down
28 changes: 28 additions & 0 deletions packages/cli-utils/src/commands/doctor/helpers/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ export const getGqlTadaVersion = async (meta: PackageJson): Promise<string | nul
return null;
}
};

export const hasSvelteSupport = async (meta: PackageJson): Promise<boolean> => {
const pkg = '@gql.tada/svelte-support';
const isInstalled = !!meta.devDependencies?.[pkg] || !!meta.devDependencies?.[pkg];
if (isInstalled) {
return true;
}
try {
// NOTE: Resolved from current folder, since it's a child dependency
return !!createRequire(__dirname)(`${pkg}/package.json`)?.version;
} catch (_error) {
return false;
}
};

export const hasVueSupport = async (meta: PackageJson): Promise<boolean> => {
const pkg = '@gql.tada/vue-support';
const isInstalled = !!meta.devDependencies?.[pkg] || !!meta.devDependencies?.[pkg];
if (isInstalled) {
return true;
}
try {
// NOTE: Resolved from current folder, since it's a child dependency
return !!createRequire(__dirname)(`${pkg}/package.json`)?.version;
} catch (_error) {
return false;
}
};
50 changes: 50 additions & 0 deletions packages/cli-utils/src/commands/doctor/runner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type ts from 'typescript';
import path from 'node:path';

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 { programFactory } from '../../ts';
import { findGraphQLConfig } from './helpers/graphqlConfig';
import * as versions from './helpers/versions';
import * as vscode from './helpers/vscode';
Expand All @@ -28,6 +30,7 @@ const enum Messages {
CHECK_TS_VERSION = 'Checking TypeScript version',
CHECK_DEPENDENCIES = 'Checking installed dependencies',
CHECK_TSCONFIG = 'Checking tsconfig.json',
CHECK_EXTERNAL_FILES = 'Checking external files support',
CHECK_VSCODE = 'Checking VSCode setup',
CHECK_SCHEMA = 'Checking schema',
}
Expand Down Expand Up @@ -145,6 +148,8 @@ export async function* run(): AsyncIterable<ComposeInput> {

yield logger.completedTask(Messages.CHECK_TSCONFIG);

yield* runExternalFilesChecks(configResult, packageJson);

yield* runVSCodeChecks();

yield logger.runningTask(Messages.CHECK_SCHEMA);
Expand Down Expand Up @@ -216,3 +221,48 @@ async function* runVSCodeChecks(): AsyncIterable<ComposeInput> {
}
}
}

async function* runExternalFilesChecks(
configResult: LoadConfigResult,
packageJson: versions.PackageJson
): AsyncIterable<ComposeInput> {
let externalFiles: readonly ts.SourceFile[] = [];
try {
const factory = programFactory(configResult);
externalFiles = factory.createExternalFiles();
} catch (_error) {
// NOTE: If the project fails to load, we currently just ignore this check and move on
return;
}

if (externalFiles.length) {
yield logger.runningTask(Messages.CHECK_EXTERNAL_FILES);
await delay();

const extensions = new Set(
externalFiles.map((sourceFile) => path.extname(sourceFile.fileName))
);

if (extensions.has('.svelte') && !(await versions.hasSvelteSupport(packageJson))) {
yield logger.failedTask(Messages.CHECK_EXTERNAL_FILES);
throw logger.errorMessage(
`A version of ${logger.code(
'@gql.tada/svelte-support'
)} must be installed for Svelte file support.\n` +
logger.hint(`Have you installed ${logger.code('@gql.tada/svelte-support')}?`)
);
}

if (extensions.has('.vue') && !(await versions.hasVueSupport(packageJson))) {
yield logger.failedTask(Messages.CHECK_EXTERNAL_FILES);
throw logger.errorMessage(
`A version of ${logger.code(
'@gql.tada/vue-support'
)} must be installed for Vue file support.\n` +
logger.hint(`Have you installed ${logger.code('@gql.tada/vue-support')}?`)
);
}

yield logger.completedTask(Messages.CHECK_EXTERNAL_FILES);
}
}
5 changes: 4 additions & 1 deletion packages/cli-utils/src/commands/shared/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export function externalError(message: string, error: unknown) {
(error.name === 'TSError' || error.name === 'TadaError' || 'code' in error)
) {
title = 'code' in error ? 'System Error' : 'Error';
text = (error as Error).message.trim();
text =
error.name === 'TadaError'
? t.text([t.cmd(t.CSI.Style, t.Style.Blue), (error as Error).message])
: (error as Error).message.trim();
} else if ('stack' in error && typeof error.stack === 'string') {
title = 'Unexpected Error';
text = `${error.stack}`;
Expand Down
75 changes: 75 additions & 0 deletions packages/cli-utils/src/ts/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type ts from 'typescript';
import * as path from 'node:path';

import { TadaError, TadaErrorCode } from '../utils/error';

let _svelte: typeof import('@gql.tada/svelte-support');
let _vue: typeof import('@gql.tada/vue-support');

const transformSvelte = async (
...args: Parameters<typeof _svelte.transform>
): Promise<ReturnType<typeof _svelte.transform>> => {
if (!_svelte) {
try {
_svelte = await import('@gql.tada/svelte-support');
} catch (_error) {
throw new TadaError(
TadaErrorCode.SVELTE_SUPPORT,
'For Svelte support the `@gql.tada/svelte-support` package must be installed.\n' +
'Install the package and try again.'
);
}
}
return _svelte.transform(...args);
};

const transformVue = async (
...args: Parameters<typeof _vue.transform>
): Promise<ReturnType<typeof _vue.transform>> => {
if (!_vue) {
try {
_vue = await import('@gql.tada/vue-support');
} catch (_error) {
throw new TadaError(
TadaErrorCode.VUE_SUPPORT,
'For Vue support the `@gql.tada/vue-support` package must be installed.\n' +
'Install the package and try again.'
);
}
}
return _vue.transform(...args);
};

const checkVue = async (): Promise<void> => {
if (!_vue) {
try {
_vue = await import('@gql.tada/vue-support');
} catch (_error) {
throw new TadaError(
TadaErrorCode.VUE_SUPPORT,
'For Vue support the `@gql.tada/vue-support` package must be installed.\n' +
'Install the package and try again.'
);
}
}
return _vue.check();
};

export const transformExtensions = ['.svelte', '.vue'] as const;

export const transform = async (sourceFile: ts.SourceFile) => {
const extname = path.extname(sourceFile.fileName);
if (extname === '.svelte') {
return transformSvelte(sourceFile);
} else if (extname === '.vue') {
await checkVue();
return transformVue(sourceFile);
} else {
throw new TadaError(
TadaErrorCode.UNKNOWN_EXTERNAL_FILE,
`Tried transforming unknown file type "${extname}". Supported: ${transformExtensions.join(
', '
)}`
);
}
};
40 changes: 0 additions & 40 deletions packages/cli-utils/src/ts/transformers/index.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/cli-utils/src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const enum TadaErrorCode {
VUE_SUPPORT,
SVELTE_SUPPORT,
UNKNOWN_EXTERNAL_FILE,
}

export class TadaError extends Error {
static isTadaError(error: unknown): error is TadaError {
return !!(typeof error === 'object' && error && 'name' in error && error.name === 'TadaError');
}

readonly code: TadaErrorCode;
constructor(code: TadaErrorCode, message: string) {
super(message);
this.code = code;
this.name = 'TadaError';
}
}
21 changes: 21 additions & 0 deletions packages/svelte-support/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 0no.co

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 478968b

Please sign in to comment.