writeForLoadedSchema method

Future<GeneratedClientWriteResult> writeForLoadedSchema(
  1. LoadedSchemaDocument loaded, {
  2. String? generatorName,
  3. String? outputPath,
})

Generates and writes client code for loaded.

Implementation

Future<GeneratedClientWriteResult> writeForLoadedSchema(
  LoadedSchemaDocument loaded, {
  String? generatorName,
  String? outputPath,
}) async {
  final generator = workflow.resolveGenerator(
    loaded,
    generatorName: generatorName,
  );
  final effectiveOutputPath = outputPath == null || outputPath.isEmpty
      ? generator.outputPath
      : File(outputPath).absolute.path;
  final outputFile = File(effectiveOutputPath);
  final schemaSource = await File(loaded.filePath).readAsString();
  final content = ClientGenerator(
    options: resolveClientGeneratorOptions(
      schema: loaded.schema,
      schemaPath: loaded.filePath,
      anchorDirectory: outputFile.parent,
    ),
  ).generateClient(loaded.schema, schemaSource: schemaSource);

  final existing = outputFile.existsSync()
      ? await outputFile.readAsString()
      : null;
  if (existing == content) {
    return GeneratedClientWriteResult(
      outputPath: outputFile.path,
      updated: false,
    );
  }

  await outputFile.parent.create(recursive: true);
  await outputFile.writeAsString(content);
  return GeneratedClientWriteResult(
    outputPath: outputFile.path,
    updated: true,
  );
}