func buildDirectoryContext()

in pkg/firebase/util/util.go [67:112]


func buildDirectoryContext(cwd, userSpecifiedAppDirPath string) (string, string, error) {
	if userSpecifiedAppDirPath == "" {
		return "", "", nil
	}

	absoluteAppDirPath := filepath.Join(cwd, userSpecifiedAppDirPath)
	_, err := os.Stat(absoluteAppDirPath)
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			return "", "", faherror.InvalidRootDirectoryError(userSpecifiedAppDirPath, err)
		}
		return "", "", err
	}
	var monorepoRootPath string
	curr := absoluteAppDirPath
	for {
		exists, err := supportedMonorepoConfigFileExists(curr)
		if err != nil {
			return "", "", err
		}
		if exists {
			monorepoRootPath = curr
			break
		}
		if curr == cwd || curr == "/" || curr == "." {
			break
		}
		curr = filepath.Dir(curr)
	}
	if monorepoRootPath == "" {
		// If no monorepo config file is detected, then the user-specified app directory path is the
		// root of an application in a subdirectory.
		return userSpecifiedAppDirPath, "", nil
	}
	// If a monorepo config file is detected, then the monorepo root is the "build directory" and the
	// user-specified app directory path is the root of the sub-application.
	mrp, err := filepath.Rel(cwd, monorepoRootPath)
	if err != nil {
		return "", "", err
	}
	adp, err := filepath.Rel(monorepoRootPath, absoluteAppDirPath)
	if err != nil {
		return "", "", err
	}
	return mrp, adp, nil
}