copyFile static method

Future<bool> copyFile({
  1. String? targetPath,
})

Create files to .git/hooks and targetPath

Implementation

static Future<bool> copyFile({String? targetPath}) async {
  if (targetPath == null) {
    targetPath = 'git_hooks.dart';
  } else {
    if (!targetPath.endsWith('.dart')) {
      print('the file what you want to create is not a dart file');
      exit(1);
    }
  }
  var relativePath = '${_rootDir}/${targetPath}';
  var hookFile = File(Utils.uri(absolute(_rootDir, relativePath)));
  var logger = Logger.standard();
  try {
    var commonStr = commonHook(Utils.uri(targetPath));
    commonStr = createHeader() + commonStr;
    var progress = logger.progress('create files');
    await _hooksCommand((hookFile) async {
      if (!hookFile.existsSync()) {
        await hookFile.create(recursive: true);
      }
      await hookFile.writeAsString(commonStr);
      if (!Platform.isWindows) {
        try {
          await Process.run('chmod', ['777', hookFile.path]);
        } catch (e) {
          print(e);
        }
      }
      return true;
    });
    if (!hookFile.existsSync()) {
      var exampleStr = userHooks;
      hookFile.createSync(recursive: true);
      hookFile.writeAsStringSync(exampleStr);
    }
    print('All files wrote successful!');
    progress.finish(showTiming: true);
    return true;
  } catch (e) {
    print(e.toString());
    return false;
  }
}