Skip to content

Commit

Permalink
tools/icons: add cli options, improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
arnemolland committed Dec 20, 2022
1 parent 4ca3bce commit ad63188
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
73 changes: 59 additions & 14 deletions tools/icons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -64,12 +65,27 @@ Future<void> optimize(Directory dir) async {
/// {@category Tools}
/// {@subCategory Figma}
/// Downloads all icons from Figma and saves them to the given directory.
Future<void> download(Directory dir) async {
Future<void> 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 =
Expand All @@ -81,12 +97,11 @@ Future<void> 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();
Expand All @@ -97,7 +112,7 @@ Future<void> download(Directory dir) async {

// Retrieve SVG for each component.
final res = await client.getImages(
'4aGRieJsOTu9aqMPFGFf2u',
ref,
FigmaQuery(
ids: ids,
format: 'svg',
Expand Down Expand Up @@ -202,20 +217,50 @@ Future<void> generateIconFont(

/// Downloads all icons from Figma, optimizes and
/// saves them to the assets folder.
Future<void> main() async {
Future<void> main(List<String> 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(
dir,
'FlumeIcon',
'${Directory.current.path}/lib/fonts/FlumeIcons.ttf',
'${Directory.current.path}/lib/icons.dart',
).catchError((e) => print(e));
).catchError((e) {
print(e);
exit(1);
});
}

0 comments on commit ad63188

Please sign in to comment.