From ec9447f3fd373753869e79a129209b1a5676d1fb Mon Sep 17 00:00:00 2001 From: Harold Shen Date: Tue, 3 Dec 2024 19:40:35 -0500 Subject: [PATCH 1/7] made adhoc files saved by default --- .../src/data-connect/ad-hoc-mutations.ts | 46 ++++++------------- .../src/data-connect/file-utils.ts | 13 ++---- .../src/test/integration/fishfood/graphql.ts | 5 ++ 3 files changed, 23 insertions(+), 41 deletions(-) diff --git a/firebase-vscode/src/data-connect/ad-hoc-mutations.ts b/firebase-vscode/src/data-connect/ad-hoc-mutations.ts index 710e24bd8e0..a5de72d1e69 100644 --- a/firebase-vscode/src/data-connect/ad-hoc-mutations.ts +++ b/firebase-vscode/src/data-connect/ad-hoc-mutations.ts @@ -148,12 +148,11 @@ query { documentPath: string, ) { // generate content for the file - const preamble = - "# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file."; - const introspect = await dataConnectService.introspect(); if (!introspect.data) { - vscode.window.showErrorMessage("Failed to generate mutation. Please check your compilation errors."); + vscode.window.showErrorMessage( + "Failed to generate mutation. Please check your compilation errors.", + ); return; } const schema = buildClientSchema(introspect.data); @@ -162,41 +161,24 @@ query { return; } - const adhocMutation = print( - await makeAdHocMutation( - Object.values(dataType.getFields()), - ast.name.value, - ), - ); - const content = [preamble, adhocMutation].join("\n"); - // get root where dataconnect.yaml lives const configs = await firstWhereDefined(dataConnectConfigs); const dataconnectConfig = configs.tryReadValue?.findEnclosingServiceForPath(documentPath); const basePath = dataconnectConfig?.path; - const filePath = vscode.Uri.file(`${basePath}/${ast.name.value}_insert.gql`); - const doesFileExist = await checkIfFileExists(filePath); - - if (!doesFileExist) { - // opens unsaved text document with name "[mutationName]_insert.gql" + const filePath = vscode.Uri.file( + `${basePath}/${ast.name.value}_insert.gql`, + ); - vscode.workspace - .openTextDocument(filePath.with({ scheme: "untitled" })) - .then((doc) => { - vscode.window.showTextDocument(doc).then((openDoc) => { - openDoc.edit((edit) => { - edit.insert(new vscode.Position(0, 0), content); - }); - }); - }); - } else { - // Opens existing text document - vscode.workspace.openTextDocument(filePath).then((doc) => { - vscode.window.showTextDocument(doc); - }); - } + await upsertFile(filePath, () => { + const preamble = + "# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file."; + const adhocMutation = print( + makeAdHocMutation(Object.values(dataType.getFields()), ast.name.value), + ); + return [preamble, adhocMutation].join("\n"); + }); } function makeAdHocMutation( diff --git a/firebase-vscode/src/data-connect/file-utils.ts b/firebase-vscode/src/data-connect/file-utils.ts index 386873010da..d7939744774 100644 --- a/firebase-vscode/src/data-connect/file-utils.ts +++ b/firebase-vscode/src/data-connect/file-utils.ts @@ -24,16 +24,11 @@ export async function upsertFile( ): Promise { const doesFileExist = await checkIfFileExists(uri); - if (!doesFileExist) { - const doc = await vscode.workspace.openTextDocument( - uri.with({ scheme: "untitled" }), - ); - const editor = await vscode.window.showTextDocument(doc); - await editor.edit((edit) => - edit.insert(new vscode.Position(0, 0), content()), - ); - return; + // Have to write to file system first before opening + // otherwise we can't save it without closing it + if (!doesFileExist) { + vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(content())); } // Opens existing text document diff --git a/firebase-vscode/src/test/integration/fishfood/graphql.ts b/firebase-vscode/src/test/integration/fishfood/graphql.ts index 1097736e233..c00bfa05c7f 100644 --- a/firebase-vscode/src/test/integration/fishfood/graphql.ts +++ b/firebase-vscode/src/test/integration/fishfood/graphql.ts @@ -150,6 +150,8 @@ firebaseSuite("GraphQL", async function () { }"`); expect(editorTitle).toBe("Post_insert.gql"); + // file should be created, saved, then opened + expect(activeEditor?.document.isDirty).toBe(false); await editorView.closeCurrentEditor(); }, ); @@ -199,6 +201,9 @@ firebaseSuite("GraphQL", async function () { } }`); expect(editorTitle).toBe("Post_read.gql"); + + // file should be created, saved, then opened + expect(activeEditor?.document.isDirty).toBe(false); }, ); From b3f68bf82d76a66a2c23756d70bf9bde8229576b Mon Sep 17 00:00:00 2001 From: Harold Shen Date: Wed, 4 Dec 2024 14:02:02 -0500 Subject: [PATCH 2/7] fix test and add changelog --- firebase-vscode/CHANGELOG.md | 1 + firebase-vscode/src/test/integration/fishfood/graphql.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/firebase-vscode/CHANGELOG.md b/firebase-vscode/CHANGELOG.md index 98d1c7eafbf..0b8ec2a32ce 100644 --- a/firebase-vscode/CHANGELOG.md +++ b/firebase-vscode/CHANGELOG.md @@ -1,4 +1,5 @@ ## NEXT +- [Fixed] Fixed an issue where generating an ad-hoc file would break codelenses ## 0.10.8 diff --git a/firebase-vscode/src/test/integration/fishfood/graphql.ts b/firebase-vscode/src/test/integration/fishfood/graphql.ts index c00bfa05c7f..92ab8f3fe85 100644 --- a/firebase-vscode/src/test/integration/fishfood/graphql.ts +++ b/firebase-vscode/src/test/integration/fishfood/graphql.ts @@ -135,7 +135,7 @@ firebaseSuite("GraphQL", async function () { await addDataButton.click(); // Wait a bit for the mutation to be generated - await browser.pause(1500); + await browser.pause(2500); // Verify the generated mutation const activeEditor = await editorView.getActiveEditor(); @@ -187,7 +187,7 @@ firebaseSuite("GraphQL", async function () { await readDataButton.click(); // Wait a bit for the query to be generated - await browser.pause(1000); + await browser.pause(2500); // Verify the generated query const activeEditor = await editorView.getActiveEditor(); From 46f56772cb73cbce5ae02ce1a8301d0e7f803546 Mon Sep 17 00:00:00 2001 From: Harold Shen Date: Wed, 4 Dec 2024 14:21:53 -0500 Subject: [PATCH 3/7] prettier --- firebase-vscode/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/firebase-vscode/CHANGELOG.md b/firebase-vscode/CHANGELOG.md index 0b8ec2a32ce..8ac9660b24b 100644 --- a/firebase-vscode/CHANGELOG.md +++ b/firebase-vscode/CHANGELOG.md @@ -1,4 +1,5 @@ ## NEXT + - [Fixed] Fixed an issue where generating an ad-hoc file would break codelenses ## 0.10.8 From a5f3c76bf454df211f87ed7366bc98a4e05165a4 Mon Sep 17 00:00:00 2001 From: Harold Shen Date: Wed, 4 Dec 2024 15:25:00 -0500 Subject: [PATCH 4/7] add longer wait --- firebase-vscode/src/test/integration/fishfood/graphql.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/firebase-vscode/src/test/integration/fishfood/graphql.ts b/firebase-vscode/src/test/integration/fishfood/graphql.ts index 92ab8f3fe85..bf3755f0bf8 100644 --- a/firebase-vscode/src/test/integration/fishfood/graphql.ts +++ b/firebase-vscode/src/test/integration/fishfood/graphql.ts @@ -135,7 +135,7 @@ firebaseSuite("GraphQL", async function () { await addDataButton.click(); // Wait a bit for the mutation to be generated - await browser.pause(2500); + await browser.pause(3500); // Verify the generated mutation const activeEditor = await editorView.getActiveEditor(); @@ -187,7 +187,7 @@ firebaseSuite("GraphQL", async function () { await readDataButton.click(); // Wait a bit for the query to be generated - await browser.pause(2500); + await browser.pause(3500); // Verify the generated query const activeEditor = await editorView.getActiveEditor(); @@ -204,6 +204,7 @@ firebaseSuite("GraphQL", async function () { // file should be created, saved, then opened expect(activeEditor?.document.isDirty).toBe(false); + await editorView.closeCurrentEditor(); }, ); @@ -287,7 +288,9 @@ firebaseSuite("GraphQL", async function () { // Verify the generated query file path const activeEditor = await editorView.getActiveEditor(); const filePath = activeEditor?.document.fileName; - expect(filePath).toContain("test_projects/fishfood/dataconnect/Post_read.gql"); + expect(filePath).toContain( + "test_projects/fishfood/dataconnect/Post_read.gql", + ); await editorView.closeCurrentEditor(); }, ); From df4b8cbdffd43b3cd726471cb4fa0e67f794698e Mon Sep 17 00:00:00 2001 From: Harold Shen Date: Wed, 4 Dec 2024 15:43:56 -0500 Subject: [PATCH 5/7] close all editors between exections --- firebase-vscode/src/test/integration/fishfood/graphql.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/firebase-vscode/src/test/integration/fishfood/graphql.ts b/firebase-vscode/src/test/integration/fishfood/graphql.ts index bf3755f0bf8..1c991659067 100644 --- a/firebase-vscode/src/test/integration/fishfood/graphql.ts +++ b/firebase-vscode/src/test/integration/fishfood/graphql.ts @@ -152,7 +152,7 @@ firebaseSuite("GraphQL", async function () { // file should be created, saved, then opened expect(activeEditor?.document.isDirty).toBe(false); - await editorView.closeCurrentEditor(); + await editorView.closeAllEditors(); }, ); @@ -204,7 +204,7 @@ firebaseSuite("GraphQL", async function () { // file should be created, saved, then opened expect(activeEditor?.document.isDirty).toBe(false); - await editorView.closeCurrentEditor(); + await editorView.closeAllEditors(); }, ); @@ -248,7 +248,7 @@ firebaseSuite("GraphQL", async function () { "test_projects/fishfood/dataconnect/Post_insert.gql", ); - await editorView.closeCurrentEditor(); + await editorView.closeAllEditors(); }, ); @@ -291,7 +291,7 @@ firebaseSuite("GraphQL", async function () { expect(filePath).toContain( "test_projects/fishfood/dataconnect/Post_read.gql", ); - await editorView.closeCurrentEditor(); + await editorView.closeAllEditors(); }, ); }); From f2316431b12f50f8481cd82a8f471bce701d33a8 Mon Sep 17 00:00:00 2001 From: Harold Shen Date: Wed, 4 Dec 2024 16:48:45 -0500 Subject: [PATCH 6/7] bump time --- firebase-vscode/src/test/integration/fishfood/graphql.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-vscode/src/test/integration/fishfood/graphql.ts b/firebase-vscode/src/test/integration/fishfood/graphql.ts index 1c991659067..d76befb13c8 100644 --- a/firebase-vscode/src/test/integration/fishfood/graphql.ts +++ b/firebase-vscode/src/test/integration/fishfood/graphql.ts @@ -187,7 +187,7 @@ firebaseSuite("GraphQL", async function () { await readDataButton.click(); // Wait a bit for the query to be generated - await browser.pause(3500); + await browser.pause(5000); // Verify the generated query const activeEditor = await editorView.getActiveEditor(); From eeaf597ccb9cbbf23b5d7820841a7fff7f552a82 Mon Sep 17 00:00:00 2001 From: Harold Shen Date: Wed, 4 Dec 2024 16:49:12 -0500 Subject: [PATCH 7/7] bump time --- firebase-vscode/src/test/integration/fishfood/graphql.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-vscode/src/test/integration/fishfood/graphql.ts b/firebase-vscode/src/test/integration/fishfood/graphql.ts index d76befb13c8..5e09b667831 100644 --- a/firebase-vscode/src/test/integration/fishfood/graphql.ts +++ b/firebase-vscode/src/test/integration/fishfood/graphql.ts @@ -135,7 +135,7 @@ firebaseSuite("GraphQL", async function () { await addDataButton.click(); // Wait a bit for the mutation to be generated - await browser.pause(3500); + await browser.pause(5000); // Verify the generated mutation const activeEditor = await editorView.getActiveEditor();