Skip to content

Commit bb5c467

Browse files
committed
Support file watching for codegen:generate
1 parent 9fb0cae commit bb5c467

4 files changed

Lines changed: 50 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: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ import { engineFlags } from "../../engine-cli";
1717
import { fromFile } from '../../fetch-schema';
1818
import { loadQueryDocuments } from 'apollo-codegen-core/lib/loading';
1919

20+
import { Gaze } from "gaze";
21+
import { eventNames } from 'cluster';
22+
23+
const waitForKey = async () => {
24+
console.log("Press any key to stop.");
25+
process.stdin.setRawMode!(true);
26+
return new Promise(resolve => process.stdin.once('data', () => {
27+
(process.stdin as any).unref();
28+
process.stdin.setRawMode!(false)
29+
resolve();
30+
}))
31+
};
32+
2033
export default class Generate extends Command {
2134
static description =
2235
"Generate static types for GraphQL queries. Can use the published schema in Apollo Engine or a downloaded schema.";
@@ -82,6 +95,9 @@ export default class Generate extends Command {
8295
outputFlat: flags.boolean({
8396
description:
8497
'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".'
98+
}),
99+
watch: flags.boolean({
100+
description: "Watch the query files to auto-generate on changes."
85101
})
86102
};
87103

@@ -282,6 +298,18 @@ export default class Generate extends Command {
282298
}
283299
]);
284300

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

0 commit comments

Comments
 (0)