From c6be76f02255908f399fcb5fcd27d4aa86066b53 Mon Sep 17 00:00:00 2001 From: John Resig Date: Tue, 2 Jul 2024 12:16:47 -0400 Subject: [PATCH] Search through untracked files for graphql-tag. Don't error out on files that can't be found. (#66) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: This fixes two bugs: 1) Creating types wouldn't work if the files hadn't yet been checked in to Git. This fixes that by now searching through untracked files, as well. 2) The script couldn't run if the graphql type files didn't exist. This fixes that by gracefully handling paths that don't exist. Issue: FEI-5065 ## Test plan: I built it and ran it in webapp and it worked as expected. Author: jeresig Reviewers: somewhatabstract, kevinb-khan, jaredly Required Reviewers: Approved By: somewhatabstract Checks: ✅ Lint & Test (ubuntu-latest, 16.x) Pull Request URL: https://github.com/Khan/graphql-flow/pull/66 --- .changeset/spicy-bugs-wave.md | 5 +++++ src/cli/run.ts | 5 ++++- src/parser/parse.ts | 4 +++- src/parser/utils.ts | 6 +++++- 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 .changeset/spicy-bugs-wave.md diff --git a/.changeset/spicy-bugs-wave.md b/.changeset/spicy-bugs-wave.md new file mode 100644 index 0000000..1e5393a --- /dev/null +++ b/.changeset/spicy-bugs-wave.md @@ -0,0 +1,5 @@ +--- +'@khanacademy/graphql-flow': patch +--- + +Search through untracked files for graphql-tag. Don't error out on files that can't be found. diff --git a/src/cli/run.ts b/src/cli/run.ts index 3708047..522dcde 100644 --- a/src/cli/run.ts +++ b/src/cli/run.ts @@ -32,8 +32,11 @@ import {dirname} from 'path'; /** Step (1) */ const findGraphqlTagReferences = (root: string): Array => { + // NOTE(john): We want to include untracked files here so that we can + // generate types for them. This is useful for when we have a new file + // that we want to generate types for, but we haven't committed it yet. const response = execSync( - "git grep -I --word-regexp --name-only --fixed-strings 'graphql-tag' -- '*.js' '*.jsx' '*.ts' '*.tsx'", + "git grep -I --word-regexp --name-only --fixed-strings --untracked 'graphql-tag' -- '*.js' '*.jsx' '*.ts' '*.tsx'", { encoding: 'utf8', cwd: root, diff --git a/src/parser/parse.ts b/src/parser/parse.ts index fad2141..5b7a146 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -105,7 +105,9 @@ const listExternalReferences = (file: FileResult): Array => { if (v.type === 'import') { if (followImports) { const absPath = getPathWithExtension(v.path); - paths[absPath] = true; + if (absPath) { + paths[absPath] = true; + } } } else { v.source.expressions.forEach((expr) => add(expr, true)); diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 56d7e6c..75f87e1 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -20,5 +20,9 @@ export const getPathWithExtension = (pathWithoutExtension: string): string => { if (fs.existsSync(pathWithoutExtension + '.ts')) { return pathWithoutExtension + '.ts'; } - throw new Error("Can't find file at " + pathWithoutExtension); + // NOTE(john): This is a bit of a hack, but it's necessary for when we + // have a file that doesn't exist. This will happen when we delete all of + // the type files before re-running graphql-flow again. We want to ensure + // that we don't error out in this case. + return ""; };