Skip to content

Commit 8138c60

Browse files
committed
Support file watching for codegen:generate
1 parent f2049dd commit 8138c60

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

@@ -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)