import.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import fs from 'fs'
  2. import chalk from 'chalk'
  3. import path from 'path'
  4. import ExitCodes from '../../ExitCodes'
  5. import AccountsCommandBase from '../../base/AccountsCommandBase'
  6. import { NamedKeyringPair } from '../../Types'
  7. type AccountImportArgs = {
  8. backupFilePath: string
  9. }
  10. export default class AccountImport extends AccountsCommandBase {
  11. static description = 'Import account using JSON backup file'
  12. static args = [
  13. {
  14. name: 'backupFilePath',
  15. required: true,
  16. description: 'Path to account backup JSON file',
  17. },
  18. ]
  19. async run() {
  20. const args: AccountImportArgs = this.parse(AccountImport).args as AccountImportArgs
  21. const backupAcc: NamedKeyringPair = this.fetchAccountFromJsonFile(args.backupFilePath)
  22. const accountName: string = backupAcc.meta.name
  23. const accountAddress: string = backupAcc.address
  24. const sourcePath: string = args.backupFilePath
  25. const destPath: string = path.join(this.getAccountsDirPath(), this.generateAccountFilename(backupAcc))
  26. try {
  27. fs.copyFileSync(sourcePath, destPath)
  28. } catch (e) {
  29. this.error('Unexpected error while trying to copy input file! Permissions issue?', {
  30. exit: ExitCodes.FsOperationFailed,
  31. })
  32. }
  33. this.log(chalk.bold.greenBright(`ACCOUNT IMPORTED SUCCESFULLY!`))
  34. this.log(chalk.bold.white(`NAME: `), accountName)
  35. this.log(chalk.bold.white(`ADDRESS: `), accountAddress)
  36. }
  37. }