All files / packages/sds-styling-hooks/src/utils validations.js

100% Statements 38/38
100% Branches 44/44
100% Functions 2/2
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                1x 6x   6x 1x   6x 1x   6x 1x   6x 5x 1x       5x 1x         6x 5x                     1x 8x   8x 1x   8x 7x 1x         8x 1x   8x 1x       8x 1x       8x 1x   8x 6x 1x     8x   7x 1x             8x 6x      
import chalk from 'chalk';
 
/**
 * Before anything runs, we validate the correct configurations are set when invoking generateDefaults()
 * @param {object} config - The configuration object to validate
 * @param {function} defaultsExists - Function to check if defaults exist
 * @throws {Error} - Throws an error if validation fails
 */
export const validateDefaultConfig = (config, defaultsExists) => {
  let errors = [];
  // Required configurations
  if (config.rootDir === undefined) {
    errors.push('rootDir is undefined. Please define a rootDir in the config object.');
  }
  if (config.outputDir === undefined) {
    errors.push('outputDir is undefined. Please define a outputDir in the config object.');
  }
  if (config.default === undefined) {
    errors.push('default is undefined. Please define a default in the config object.');
  }
  if (config.default) {
    if (!defaultsExists()) {
      errors.push(
        `No files found in ${config.rootDir}, a default directory requires a JSON file with a styling hooks schema.`,
      );
    }
    if (config.themes || config.shared) {
      errors.push(
        'themes and shared cannot be defined when calling generateDefaults(). Please call generateTheme() instead and ensure you are not setting defaults to true in the config object.',
      );
    }
  }
  if (errors.length > 0) {
    throw new Error(errors.join('\n - '));
  }
};
 
/**
 * Before anything runs, we validate the correct configurations are set when invoking generateThemes()
 * @param {object} config - The configuration object to validate
 * @param {function} themeExists - Function to check if theme exists
 * @param {function} sharedExists - Function to check if shared exists
 * @throws {Error} - Throws an error if validation fails
 */
export const validateThemeConfig = (config, themeExists, sharedExists) => {
  let errors = [];
  // Required configurations
  if (config.rootDir === undefined) {
    errors.push('rootDir is undefined. Please define a rootDir in the config object.');
  }
  if (config.rootDir) {
    if (!themeExists()) {
      errors.push(
        `No files found in ${config.rootDir}, a theme directory requires a JSON file with a styling hooks schema.`,
      );
    }
  }
  if (config.outputDir === undefined) {
    errors.push('outputDir is undefined. Please define a outputDir in the config object.');
  }
  if (config.default) {
    errors.push(
      'default cannot be defined when calling generateTheme(). Please call generateDefaults() instead and ensure you are not setting themes to true in the config object.',
    );
  }
  if (config.themes === undefined) {
    errors.push(
      'themes is undefined. Please specify a namespace or an array of namespaces in the config object.',
    );
  }
  if (config.themesDir === undefined) {
    errors.push('themesDir is undefined. Please define a themesDir in the config object.');
  }
  if (config.themes && config.themesDir) {
    if (!themeExists(config.themes)) {
      errors.push(`Theme ${config.themes} does not exist under ${config.themesDir}.`);
    }
  }
  if (config.shared && config.themesDir) {
    // this is a silent warning since a theme can exist without a shared directory
    if (!sharedExists()) {
      console.error(
        `${chalk.bgYellow('Warning')} No files found in ${config.themesDir}/${
          config.themes
        }/shared, a shared directory requires a JSON file with a styling hooks schema.`,
      );
    }
  }
  if (errors.length > 0) {
    throw new Error(errors.join('\n - '));
  }
};