import { getSettings , resetSettings , setSettings } from './utilities/settings' ; import { getAccessToken , setAccessToken } from './utilities/accessToken' ; import { Settings as UserSettings } from '@typings/settings' ; import config from '@config/config' ; import { commands , PluginCommands } from '@config/commands' ; import getVersionDifference from './utilities/getVersionDifference' ; import getFileId from './utilities/getFileId' ; import { PluginMessage } from '../types/pluginEvent' ; import { exportRawTokenArray } from './utilities/getTokenJson' ; import { stringifyJson } from './utilities/stringifyJson' ; figma . showUI ( __html__ , { visible : false } ) ; if ( [ commands . export , commands . urlExport , commands . generalSettings ] . includes ( figma . command as PluginCommands ) ) { const openUi = async ( ) => { const userSettings = getSettings ( ) ; const versionDifference = await getVersionDifference ( figma ) ; figma . ui . resize ( config . ui [ figma . command ] . width , config . ui [ figma . command ] . height ) ; if ( versionDifference !== undefined && versionDifference !== 'patch' ) { figma . ui . resize ( config . ui [ figma . command ] . width , config . ui [ figma . command ] . height + 60 ) ; } figma . ui . postMessage ( { command : figma . command as PluginCommands , payload : { settings : { ... userSettings , ... { accessToken : await getAccessToken ( getFileId ( figma ) ) } } , data : stringifyJson ( exportRawTokenArray ( figma , userSettings ) ) , versionDifference : versionDifference , metadata : { filename : figma . root . name } } } as PluginMessage ) ; figma . ui . show ( ) ; } ; openUi ( ) ; } /**
 * Open Help
 * Open github help page
 */ if ( figma . command === commands . help ) { figma . ui . postMessage ( { command : commands . help , payload : { url : 'https://github.com/lukasoppermann/design-tokens' } } as PluginMessage ) ; } /**
 * Open demo
 */ if ( figma . command === commands . demo ) { figma . ui . postMessage ( { command : commands . demo , payload : { url : 'https://www.figma.com/file/2MQ759R5kJtzQn4qSHuqR7/Design-Tokens-for-Figma?node-id=231%3A2' } } as PluginMessage ) ; } /**
 * Reset settings
 */ if ( figma . command === commands . reset ) { resetSettings ( ) ; figma . notify ( '⚙️ Settings have been reset.' ) ; figma . closePlugin ( ) ; } /**
 * React to messages
 */ figma . ui . onmessage = async ( message ) => { const { command , payload } = message ; /**
     * on closePlugin
     * close plugin and show notification if available
     */ if ( command === commands . closePlugin ) { if ( payload ?. notification !== undefined && payload ?. notification !== '' ) { figma . notify ( payload . notification ) ; } figma . ui . hide ( ) ; figma . closePlugin ( ) ; } /**
     * on saveSettings
     * save settings, access token and close plugin
     */ if ( command === commands . saveSettings ) { setSettings ( payload . settings ) ; await setAccessToken ( getFileId ( figma ) , payload . accessToken ) ; if ( payload . closePlugin && payload . closePlugin === true ) { figma . closePlugin ( ) ; } } } ;