Skip to content

Commit b202d83

Browse files
committed
Added configDirURI to the ApolloConfig class
1 parent 828ea13 commit b202d83

5 files changed

Lines changed: 17 additions & 40 deletions

File tree

packages/apollo-language-server/src/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cosmiconfig from "cosmiconfig";
22
import { LoaderEntry } from "cosmiconfig";
33
import TypeScriptLoader from "@endemolshinegroup/cosmiconfig-typescript-loader";
4-
import { resolve } from "path";
4+
import { resolve, dirname } from "path";
55
import { readFileSync, existsSync } from "fs";
66
import { merge } from "lodash/fp";
77

@@ -194,6 +194,7 @@ export class ApolloConfig {
194194
public name?: string;
195195
public service?: ServiceConfigFormat;
196196
public client?: ClientConfigFormat;
197+
public configDirURI?: URI; // dir containing the config
197198
private _tag?: string;
198199

199200
constructor(public rawConfig: ApolloConfigFormat, public configURI?: URI) {
@@ -203,6 +204,10 @@ export class ApolloConfig {
203204
this.name = getServiceName(rawConfig);
204205
this.client = rawConfig.client;
205206
this.service = rawConfig.service;
207+
this.configDirURI =
208+
configURI && configURI.fsPath.includes(".js")
209+
? URI.parse(dirname(configURI.fsPath))
210+
: configURI;
206211
}
207212

208213
get projects() {

packages/apollo-language-server/src/fileSet.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import { relative, dirname } from "path";
1+
import { relative } from "path";
22
import minimatch = require("minimatch");
33
import * as glob from "glob";
44
import { invariant } from "@apollographql/apollo-tools";
55
import URI from "vscode-uri";
66

77
export class FileSet {
8-
private configDirURI?: URI;
98
private rootURI: URI;
109
private includes: string[];
1110
private excludes: string[];
1211

1312
constructor({
14-
configURI,
1513
rootURI,
1614
includes,
1715
excludes
1816
}: {
19-
configURI?: URI;
2017
rootURI: URI;
2118
includes: string[];
2219
excludes: string[];
@@ -25,24 +22,14 @@ export class FileSet {
2522
invariant(includes, `Must provide "includes".`);
2623
invariant(excludes, `Must provide "excludes".`);
2724

28-
// the URI of the folder _containing_ the apollo.config.js
29-
this.configDirURI =
30-
configURI && configURI.fsPath.includes(".js")
31-
? URI.parse(dirname(configURI.fsPath))
32-
: configURI;
3325
this.rootURI = rootURI;
3426
this.includes = includes;
3527
this.excludes = excludes;
3628
}
3729

38-
includesFile(filePath: string, configDirURI?: URI): boolean {
39-
// if we're given a path to the config file, we should match the
40-
// "includes" glob relative to that dir. This allows us to run this command
41-
// from a "root" dir above the root of the project, like when at the root of a monorepo
42-
filePath = relative(
43-
configDirURI ? configDirURI.path : this.rootURI.fsPath,
44-
filePath
45-
);
30+
includesFile(filePath: string): boolean {
31+
filePath = relative(this.rootURI.fsPath, filePath);
32+
4633
return (
4734
this.includes.some(include => minimatch(filePath, include)) &&
4835
!this.excludes.some(exclude => minimatch(filePath, exclude))
@@ -52,25 +39,12 @@ export class FileSet {
5239
allFiles(): string[] {
5340
return this.includes
5441
.flatMap(include =>
55-
glob.sync(include, {
56-
cwd: this.configDirURI
57-
? this.configDirURI.fsPath
58-
: this.rootURI.fsPath,
59-
absolute: true
60-
})
42+
glob.sync(include, { cwd: this.rootURI.fsPath, absolute: true })
6143
)
6244
.filter(
6345
filePath =>
6446
!this.excludes.some(exclude =>
65-
minimatch(
66-
relative(
67-
this.configDirURI
68-
? this.configDirURI.fsPath
69-
: this.rootURI.fsPath,
70-
filePath
71-
),
72-
exclude
73-
)
47+
minimatch(relative(this.rootURI.fsPath, filePath), exclude)
7448
)
7549
);
7650
}

packages/apollo-language-server/src/project/base.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ export abstract class GraphQLProject implements GraphQLSchemaProvider {
158158
}
159159

160160
includesFile(uri: DocumentUri) {
161-
return this.fileSet.includesFile(
162-
URI.parse(uri).fsPath,
163-
this.config.configURI
164-
);
161+
return this.fileSet.includesFile(URI.parse(uri).fsPath);
165162
}
166163

167164
async scanAllIncludedFiles() {

packages/apollo-language-server/src/project/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ export class GraphQLClientProject extends GraphQLProject {
9191
clientIdentity
9292
}: GraphQLClientProjectConfig) {
9393
const fileSet = new FileSet({
94-
configURI: config.configURI,
95-
rootURI: rootURI,
94+
// the URI of the folder _containing_ the apollo.config.js is the true project's root.
95+
// if a config doesn't have a uri associated, we can assume the `rootURI` is the project's root.
96+
rootURI: config.configDirURI || rootURI,
9697
includes: config.client.includes,
9798
excludes: config.client.excludes
9899
});

packages/apollo-language-server/src/project/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class GraphQLServiceProject extends GraphQLProject {
2525
loadingHandler
2626
}: GraphQLServiceProjectConfig) {
2727
const fileSet = new FileSet({
28-
rootURI: rootURI,
28+
rootURI: config.configDirURI || rootURI,
2929
includes: config.service.includes,
3030
excludes: config.service.excludes
3131
});

0 commit comments

Comments
 (0)