Skip to content

Commit

Permalink
Search through untracked files for graphql-tag. Don't error out on fi…
Browse files Browse the repository at this point in the history
…les that can't be found. (#66)

## 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: #66
  • Loading branch information
jeresig authored Jul 2, 2024
1 parent 4a4aebd commit c6be76f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-bugs-wave.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 4 additions & 1 deletion src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import {dirname} from 'path';
/** Step (1) */

const findGraphqlTagReferences = (root: string): Array<string> => {
// 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,
Expand Down
4 changes: 3 additions & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ const listExternalReferences = (file: FileResult): Array<string> => {
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));
Expand Down
6 changes: 5 additions & 1 deletion src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
};

0 comments on commit c6be76f

Please sign in to comment.