Skip to content

Commit 473393f

Browse files
authored
Support file watching for codegen:generate (#484)
* Support file watching for codegen:generate * Fix compile errors after merge
1 parent f2049dd commit 473393f

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module "gaze";

packages/apollo-cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"node-fetch": "^2.1.2",
4343
"react": "^16.4.0",
4444
"react-dom": "^16.4.0",
45-
"rimraf": "^2.6.2"
45+
"rimraf": "^2.6.2",
46+
"gaze": "^1.1.3"
4647
},
4748
"devDependencies": {
4849
"@fancy-test/nock": "^0.1.1",

packages/apollo-cli/src/commands/codegen/generate.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ import { engineFlags } from "../../engine-cli";
1616
import { fetchSchema } from '../../fetch-schema';
1717
import { loadQueryDocuments } from 'apollo-codegen-core/lib/loading';
1818

19+
import { Gaze } from "gaze";
20+
21+
const waitForKey = async () => {
22+
console.log("Press any key to stop.");
23+
process.stdin.setRawMode!(true);
24+
return new Promise(resolve => process.stdin.once('data', () => {
25+
(process.stdin as any).unref();
26+
process.stdin.setRawMode!(false)
27+
resolve();
28+
}))
29+
};
30+
1931
export default class Generate extends Command {
2032
static description =
2133
"Generate static types for GraphQL queries. Can use the published schema in Apollo Engine or a downloaded schema.";
@@ -81,6 +93,9 @@ export default class Generate extends Command {
8193
outputFlat: flags.boolean({
8294
description:
8395
'By default, TypeScript/Flow will put each generated file in a directory next to its source file using the value of the "output" as the directory name. Set "outputFlat" to put all generated files in the directory relative to the current working directory defined by "output".'
96+
}),
97+
watch: flags.boolean({
98+
description: "Watch the query files to auto-generate on changes."
8499
})
85100
};
86101

@@ -282,6 +297,18 @@ export default class Generate extends Command {
282297
}
283298
]);
284299

285-
return tasks.run();
300+
if (flags.watch) {
301+
await tasks.run().catch(() => {});
302+
const watcher = new Gaze(flags.queries!);
303+
watcher.on("all", () => {
304+
console.log("\nChange detected, generating types...")
305+
tasks.run().catch(() => {});
306+
});
307+
await waitForKey();
308+
watcher.close();
309+
return;
310+
} else {
311+
return tasks.run();
312+
}
286313
}
287314
}

0 commit comments

Comments
 (0)