Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ common:
nameValidateRegexp: '^([a-zA-Z_]+)$' # RegExp pattern for: background, background_primary, widget_primary_background
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'color_$1'
# [optional] List of color names to exclude from export. Supports wildcard patterns, for example: ["background", "background/*"]
exclude: []
# [optional] Extract light and dark mode colors from the lightFileId specified in the figma params. Defaults to false
useSingleFile: true
# [optional] If useSingleFile is true, customize the suffix to denote a dark mode color. Defaults to '_dark'
Expand Down Expand Up @@ -58,6 +60,8 @@ common:
nameValidateRegexp: '^([a-zA-Z_]+)$'
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'color_$1'
# [optional] List of variable names to exclude from export. Supports wildcard patterns, for example: ["background", "background/*"]
exclude: []
# [optional]
icons:
# [optional] Name of the Figma's frame where icons components are located
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ If you want to export specific colors/icons/images you can list their names in t

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

If you need to export all colors except a few, use `common.colors.exclude` or `common.variablesColors.exclude` in `figma-export.yaml`.

⚠️ Wildcard doesn't work on Linux.

Argument `-i` or `-input` specifies path to FigmaExport configuration file `figma-export.yaml`.
Expand Down
2 changes: 2 additions & 0 deletions Sources/FigmaExport/Input/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Params: Decodable {
struct Colors: Decodable {
let nameValidateRegexp: String?
let nameReplaceRegexp: String?
let exclude: [String]?
let useSingleFile: Bool?
let darkModeSuffix: String?
let lightHCModeSuffix: String?
Expand All @@ -36,6 +37,7 @@ struct Params: Decodable {

let nameValidateRegexp: String?
let nameReplaceRegexp: String?
let exclude: [String]?
}

struct Icons: Decodable {
Expand Down
14 changes: 13 additions & 1 deletion Sources/FigmaExport/Loaders/Colors/ColorsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class ColorsLoader {
private let figmaParams: Params.Figma
private let colorParams: Params.Common.Colors?
private let filter: String?
private let excludeFilter: AssetsFilter?

init(
client: Client,
Expand All @@ -21,6 +22,11 @@ final class ColorsLoader {
self.figmaParams = figmaParams
self.colorParams = colorParams
self.filter = filter
if let excludes = colorParams?.exclude, !excludes.isEmpty {
excludeFilter = AssetsFilter(filters: excludes)
} else {
excludeFilter = nil
}
}

func load() throws -> ColorsLoaderOutput {
Expand Down Expand Up @@ -77,7 +83,13 @@ final class ColorsLoader {
assetsFilter.match(name: style.name)
}
}


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

guard !styles.isEmpty else {
throw FigmaExportError.stylesNotFound
}
Expand Down
21 changes: 18 additions & 3 deletions Sources/FigmaExport/Loaders/Colors/ColorsVariablesLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final class ColorsVariablesLoader {
private let client: Client
private let variableParams: Params.Common.VariablesColors?
private let filter: String?
private let excludeFilter: AssetsFilter?

init(
client: Client,
Expand All @@ -16,6 +17,11 @@ final class ColorsVariablesLoader {
self.client = client
self.variableParams = variableParams
self.filter = filter
if let excludes = variableParams?.exclude, !excludes.isEmpty {
excludeFilter = AssetsFilter(filters: excludes)
} else {
excludeFilter = nil
}
}

func load() throws -> ColorsLoaderOutput {
Expand Down Expand Up @@ -136,9 +142,18 @@ final class ColorsVariablesLoader {
}

private func doesColorMatchFilter(from variable: Variable) -> Bool {
guard let filter = filter else { return true }
let assetsFilter = AssetsFilter(filter: filter)
return assetsFilter.match(name: variable.name)
if let filter {
let assetsFilter = AssetsFilter(filter: filter)
guard assetsFilter.match(name: variable.name) else {
return false
}
}

if let excludeFilter, excludeFilter.match(name: variable.name) {
return false
}

return true
}

private func createColor(from variable: Variable, color: PaintColor) -> Color {
Expand Down
2 changes: 2 additions & 0 deletions Sources/FigmaExport/Resources/androidConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ common:
nameValidateRegexp: '^([a-zA-Z_]+)$' # RegExp pattern for: background, background_primary, widget_primary_background
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'color_$1'
# [optional] List of color names to exclude from export. Supports wildcard patterns, for example: ["background", "background/*"]
exclude: []
# [optional] Extract light and dark mode colors from the lightFileId specified in the figma params. Defaults to false
useSingleFile: false
# [optional] If useSingleFile is true, customize the suffix to denote a dark mode color. Defaults to '_dark'
Expand Down
2 changes: 2 additions & 0 deletions Sources/FigmaExport/Resources/iOSConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ common:
nameValidateRegexp: '^([a-zA-Z_]+)$' # RegExp pattern for: background, background_primary, widget_primary_background
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'color_$1'
# [optional] List of color names to exclude from export. Supports wildcard patterns, for example: ["background", "background/*"]
exclude: []
# [optional] Extract light and dark mode colors from the lightFileId specified in the figma params. Defaults to false
useSingleFile: false
# [optional] If useSingleFile is true, customize the suffix to denote a dark mode color. Defaults to '_dark'
Expand Down
56 changes: 56 additions & 0 deletions Tests/FigmaExportTests/ParamsDecodingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import XCTest
import Yams
@testable import FigmaExport

final class ParamsDecodingTests: XCTestCase {

func testDecodeColorsExcludeList() throws {
let yaml = """
figma:
lightFileId: file_id
common:
colors:
exclude:
- background
- background/*
"""

let params = try YAMLDecoder().decode(Params.self, from: yaml)

XCTAssertEqual(params.common?.colors?.exclude, ["background", "background/*"])
}

func testDecodeVariablesColorsExcludeList() throws {
let yaml = """
figma:
lightFileId: file_id
common:
variablesColors:
tokensFileId: tokens_id
tokensCollectionName: Base collection
lightModeName: Light
exclude:
- background
- backgroundSecondary
"""

let params = try YAMLDecoder().decode(Params.self, from: yaml)

XCTAssertEqual(params.common?.variablesColors?.exclude, ["background", "backgroundSecondary"])
}

func testDecodeWithoutExcludeList() throws {
let yaml = """
figma:
lightFileId: file_id
common:
colors:
nameValidateRegexp: '^([a-zA-Z_]+)$'
"""

let params = try YAMLDecoder().decode(Params.self, from: yaml)

XCTAssertNil(params.common?.colors?.exclude)
XCTAssertNil(params.common?.variablesColors?.exclude)
}
}