Skip to content

Commit 9ea466f

Browse files
authored
make adhoc files saved by default (#8025)
* made adhoc files saved by default * fix test and add changelog * prettier * add longer wait * close all editors between exections * bump time * bump time
1 parent a9bc138 commit 9ea466f

File tree

4 files changed

+34
-47
lines changed

4 files changed

+34
-47
lines changed

firebase-vscode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## NEXT
22

3+
- [Fixed] Fixed an issue where generating an ad-hoc file would break codelenses
4+
35
## 0.10.8
46

57
- Updated internal firebase-tools dependency to 13.25.0

firebase-vscode/src/data-connect/ad-hoc-mutations.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,11 @@ query {
148148
documentPath: string,
149149
) {
150150
// generate content for the file
151-
const preamble =
152-
"# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file.";
153-
154151
const introspect = await dataConnectService.introspect();
155152
if (!introspect.data) {
156-
vscode.window.showErrorMessage("Failed to generate mutation. Please check your compilation errors.");
153+
vscode.window.showErrorMessage(
154+
"Failed to generate mutation. Please check your compilation errors.",
155+
);
157156
return;
158157
}
159158
const schema = buildClientSchema(introspect.data);
@@ -162,41 +161,24 @@ query {
162161
return;
163162
}
164163

165-
const adhocMutation = print(
166-
await makeAdHocMutation(
167-
Object.values(dataType.getFields()),
168-
ast.name.value,
169-
),
170-
);
171-
const content = [preamble, adhocMutation].join("\n");
172-
173164
// get root where dataconnect.yaml lives
174165
const configs = await firstWhereDefined(dataConnectConfigs);
175166
const dataconnectConfig =
176167
configs.tryReadValue?.findEnclosingServiceForPath(documentPath);
177168
const basePath = dataconnectConfig?.path;
178169

179-
const filePath = vscode.Uri.file(`${basePath}/${ast.name.value}_insert.gql`);
180-
const doesFileExist = await checkIfFileExists(filePath);
181-
182-
if (!doesFileExist) {
183-
// opens unsaved text document with name "[mutationName]_insert.gql"
170+
const filePath = vscode.Uri.file(
171+
`${basePath}/${ast.name.value}_insert.gql`,
172+
);
184173

185-
vscode.workspace
186-
.openTextDocument(filePath.with({ scheme: "untitled" }))
187-
.then((doc) => {
188-
vscode.window.showTextDocument(doc).then((openDoc) => {
189-
openDoc.edit((edit) => {
190-
edit.insert(new vscode.Position(0, 0), content);
191-
});
192-
});
193-
});
194-
} else {
195-
// Opens existing text document
196-
vscode.workspace.openTextDocument(filePath).then((doc) => {
197-
vscode.window.showTextDocument(doc);
198-
});
199-
}
174+
await upsertFile(filePath, () => {
175+
const preamble =
176+
"# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file.";
177+
const adhocMutation = print(
178+
makeAdHocMutation(Object.values(dataType.getFields()), ast.name.value),
179+
);
180+
return [preamble, adhocMutation].join("\n");
181+
});
200182
}
201183

202184
function makeAdHocMutation(

firebase-vscode/src/data-connect/file-utils.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,11 @@ export async function upsertFile(
2424
): Promise<void> {
2525
const doesFileExist = await checkIfFileExists(uri);
2626

27-
if (!doesFileExist) {
28-
const doc = await vscode.workspace.openTextDocument(
29-
uri.with({ scheme: "untitled" }),
30-
);
31-
const editor = await vscode.window.showTextDocument(doc);
3227

33-
await editor.edit((edit) =>
34-
edit.insert(new vscode.Position(0, 0), content()),
35-
);
36-
return;
28+
// Have to write to file system first before opening
29+
// otherwise we can't save it without closing it
30+
if (!doesFileExist) {
31+
vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(content()));
3732
}
3833

3934
// Opens existing text document

firebase-vscode/src/test/integration/fishfood/graphql.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ firebaseSuite("GraphQL", async function () {
135135
await addDataButton.click();
136136

137137
// Wait a bit for the mutation to be generated
138-
await browser.pause(1500);
138+
await browser.pause(5000);
139139

140140
// Verify the generated mutation
141141
const activeEditor = await editorView.getActiveEditor();
@@ -150,7 +150,9 @@ firebaseSuite("GraphQL", async function () {
150150
}"`);
151151
expect(editorTitle).toBe("Post_insert.gql");
152152

153-
await editorView.closeCurrentEditor();
153+
// file should be created, saved, then opened
154+
expect(activeEditor?.document.isDirty).toBe(false);
155+
await editorView.closeAllEditors();
154156
},
155157
);
156158

@@ -185,7 +187,7 @@ firebaseSuite("GraphQL", async function () {
185187
await readDataButton.click();
186188

187189
// Wait a bit for the query to be generated
188-
await browser.pause(1000);
190+
await browser.pause(5000);
189191

190192
// Verify the generated query
191193
const activeEditor = await editorView.getActiveEditor();
@@ -199,6 +201,10 @@ firebaseSuite("GraphQL", async function () {
199201
}
200202
}`);
201203
expect(editorTitle).toBe("Post_read.gql");
204+
205+
// file should be created, saved, then opened
206+
expect(activeEditor?.document.isDirty).toBe(false);
207+
await editorView.closeAllEditors();
202208
},
203209
);
204210

@@ -242,7 +248,7 @@ firebaseSuite("GraphQL", async function () {
242248
"test_projects/fishfood/dataconnect/Post_insert.gql",
243249
);
244250

245-
await editorView.closeCurrentEditor();
251+
await editorView.closeAllEditors();
246252
},
247253
);
248254

@@ -282,8 +288,10 @@ firebaseSuite("GraphQL", async function () {
282288
// Verify the generated query file path
283289
const activeEditor = await editorView.getActiveEditor();
284290
const filePath = activeEditor?.document.fileName;
285-
expect(filePath).toContain("test_projects/fishfood/dataconnect/Post_read.gql");
286-
await editorView.closeCurrentEditor();
291+
expect(filePath).toContain(
292+
"test_projects/fishfood/dataconnect/Post_read.gql",
293+
);
294+
await editorView.closeAllEditors();
287295
},
288296
);
289297
});

0 commit comments

Comments
 (0)