1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.GraphQLWorkspace = void 0;
|
7 | const path_1 = require("path");
|
8 | const glob_1 = __importDefault(require("glob"));
|
9 | const config_1 = require("./config");
|
10 | const client_1 = require("./project/client");
|
11 | const service_1 = require("./project/service");
|
12 | const vscode_uri_1 = __importDefault(require("vscode-uri"));
|
13 | const utilities_1 = require("./utilities");
|
14 | class GraphQLWorkspace {
|
15 | constructor(LanguageServerLoadingHandler, config) {
|
16 | this.LanguageServerLoadingHandler = LanguageServerLoadingHandler;
|
17 | this.config = config;
|
18 | this._projectForFileCache = new Map();
|
19 | this.projectsByFolderUri = new Map();
|
20 | }
|
21 | onDiagnostics(handler) {
|
22 | this._onDiagnostics = handler;
|
23 | }
|
24 | onDecorations(handler) {
|
25 | this._onDecorations = handler;
|
26 | }
|
27 | onSchemaTags(handler) {
|
28 | this._onSchemaTags = handler;
|
29 | }
|
30 | onConfigFilesFound(handler) {
|
31 | this._onConfigFilesFound = handler;
|
32 | }
|
33 | createProject({ config, folder }) {
|
34 | const { clientIdentity } = this.config;
|
35 | const project = config_1.isClientConfig(config)
|
36 | ? new client_1.GraphQLClientProject({
|
37 | config,
|
38 | loadingHandler: this.LanguageServerLoadingHandler,
|
39 | rootURI: vscode_uri_1.default.parse(folder.uri),
|
40 | clientIdentity
|
41 | })
|
42 | : new service_1.GraphQLServiceProject({
|
43 | config: config,
|
44 | loadingHandler: this.LanguageServerLoadingHandler,
|
45 | rootURI: vscode_uri_1.default.parse(folder.uri),
|
46 | clientIdentity
|
47 | });
|
48 | project.onDiagnostics(params => {
|
49 | this._onDiagnostics && this._onDiagnostics(params);
|
50 | });
|
51 | if (client_1.isClientProject(project)) {
|
52 | project.onDecorations(params => {
|
53 | this._onDecorations && this._onDecorations(params);
|
54 | });
|
55 | project.onSchemaTags(tags => {
|
56 | this._onSchemaTags && this._onSchemaTags(tags);
|
57 | });
|
58 | }
|
59 | project.whenReady.then(() => project.validate());
|
60 | return project;
|
61 | }
|
62 | async addProjectsInFolder(folder) {
|
63 | const apolloConfigFiles = glob_1.default.sync("**/apollo.config.@(js|ts)", {
|
64 | cwd: vscode_uri_1.default.parse(folder.uri).fsPath,
|
65 | absolute: true,
|
66 | ignore: "**/node_modules/**"
|
67 | });
|
68 | const apolloConfigFolders = new Set(apolloConfigFiles.map(path_1.dirname));
|
69 | let foundConfigs = [];
|
70 | const projectConfigs = Array.from(apolloConfigFolders).map(configFolder => config_1.loadConfig({ configPath: configFolder, requireConfig: true })
|
71 | .then(config => {
|
72 | if (config) {
|
73 | foundConfigs.push(config);
|
74 | const projectsForConfig = config.projects.map(projectConfig => this.createProject({ config, folder }));
|
75 | const existingProjects = this.projectsByFolderUri.get(folder.uri) || [];
|
76 | this.projectsByFolderUri.set(folder.uri, [
|
77 | ...existingProjects,
|
78 | ...projectsForConfig
|
79 | ]);
|
80 | }
|
81 | else {
|
82 | utilities_1.Debug.error(`Workspace failed to load config from: ${configFolder}/`);
|
83 | }
|
84 | })
|
85 | .catch(error => utilities_1.Debug.error(error)));
|
86 | await Promise.all(projectConfigs);
|
87 | if (this._onConfigFilesFound) {
|
88 | this._onConfigFilesFound(foundConfigs);
|
89 | }
|
90 | }
|
91 | reloadService() {
|
92 | this._projectForFileCache.clear();
|
93 | this.projectsByFolderUri.forEach((projects, uri) => {
|
94 | this.projectsByFolderUri.set(uri, projects.map(project => {
|
95 | project.clearAllDiagnostics();
|
96 | return this.createProject({
|
97 | config: project.config,
|
98 | folder: { uri }
|
99 | });
|
100 | }));
|
101 | });
|
102 | }
|
103 | async reloadProjectForConfig(configUri) {
|
104 | const configPath = path_1.dirname(vscode_uri_1.default.parse(configUri).fsPath);
|
105 | let config, error;
|
106 | try {
|
107 | config = await config_1.loadConfig({ configPath, requireConfig: true });
|
108 | }
|
109 | catch (e) {
|
110 | error = e;
|
111 | }
|
112 | const project = this.projectForFile(configUri);
|
113 | if (!config && this._onConfigFilesFound) {
|
114 | this._onConfigFilesFound(error);
|
115 | }
|
116 | if (project && config) {
|
117 | await Promise.all(project.updateConfig(config));
|
118 | this.reloadService();
|
119 | }
|
120 | if (!project && config) {
|
121 | const folderUri = vscode_uri_1.default.file(configPath).toString();
|
122 | const newProject = this.createProject({
|
123 | config,
|
124 | folder: { uri: folderUri }
|
125 | });
|
126 | const existingProjects = this.projectsByFolderUri.get(folderUri) || [];
|
127 | this.projectsByFolderUri.set(folderUri, [
|
128 | ...existingProjects,
|
129 | newProject
|
130 | ]);
|
131 | this.reloadService();
|
132 | }
|
133 | }
|
134 | updateSchemaTag(selection) {
|
135 | const serviceID = selection.detail;
|
136 | if (!serviceID)
|
137 | return;
|
138 | this.projectsByFolderUri.forEach(projects => {
|
139 | projects.forEach(project => {
|
140 | if (client_1.isClientProject(project) && project.serviceID === serviceID) {
|
141 | project.updateSchemaTag(selection.label);
|
142 | }
|
143 | });
|
144 | });
|
145 | }
|
146 | removeProjectsInFolder(folder) {
|
147 | const projects = this.projectsByFolderUri.get(folder.uri);
|
148 | if (projects) {
|
149 | projects.forEach(project => project.clearAllDiagnostics());
|
150 | this.projectsByFolderUri.delete(folder.uri);
|
151 | }
|
152 | }
|
153 | get projects() {
|
154 | return Array.from(this.projectsByFolderUri.values()).flat();
|
155 | }
|
156 | projectForFile(uri) {
|
157 | const cachedResult = this._projectForFileCache.get(uri);
|
158 | if (cachedResult) {
|
159 | return cachedResult;
|
160 | }
|
161 | for (const projects of this.projectsByFolderUri.values()) {
|
162 | const project = projects.find(project => project.includesFile(uri));
|
163 | if (project) {
|
164 | this._projectForFileCache.set(uri, project);
|
165 | return project;
|
166 | }
|
167 | }
|
168 | return undefined;
|
169 | }
|
170 | }
|
171 | exports.GraphQLWorkspace = GraphQLWorkspace;
|
172 |
|
\ | No newline at end of file |