getDefinition method

Definition getDefinition(
  1. String scriptString
)

Get a serialized Definition for a script string if it exists. This function will throw errors if the script is not defined or if the script is not valid.

Implementation

Definition getDefinition(String scriptString) {
  if (!_serializedDefinitions.containsKey(scriptString)) {
    final scriptFound = lookup(scriptString);

    /// for when script is not defined at all
    if (scriptFound == null) {
      throw MerryError(
        type: ErrorCode.scriptNotDefined,
        body: {'script': scriptString, 'suggestions': getPaths()},
      );
    }

    // for when script is not a type we want
    if (scriptFound is! Map && scriptFound is! List && scriptFound is! String) {
      throw MerryError(
        type: ErrorCode.invalidScript,
        body: {'script': scriptString},
      );
    }

    // for when script is a map
    if (scriptFound is Map) {
      final scripts = runnableScripts(scriptFound);

      if (scripts == null) {
        throw MerryError(
          type: ErrorCode.invalidScript,
          body: {'script': scriptString, 'paths': getPaths()},
        );
      }

      // keep the surrounding (description)/(workdir) when the commands come
      // from a platform or (default) key instead of (scripts)
      _serializedDefinitions[scriptString] = Definition.from({
        ...scriptFound,
        scriptsDefinitionKey: scripts,
      });
    } else {
      _serializedDefinitions[scriptString] = Definition.from(scriptFound);
    }
  }

  return _serializedDefinitions[scriptString]!;
}