Skip to content

Commit

Permalink
fix(cli): fix watching of schema files outside of working directory (#…
Browse files Browse the repository at this point in the history
…1998)

* chore(package/cli): cosmetic touch-ups

* chore(package/cli): changeset

---------

Co-authored-by: Vicary A <vicary.archangel@member.mensa.org>
  • Loading branch information
lubosmato and vicary committed Aug 20, 2024
1 parent 1be3502 commit b4d037c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-beers-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gqty/cli': patch
---

Allow watch targets above current working directory
7 changes: 6 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@
"typescript": "^5.5.4",
"wait-on": "^7.2.0"
},
"optionalDependencies": {
"peerDependencies": {
"trading-signals": "^5.0.4"
},
"peerDependenciesMeta": {
"trading-signals": {
"optional": true
}
},
"publishConfig": {
"directory": "dist"
}
Expand Down
61 changes: 37 additions & 24 deletions packages/cli/src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { PackageJSON } from 'bob-esbuild/config/packageJson';
import type { Command } from 'commander';
import { cosmiconfig } from 'cosmiconfig';
import { readFile, watch } from 'node:fs/promises';
import path from 'node:path';
import type { GQtyConfig } from '../config';
import { inquirer } from '../deps';
import { fg, inquirer } from '../deps';
import { convertHeadersInput } from './default/convertHeadersInput';
import { fetchSchema, isURL } from './default/fetchSchema';
import { generateClient } from './default/generateClient';
Expand Down Expand Up @@ -180,14 +181,14 @@ export const addCommand = (command: Command) => {

// Watch mode
if (options.watch) {
const { printSchema } = await import('graphql');
const { FasterSMA: SMA } = await import(
'trading-signals/dist/SMA/SMA.js'
);
const { default: throttle } = await import('lodash-es/throttle.js');
const {
default: { isMatch },
} = await import('micromatch');
const { default: throttle } = await import('lodash-es/throttle.js');
const { FasterSMA: SMA } = await import(
'trading-signals/dist/SMA/SMA.js'
);
const { printSchema } = await import('graphql');

const sma = new SMA(3);
const getMovingAverage = () => {
Expand All @@ -202,7 +203,6 @@ export const addCommand = (command: Command) => {
const doGenerateSchema = throttle(
async () => {
if (mutexLock) return;

mutexLock = true;

const start = Date.now();
Expand Down Expand Up @@ -272,23 +272,36 @@ export const addCommand = (command: Command) => {

// Watch file changes
(async () => {
let shouldRun = false;

for await (const { filename } of watch('.', { recursive: true })) {
if (filename && isMatch(filename, endpoints)) {
// Already queued
if (shouldRun) continue;
shouldRun = true;

process.nextTick(() => {
// Already executed
if (!shouldRun) return;
shouldRun = false;

doGenerateSchema();
});
}
}
// micromatch does not understand relative patterns, normalize them
// ahead of time.
const matchPatterns = endpoints.map((endpoint) =>
path.resolve(endpoint)
);
const watchTargets = await fg(matchPatterns).then((files) => [
...new Set(files.map((file) => path.dirname(file))),
]);

let queued = false;

await Promise.all(
watchTargets.map(async (dir) => {
for await (const { filename } of watch(dir)) {
if (!filename) continue;

const absolutePath = path.resolve(dir, filename);
if (!isMatch(absolutePath, matchPatterns)) continue;
if (mutexLock || queued) continue;

queued = true;

setTimeout(() => {
doGenerateSchema().finally(() => {
queued = false;
});
});
}
})
);
})();
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/default/fetchSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class FetchError extends Error {
}

export type FetchSchemasOptions = {
headers?: Record<string, string>;
headers?: HeadersInit;
headersByEndpoint?: GQtyConfig['introspections'];
silent?: boolean;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/gqty/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
]
},
"dependencies": {
"debounce-microtasks": "^0.1.7",
"debounce-microtasks": "^0.1.8",
"flatted": "^3.3.1",
"frail-map": "^1.0.10",
"just-extend": "^6.2.0",
Expand Down
12 changes: 4 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b4d037c

Please sign in to comment.