previewReferenceFile function
Transforms filePath under referencePath and optionally renders Mason
vars.
When templateOnly is true, Mustache tags remain in the output.
Implementation
Future<String> previewReferenceFile({
required String filePath,
required String referencePath,
required ClayConfig config,
required bool templateOnly,
Map<String, dynamic> vars = const {},
}) async {
final referenceDir = Directory(referencePath);
if (!referenceDir.existsSync()) {
throw PreviewException(
'Reference directory not found ($referencePath).',
);
}
final resolvedFilePath = resolveReferenceFilePath(
filePath: filePath,
referencePath: referencePath,
);
assertPreviewPathIsFile(resolvedFilePath);
final file = File(resolvedFilePath);
final tempTargetDir = await Directory.systemTemp.createTemp('clay_preview_');
try {
final referenceRelativePath = p.relative(
resolvedFilePath,
from: referenceDir.path,
);
final simulatedTargetPath = p.join(
tempTargetDir.path,
referenceRelativePath,
);
final resolvedTargetPath = resolveTargetFilePath(
absolutePath: simulatedTargetPath,
targetAbsolutePath: tempTargetDir.path,
replacements: config.replacements,
);
final targetRelativePath = p.relative(
resolvedTargetPath,
from: tempTargetDir.path,
);
final annotatedContent = resolveReferenceContent(
content: await file.readAsString(),
targetRelativePath: targetRelativePath,
targetAbsolutePath: tempTargetDir.path,
config: config,
);
return templateOnly
? annotatedContent
: annotatedContent.render(
vars,
loadPreviewPartials(tempTargetDir),
);
} on GenerationException catch (error) {
throw PreviewException(error.message);
} finally {
if (tempTargetDir.existsSync()) {
await tempTargetDir.delete(recursive: true);
}
}
}