-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizer.dart
61 lines (52 loc) · 1.58 KB
/
optimizer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'dart:io';
import 'package:args/args.dart';
const String _inputOptionName = 'input';
const String _outputOptionName = 'output';
const String _configOptionName = 'config';
void optimizeSvg(List<String> arguments) {
final ArgParser argParser = buildParser();
try {
final ArgResults results = argParser.parse(arguments);
final String inputName = results[_inputOptionName];
final String outputName = results[_outputOptionName];
if (!inputName.toLowerCase().endsWith('.svg')) {
File(inputName).copySync(outputName);
exit(0);
}
final String? configPath = results[_configOptionName];
final ProcessResult result = Process.runSync(
'svgo',
[
'-i',
inputName,
'-o',
outputName,
if (configPath != null) ...['--config', configPath],
],
);
if (result.exitCode != 0) {
print(result.stderr);
stderr.write(result.stderr);
exit(result.exitCode);
} else {
print(result.stdout);
exit(0);
}
} on FormatException catch (e) {
print(e.message);
print(argParser.usage);
exit(1);
} on ProcessException catch (_) {
print(
'It looks like svgo is not configured properly. To install it, refer to README file.');
exit(1);
} catch (e) {
print(e.toString());
exit(1);
}
}
/// Build argument parser with input and output options
ArgParser buildParser() => ArgParser()
..addOption(_inputOptionName, mandatory: true, abbr: 'i')
..addOption(_outputOptionName, mandatory: true, abbr: 'o')
..addOption(_configOptionName, mandatory: false);