Skip to content

Commit 4d69bbd

Browse files
committed
refactor: cli exports
1 parent 67b58e5 commit 4d69bbd

File tree

5 files changed

+77
-63
lines changed

5 files changed

+77
-63
lines changed

cli/commands/autogen/deployments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export function createDeploymentsCommand(): Command {
1414
});
1515
}
1616

17+
export const deploymentsCmd = createDeploymentsCommand();
18+
1719
export async function generateDeployments(options: CliOptions = {}): Promise<void> {
1820
for (const release of sablier.releases.getAll()) {
1921
if (release.protocol === Protocol.Legacy) {

cli/commands/autogen/graphql.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export function createGraphQLCommand(): Command {
3939
});
4040
}
4141

42+
export const graphQLCmd = createGraphQLCommand();
43+
4244
export async function generateGraphQL(options: GraphQLOptions): Promise<void> {
4345
const protocols: Indexer.Protocol[] = ["airdrops", "flow", "lockup"];
4446
const vendors = ["graph", "envio"];
@@ -55,18 +57,21 @@ export async function generateGraphQL(options: GraphQLOptions): Promise<void> {
5557
targetProtocols = _.filter(protocols, (p) => p === options.protocol);
5658
}
5759

58-
const basePaths: string[] = [];
60+
const docsPaths: string[] = [];
5961
for (const p of targetProtocols) {
6062
if (options.vendor === "all" || options.vendor === "graph") {
61-
basePaths.push(await generateGraph(p));
63+
const docsPath = await generateGraph(p);
64+
docsPaths.push(docsPath);
65+
await runPrettier(docsPath);
6266
}
6367
if (options.vendor === "all" || options.vendor === "envio") {
64-
basePaths.push(await generateEnvio(p));
68+
const docsPath = await generateEnvio(p);
69+
docsPaths.push(docsPath);
70+
await runPrettier(docsPath);
6571
}
6672
}
6773

68-
cleanupDocs(basePaths);
69-
await runPrettier(basePaths);
74+
cleanupDocs(docsPaths);
7075
}
7176

7277
/* -------------------------------------------------------------------------- */
@@ -86,50 +91,50 @@ const ENUMS_CATEGORY: Category = { ...COLLAPSED, className: "hidden", label: "En
8691
const INPUTS_CATEGORY: Category = { ...COLLAPSED, className: "hidden", label: "Inputs", position: 5 };
8792
const SCALARS_CATEGORY: Category = { ...COLLAPSED, className: "hidden", label: "Scalars", position: 6 };
8893

89-
function cleanupDocs(basePaths: string[]): void {
90-
for (const basePath of basePaths) {
91-
fs.rmSync(join(basePath, "directives"), { force: true, recursive: true });
92-
fs.rmSync(join(basePath, "subscriptions"), { force: true, recursive: true });
94+
function cleanupDocs(docsPaths: string[]): void {
95+
for (const docsPath of docsPaths) {
96+
fs.rmSync(join(docsPath, "directives"), { force: true, recursive: true });
97+
fs.rmSync(join(docsPath, "subscriptions"), { force: true, recursive: true });
9398

9499
// Delete all _category_.yml files
95-
fs.unlinkSync(join(basePath, "queries", "_category_.yml"));
96-
fs.unlinkSync(join(basePath, "enums", "_category_.yml"));
97-
fs.unlinkSync(join(basePath, "inputs", "_category_.yml"));
98-
fs.unlinkSync(join(basePath, "objects", "_category_.yml"));
99-
fs.unlinkSync(join(basePath, "scalars", "_category_.yml"));
100+
fs.unlinkSync(join(docsPath, "queries", "_category_.yml"));
101+
fs.unlinkSync(join(docsPath, "enums", "_category_.yml"));
102+
fs.unlinkSync(join(docsPath, "inputs", "_category_.yml"));
103+
fs.unlinkSync(join(docsPath, "objects", "_category_.yml"));
104+
fs.unlinkSync(join(docsPath, "scalars", "_category_.yml"));
100105

101106
// Rewrite _category_.yml files
102-
fs.writeFileSync(join(basePath, "queries", "_category_.yml"), yaml.dump(QUERIES_CATEGORY));
103-
fs.writeFileSync(join(basePath, "objects", "_category_.yml"), yaml.dump(OBJECTS_CATEGORY));
104-
fs.writeFileSync(join(basePath, "enums", "_category_.yml"), yaml.dump(ENUMS_CATEGORY));
105-
fs.writeFileSync(join(basePath, "inputs", "_category_.yml"), yaml.dump(INPUTS_CATEGORY));
106-
fs.writeFileSync(join(basePath, "scalars", "_category_.yml"), yaml.dump(SCALARS_CATEGORY));
107+
fs.writeFileSync(join(docsPath, "queries", "_category_.yml"), yaml.dump(QUERIES_CATEGORY));
108+
fs.writeFileSync(join(docsPath, "objects", "_category_.yml"), yaml.dump(OBJECTS_CATEGORY));
109+
fs.writeFileSync(join(docsPath, "enums", "_category_.yml"), yaml.dump(ENUMS_CATEGORY));
110+
fs.writeFileSync(join(docsPath, "inputs", "_category_.yml"), yaml.dump(INPUTS_CATEGORY));
111+
fs.writeFileSync(join(docsPath, "scalars", "_category_.yml"), yaml.dump(SCALARS_CATEGORY));
107112

108113
// Write vendor category file at base path
109-
if (basePath.includes("envio")) {
110-
fs.writeFileSync(join(basePath, "_category_.yml"), yaml.dump(ENVIO_CATEGORY));
111-
} else if (basePath.includes("the-graph")) {
112-
fs.writeFileSync(join(basePath, "_category_.yml"), yaml.dump(THE_GRAPH_CATEGORY));
114+
if (docsPath.includes("envio")) {
115+
fs.writeFileSync(join(docsPath, "_category_.yml"), yaml.dump(ENVIO_CATEGORY));
116+
} else if (docsPath.includes("the-graph")) {
117+
fs.writeFileSync(join(docsPath, "_category_.yml"), yaml.dump(THE_GRAPH_CATEGORY));
113118
}
114119
}
115120
}
116121

117122
async function generateEnvio(protocol: Indexer.Protocol): Promise<string> {
118-
const basePath = `./docs/api/${protocol}/graphql/envio`;
123+
const docsPath = `./docs/api/${protocol}/graphql/envio`;
119124
const schemaURL = getSablierIndexerEnvio({ chainId: CHAIN_ID_SEPOLIA, protocol }).endpoint.url;
120125

121-
await runGenerator(basePath, schemaURL);
126+
await runGenerator(docsPath, schemaURL);
122127
console.log(`✔️ Generated GraphQL docs for Envio vendor and ${_.capitalize(protocol)} protocol\n`);
123-
return basePath;
128+
return docsPath;
124129
}
125130

126131
async function generateGraph(protocol: Indexer.Protocol): Promise<string> {
127-
const basePath = `./docs/api/${protocol}/graphql/the-graph`;
132+
const docsPath = `./docs/api/${protocol}/graphql/the-graph`;
128133
const schemaURL = `https://api.studio.thegraph.com/query/112500/sablier-${protocol}-experimental/version/latest`;
129134

130-
await runGenerator(basePath, schemaURL);
135+
await runGenerator(docsPath, schemaURL);
131136
console.log(`✔️ Generated GraphQL docs for The Graph vendor and ${_.capitalize(protocol)} protocol\n`);
132-
return basePath;
137+
return docsPath;
133138
}
134139

135140
/**
@@ -140,7 +145,6 @@ async function runGenerator(base: string, schema: string): Promise<void> {
140145
await $("bun", ["docusaurus", "graphql-to-doc", "--base", base, "--schema", schema], { stdio: "inherit" });
141146
}
142147

143-
async function runPrettier(targetPaths: string[]): Promise<void> {
144-
const patterns = targetPaths.map((path) => `${path}/**/*.{md,mdx,yml,yaml}`);
145-
await $("bun", ["prettier", "--write", ...patterns]);
148+
async function runPrettier(targetPath: string): Promise<void> {
149+
await $("bun", ["prettier", "--write", `${targetPath}/**/*.{md,mdx,yml,yaml}`]);
146150
}

cli/commands/autogen/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Command } from "commander";
2+
import { deploymentsCmd, generateDeployments } from "./deployments";
3+
import { graphQLCmd } from "./graphql";
4+
import { generateIndexers, indexersCmd } from "./indexers";
5+
6+
export function createAutogenCommand(): Command {
7+
const autogenCommand = new Command("autogen")
8+
.description("Auto-generate documentation and indexers tables")
9+
.action(async (_options, command) => {
10+
const globalOptions = command.parent?.opts() || {};
11+
12+
console.log("🚀 Generating all documentation tables...\n");
13+
14+
// Run deployments generation
15+
await generateDeployments(globalOptions);
16+
17+
console.log();
18+
19+
// Run indexers generation
20+
await generateIndexers(globalOptions);
21+
22+
console.log();
23+
console.log("✅ All documentation tables generated successfully!");
24+
});
25+
26+
// Add subcommands to autogen
27+
autogenCommand.addCommand(deploymentsCmd);
28+
autogenCommand.addCommand(indexersCmd);
29+
autogenCommand.addCommand(graphQLCmd);
30+
31+
return autogenCommand;
32+
}
33+
34+
export const autogenCmd = createAutogenCommand();

cli/commands/autogen/indexers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export function createIndexersCommand(): Command {
1414
});
1515
}
1616

17+
export const indexersCmd = createIndexersCommand();
18+
1719
export async function generateIndexers(options: CliOptions = {}): Promise<void> {
1820
generateTables("airdrops", options);
1921
generateTables("flow", options);

cli/index.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
22
import { Command } from "commander";
33

4+
import { autogenCmd } from "./commands/autogen";
5+
46
async function main() {
57
const program = new Command();
68

@@ -9,38 +11,8 @@ async function main() {
911
// Global options
1012
program.option("-o, --overwrite", "overwrite existing files", false);
1113

12-
// Import and create subcommands
13-
const { createDeploymentsCommand, generateDeployments } = await import("./commands/autogen/deployments.js");
14-
const { createIndexersCommand, generateIndexers } = await import("./commands/autogen/indexers.js");
15-
const { createGraphQLCommand } = await import("./commands/autogen/graphql.js");
16-
17-
// Create autogen parent command
18-
const autogenCommand = new Command("autogen")
19-
.description("Auto-generate documentation and indexers tables")
20-
.action(async (_options, command) => {
21-
const globalOptions = command.parent?.opts() || {};
22-
23-
console.log("🚀 Generating all documentation tables...\n");
24-
25-
// Run deployments generation
26-
await generateDeployments(globalOptions);
27-
28-
console.log();
29-
30-
// Run indexers generation
31-
await generateIndexers(globalOptions);
32-
33-
console.log();
34-
console.log("✅ All documentation tables generated successfully!");
35-
});
36-
37-
// Add subcommands to autogen
38-
autogenCommand.addCommand(createDeploymentsCommand());
39-
autogenCommand.addCommand(createIndexersCommand());
40-
autogenCommand.addCommand(createGraphQLCommand());
41-
4214
// Add the autogen command to the main program
43-
program.addCommand(autogenCommand);
15+
program.addCommand(autogenCmd);
4416

4517
program.parse();
4618
}

0 commit comments

Comments
 (0)