From ad63188e6191e3e57e707aeb72775ab9c46d7f37 Mon Sep 17 00:00:00 2001 From: Arne Molland Date: Tue, 20 Dec 2022 10:10:07 +0100 Subject: [PATCH] tools/icons: add cli options, improve error messages --- pubspec.yaml | 3 +- tools/icons.dart | 73 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index b9ceda2..42e5f5d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,11 +16,12 @@ dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^2.0.0 + flutter_lints: ^2.0.1 figma: ^3.6.0 progress_bar: ^1.0.0 http: ^0.13.5 test: ^1.21.4 + args: ^2.3.1 flutter: assets: diff --git a/tools/icons.dart b/tools/icons.dart index 7ef971d..9c52a2f 100644 --- a/tools/icons.dart +++ b/tools/icons.dart @@ -4,6 +4,7 @@ library tools; import 'dart:io'; +import 'package:args/args.dart'; import 'package:figma/figma.dart'; import 'package:http/http.dart'; import 'package:progress_bar/progress_bar.dart'; @@ -64,12 +65,27 @@ Future optimize(Directory dir) async { /// {@category Tools} /// {@subCategory Figma} /// Downloads all icons from Figma and saves them to the given directory. -Future download(Directory dir) async { +Future download( + Directory dir, + String ref, + String accessToken, +) async { // Create a Figma client. - final client = FigmaClient('figd_WHj1ryknfvPbkmyJ4kFR00I7OM0_sn7KZ58QaEKR'); + final client = FigmaClient(accessToken); // Retrieve Flume Web file. - final file = await client.getFile('4aGRieJsOTu9aqMPFGFf2u'); + final file = await client.getFile(ref).catchError((e) { + if (e is FigmaError) { + switch (e.code) { + case 403: + throw Exception('Invalid access token'); + case 404: + throw Exception('File not found'); + default: + throw Exception('Could not retrieve file'); + } + } + }); // Retrieve icon canvas. final canvas = @@ -81,12 +97,11 @@ Future download(Directory dir) async { // Get nodes from icon canvas. final node = await client.getFileNodes( - '4aGRieJsOTu9aqMPFGFf2u', - FigmaQuery( - ids: [canvas!.id], - format: 'svg', - ), - ); + ref, + FigmaQuery( + ids: [canvas!.id], + format: 'svg', + )); final components = node.nodes?[canvas.id]?.components; final ids = components?.keys.toList(); @@ -97,7 +112,7 @@ Future download(Directory dir) async { // Retrieve SVG for each component. final res = await client.getImages( - '4aGRieJsOTu9aqMPFGFf2u', + ref, FigmaQuery( ids: ids, format: 'svg', @@ -202,14 +217,41 @@ Future generateIconFont( /// Downloads all icons from Figma, optimizes and /// saves them to the assets folder. -Future main() async { +Future main(List args) async { + final parser = ArgParser(); + + parser.addOption( + 'file', + abbr: 'f', + defaultsTo: '4aGRieJsOTu9aqMPFGFf2u', + ); + + parser.addOption( + 'access-token', + abbr: 'a', + defaultsTo: 'figd_WHj1ryknfvPbkmyJ4kFR00I7OM0_sn7KZ58QaEKR', + ); + + // Parse arguments. + final results = parser.parse(args); + + // Get file reference. + final ref = results['file']; + final accessToken = results['access-token']; + final dir = Directory('${Directory.current.path}/.cache'); // Download icons. - await download(dir); + await download(dir, ref, accessToken).catchError((e) { + print(e); + exit(1); + }); // Optimize icons. - await optimize(dir).catchError((e) => print(e)); + await optimize(dir).catchError((e) { + print(e); + exit(1); + }); // Generate icon font. await generateIconFont( @@ -217,5 +259,8 @@ Future main() async { 'FlumeIcon', '${Directory.current.path}/lib/fonts/FlumeIcons.ttf', '${Directory.current.path}/lib/icons.dart', - ).catchError((e) => print(e)); + ).catchError((e) { + print(e); + exit(1); + }); }