|
| 1 | +import { findAccountByName, listAccounts, updateAccountTokens } from '@core/auth/config-store'; |
| 2 | +import { buildClient } from '@core/client/build-client'; |
| 3 | +import { EXIT_CODE } from '@core/errors/exit-codes'; |
| 4 | +import { Args, Command, Flags } from '@oclif/core'; |
| 5 | +import { readMaskedInput } from '@utils/masked-input'; |
| 6 | +import { isStdinTty } from '@utils/tty'; |
| 7 | + |
| 8 | +export default class AccountUpdate extends Command { |
| 9 | + static override description = 'Update tokens for a saved LinkedIn account'; |
| 10 | + |
| 11 | + static override args = { |
| 12 | + name: Args.string({ |
| 13 | + description: |
| 14 | + 'Account name to update (case-insensitive substring match). Defaults to active account.', |
| 15 | + }), |
| 16 | + }; |
| 17 | + |
| 18 | + static override flags = { |
| 19 | + 'linked-api-token': Flags.string({ |
| 20 | + description: 'New Linked API Token (for non-interactive use)', |
| 21 | + }), |
| 22 | + 'identification-token': Flags.string({ |
| 23 | + description: 'New Identification Token (for non-interactive use)', |
| 24 | + }), |
| 25 | + }; |
| 26 | + |
| 27 | + static override examples = [ |
| 28 | + '<%= config.bin %> account update', |
| 29 | + '<%= config.bin %> account update "John"', |
| 30 | + '<%= config.bin %> account update --linked-api-token=xxx --identification-token=yyy', |
| 31 | + ]; |
| 32 | + |
| 33 | + public async run(): Promise<void> { |
| 34 | + const { args, flags } = await this.parse(AccountUpdate); |
| 35 | + |
| 36 | + const account = this.resolveAccount(args.name); |
| 37 | + |
| 38 | + if (!account) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + let linkedApiToken: string; |
| 43 | + let identificationToken: string; |
| 44 | + |
| 45 | + if (flags['linked-api-token'] && flags['identification-token']) { |
| 46 | + linkedApiToken = flags['linked-api-token']; |
| 47 | + identificationToken = flags['identification-token']; |
| 48 | + } else if (!isStdinTty()) { |
| 49 | + process.stderr.write(`Cannot run interactive update in non-interactive mode. |
| 50 | +Use flags instead: |
| 51 | +
|
| 52 | + linkedin account update --linked-api-token=xxx --identification-token=yyy |
| 53 | +
|
| 54 | +Get tokens at https://app.linkedapi.io\n`); |
| 55 | + this.exit(EXIT_CODE.AUTH); |
| 56 | + return; |
| 57 | + } else { |
| 58 | + process.stdout.write( |
| 59 | + `Updating tokens for "${account.name}".\n` + |
| 60 | + 'Get new tokens at https://app.linkedapi.io\n\n', |
| 61 | + ); |
| 62 | + |
| 63 | + linkedApiToken = await readMaskedInput('New Linked API Token: ', 'account_'); |
| 64 | + identificationToken = await readMaskedInput('New Identification Token: ', 'id_'); |
| 65 | + } |
| 66 | + |
| 67 | + if (!linkedApiToken || !identificationToken) { |
| 68 | + process.stderr.write('Both tokens are required.\n'); |
| 69 | + this.exit(EXIT_CODE.AUTH); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + process.stdout.write('Verifying tokens... '); |
| 74 | + |
| 75 | + let accountName = ''; |
| 76 | + |
| 77 | + try { |
| 78 | + const tempClient = buildClient({ |
| 79 | + linkedApiToken, |
| 80 | + identificationToken, |
| 81 | + }); |
| 82 | + const accountInfo = await tempClient.getAccountInfo(); |
| 83 | + accountName = accountInfo.data?.name ?? ''; |
| 84 | + process.stdout.write('OK\n'); |
| 85 | + } catch { |
| 86 | + process.stdout.write('FAILED\n'); |
| 87 | + process.stderr.write( |
| 88 | + '\nInvalid tokens. Make sure you copied the correct tokens from https://app.linkedapi.io\n', |
| 89 | + ); |
| 90 | + this.exit(EXIT_CODE.AUTH); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const displayName = accountName || account.name; |
| 95 | + |
| 96 | + const updated = updateAccountTokens(account.identificationToken, { |
| 97 | + name: displayName, |
| 98 | + linkedApiToken, |
| 99 | + identificationToken, |
| 100 | + }); |
| 101 | + |
| 102 | + if (!updated) { |
| 103 | + process.stderr.write('Failed to update account.\n'); |
| 104 | + this.exit(EXIT_CODE.GENERAL); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + process.stdout.write(`Account "${displayName}" updated successfully.\n`); |
| 109 | + } |
| 110 | + |
| 111 | + private resolveAccount( |
| 112 | + nameQuery?: string, |
| 113 | + ): { name: string; identificationToken: string } | undefined { |
| 114 | + const accounts = listAccounts(); |
| 115 | + |
| 116 | + if (accounts.length === 0) { |
| 117 | + process.stderr.write('No accounts configured. Run "linkedin setup" to add one.\n'); |
| 118 | + this.exit(EXIT_CODE.AUTH); |
| 119 | + return undefined; |
| 120 | + } |
| 121 | + |
| 122 | + if (nameQuery) { |
| 123 | + const account = findAccountByName(nameQuery); |
| 124 | + |
| 125 | + if (!account) { |
| 126 | + process.stderr.write( |
| 127 | + `Account "${nameQuery}" not found. Run "linkedin account list" to see available accounts.\n`, |
| 128 | + ); |
| 129 | + this.exit(EXIT_CODE.GENERAL); |
| 130 | + return undefined; |
| 131 | + } |
| 132 | + |
| 133 | + return account; |
| 134 | + } |
| 135 | + |
| 136 | + const current = accounts.find((account) => account.isCurrent); |
| 137 | + |
| 138 | + if (!current) { |
| 139 | + process.stderr.write( |
| 140 | + 'No active account. Run "linkedin account list" to see available accounts.\n', |
| 141 | + ); |
| 142 | + this.exit(EXIT_CODE.AUTH); |
| 143 | + return undefined; |
| 144 | + } |
| 145 | + |
| 146 | + return current; |
| 147 | + } |
| 148 | +} |
0 commit comments