import 'dart:io'; import '../command.dart'; import '../local_state.dart' show joinPath; import '_shared.dart'; Future run(CommandContext context, List args) async { final rawArgs = args.join(' ').trim(); if (commonHelpArgs.contains(rawArgs.toLowerCase())) { context.writeLine('Usage: /lint\n\nRun the project linter.'); return const CommandResult(); } final pubspecFile = File(joinPath(context.workingDirectory, 'pubspec.yaml')); final packageJsonFile = File(joinPath(context.workingDirectory, 'package.json')); List lintCmd; String label; if (await pubspecFile.exists()) { lintCmd = ['dart', 'analyze']; label = 'dart analyze'; } else if (await packageJsonFile.exists()) { lintCmd = ['npm', 'run', 'lint']; label = 'npm run lint'; } else { context.writeLine('Could not detect project type. No pubspec.yaml or package.json found.'); context.writeLine('Run your linter manually.'); return const CommandResult(exitCode: 1); } context.writeLine('Running: $label'); context.writeLine(''); try { final result = await Process.run( lintCmd.first, lintCmd.sublist(1), workingDirectory: context.workingDirectory, ); final out = (result.stdout as String).trim(); final err = (result.stderr as String).trim(); if (out.isNotEmpty) context.writeLine(out); if (err.isNotEmpty) context.writeError(err); if (result.exitCode == 0) { context.writeLine(''); context.writeLine('No issues found.'); } return CommandResult(exitCode: result.exitCode); } on ProcessException catch (e) { context.writeError('Could not run $label: ${e.message}'); return const CommandResult(exitCode: 1); } }