Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facelift for Queries Generator error logging (#866) #874

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
43 changes: 34 additions & 9 deletions packages/generate/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import { type CommandOptions } from "./commandutil";
import { headerComment } from "./genutil";
import { type Target, camelify } from "./genutil";

function formatError(path: string, err: any) {
const message = `Error in file '${path}': ${err.toString()}`;
return `\x1b[31m${message}\x1b[0m`;
}

function printSummary(matches: string[], errs: string[], t0: number) {
const passedCount = matches.length - errs.length;
const summary = `\n\x1b[${
errs.length > 0 ? "1;31m" : "1;32m"
}${passedCount}/${matches.length} queries passed${
errs.length ? ", " + errs.length + " failed" : "!"
}`;
console.log(summary + "\n");
for (const err of errs) {
console.log("\x1b[22m" + err);
}
if (errs.length > 0) console.log(summary);
const t1Pretty = ((performance.now() - t0) / 1000).toFixed(2);
console.log(`\x1b[1;33mCompleted in: ${t1Pretty}s\x1b[0m\n`);
}

// generate per-file queries
// generate queries in a single file
// generate per-file queries, then listen for changes and update
Expand All @@ -13,9 +34,11 @@ export async function generateQueryFiles(params: {
client: Client;
schemaDir: string;
}) {
const t0 = performance.now();
if (params.options.file && params.options.watch) {
throw new Error(`Using --watch and --file mode simultaneously is not
currently supported.`);
throw new Error(
`Using --watch and --file mode simultaneously is not currently supported.`
);
}

const noRoot = !params.root;
Expand All @@ -38,6 +61,9 @@ currently supported.`);

console.log(`Detected schema directory: ${params.schemaDir}`);
const matches = await getMatches(root, params.schemaDir);

const errs: string[] = [];

if (matches.length === 0) {
console.log(`No .edgeql files found outside of ${params.schemaDir}`);
return;
Expand Down Expand Up @@ -80,9 +106,7 @@ currently supported.`);
}
} catch (err) {
wasError = true;
console.log(
`Error in file '${prettyPath}': ${(err as Error).toString()}`
);
errs.push(formatError(prettyPath, err));
}
})
);
Expand All @@ -109,6 +133,7 @@ currently supported.`);
);
}
}
printSummary(matches, errs, t0);
return;
}

Expand All @@ -131,10 +156,8 @@ currently supported.`);
);
}
} catch (err) {
console.log(
`Error in file './${adapter.path.posix.relative(root, path)}': ${(
err as any
).toString()}`
errs.push(
formatError("./" + adapter.path.posix.relative(root, path), err)
);
}
}
Expand All @@ -146,6 +169,8 @@ currently supported.`);
// find all *.edgeql files
// for query in queries:
// generate output file

printSummary(matches, errs, t0);
}

export function stringifyImports(imports: { [k: string]: boolean }) {
Expand Down