getTemplatePath method

Future<String> getTemplatePath()

Resolves the base template directory path.

Implementation

Future<String> getTemplatePath() async {
  // We bundle templates in lib/templates to ensure they publish correctly with pub.dev
  final packageUri = Uri.parse('package:flutter_init_cli/templates/');
  final resolved = await Isolate.resolvePackageUri(packageUri);
  if (resolved != null) {
    final path = Directory.fromUri(resolved).path;
    if (Directory(path).existsSync()) {
      return path;
    }
  }

  // Fallback search paths for local development and testing environment
  final localLibTemplates = p.join(Directory.current.path, 'lib', 'templates');
  if (Directory(localLibTemplates).existsSync()) {
    return localLibTemplates;
  }

  final localRootTemplates = p.join(Directory.current.path, 'templates');
  if (Directory(localRootTemplates).existsSync()) {
    return localRootTemplates;
  }

  throw CliException(
    'Could not resolve templates folder path. Ensure you are running the CLI from the package root or the package is activated globally.',
  );
}