import { customTokenNode } from '@typings/tokenNodeTypes' ; import extractTokenNodeValues from './extractTokenNodeValues' ; import isTokenNode from './isTokenNode' ; const tokenFrameName = '_tokens' ; const isTokenFrame = ( node ) : boolean => node . type === 'FRAME' && node . name . trim ( ) . toLowerCase ( ) . substr ( 0 , tokenFrameName . length ) === tokenFrameName ; const getFrameNodes = ( nodes ) : FrameNode [ ] => [ ... nodes . map ( page => page . findChildren ( node => isTokenFrame ( node ) ) ) . reduce ( ( flatten , arr ) => [ ... flatten , ... arr ] ) ] ; /**
 * getVariantName
 * creates the variant name of the parent and child name
 */ const getVariantName = ( parentName , childName ) : string => { childName = childName . split ( ',' ) . filter ( part => ! [ '_' , '.' ] . includes ( part . trim ( ) . substr ( 0 , 1 ) ) ) . map ( part => part . split ( '=' ) [ 1 ] ) . join ( '/' ) ; return `${ parentName }/${ childName }` ; } ; param pages const getTokenNodes = ( pages ) : customTokenNode [ ] => { const tokenFrames = getFrameNodes ( pages ) ; return tokenFrames . map ( frame => < ComponentSetNode [ ] | ComponentNode [ ] | RectangleNode [ ] | FrameNode [ ] > frame . findAll ( node => isTokenNode ( node ) ) ) . reduce ( ( flatten , arr ) => [ ... flatten , ... arr ] , [ ] ) . map ( ( item ) : customTokenNode [ ] => { if ( item . type === 'RECTANGLE' || item . type === 'FRAME' ) { console . warn ( 'Please use only main components and variants, other types may be deprecated as tokens in the future' ) ; } if ( item . type === 'COMPONENT_SET' ) { return item . children . map ( ( child ) => ( { ... extractTokenNodeValues ( child ) , ... { name : getVariantName ( item . name , child . name ) } } ) ) ; } return [ extractTokenNodeValues ( item ) ] ; } ) . reduce ( ( flatten , arr ) => [ ... flatten , ... arr ] , [ ] ) ; } ; export default getTokenNodes ; export const __testing = { isTokenNode : isTokenNode , isTokenFrame : isTokenFrame } ;