fromDirectory static method

Future<MelosWorkspaceConfig> fromDirectory(
  1. Directory directory
)

Creates a new configuration from a Directory.

If no melos.yaml is found, but Directory contains a packages/ sub-directory, a configuration for those packages will be created.

Implementation

static Future<MelosWorkspaceConfig> fromDirectory(
  Directory directory,
) async {
  final melosWorkspaceDirectory =
      _searchForAncestorDirectoryWithMelosYaml(directory);

  if (melosWorkspaceDirectory == null) {
    // Allow melos to use a project without a `melos.yaml` file if a
    // `packages` directory exists.
    final packagesDirectory = joinAll([directory.path, 'packages']);

    if (dirExists(packagesDirectory)) {
      return MelosWorkspaceConfig.fallback(path: directory.path)
        ..validatePhysicalWorkspace();
    }

    throw MelosConfigException(
      '''
Your current directory does not appear to be a valid Melos workspace.

You must have one of the following to be a valid Melos workspace:
- a "melos.yaml" file in the root with a "packages" option defined
- a "packages" directory
''',
    );
  }

  final melosYamlPath =
      melosYamlPathForDirectory(melosWorkspaceDirectory.path);
  final yamlContents = await loadYamlFile(melosYamlPath);

  if (yamlContents == null) {
    throw MelosConfigException('Failed to parse the melos.yaml file');
  }

  return MelosWorkspaceConfig.fromYaml(
    yamlContents,
    path: melosWorkspaceDirectory.path,
  )..validatePhysicalWorkspace();
}