loadFromDisk static method

Future<void> loadFromDisk({
  1. String? customPath,
})

Carrega o registro de componentes a partir do arquivo JSON.

Em desenvolvimento, busca em lib/src/registry/components.json. Em produção, busca no diretório de instalação do pacote.

Implementation

static Future<void> loadFromDisk({String? customPath}) async {
  try {
    String path;

    if (customPath != null) {
      path = customPath;
    } else {
      // Tenta localizar o arquivo relativo ao script em execução
      // Isso ajuda a encontrar o arquivo tanto em dev quanto binário
      final scriptDir = p.dirname(Platform.script.toFilePath());

      // Se estiver rodando via 'dart run', o script costuma estar em bin/
      // O JSON está em lib/src/registry/
      path = p.join(p.dirname(scriptDir), 'lib', 'src', 'registry', 'components.json');

      if (!await File(path).exists()) {
        // Fallback para quando o comando é chamado de formas diferentes
        path = p.join(scriptDir, 'src', 'registry', 'components.json');
      }
    }

    final file = File(path);
    if (!await file.exists()) {
      throw Exception('Component registry file not found at: $path');
    }

    final String jsonContent = await file.readAsString();
    final Map<String, dynamic> data = json.decode(jsonContent) as Map<String, dynamic>;

    _components = data.map(
      (key, value) => MapEntry(
        key,
        ComponentConfig.fromMap(value as Map<String, dynamic>),
      ),
    );
  } catch (e) {
    // Em caso de erro crítico no carregamento, não podemos continuar
    print('Fatal Error loading Component Registry: $e');
    rethrow;
  }
}