run method

  1. @override
Future<bool> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<bool> run() async {
  final List<String> errors = [];
  final closer = logger.printFixed('📝 Writing schema references', inRs);
  final schemasDir = Utils.pathFromRoot(KnownPaths.schemas, rootDir);

  await Utils.findFiles(matcher: RegExps.fileIaCJson).listen((file) {
    try {
      var schemaName = file.path.contains('/lambdas/')
          ? 'iac.lambda.schema.json'
          : 'iac.shared.schema.json';
      var relSchemaPath = path.relative(schemasDir, from: file.parent.path);
      var iacJson = jsonDecode(file.readAsStringSync());
      iacJson[r'$schema'] = path.join(relSchemaPath, schemaName);
      file.writeAsStringSync(JsonEncoder.withIndent('  ').convert(iacJson));
    } on FileSystemException catch (e) {
      errors.add(
          "${e.message} → ${path.relative(e.path ?? file.path, from: rootDir)}");
    }
  }).asFuture();

  final result = closer(errors.isEmpty);
  for (var error in errors) {
    logger.printFailed(error, inRs + inRs);
  }

  return result;
}