Skip to content

Commit

Permalink
wip(refactor): typechecks, modularize @fadroma/deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 16, 2024
1 parent 131f0e1 commit aae8ec4
Show file tree
Hide file tree
Showing 31 changed files with 1,421 additions and 864 deletions.
15 changes: 7 additions & 8 deletions packages/cw/cw-cli.ts → packages/cli/cli-cw.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { bold } from '@hackbg/fadroma'
import CLI from '@hackbg/cmds'
import * as Chains from './cw-chains'
import { CWConnection } from './cw-connection'
import * as CW from '@fadroma/cw'

export default class CWCLI extends CLI {

Expand All @@ -10,11 +9,11 @@ export default class CWCLI extends CLI {
this.log.label = ``
}

archway = this.commands('archway', 'commands for Archway', new Chains.Archway.CLI())
axelar = this.commands('axelar', 'commands for Axelar', new Chains.Axelar.CLI())
injective = this.commands('injective', 'commands for Injective', new Chains.Injective.CLI())
okp4 = this.commands('okp4', 'commands for OKP4', new Chains.OKP4.CLI())
osmosis = this.commands('osmosis', 'commands for Osmosis', new Chains.Osmosis.CLI())
archway = this.commands('archway', 'commands for Archway', new CW.Archway.CLI())
axelar = this.commands('axelar', 'commands for Axelar', new CW.Axelar.CLI())
injective = this.commands('injective', 'commands for Injective', new CW.Injective.CLI())
okp4 = this.commands('okp4', 'commands for OKP4', new CW.OKP4.CLI())
osmosis = this.commands('osmosis', 'commands for Osmosis', new CW.Osmosis.CLI())

check = this.command({
name: 'check',
Expand All @@ -25,7 +24,7 @@ export default class CWCLI extends CLI {
this.log.error(bold('Pass a RPC URL to connect.'))
process.exit(1)
}
const connection = new CWConnection({ url })
const connection = await CW.connect({ url })
this.log.info(`Will exit with error code if not connected in ${timeout}s.`)
const timer = setTimeout(()=>{
this.log.error(`Failed to connect in ${timeout}s.`)
Expand Down
135 changes: 135 additions & 0 deletions packages/cli/cli-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import type { Path } from '@hackbg/file'
import { FileFormat, SyncFS } from '@hackbg/file'
import Commands from '@hackbg/cmds'
import type { ChainId, Connection, Agent } from '@hackbg/fadroma'
import { bold } from '@hackbg/fadroma'
import { getProject, ProjectPrompter } from '@fadroma/create'
import { getCompiler } from '@fadroma/compile'
import {
getUploadStore, getDeployStore, Deployment, selectDeployment, exportDeployment
} from '@fadroma/deploy'

export default function main (...args: any) {
console.debug('Running main...')
return new Commands()
.addCommand(
{ name: 'run', info: 'execute a script', args: 'SCRIPT' },
(script: string, ...args: string[]) => runScript({ project: getProject(), script, args }))
.addCommand(
{name: 'repl', info: 'open an interactive Fadroma shell', args: '' },
(script: string, ...args: string[]) => runRepl({ project: getProject(), script, args }))
.addCommand(
{name: 'status', info: 'show the status of the project', args: '' },
() => getProject().logStatus())
.addCommand(
{name: 'build', info: 'build the project or specific contracts from it', args: '[CONTRACT...]'},
(...units: string[]) => getProject().getDeployment().then(async deployment=>deployment.build({
compiler: await getCompiler(),
units
})))
.addCommand(
{name: 'rebuild', info: 'rebuild the project or specific contracts from it', args: ''},
(...units: string[]) => getProject().getDeployment().then(async deployment=>deployment.build({
compiler: await getCompiler(),
units,
rebuild: true
})))
.addCommand(
{name: 'upload', info: 'upload the project or specific contracts from it', args: ''},
(...units: string[]) => getProject().getDeployment().then(async deployment=>deployment.upload({
compiler: await getCompiler(),
uploadStore: getUploadStore(),
uploader: getAgent(),
units
})))
.addCommand(
{name: 'reupload', info: 'reupload the project or specific contracts from it', args: ''},
(...units: string[]) => getProject().getDeployment().then(async deployment=>deployment.upload({
compiler: await getCompiler(),
uploadStore: getUploadStore(),
uploader: getAgent(),
reupload: true,
units,
})))
.addCommand(
{name: 'deploy', info: 'deploy getProject() or continue an interrupted deployment', args: ''},
(...units: string[]) => getProject().getDeployment().then(async deployment=>deployment.deploy({
compiler: await getCompiler(),
uploadStore: getUploadStore(),
deployStore: getDeployStore(),
deployer: getAgent(),
units
})))
.addCommand(
{name: 'redeploy', info: 'redeploy getProject() from scratch', args: ''},
(...units: string[]) => getProject().getDeployment().then(async deployment=>deployment.deploy({
compiler: await getCompiler(),
uploadStore: getUploadStore(),
deployStore: getDeployStore(),
deployer: getAgent(),
redeploy: true,
units,
})))
.addCommand(
{name: 'select', info: `activate another deployment`, args: ''},
async (name?: string): Promise<Deployment|undefined> => selectDeployment(
getProject().root,
name
))
.addCommand(
{name: 'export', info: `export current deployment to JSON`, args: ''},
async (path?: string) => exportDeployment(
getProject().root,
await getProject().getDeployment(),
path
))
//.addCommand({name: 'reset', 'stop and erase running devnets',
//(...ids: ChainId[]) => Devnets.deleteDevnets(
//getProject().root, ids))
}

export async function runScript (context?: { project?: Project, script?: string, args: string[] }) {
const { project, script, args } = context || {}
if (!script) {
throw new Error(`Usage: fadroma run SCRIPT [...ARGS]`)
}
const scriptPath = new SyncFS.Path(script)
if (!scriptPath.exists()) {
throw new Error(`${script} doesn't exist`)
}
console.log(`Running ${script}`)
const { default: main } = await import(scriptPath.absolute)
if (typeof main === 'function') {
return main(project, ...args||[])
} else {
console.error(`The default export of ${bold(scriptPath.short)} is not a function`)
process.exit(1)
}
}

export async function runRepl (context?: { project?: Project, script?: string, args: string[] }) {
const { project, script, args } = context || {}
let start
try {
const repl = await import('node:repl')
start = repl.start
} catch (e) {
console.error('Node REPL unavailable.')
throw e
}
const context2 = start() || project?.getDeployment()
}

export function getConnection (): Connection {
throw new Error('not implemented')
}

export function getAgent (): Agent {
throw new Error('not implemented')
}

interface Project { // FIXME
root: any
getDeployment(): Promise<Deployment>
logStatus(): unknown
}
Loading

0 comments on commit aae8ec4

Please sign in to comment.