Skip to content

Commit

Permalink
Add ability to export specified colors (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Subbotin authored May 14, 2022
1 parent 3483e2b commit 54813f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,17 @@ For Typography, Colors and Icons you can enable code generation for the use with

### Arguments

If you want to export specific icons/images you can list their names in the last argument like this:
If you want to export specific colors/icons/images you can list their names in the last argument like this:

`./figma-export icons "ic/24/edit"` — Exports only one icon.

`./figma-export icons "ic/24/edit, ic/16/notification"` — Exports two icons

`./figma-export icons "ic/24/videoplayer/*"` — Exports all icons which names starts with `ic/24/videoplayer/`

`./figma-export icons` — Exports all the icons.
`./figma-export colors "common/*"` — Exports all the colors which names starts with `common`

`./figma-export colors` — Exports all the colors.

Argument `-i` or `-input` specifies path to FigmaExport configuration file `figma-export.yaml`.

Expand Down
44 changes: 26 additions & 18 deletions Sources/FigmaExport/Loaders/ColorsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@ final class ColorsLoader {
self.colorParams = colorParams
}

func load() throws -> (light: [Color], dark: [Color]?, lightHC: [Color]?, darkHC: [Color]?) {
func load(filter: String?) throws -> (light: [Color], dark: [Color]?, lightHC: [Color]?, darkHC: [Color]?) {
guard let useSingleFile = colorParams?.useSingleFile, useSingleFile else {
return try loadColorsFromLightAndDarkFile()
return try loadColorsFromLightAndDarkFile(filter: filter)
}
return try loadColorsFromSingleFile()
return try loadColorsFromSingleFile(filter: filter)
}

private func loadColorsFromLightAndDarkFile() throws -> (light: [Color],
dark: [Color]?,
lightHC: [Color]?,
darkHC: [Color]?) {
let lightColors = try loadColors(fileId: figmaParams.lightFileId)
let darkColors = try figmaParams.darkFileId.map { try loadColors(fileId: $0) }
let lightHighContrastColors = try figmaParams.lightHighContrastFileId.map { try loadColors(fileId: $0) }
let darkHighContrastColors = try figmaParams.darkHighContrastFileId.map { try loadColors(fileId: $0) }
private func loadColorsFromLightAndDarkFile(filter: String?) throws -> (light: [Color],
dark: [Color]?,
lightHC: [Color]?,
darkHC: [Color]?) {
let lightColors = try loadColors(fileId: figmaParams.lightFileId, filter: filter)
let darkColors = try figmaParams.darkFileId.map { try loadColors(fileId: $0, filter: filter) }
let lightHighContrastColors = try figmaParams.lightHighContrastFileId.map { try loadColors(fileId: $0, filter: filter) }
let darkHighContrastColors = try figmaParams.darkHighContrastFileId.map { try loadColors(fileId: $0, filter: filter) }
return (lightColors, darkColors, lightHighContrastColors, darkHighContrastColors)
}

private func loadColorsFromSingleFile() throws -> (light: [Color],
dark: [Color]?,
lightHC: [Color]?,
darkHC: [Color]?) {
let colors = try loadColors(fileId: figmaParams.lightFileId)
private func loadColorsFromSingleFile(filter: String?) throws -> (light: [Color],
dark: [Color]?,
lightHC: [Color]?,
darkHC: [Color]?) {
let colors = try loadColors(fileId: figmaParams.lightFileId, filter: filter)

let darkSuffix = colorParams?.darkModeSuffix ?? "_dark"
let lightHCSuffix = colorParams?.lightHCModeSuffix ?? "_lightHC"
let darkHCSuffix = colorParams?.darkHCModeSuffix ?? "_darkHC"
Expand Down Expand Up @@ -64,8 +65,15 @@ final class ColorsLoader {
return filteredColors
}

private func loadColors(fileId: String) throws -> [Color] {
let styles = try loadStyles(fileId: fileId)
private func loadColors(fileId: String, filter: String?) throws -> [Color] {
var styles = try loadStyles(fileId: fileId)

if let filter = filter {
let assetsFilter = AssetsFilter(filter: filter)
styles = styles.filter { style -> Bool in
assetsFilter.match(name: style.name)
}
}

guard !styles.isEmpty else {
throw FigmaExportError.stylesNotFound
Expand Down
9 changes: 8 additions & 1 deletion Sources/FigmaExport/Subcommands/ExportColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ extension FigmaExportCommand {
@OptionGroup
var options: FigmaExportOptions

@Argument(help: """
[Optional] Name of the colors to export. For example \"background/default\" \
to export single color, \"background/default, background/secondary\" to export several colors and \
\"background/*\" to export all colors from the folder.
""")
var filter: String?

func run() throws {
let client = FigmaClient(accessToken: options.accessToken, timeout: options.params.figma.timeout)

logger.info("Using FigmaExport \(FigmaExportCommand.version) to export colors.")

logger.info("Fetching colors. Please wait...")
let loader = ColorsLoader(client: client, figmaParams: options.params.figma, colorParams: options.params.common?.colors)
let colors = try loader.load()
let colors = try loader.load(filter: filter)

if let ios = options.params.ios {
logger.info("Processing colors...")
Expand Down

0 comments on commit 54813f8

Please sign in to comment.