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 <[email protected]>
  • Loading branch information
lubosmato and vicary committed Aug 20, 2024
1 parent 1be3502 commit 6bbdce8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 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
11 changes: 10 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,18 @@
"typescript": "^5.5.4",
"wait-on": "^7.2.0"
},
"optionalDependencies": {
"peerDependencies": {
"iter-ops": "^3.5.0",
"trading-signals": "^5.0.4"
},
"peerDependenciesMeta": {
"iter-ops": {
"optional": true
},
"trading-signals": {
"optional": true
}
},
"publishConfig": {
"directory": "dist"
}
Expand Down
60 changes: 42 additions & 18 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,15 @@ 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 { flat, map, pipeAsync } = await import('iter-ops');
const { printSchema } = await import('graphql');

const sma = new SMA(3);
const getMovingAverage = () => {
Expand Down Expand Up @@ -272,22 +274,44 @@ export const addCommand = (command: Command) => {

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

const watchIterable = pipeAsync(
watchTargets,
map((watchTarget) =>
pipeAsync(
watch(watchTarget),
map(({ filename }) =>
filename ? path.resolve(watchTarget, filename) : null
)
)
),
flat()
);

let shouldRun = false;

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

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

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

doGenerateSchema();
});
}
})();
}
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
13 changes: 9 additions & 4 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 6bbdce8

Please sign in to comment.