writeDslSnapshots function

DslSnapshotWriteResult writeDslSnapshots(
  1. FFProject project, {
  2. String? directory,
})

Writes DSL JSON snapshots to disk for the entire project.

Creates:

  • dsl_json/pages/<PageName>.json for each page
  • dsl_json/components/<ComponentName>.json for each component
  • dsl_json/project.json with the full project snapshot
  • .flash/dsl_snapshot_meta.json with generation metadata

This replaces the old yaml_v2/ on-disk context layer. Same lifecycle — called on push, refresh-context, and workspace init.

Implementation

DslSnapshotWriteResult writeDslSnapshots(
  FFProject project, {
  String? directory,
}) {
  final dir = directory ?? Directory.current.path;
  final snapshot = buildBrownfieldProjectSnapshot(project);
  const encoder = JsonEncoder.withIndent('  ');
  final files = <String>[];

  // Pages.
  final pagesDir = Directory('$dir/dsl_json/pages');
  if (!pagesDir.existsSync()) pagesDir.createSync(recursive: true);
  for (final page in snapshot.pages) {
    final path = 'dsl_json/pages/${page.name}.json';
    File('$dir/$path').writeAsStringSync(encoder.convert(page.toJson()));
    files.add(path);
  }

  // Components.
  final componentsDir = Directory('$dir/dsl_json/components');
  if (!componentsDir.existsSync()) componentsDir.createSync(recursive: true);
  for (final component in snapshot.components) {
    final path = 'dsl_json/components/${component.name}.json';
    File('$dir/$path').writeAsStringSync(encoder.convert(component.toJson()));
    files.add(path);
  }

  // Full project snapshot.
  final projectPath = 'dsl_json/project.json';
  File(
    '$dir/$projectPath',
  ).writeAsStringSync(encoder.convert(snapshot.toJson()));
  files.add(projectPath);

  // Metadata.
  final metaDir = Directory('$dir/.flash');
  if (!metaDir.existsSync()) metaDir.createSync(recursive: true);
  final meta = <String, dynamic>{
    'generatedAt': DateTime.now().toUtc().toIso8601String(),
    'pageCount': snapshot.pages.length,
    'componentCount': snapshot.components.length,
  };
  File(
    '$dir/.flash/dsl_snapshot_meta.json',
  ).writeAsStringSync(encoder.convert(meta));

  return DslSnapshotWriteResult(files: files);
}