createModel function

void createModel(
  1. CreateOptions opts,
  2. ProjectContext ctx
)

river_cli create model:<name> — from a --fields spec or a --json sample (which can produce nested models).

Implementation

void createModel(CreateOptions opts, ProjectContext ctx) {
  final snake = Naming.snake(opts.name);
  final writer = FileWriter(force: opts.force, dryRun: opts.dryRun);
  print('Creating model: ${Naming.pascal(opts.name)}');

  final String content;
  final inferred = _inferredModels(opts);
  if (inferred != null) {
    final names = inferred.map((m) => m.name).toList();
    print('  inferred ${names.length} model(s) from JSON: ${names.join(', ')}');
    content = modelsFileTemplate(inferred);
  } else {
    if (opts.fields.isEmpty) {
      print('  (no --fields given; generating a sample "id" field)');
    }
    content = modelTemplate(opts.name, opts.fields);
  }

  writer.write('lib/data/models/${snake}_model.dart', content);
  _maybeWriteModelTest(opts, ctx, writer,
      fieldsForTest: opts.fields, blockedByJson: inferred != null);
  writer.printSummary();
}