parseOne method

Map<String, String> parseOne(
  1. String line, {
  2. Map<String, String> envMap = const {},
})

Parses a single line into a key-value pair.

Implementation

Map<String, String> parseOne(String line,
    {Map<String, String> envMap = const {}}) {
  final lineWithoutComments = removeCommentsFromLine(line);
  if (!_isStringWithEqualsChar(lineWithoutComments)) return {};

  final indexOfEquals = lineWithoutComments.indexOf('=');
  final envKey =
      trimExportKeyword(lineWithoutComments.substring(0, indexOfEquals));
  if (envKey.isEmpty) return {};

  final envValue = lineWithoutComments
      .substring(indexOfEquals + 1, lineWithoutComments.length)
      .trim();
  final quoteChar = getSurroundingQuoteCharacter(envValue);
  var envValueWithoutQuotes = removeSurroundingQuotes(envValue);
  // Add any escapted quotes
  if (quoteChar == _singleQuote) {
    envValueWithoutQuotes = envValueWithoutQuotes.replaceAll("\\'", "'");
    // Return. We don't expect any bash variables in single quoted strings
    return {envKey: envValueWithoutQuotes};
  }
  if (quoteChar == '"') {
    envValueWithoutQuotes =
        envValueWithoutQuotes.replaceAll('\\"', '"').replaceAll('\\n', '\n');
  }
  // Interpolate bash variables
  final interpolatedValue =
      interpolate(envValueWithoutQuotes, envMap).replaceAll("\\\$", "\$");
  return {envKey: interpolatedValue};
}