|
| 1 | +import { ChildProcess, spawn } from 'child_process'; |
| 2 | +import ora from 'ora'; |
| 3 | + |
| 4 | +let currentProcess: ChildProcess | null = null; |
| 5 | + |
| 6 | +async function promptUser(question: string): Promise<string> { |
| 7 | + const { default: readline } = await import('readline/promises'); |
| 8 | + const rl = readline.createInterface({ |
| 9 | + input: process.stdin, |
| 10 | + output: process.stdout, |
| 11 | + }); |
| 12 | + const answer = await rl.question(question); |
| 13 | + rl.close(); |
| 14 | + return answer; |
| 15 | +} |
| 16 | + |
| 17 | +async function executeCommand(command: string[], cwd: string = process.cwd()): Promise<void> { |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + console.log('\nExecuting command:', command.join(' ')); |
| 20 | + |
| 21 | + currentProcess = spawn(command[0], command.slice(1), { |
| 22 | + cwd, |
| 23 | + stdio: 'inherit', |
| 24 | + shell: true |
| 25 | + }); |
| 26 | + |
| 27 | + currentProcess.on('close', (code) => { |
| 28 | + currentProcess = null; |
| 29 | + if (code === 0) { |
| 30 | + console.log('Command executed successfully'); |
| 31 | + resolve(); |
| 32 | + } else { |
| 33 | + reject(new Error(`Command failed with exit code ${code}`)); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + currentProcess.on('error', (err) => { |
| 38 | + currentProcess = null; |
| 39 | + reject(new Error(`Failed to start command: ${err.message}`)); |
| 40 | + }); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function cleanupAndExit() { |
| 45 | + console.log('\nInterrupted. Cleaning up...'); |
| 46 | + if (currentProcess) { |
| 47 | + currentProcess.kill('SIGINT'); |
| 48 | + } |
| 49 | + process.exit(0); |
| 50 | +} |
| 51 | + |
| 52 | +// Set up SIGINT handler |
| 53 | +process.on('SIGINT', cleanupAndExit); |
| 54 | + |
| 55 | +async function checkAccountExists(network: string, account: string): Promise<boolean> { |
| 56 | + try { |
| 57 | + const command = [ |
| 58 | + 'npx', |
| 59 | + 'near-cli-rs', |
| 60 | + 'account', |
| 61 | + 'view-account-summary', |
| 62 | + account, |
| 63 | + 'network-config', |
| 64 | + network, |
| 65 | + 'now' |
| 66 | + ]; |
| 67 | + await executeCommand(command); |
| 68 | + return true; |
| 69 | + } catch (error) { |
| 70 | + return false; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +async function createAccount(network: string, account: string): Promise<void> { |
| 75 | + const spinner = ora('Checking web4 subaccount').start(); |
| 76 | + const subaccount = `web4.${account}`; |
| 77 | + try { |
| 78 | + const exists = await checkAccountExists(network, subaccount); |
| 79 | + if (exists) { |
| 80 | + spinner.succeed(`Web4 subaccount already exists`); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + spinner.text = 'Creating web4 subaccount'; |
| 85 | + const command = [ |
| 86 | + 'near', |
| 87 | + 'account', |
| 88 | + 'create-account', |
| 89 | + 'fund-myself', |
| 90 | + subaccount, |
| 91 | + "'1 NEAR'", |
| 92 | + 'autogenerate-new-keypair', |
| 93 | + 'save-to-keychain', |
| 94 | + `sign-as`, |
| 95 | + account, |
| 96 | + 'network-config', |
| 97 | + network |
| 98 | + ]; |
| 99 | + await executeCommand(command); |
| 100 | + spinner.succeed('Web4 subaccount created successfully'); |
| 101 | + } catch (error) { |
| 102 | + spinner.fail(`Failed to create web4 subaccount: ${error}`); |
| 103 | + throw error; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +async function deployToWeb4(network: string, account: string): Promise<void> { |
| 108 | + const spinner = ora('Deploying to web4').start(); |
| 109 | + try { |
| 110 | + const command = [ |
| 111 | + 'npx', |
| 112 | + 'github:vgrichina/web4-deploy', |
| 113 | + 'dist', |
| 114 | + account, |
| 115 | + '--deploy-contract', |
| 116 | + '--nearfs', |
| 117 | + '--network', |
| 118 | + network |
| 119 | + ]; |
| 120 | + await executeCommand(command); |
| 121 | + spinner.succeed('Deployed to web4 successfully'); |
| 122 | + } catch (error) { |
| 123 | + spinner.fail(`Failed to deploy to web4: ${error}`); |
| 124 | + throw error; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +async function main() { |
| 129 | + console.log('Starting deployment process...'); |
| 130 | + |
| 131 | + const network = await promptUser('Enter the network (mainnet/testnet): '); |
| 132 | + if (network !== 'mainnet' && network !== 'testnet') { |
| 133 | + throw new Error('Invalid network. Please enter either "mainnet" or "testnet".'); |
| 134 | + } |
| 135 | + |
| 136 | + const accountSuffix = network === 'mainnet' ? 'near' : 'testnet'; |
| 137 | + const account = await promptUser(`Enter your account name (e.g root.${accountSuffix}): `); |
| 138 | + const fullAccount = `web4.${account}`; |
| 139 | + |
| 140 | + console.log(`\nCreating your web4 account... web4.${account}`); |
| 141 | + await createAccount(network, account); |
| 142 | + |
| 143 | + console.log('\nDeploying profile to web4 contract...'); |
| 144 | + await deployToWeb4(network, fullAccount); |
| 145 | +} |
| 146 | + |
| 147 | +main().catch((error) => { |
| 148 | + console.error('An error occurred:', error); |
| 149 | + cleanupAndExit(); |
| 150 | +}); |
0 commit comments