import.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 = <AccountImportArgs> this.parse(AccountImport).args;
  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. }
  29. catch (e) {
  30. this.error(
  31. 'Unexpected error while trying to copy input file! Permissions issue?',
  32. { exit: ExitCodes.FsOperationFailed }
  33. );
  34. }
  35. this.log(chalk.bold.greenBright(`ACCOUNT IMPORTED SUCCESFULLY!`));
  36. this.log(chalk.bold.white(`NAME: `), accountName);
  37. this.log(chalk.bold.white(`ADDRESS: `), accountAddress);
  38. }
  39. }