Skip to content

Commit

Permalink
make adhoc files saved by default (#8025)
Browse files Browse the repository at this point in the history
* made adhoc files saved by default

* fix test and add changelog

* prettier

* add longer wait

* close all editors between exections

* bump time

* bump time
  • Loading branch information
hlshen authored Dec 4, 2024
1 parent a9bc138 commit 9ea466f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
2 changes: 2 additions & 0 deletions firebase-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT

- [Fixed] Fixed an issue where generating an ad-hoc file would break codelenses

## 0.10.8

- Updated internal firebase-tools dependency to 13.25.0
Expand Down
46 changes: 14 additions & 32 deletions firebase-vscode/src/data-connect/ad-hoc-mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
13 changes: 4 additions & 9 deletions firebase-vscode/src/data-connect/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ export async function upsertFile(
): Promise<void> {
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
Expand Down
20 changes: 14 additions & 6 deletions firebase-vscode/src/test/integration/fishfood/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(5000);

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

await editorView.closeCurrentEditor();
// file should be created, saved, then opened
expect(activeEditor?.document.isDirty).toBe(false);
await editorView.closeAllEditors();
},
);

Expand Down Expand Up @@ -185,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(5000);

// Verify the generated query
const activeEditor = await editorView.getActiveEditor();
Expand All @@ -199,6 +201,10 @@ firebaseSuite("GraphQL", async function () {
}
}`);
expect(editorTitle).toBe("Post_read.gql");

// file should be created, saved, then opened
expect(activeEditor?.document.isDirty).toBe(false);
await editorView.closeAllEditors();
},
);

Expand Down Expand Up @@ -242,7 +248,7 @@ firebaseSuite("GraphQL", async function () {
"test_projects/fishfood/dataconnect/Post_insert.gql",
);

await editorView.closeCurrentEditor();
await editorView.closeAllEditors();
},
);

Expand Down Expand Up @@ -282,8 +288,10 @@ 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");
await editorView.closeCurrentEditor();
expect(filePath).toContain(
"test_projects/fishfood/dataconnect/Post_read.gql",
);
await editorView.closeAllEditors();
},
);
});

0 comments on commit 9ea466f

Please sign in to comment.