writeForLoadedSchemaSync method

GeneratedClientWriteResult writeForLoadedSchemaSync(
  1. LoadedSchemaDocument loaded, {
  2. String? generatorName,
  3. String? outputPath,
})

Synchronously generates and writes client code for loaded.

Implementation

GeneratedClientWriteResult writeForLoadedSchemaSync(
  LoadedSchemaDocument loaded, {
  String? generatorName,
  String? outputPath,
}) {
  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 = File(loaded.filePath).readAsStringSync();
  final content = ClientGenerator(
    options: resolveClientGeneratorOptions(
      schema: loaded.schema,
      schemaPath: loaded.filePath,
      anchorDirectory: outputFile.parent,
    ),
  ).generateClient(loaded.schema, schemaSource: schemaSource);

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

  outputFile.parent.createSync(recursive: true);
  outputFile.writeAsStringSync(content);
  return GeneratedClientWriteResult(
    outputPath: outputFile.path,
    updated: true,
  );
}