UNPKG

3.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const codegen_1 = require("../codegen");
4const graphql_codegen_plugin_helpers_1 = require("graphql-codegen-plugin-helpers");
5const helpers_1 = require("../helpers");
6const isValidPath = require("is-valid-path");
7const isGlob = require("is-glob");
8const logSymbols = require("log-symbols");
9function log(msg) {
10 // double spaces to inline the message with Listr
11 graphql_codegen_plugin_helpers_1.getLogger().info(` ${msg}`);
12}
13function emitWatching() {
14 log(`${logSymbols.info} Watching for changes...`);
15}
16exports.createWatcher = (config, onNext) => {
17 graphql_codegen_plugin_helpers_1.debugLog(`[Watcher] Starting watcher...`);
18 const files = [];
19 const documents = helpers_1.normalizeInstanceOrArray(config.documents);
20 const schemas = helpers_1.normalizeInstanceOrArray(config.schema);
21 // Add schemas and documents from "generates"
22 Object.keys(config.generates)
23 .map(filename => helpers_1.normalizeOutputParam(config.generates[filename]))
24 .forEach(conf => {
25 schemas.push(...helpers_1.normalizeInstanceOrArray(conf.schema));
26 documents.push(...helpers_1.normalizeInstanceOrArray(conf.documents));
27 });
28 if (documents) {
29 documents.forEach(doc => {
30 if (typeof doc === 'string') {
31 files.push(doc);
32 }
33 else {
34 files.push(...Object.keys(doc));
35 }
36 });
37 }
38 schemas.forEach((schema) => {
39 if (isGlob(schema) || isValidPath(schema)) {
40 files.push(schema);
41 }
42 });
43 if (typeof config.watch !== 'boolean') {
44 files.push(...helpers_1.normalizeInstanceOrArray(config.watch));
45 }
46 let watcher;
47 const runWatcher = async () => {
48 const chokidar = await Promise.resolve().then(() => require('chokidar'));
49 emitWatching();
50 watcher = chokidar.watch(files, {
51 persistent: true,
52 ignoreInitial: true,
53 followSymlinks: true,
54 cwd: process.cwd(),
55 disableGlobbing: false,
56 usePolling: true,
57 interval: 100,
58 binaryInterval: 300,
59 depth: 99,
60 awaitWriteFinish: true,
61 ignorePermissionErrors: false,
62 atomic: true
63 });
64 graphql_codegen_plugin_helpers_1.debugLog(`[Watcher] Started`);
65 let isShutdown = false;
66 const shutdown = async () => {
67 isShutdown = true;
68 graphql_codegen_plugin_helpers_1.debugLog(`[Watcher] Shutting down`);
69 log(`Shutting down watch...`);
70 watcher.close();
71 };
72 // it doesn't matter what has changed, need to run whole process anyway
73 watcher.on('all', () => {
74 if (!isShutdown) {
75 codegen_1.executeCodegen(config)
76 .then(onNext, () => Promise.resolve())
77 .then(() => emitWatching());
78 }
79 });
80 process.once('SIGINT', shutdown);
81 process.once('SIGTERM', shutdown);
82 };
83 // the promise never resolves to keep process running
84 return new Promise((_, reject) => {
85 codegen_1.executeCodegen(config)
86 .then(onNext, () => Promise.resolve())
87 .then(runWatcher)
88 .catch(err => {
89 watcher.close();
90 reject(err);
91 });
92 });
93};
94//# sourceMappingURL=watcher.js.map
\No newline at end of file