Skip to content

Instantly share code, notes, and snippets.

@guiseek
Last active July 29, 2023 03:57
Show Gist options
  • Save guiseek/89c14032f191163508bd48be2836a9ea to your computer and use it in GitHub Desktop.
Save guiseek/89c14032f191163508bd48be2836a9ea to your computer and use it in GitHub Desktop.
Create Nx Monorepo from Figma Figjam
type NxPluginName =
| 'js'
| 'web'
| 'node'
| 'nest'
| 'react'
| 'angular'
| 'express'
type NxProjectType = 'application' | 'library'
type LibraryType = 'data-access' | 'domain' | 'feature' | 'util' | 'ui'
interface PluginProperties {
type: LibraryType
collection: NxProjectType
plugin: NxPluginName
}
interface VariantMixin {
readonly variantProperties: PluginProperties
}
function isRepoScope<T extends SceneNode>(node: T) {
return node.type === 'SECTION'
}
function isTextNode<T extends SceneNode>(node: T) {
return node.type === 'TEXT'
}
function isNxPlugin<T extends SceneNode>(node: T) {
return node.name === '@nx' && node.type === 'INSTANCE'
}
function findScopesOnPage<R extends SectionNode>(page: PageNode) {
return page.children.filter(isRepoScope) as R[]
}
function findLibsOnScope(scope: SectionNode) {
return scope.children.filter(isNxPlugin).filter((child: InstanceNode) => {
const {collection} = child.variantProperties
return collection === 'library'
}) as InstanceNode[]
}
function findLibTextNodes(libs: InstanceNode) {
const frame = libs.children[0] as FrameNode
return frame.children.filter(isTextNode) as TextNode[]
}
function getMetadata<T extends TextNode>(nodes: T[]) {
const chars = nodes.map((node) => node.characters)
const [plugin, collection, name, libraryType] = chars
return {plugin, collection, name, libraryType}
}
figma.on('run', () => {
findScopesOnPage(figma.currentPage).map((scope) => {
console.log('\n---------------------\n')
console.log('Scope: ', scope.name)
console.log('')
findLibsOnScope(scope).map((lib) => {
const {name, plugin, libraryType, collection} = getMetadata(
findLibTextNodes(lib)
)
console.log(`Name : ${name}`)
console.log(`Metadata : ${plugin}, ${libraryType}, ${collection}`)
console.log(``)
})
})
figma.closePlugin('Closed')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment