From 54813f858b0fa1730d22f964206db08d9823f42f Mon Sep 17 00:00:00 2001 From: Daniil Subbotin Date: Sat, 14 May 2022 12:59:56 +0300 Subject: [PATCH] Add ability to export specified colors (#169) --- README.md | 6 ++- .../FigmaExport/Loaders/ColorsLoader.swift | 44 +++++++++++-------- .../Subcommands/ExportColors.swift | 9 +++- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9a35bee9..d85dc3f7 100644 --- a/README.md +++ b/README.md @@ -370,7 +370,7 @@ 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. @@ -378,7 +378,9 @@ If you want to export specific icons/images you can list their names in the last `./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`. diff --git a/Sources/FigmaExport/Loaders/ColorsLoader.swift b/Sources/FigmaExport/Loaders/ColorsLoader.swift index 2de005b8..25bce782 100644 --- a/Sources/FigmaExport/Loaders/ColorsLoader.swift +++ b/Sources/FigmaExport/Loaders/ColorsLoader.swift @@ -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" @@ -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 diff --git a/Sources/FigmaExport/Subcommands/ExportColors.swift b/Sources/FigmaExport/Subcommands/ExportColors.swift index 9a345ce1..6f129122 100644 --- a/Sources/FigmaExport/Subcommands/ExportColors.swift +++ b/Sources/FigmaExport/Subcommands/ExportColors.swift @@ -17,6 +17,13 @@ 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) @@ -24,7 +31,7 @@ extension FigmaExportCommand { 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...")