export function isValidGlobExpression()

in src/core/utils/glob.ts [44:98]


export function isValidGlobExpression(glob: string | undefined) {
  logger.silly(`checking if glob expression is valid`);
  logger.silly(` - glob: ${chalk.yellow(glob)}`);

  if (!glob) {
    logger.silly(` - glob is empty. Return false`);
    return false;
  }

  if (glob === "*") {
    logger.silly(` - glob is *`);
    return true;
  }

  const hasWildcard = glob.includes("*");

  if (hasWildcard) {
    const paths = glob.split("*");
    if (paths.length > 2) {
      logger.silly(` - glob has more than one wildcard. Return false`);
      return false;
    }

    const pathBeforeWildcard = paths[0];
    if (pathBeforeWildcard && glob.endsWith("*")) {
      logger.silly(` - glob ends with *. Return true`);
      return true;
    }

    const pathAfterWildcard = paths[1];
    if (pathAfterWildcard) {
      logger.silly(` - pathAfterWildcard: ${chalk.yellow(pathAfterWildcard)}`);

      if (isBalancedCurlyBrackets(glob) === false) {
        logger.silly(` - pathAfterWildcard contains unbalanced { } syntax. Return false`);
        return false;
      }

      // match exactly extensions of type:
      // -->  /blog/*.html
      // --> /blog/*.{html,jpg}
      const filesExtensionMatch = pathAfterWildcard.match(/\.(\w+|\{\w+(,\w+)*\})$/);

      if (filesExtensionMatch) {
        logger.silly(`  - pathAfterWildcard match a file extension. Return true`);
        return true;
      } else {
        logger.silly(`  - pathAfterWildcard doesn't match a file extension. Return false`);
        return false;
      }
    }
  }

  return false;
}