Skip to content

Commit

Permalink
Allow programmatic access to queries generator (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Aug 1, 2023
1 parent b804348 commit 2b09ed0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
4 changes: 0 additions & 4 deletions packages/generate/src/commandutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,3 @@ export function readPasswordFromStdin() {
adapter.process.stdin.on("end", () => resolve(data.trimEnd()));
});
}

export function getPackageVersion(): string {
return "__@edgedb/generate__VERSION_PLACEHOLDER__";
}
10 changes: 2 additions & 8 deletions packages/generate/src/edgeql-js.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// tslint:disable:no-console

import { $, adapter, type Client } from "edgedb";
import {
type CommandOptions,
getPackageVersion,
isTTY,
promptBoolean,
} from "./commandutil";
import { type CommandOptions, isTTY, promptBoolean } from "./commandutil";
import { headerComment } from "./genutil";
import { DirBuilder } from "./builders";
import { syntax } from "./FILES";

Expand All @@ -22,8 +18,6 @@ import { generateSetImpl } from "./edgeql-js/generateSetImpl";

const { path, fs, readFileUtf8, exists, walk } = adapter;

const headerComment = `// GENERATED by @edgedb/generate v${getPackageVersion()}\n\n`;

// tslint:disable-next-line
export const configFileHeader = `// EdgeDB query builder`;

Expand Down
4 changes: 4 additions & 0 deletions packages/generate/src/genutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { adapter } from "edgedb";

export const splitName = util.splitName;

const PACKAGE_VERSION_TEMPLATE = "__@edgedb/generate__VERSION_PLACEHOLDER__";

export const headerComment = `// GENERATED by @edgedb/generate v${PACKAGE_VERSION_TEMPLATE}\n\n`;

export function toIdent(name: string): string {
if (name.includes("::")) {
throw new Error(`toIdent: invalid name ${name}`);
Expand Down
5 changes: 3 additions & 2 deletions packages/generate/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// tslint:disable:no-console
import { CommandOptions, getPackageVersion } from "./commandutil";
import type { CommandOptions } from "./commandutil";
import { headerComment } from "./genutil";
import { $, adapter, type Client } from "edgedb";
import { DirBuilder } from "./builders";

Expand Down Expand Up @@ -57,7 +58,7 @@ export async function runInterfacesGenerator(params: {

const file = dir.getPath("interfaces");
const rendered =
`// GENERATED by @edgedb/generate v${getPackageVersion()}\n\n` +
headerComment +
file.render({
mode: "ts",
moduleKind: "esm",
Expand Down
12 changes: 6 additions & 6 deletions packages/generate/src/queries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $, adapter, type Client } from "edgedb";
import { Cardinality } from "edgedb/dist/ifaces";
import { type CommandOptions, getPackageVersion } from "./commandutil";
import { type CommandOptions } from "./commandutil";
import { headerComment } from "./genutil";
import type { Target } from "./genutil";

// generate per-file queries
Expand Down Expand Up @@ -103,7 +104,7 @@ currently supported.`);
console.log(` ${prettyPath}`);
await adapter.fs.writeFile(
filePath,
`// GENERATED by @edgedb/generate v${getPackageVersion()}\n\n` +
headerComment +
`${stringifyImports(file.imports)}\n\n${file.contents}`
);
}
Expand All @@ -126,8 +127,7 @@ currently supported.`);
console.log(` ${prettyPath}`);
await adapter.fs.writeFile(
f.path,
`// GENERATED by @edgedb/generate v${getPackageVersion()}\n\n` +
`${stringifyImports(f.imports)}\n\n${f.contents}`
headerComment + `${stringifyImports(f.imports)}\n\n${f.contents}`
);
}
} catch (err) {
Expand All @@ -148,7 +148,7 @@ currently supported.`);
// generate output file
}

function stringifyImports(imports: { [k: string]: boolean }) {
export function stringifyImports(imports: { [k: string]: boolean }) {
if (Object.keys(imports).length === 0) return "";
return `import type {${Object.keys(imports).join(", ")}} from "edgedb";`;
}
Expand All @@ -170,7 +170,7 @@ async function getMatches(root: string) {

type QueryType = Awaited<ReturnType<(typeof $)["analyzeQuery"]>>;

function generateFiles(params: {
export function generateFiles(params: {
target: Target;
path: string;
types: QueryType;
Expand Down

0 comments on commit 2b09ed0

Please sign in to comment.