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AA0B,eAAK,CAAC,WAAW,CAAC,2CAA2C,CAAC,sEAAsE,CAAC,CAAA;IACzO,CAAC;IAED,IAAI,OAAO,EAAE,CAAC;QACZ,gBAAM,CAAC,IAAI,CAAC,KAAK,OAAO,EAAE,CAAC,CAAA;IAC7B,CAAC;AACH,CAAC;AAED,SAAgB,SAAS;IACvB,IAAA,wBAAQ,EAAC,gBAAgB,EAAE,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAA;AACpD,CAAC","sourcesContent":["import chalk from 'chalk'\nimport { logger } from './logging'\nimport { execSync } from 'child_process'\nimport { compareVersions } from 'compare-versions'\nimport { BaseCommand } from '../commands/connect'\nimport { Command } from 'commander'\nimport { request } from './fetch'\n\nlet updatedVersionAvailable: string | false | undefined = undefined\nlet message: string | undefined = undefined\n\n// The type of the arguments passed to a command handler:\n// any arguments, then the command arguments, then the Command object\ntype CommandArgs = [...any[], T, Command]\n\n// Wrap action handlers to check for updates or a message, and output a message\n// after the action if any are available\nexport function withUpdateCheck(\n // The second to last argument is always the command args, but I couldn't work\n // out how to model this with Typescript here\n fn: (...args: any[]) => void | Promise,\n) {\n return (...args: CommandArgs) => {\n // Get the args passed at the command line (the second to last argument)\n const commandArgs = args[args.length - 2]\n // Anything before that is a regular arg\n const restArgs = args.slice(0, -2)\n\n if (commandArgs.skipUpdateCheck) {\n return fn(...restArgs, commandArgs)\n }\n\n startUpdateCheck()\n\n const result = fn(...restArgs, commandArgs)\n if (result instanceof Promise) {\n result.finally(waitAndCheckForUpdates)\n } else {\n waitAndCheckForUpdates()\n }\n }\n}\n\n// Start checking for updates in the background. We don't wait for this before\n// running the action, as we will show the result at the end\nfunction startUpdateCheck() {\n request\n .get<{ tag_name: string }>('https://api.github.com/repos/figma/code-connect/releases/latest')\n .then((response) => {\n const latestVersion = response.data.tag_name.replace(/^v/, '')\n const currentVersion = require('../../package.json').version\n\n if (compareVersions(latestVersion, currentVersion) === 1) {\n updatedVersionAvailable = latestVersion\n } else {\n updatedVersionAvailable = false\n }\n })\n .catch(() => {\n // Silently fail if we can't check for updates\n updatedVersionAvailable = false\n })\n}\n\n// Wait for up to 1 second for the result of update checking to be available,\n// and output a message if there is an update or a message. If there is no\n// result after the timeout, the app will exit.\nasync function waitAndCheckForUpdates() {\n for (let i = 0; i < 10; i++) {\n if (updatedVersionAvailable !== undefined) {\n break\n }\n await new Promise((resolve) => setTimeout(resolve, 100))\n }\n\n maybeShowUpdateMessage()\n}\n\n// Exit the process, first checking if there is an update or message available.\n// Unlike waitAndCheckForUpdates, this will not wait for the update check to\n// complete, so if the request has not completed yet, nothing will be shown.\n// This is to avoid confusion about when the process _actually_ exits.\nexport function exitWithUpdateCheck(errorCode = 1): never {\n maybeShowUpdateMessage()\n process.exit(errorCode)\n}\n\nfunction getUpdateCommand() {\n return 'npm update -g @figma/code-connect'\n}\n\nfunction maybeShowUpdateMessage() {\n if (updatedVersionAvailable) {\n logger.warn(`\\nA new version of the Figma CLI is available. v${require('../../package.json').version} is currently installed, and the latest version available is v${updatedVersionAvailable}.\n\nTo update, run ${chalk.whiteBright('npm install @figma/code-connect@latest')} for React or HTML, or ${chalk.whiteBright('npm install -g @figma/code-connect@latest')} for other targets (or if you have Code Connect installed globally).`)\n }\n\n if (message) {\n logger.warn(`\\n${message}`)\n }\n}\n\nexport function updateCli() {\n execSync(getUpdateCommand(), { stdio: 'inherit' })\n}\n"]}