run method
Runs this command.
The return value is wrapped in a Future
if necessary and returned by
CommandRunner.runCommand
.
Implementation
@override
Future run() async {
var (json, output, routeExportPath) = (
argResults!.flag('json'),
argResults!.option('output'),
argResults!.option('route-export-path'),
);
routeExportPath ??= _randomFileName();
final binDir = join(Directory.current.path, 'bin');
final routeExportFile = File(join(binDir, routeExportPath));
routeExportFile.createSync(recursive: true);
routeExportFile.writeAsStringSync(await _makeRouteExportSource());
final routeExportResult = Process.runSync(
'dart',
['run', routeExportFile.path],
stdoutEncoding: utf8,
stderrEncoding: utf8,
);
routeExportFile.deleteSync();
if (routeExportResult.exitCode != 0) {
throw UsageException(
'Failed to export routes: ${routeExportResult.stderr}',
usage,
);
}
final routeExportOutput = routeExportResult.stdout as String;
final data = jsonDecode(routeExportOutput) as List;
if (json) {
print(
const JsonEncoder.withIndent(' ').convert(data),
);
} else {
final formatter = TableFormatter(
data.cast<Map<String, dynamic>>().map(RouteMetadata.fromJson).toList(),
);
print('\n${formatter.format()}');
}
}