Skip to content
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(internal): Add support for ${configDir} to directory path configs #239

Merged
merged 4 commits into from
Apr 21, 2024
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/tame-beers-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/cli-utils": minor
---

Add support for `${configDir}` to directory path configs. When used in paths in our configuration, `${configDir}` will be substituted with the location of the main `tsconfig.json`
5 changes: 5 additions & 0 deletions .changeset/twenty-students-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/internal": patch
---

Add support for `${configDir}` to directory path configs.
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/check/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function* run(tty: TTY, opts: Options): AsyncIterable<ComposeInput>
let pluginConfig: GraphQLSPConfig;
try {
configResult = await loadConfig(opts.tsconfig);
pluginConfig = parseConfig(configResult.pluginConfig);
pluginConfig = parseConfig(configResult.pluginConfig, configResult.rootPath);
} catch (error) {
throw logger.externalError('Failed to load configuration.', error);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/doctor/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export async function* run(): AsyncIterable<ComposeInput> {

let pluginConfig: GraphQLSPConfig;
try {
pluginConfig = parseConfig(configResult.pluginConfig);
pluginConfig = parseConfig(configResult.pluginConfig, configResult.rootPath);
} catch (error) {
throw logger.externalError(
`The plugin configuration for ${logger.code('"@0no-co/graphqlsp"')} seems to be invalid.`,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/generate-output/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function* run(tty: TTY, opts: OutputOptions): AsyncIterable<Compose
let pluginConfig: GraphQLSPConfig;
try {
configResult = await loadConfig(opts.tsconfig);
pluginConfig = parseConfig(configResult.pluginConfig);
pluginConfig = parseConfig(configResult.pluginConfig, configResult.rootPath);
} catch (error) {
throw logger.externalError('Failed to load configuration.', error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function* run(tty: TTY, opts: PersistedOptions): AsyncIterable<Comp
let pluginConfig: GraphQLSPConfig;
try {
configResult = await loadConfig(opts.tsconfig);
pluginConfig = parseConfig(configResult.pluginConfig);
pluginConfig = parseConfig(configResult.pluginConfig, configResult.rootPath);
} catch (error) {
throw logger.externalError('Failed to load configuration.', error);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/generate-schema/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function* run(tty: TTY, opts: SchemaOptions): AsyncIterable<Compose
let pluginConfig: GraphQLSPConfig;
try {
configResult = await loadConfig(opts.tsconfig);
pluginConfig = parseConfig(configResult.pluginConfig);
pluginConfig = parseConfig(configResult.pluginConfig, configResult.rootPath);
} catch (error) {
throw logger.externalError('Failed to load configuration.', error);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-utils/src/commands/turbo/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function* run(tty: TTY, opts: TurboOptions): AsyncIterable<ComposeI
let pluginConfig: GraphQLSPConfig;
try {
configResult = await loadConfig(opts.tsconfig);
pluginConfig = parseConfig(configResult.pluginConfig);
pluginConfig = parseConfig(configResult.pluginConfig, configResult.rootPath);
} catch (error) {
throw logger.externalError('Failed to load configuration.', error);
}
Expand Down
45 changes: 42 additions & 3 deletions packages/internal/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as path from 'node:path';
import { TadaError } from './errors';
import type { SchemaOrigin } from './loaders/types';
import { getURLConfig } from './loaders';
import type { SchemaOrigin } from './loaders';

export interface GraphQLSPConfig {
schema: SchemaOrigin;
Expand All @@ -9,7 +11,31 @@ export interface GraphQLSPConfig {
template?: string;
}

export const parseConfig = (input: Record<string, unknown>) => {
export const parseConfig = (
input: Record<string, unknown>,
/** Defines the path of the "main" `tsconfig.json` file.
* @remarks
* This should be the `rootPath` output from `loadConfig`,
* which is the path of the user's `tsconfig.json` before
* resolving `extends` options.
*/
rootPath: string = process.cwd()
) => {
const resolveConfigDir = (input: string | undefined) => {
if (!input) return input;
return path.normalize(
input.replace(/\${([^}]+)}/, (_match, name) => {
if (name === 'configDir') {
return rootPath;
} else {
throw new TadaError(
`Substitution "\${${name}}" is not recognized (did you mean 'configDir'?)`
);
}
})
);
};

if (input.schema && typeof input.schema === 'object') {
const { schema } = input;
if (!('url' in schema)) {
Expand Down Expand Up @@ -59,5 +85,18 @@ export const parseConfig = (input: Record<string, unknown>) => {
throw new TadaError("Configuration contains a `template` property, but it's not a string");
}

return input as any as GraphQLSPConfig;
const output = input as any as GraphQLSPConfig;

let schema: SchemaOrigin = output.schema;
if (typeof schema === 'string') {
const url = getURLConfig(schema);
if (!url) schema = resolveConfigDir(schema) || schema;
}

return {
...output,
tadaOutputLocation: resolveConfigDir(output.tadaOutputLocation),
tadaTurboLocation: resolveConfigDir(output.tadaTurboLocation),
tadaPersistedLocation: resolveConfigDir(output.tadaPersistedLocation),
} satisfies GraphQLSPConfig;
};
2 changes: 1 addition & 1 deletion packages/internal/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { loadFromURL } from './url';

export { loadFromSDL, loadFromURL };

const getURLConfig = (origin: SchemaOrigin | null) => {
export const getURLConfig = (origin: SchemaOrigin | null) => {
try {
return origin
? {
Expand Down
Loading