currentVersion function

String? currentVersion()

Returns the current fts version. Validating if it runs in local mode CliConfig.isDev, or installed as snapshot.

Implementation

String? currentVersion() {
  var scriptFile = Platform.script.toFilePath();
  if (CliConfig.isDev) {
    /// navigate up in the folders up to the project dir
    /// ./flutter_translation_sheet/.dart_tool/pub/bin/flutter_translation_sheet/
    var basePath = p.dirname(scriptFile);
    basePath = p.dirname(basePath);
    basePath = p.dirname(basePath);
    basePath = p.dirname(basePath);
    basePath = p.dirname(basePath);
    var pubSpec = p.join(basePath, 'pubspec.yaml');
    final str = openString(pubSpec);
    if (str.isEmpty) return null;
    final data = loadYaml(str);
    if (data is YamlMap) {
      return 'dev-${data['version']}';
    }
    return 'Could not find pubspec.yaml version in local enviroment.';
  }
  // trace('script file: ', basename(scriptFile));
  var pathToPubLock =
      canonicalize(join(dirname(scriptFile), '../pubspec.lock'));
  var str = openString(pathToPubLock);
  if (str.isEmpty) {
    return null;
  }
  var yaml = loadYaml(str);
  if (yaml['packages'][CliConfig.packageName] == null) {
    /// running local version? might read the pubspec here.
    var pathToPubSpec =
        canonicalize(join(dirname(pathToPubLock), 'pubspec.yaml'));
    str = openString(pathToPubSpec);
    if (str.isEmpty) {
      /// Impossible scenario. But just in case.
      error('Report version error to the developers of the package.');
      return null;
    }
    yaml = loadYaml(str);
    var version = yaml['version'];
    return version;
  } else {
    var version = yaml['packages'][CliConfig.packageName]['version'].toString();
    return version;
  }
}