run method

Future<int> run()

Implementation

Future<int> run() async {
  print('🩺  Nebula Doctor — environment check\n');

  var allOk = true;

  // ── 1. Dart SDK ─────────────────────────────────────────────────────────
  final dartVersion = Platform.version.split(' ').first;
  _pass('Dart SDK', dartVersion);

  // ── 2. Config file ──────────────────────────────────────────────────────
  final config = await ConfigLoader.load();
  if (config == null) {
    _warn('nebula.config.json',
        'not found — run `nebula init` to create one');
    allOk = false;
  } else {
    _pass('nebula.config.json', 'found (input: ${config.input})');

    // ── 3. Spec file ───────────────────────────────────────────────────
    final specFile = File(config.input);
    if (!await specFile.exists()) {
      _fail('Spec file', 'not found: ${config.input}');
      allOk = false;
    } else {
      _pass('Spec file', 'found: ${config.input}');

      // ── 4. Spec is parseable ─────────────────────────────────────────
      try {
        final compiler = NebulaCompiler(
            parsers: [const OpenApiParser(), const SwaggerParser()]);
        final spec = await _loadSpec(config.input);
        compiler.parseOnly(
            spec, CompilerOptions(outputDir: config.output, verbose: false));
        _pass('Spec parseable', 'yes');
      } catch (e) {
        _fail('Spec parseable', 'parse error: $e');
        allOk = false;
      }
    }

    // ── 5. Output directory ────────────────────────────────────────────
    final outDir = Directory(config.output);
    if (await outDir.exists()) {
      _pass('Output directory', '${config.output} (exists)');

      // Check generated files
      final modelsFile = File('${config.output}/models.dart');
      if (await modelsFile.exists()) {
        _pass('Generated models.dart', 'found');
      } else {
        _info('Generated models.dart',
            'not found — run `nebula generate` first');
      }
    } else {
      _info('Output directory',
          '${config.output} (will be created on generate)');
    }
  }

  // ── 6. dart format ──────────────────────────────────────────────────────
  try {
    final result = await Process.run('dart', ['format', '--version']);
    final ver = result.stdout.toString().trim().isNotEmpty
        ? result.stdout.toString().trim()
        : result.stderr.toString().trim();
    _pass('dart format', ver.isNotEmpty ? ver : 'available');
  } catch (_) {
    _warn('dart format', 'not available (generated code will not be formatted)');
  }

  // ── 7. Package version ──────────────────────────────────────────────────
  _info('nebula_api_studio', 'v0.1.0');

  // ── Summary ─────────────────────────────────────────────────────────────
  print('');
  if (allOk) {
    print('✅  All checks passed. Ready to generate!');
    print('   Run: nebula generate');
  } else {
    print('⚠️  Some issues found. Fix the above and rerun `nebula doctor`.');
  }

  return allOk ? 0 : 1;
}