-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
create-ponder --from-subgraph-id
(#110)
* wip: subgraph conversion tool * feat: add from-subgraph-id template and prompts * chore: improve log style and simplify bin script * chore: rename option * chore: changeset * fix: change directory to root before git init * fix: use template passed as option * fix: allow Bytes and String as id field type * chore: changeset * test: add create-ponder subgraph id test
- Loading branch information
Showing
16 changed files
with
527 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"create-ponder": patch | ||
--- | ||
|
||
Added `--from-subgraph-id` option, renamed `--from-subgraph` to `--from-subgraph-repo`, and added `prompts` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ponder/core": patch | ||
--- | ||
|
||
Updated schema definition to allow `Bytes` and `String` types for entity `id` fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,126 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
import { cac } from "cac"; | ||
import path from "node:path"; | ||
import prompts from "prompts"; | ||
|
||
import { CreatePonderOptions, Template, TemplateKind } from "@/common"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import packageJson from "../../../package.json"; | ||
import { run } from "../index"; | ||
|
||
const cli = cac(packageJson.name) | ||
.version(packageJson.version) | ||
.usage("[options]") | ||
.help() | ||
.option("--dir [path]", "Path to directory for generated project") | ||
.option("--from-subgraph [path]", "Path to subgraph directory") | ||
.option("--from-etherscan [url]", "Link to etherscan contract page") | ||
.option("--etherscan-api-key [key]", "Etherscan API key"); | ||
|
||
const parsed = cli.parse(process.argv); | ||
|
||
const options = parsed.options as { | ||
help?: boolean; | ||
dir?: string; | ||
fromSubgraph?: string; | ||
fromEtherscan?: string; | ||
etherscanApiKey?: string; | ||
}; | ||
const createPonder = async () => { | ||
const cli = cac(packageJson.name) | ||
.version(packageJson.version) | ||
.usage("[options]") | ||
.help() | ||
.option("--dir [path]", "Path to directory for generated project") | ||
.option("--from-subgraph-id [id]", "Subgraph deployment ID") | ||
.option("--from-subgraph-repo [path]", "Path to subgraph repository") | ||
.option("--from-etherscan [url]", "Link to etherscan contract page") | ||
.option("--etherscan-api-key [key]", "Etherscan API key"); | ||
|
||
const parsed = cli.parse(process.argv); | ||
|
||
const options = parsed.options as { | ||
help?: boolean; | ||
dir?: string; | ||
fromSubgraphId?: string; | ||
fromSubgraphRepo?: string; | ||
fromEtherscan?: string; | ||
etherscanApiKey?: string; | ||
}; | ||
|
||
if (options.help) { | ||
process.exit(0); | ||
} | ||
|
||
const { fromEtherscan, fromSubgraphId, fromSubgraphRepo } = options; | ||
|
||
// Validate CLI options. | ||
if ( | ||
(fromSubgraphId && fromSubgraphRepo) || | ||
(fromSubgraphId && fromEtherscan) || | ||
(fromSubgraphRepo && fromEtherscan) | ||
) { | ||
throw new Error( | ||
`Cannot specify more than one "--from" option:\n --from-subgraph\n --from-etherscan-id\n --from-etherscan-repo` | ||
); | ||
} | ||
|
||
const { projectName } = await prompts({ | ||
type: "text", | ||
name: "projectName", | ||
message: "What is your project named?", | ||
initial: "my-app", | ||
}); | ||
|
||
// Get template from options if provided. | ||
let template: Template | undefined = undefined; | ||
if (fromEtherscan) { | ||
template = { kind: TemplateKind.ETHERSCAN, link: fromEtherscan }; | ||
} | ||
if (fromSubgraphId) { | ||
template = { kind: TemplateKind.SUBGRAPH_ID, id: fromSubgraphId }; | ||
} | ||
if (fromSubgraphRepo) { | ||
template = { kind: TemplateKind.SUBGRAPH_REPO, path: fromSubgraphRepo }; | ||
} | ||
|
||
// Get template from prompts if not provided. | ||
if (!fromSubgraphId && !fromSubgraphRepo && !fromEtherscan) { | ||
const { template: templateKind } = await prompts({ | ||
type: "select", | ||
name: "template", | ||
message: "Would you like to use a template for this project?", | ||
choices: [ | ||
{ title: "None" }, | ||
{ title: "Etherscan contract link" }, | ||
{ title: "Subgraph ID" }, | ||
{ title: "Subgraph repository" }, | ||
], | ||
}); | ||
|
||
if (templateKind === TemplateKind.ETHERSCAN) { | ||
const { link } = await prompts({ | ||
type: "text", | ||
name: "link", | ||
message: "Enter an Etherscan contract link", | ||
initial: "https://etherscan.io/address/0x97...", | ||
}); | ||
template = { kind: TemplateKind.ETHERSCAN, link }; | ||
} | ||
|
||
if (templateKind === TemplateKind.SUBGRAPH_ID) { | ||
const { id } = await prompts({ | ||
type: "text", | ||
name: "id", | ||
message: "Enter a subgraph deployment ID", | ||
initial: "QmNus...", | ||
}); | ||
template = { kind: TemplateKind.SUBGRAPH_ID, id }; | ||
} | ||
|
||
if (templateKind === TemplateKind.SUBGRAPH_REPO) { | ||
const { path } = await prompts({ | ||
type: "text", | ||
name: "path", | ||
message: "Enter a path to a subgraph repository", | ||
initial: "../subgraph", | ||
}); | ||
template = { kind: TemplateKind.SUBGRAPH_REPO, path }; | ||
} | ||
} | ||
|
||
const validatedOptions: CreatePonderOptions = { | ||
projectName, | ||
rootDir: path.resolve(".", options.dir ? options.dir : projectName), | ||
template, | ||
etherscanApiKey: options.etherscanApiKey, | ||
}; | ||
|
||
if (options.help) { | ||
process.exit(0); | ||
} | ||
|
||
// Validate CLI options. | ||
if (options.fromSubgraph && options.fromEtherscan) { | ||
throw new Error(`Cannot specify more than one "--from" option: | ||
--from-subgraph | ||
--from-etherscan | ||
`); | ||
} | ||
|
||
export interface CreatePonderOptions { | ||
ponderRootDir: string; | ||
fromSubgraph?: string; | ||
fromEtherscan?: string; | ||
etherscanApiKey?: string; | ||
} | ||
|
||
const validatedOptions: CreatePonderOptions = { | ||
// Default `dir` to "ponder". | ||
ponderRootDir: path.resolve(".", options.dir ? options.dir : "ponder"), | ||
fromSubgraph: options.fromSubgraph, | ||
fromEtherscan: options.fromEtherscan, | ||
etherscanApiKey: options.etherscanApiKey, | ||
await run(validatedOptions); | ||
}; | ||
|
||
require("../index").run(validatedOptions); | ||
createPonder(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export enum TemplateKind { | ||
NONE, | ||
ETHERSCAN, | ||
SUBGRAPH_ID, | ||
SUBGRAPH_REPO, | ||
} | ||
|
||
export type Template = | ||
| { | ||
kind: TemplateKind.ETHERSCAN; | ||
link: string; | ||
} | ||
| { | ||
kind: TemplateKind.SUBGRAPH_ID; | ||
id: string; | ||
} | ||
| { | ||
kind: TemplateKind.SUBGRAPH_REPO; | ||
path: string; | ||
}; | ||
|
||
export interface CreatePonderOptions { | ||
rootDir: string; | ||
projectName: string; | ||
template?: Template; | ||
etherscanApiKey?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-disable no-empty */ | ||
import { execSync } from "child_process"; | ||
import path from "path"; | ||
import rimraf from "rimraf"; | ||
|
||
// File adapted from next.js | ||
// https://github.dev/vercel/next.js/blob/9ad1f321b7902542acd2be041fb2f15f023a0ed9/packages/create-next-app/helpers/git.ts | ||
|
||
function isInGitRepository(): boolean { | ||
try { | ||
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" }); | ||
return true; | ||
} catch (_) {} | ||
return false; | ||
} | ||
|
||
function isInMercurialRepository(): boolean { | ||
try { | ||
execSync("hg --cwd . root", { stdio: "ignore" }); | ||
return true; | ||
} catch (_) {} | ||
return false; | ||
} | ||
|
||
export function tryGitInit(root: string): boolean { | ||
let didInit = false; | ||
try { | ||
execSync("git --version", { stdio: "ignore" }); | ||
if (isInGitRepository() || isInMercurialRepository()) { | ||
return false; | ||
} | ||
|
||
execSync("git init", { stdio: "ignore" }); | ||
didInit = true; | ||
|
||
execSync("git checkout -b main", { stdio: "ignore" }); | ||
|
||
execSync("git add -A", { stdio: "ignore" }); | ||
execSync('git commit -m "chore: initial commit from create-ponder"', { | ||
stdio: "ignore", | ||
}); | ||
return true; | ||
} catch (e) { | ||
if (didInit) { | ||
try { | ||
rimraf.sync(path.join(root, ".git")); | ||
} catch (_) {} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const wait = (ms: number) => | ||
new Promise((resolve) => setTimeout(resolve, ms)); |
Oops, something went wrong.
754f8dd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ponder-docs – ./
ponder-docs-git-main-0xolias.vercel.app
ponder-docs.vercel.app
ponder-docs-0xolias.vercel.app
www.ponder.sh
ponder.sh