-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6e96d8
commit 65c2868
Showing
18 changed files
with
598 additions
and
40 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-lz-oapp": minor | ||
--- | ||
|
||
Add ability to download & install OApp & OFT examples |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from "react" | ||
import { Box, Text } from "ink" | ||
import { Config } from "@/types.js" | ||
import { resolve } from "path" | ||
|
||
interface Props { | ||
value: Config | ||
} | ||
|
||
export const ConfigSummary: React.FC<Props> = ({ value }) => { | ||
return ( | ||
<Box flexDirection="column"> | ||
<Text> | ||
Will create a project in <Text bold>{value.destination || "current"}</Text> directory ( | ||
<Text bold>{resolve(value.destination)}</Text>) | ||
</Text> | ||
<Text> | ||
Will use the <Text bold>{value.example.label}</Text> example | ||
</Text> | ||
<Text> | ||
Will use <Text bold>{value.packageManager.label}</Text> to install dependencies | ||
</Text> | ||
</Box> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
import React from "react" | ||
import { Box, Text } from "ink" | ||
import Spinner from "ink-spinner" | ||
import { UseMutationResult } from "@tanstack/react-query" | ||
|
||
type ErrorComponent = React.ComponentType<{ error: unknown }> | ||
|
||
interface Props { | ||
error: ErrorComponent | ||
message: string | ||
mutation: Pick<UseMutationResult, "isPending" | "isSuccess" | "error"> | ||
} | ||
|
||
export const Progress: React.FC<Props> = ({ mutation, message, error: Error }) => { | ||
const { isPending, isSuccess, error } = mutation | ||
|
||
return ( | ||
<Box flexDirection="column"> | ||
<Box> | ||
{isPending ? ( | ||
<Spinner /> | ||
) : isSuccess ? ( | ||
<Text color="green">✔</Text> | ||
) : error ? ( | ||
<Text color="red">𐄂</Text> | ||
) : ( | ||
<Text color="yellow">○</Text> | ||
)} | ||
<Text> {message}</Text> | ||
</Box> | ||
|
||
{error == null ? null : ( | ||
<Box marginLeft={2}> | ||
<Error error={error} /> | ||
</Box> | ||
)} | ||
</Box> | ||
) | ||
} |
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,8 @@ | ||
import React, { useState } from "react" | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query" | ||
|
||
export const Providers: React.FC<React.PropsWithChildren> = ({ children }) => { | ||
const [queryClient] = useState(() => new QueryClient()) | ||
|
||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
} |
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,91 @@ | ||
import React, { useEffect } from "react" | ||
import type { Config } from "@/types.js" | ||
import { Box, Text } from "ink" | ||
import { useMutation } from "@tanstack/react-query" | ||
import { DestinationNotEmptyError, DownloadError, MissingGitRefError, cloneExample } from "@/utilities/cloning.js" | ||
import { Progress } from "./progress.js" | ||
import { installDependencies } from "@/utilities/installation.js" | ||
|
||
interface Props { | ||
config: Config | ||
} | ||
|
||
export const Setup: React.FC<Props> = ({ config }) => { | ||
const clone = useMutation({ | ||
mutationKey: ["setup", "clone"], | ||
mutationFn: () => cloneExample(config), | ||
}) | ||
|
||
const install = useMutation({ | ||
mutationKey: ["setup", "install"], | ||
mutationFn: () => installDependencies(config), | ||
}) | ||
|
||
const { mutate: setup } = useMutation({ | ||
mutationKey: ["setup", "flow"], | ||
mutationFn: async () => { | ||
await clone.mutateAsync() | ||
await install.mutateAsync() | ||
}, | ||
}) | ||
|
||
useEffect(() => setup(), [setup]) | ||
|
||
return ( | ||
<Box flexDirection="column"> | ||
<Progress | ||
message="Getting example source code" | ||
mutation={clone} | ||
error={({ error }) => <ErrorMessage config={config} error={error} />} | ||
/> | ||
<Progress | ||
message="Installing dependencies" | ||
mutation={install} | ||
error={({ error }) => <ErrorMessage config={config} error={error} />} | ||
/> | ||
</Box> | ||
) | ||
} | ||
|
||
interface ErrorMessageProps { | ||
config: Config | ||
error?: unknown | ||
} | ||
|
||
const ErrorMessage: React.FC<ErrorMessageProps> = ({ config, error }) => { | ||
if (error == null) return null | ||
|
||
switch (true) { | ||
case error instanceof DestinationNotEmptyError: | ||
return ( | ||
<Text color="red"> | ||
Destination directory <Text bold>{config.destination}</Text> is not empty | ||
</Text> | ||
) | ||
|
||
case error instanceof MissingGitRefError: | ||
return ( | ||
<Text color="red"> | ||
The example <Text bold>{config.example.label}</Text> does not seem to exist in the repository | ||
</Text> | ||
) | ||
|
||
case error instanceof DownloadError: | ||
return ( | ||
<Box flexDirection="column"> | ||
<Text color="red">There was a problem downloading the example</Text> | ||
<Text>○ Please check your internet connection</Text> | ||
<Text> | ||
○ Please check that the example exists (<Text bold>{config.example.repository}</Text>) | ||
</Text> | ||
</Box> | ||
) | ||
|
||
default: | ||
return ( | ||
<Text color="red"> | ||
An unknown error happened: <Text bold>{String(error)}</Text> | ||
</Text> | ||
) | ||
} | ||
} |
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,35 @@ | ||
import type { Example, PackageManager } from "@/types.js" | ||
|
||
export const EXAMPLES: Example[] = [ | ||
{ | ||
id: "oft", | ||
label: "OFT", | ||
repository: "[email protected]:LayerZero-Labs/lz-examples", | ||
directory: "packages/oft", | ||
}, | ||
{ | ||
id: "oapp", | ||
label: "OApp", | ||
repository: "[email protected]:LayerZero-Labs/lz-examples", | ||
directory: "packages/oapp", | ||
}, | ||
] | ||
|
||
export const PACKAGE_MANAGERS: PackageManager[] = [ | ||
{ | ||
command: "npm", | ||
label: "npm", | ||
}, | ||
{ | ||
command: "yarn", | ||
label: "yarn", | ||
}, | ||
{ | ||
command: "pnpm", | ||
label: "pnpm", | ||
}, | ||
{ | ||
command: "bun", | ||
label: "bun", | ||
}, | ||
] |
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,19 +1,41 @@ | ||
import React from "react" | ||
import { render } from "ink" | ||
import { Command } from "commander" | ||
import { Placeholder } from "./components/placeholder.js" | ||
import { altScreen } from "./utilities/terminal.js" | ||
import { promptForConfig, promptForContinue } from "@/utilities/prompts.js" | ||
import { Header } from "@/components/branding.js" | ||
import { ConfigSummary } from "@/components/config.js" | ||
import { Setup } from "@/components/setup.js" | ||
import { Providers } from "@/components/providers.js" | ||
|
||
new Command("create-lz-oapp") | ||
.description("Create LayerZero OApp with one command") | ||
.action(async () => { | ||
const exitAltScreen = await altScreen() | ||
// const exitAltScreen = await altScreen() | ||
|
||
try { | ||
const { waitUntilExit } = render(<Placeholder />) | ||
await waitUntilExit() | ||
render(<Header />).unmount() | ||
|
||
// First we get the config from the user | ||
const config = await promptForConfig() | ||
render(<ConfigSummary value={config} />).unmount() | ||
|
||
// Then we confirm we want to do this after showing the user what they have specified | ||
const continuePlease = await promptForContinue() | ||
if (!continuePlease) { | ||
return | ||
} | ||
|
||
// Then the last step is to show the setup flow | ||
const setup = render( | ||
<Providers> | ||
<Setup config={config} /> | ||
</Providers> | ||
) | ||
|
||
// And wait for it to exit | ||
await setup.waitUntilExit() | ||
} finally { | ||
await exitAltScreen() | ||
// await exitAltScreen() | ||
} | ||
}) | ||
.parseAsync() |
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,17 @@ | ||
export interface Config { | ||
destination: string | ||
example: Example | ||
packageManager: PackageManager | ||
} | ||
|
||
export interface Example { | ||
id: string | ||
label: string | ||
repository: string | ||
directory?: string | ||
} | ||
|
||
export interface PackageManager { | ||
command: string | ||
label: 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,63 @@ | ||
import { Config } from "@/types.js" | ||
import { rm } from "fs/promises" | ||
import tiged from "tiged" | ||
|
||
export const cloneExample = async ({ example, destination }: Config) => { | ||
const qualifier = example.directory ? `${example.repository}/${example.directory}` : example.repository | ||
const emitter = tiged(qualifier, { | ||
disableCache: true, | ||
mode: "git", | ||
verbose: true, | ||
}) | ||
|
||
try { | ||
return await emitter.clone(destination) | ||
} catch (error: unknown) { | ||
try { | ||
// Let's make sure to clean up after us | ||
await rm(destination, { recursive: true, force: true }) | ||
} catch { | ||
// If the cleanup fails let's just do nothing for now | ||
} | ||
|
||
if (error instanceof Error && "code" in error) { | ||
switch (error.code) { | ||
case "DEST_NOT_EMPTY": | ||
throw new DestinationNotEmptyError() | ||
|
||
case "ENOENT": | ||
case "MISSING_REF": | ||
throw new MissingGitRefError() | ||
|
||
case "COULD_NOT_DOWNLOAD": | ||
throw new DownloadError() | ||
} | ||
} | ||
|
||
throw new CloningError() | ||
} | ||
} | ||
|
||
export class CloningError extends Error { | ||
constructor(message: string = "Unknown error during example cloning") { | ||
super(message) | ||
} | ||
} | ||
|
||
export class DestinationNotEmptyError extends CloningError { | ||
constructor(message: string = "Project destination directory is not empty") { | ||
super(message) | ||
} | ||
} | ||
|
||
export class MissingGitRefError extends CloningError { | ||
constructor(message: string = "Could not find the example repository or branch") { | ||
super(message) | ||
} | ||
} | ||
|
||
export class DownloadError extends CloningError { | ||
constructor(message: string = "Could not download the example from repository") { | ||
super(message) | ||
} | ||
} |
Oops, something went wrong.