getTemplateDirectoryPath static method

Future<String?> getTemplateDirectoryPath()

Implementation

static Future<String?> getTemplateDirectoryPath() async {
  final Config config = await Config.fromFile();
  String? templatePath = config.templatePath;
  Directory dir = Directory('${Directory.current.path}/templates');
  if (templatePath == null) {
    ColoredLog.yellow(
      'No template folder found! Do you want to create template folder in the working directory (y/N):',
      name: 'Input',
    );
    final input = stdin.readLineSync();
    if ((input ?? '') == 'y' ||
        (input ?? '') == 'Y' ||
        (input ?? '') == 'yes') {
      await dir.create(recursive: true);
      ColoredLog.green(dir.path, name: 'Directory created');
    } else {
      ColoredLog.white(
        'Enter the path of the template directory where you will store all your templates :',
        name: 'Input',
      );
      final path = stdin.readLineSync();

      if ((path ?? '').isNotEmpty) {
        String actualPath = path!.replaceAll('\'', '');
        actualPath = actualPath.replaceAll('"', '');
        final temPath = Directory(actualPath);
        await temPath.create(recursive: true);
        dir = temPath;
        await Config.save(templatePath: temPath.path);
        templatePath = temPath.path;
      } else {
        ColoredLog.red('Invalid input', name: 'Error');
        ColoredLog.yellow(
          'To update the template directory use the below command',
        );
        ColoredLog('⚪️ templify config -d "TEMPLATE DIRECTORY"');
        ColoredLog('⚪️ templify config --dir "TEMPLATE DIRECTORY"');
        print('');
        exit(0);
      }
    }
  }

  if (templatePath == null) {
    exit(0);
  } else {
    return templatePath;
  }
}